Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds workflow:validate command #2650

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"indent-string": "4.0.0",
"jks-js": "1.1.0",
"joi": "17.11.0",
"js-yaml": "^4.1.0",
"jsonwebtoken": "9.0.0",
"keychain": "1.5.0",
"log-symbols": "4.1.0",
Expand Down
73 changes: 73 additions & 0 deletions packages/eas-cli/src/commands/workflow/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'fs/promises';
import yaml from 'js-yaml';
import path from 'path';

import EasCommand from '../../commandUtils/EasCommand';
import Log from '../../log';
import { ora } from '../../ora';

export default class WorkflowValidate extends EasCommand {
static override description = 'validate a workflow configuration yaml file';

static override args = [
{
name: 'path',
description: 'Path to the workflow configuration YAML file (must end with .yml or .yaml)',
required: true,
},
];

async runAsync(): Promise<void> {
const {
args: { path: filePath },
} = await this.parse(WorkflowValidate);

const spinner = ora().start('Validating the workflow YAML file…');

try {
await checkIfFileExistsAsync(filePath);
await checkIfValidYAMLFileExtensionAsync(filePath);
await validateYAMLAsync(filePath);

spinner.succeed('Workflow configuration YAML is valid.');
} catch (error) {
spinner.fail('Workflow configuration YAML is not valid.');
if (error instanceof yaml.YAMLException) {
Log.error(`YAML parsing error: ${error.message}`);
}
throw error;
}
}
}

async function checkIfFileExistsAsync(filePath: string): Promise<void> {
try {
await fs.access(filePath);
} catch {
throw new Error(`File does not exist: ${filePath}`);
}
}

async function checkIfValidYAMLFileExtensionAsync(filePath: string): Promise<void> {
const fileExtension = path.extname(filePath).toLowerCase();

if (fileExtension !== '.yml' && fileExtension !== '.yaml') {
throw new Error('File must have a .yml or .yaml extension');
}
}

async function validateYAMLAsync(filePath: string): Promise<void> {
try {
const fileContents = await fs.readFile(filePath, 'utf8');
const parsedYaml = yaml.load(fileContents);
if (parsedYaml === undefined) {
throw new Error('YAML file is empty or contains only comments');
}
} catch (error) {
if (error instanceof yaml.YAMLException || error instanceof Error) {
throw new Error(`YAML parsing error: ${error.message}`);
} else {
throw new Error(`YAML parsing error: ${String(error)}`);
}
}
}
Loading
Loading