From 17062405591194a3764eae5bd363c8d87664c4da Mon Sep 17 00:00:00 2001 From: Jon Samp Date: Thu, 24 Oct 2024 00:25:02 -0500 Subject: [PATCH] refactor --- .../eas-cli/src/commands/workflow/validate.ts | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/eas-cli/src/commands/workflow/validate.ts b/packages/eas-cli/src/commands/workflow/validate.ts index dc70463e50..75e42a3447 100644 --- a/packages/eas-cli/src/commands/workflow/validate.ts +++ b/packages/eas-cli/src/commands/workflow/validate.ts @@ -12,7 +12,7 @@ export default class WorkflowValidate extends EasCommand { static override args = [ { name: 'path', - description: 'Path to the workflow configuration yaml file (must end with .yml or .yaml)', + description: 'Path to the workflow configuration YAML file (must end with .yml or .yaml)', required: true, }, ]; @@ -25,38 +25,13 @@ export default class WorkflowValidate extends EasCommand { const spinner = ora().start('Validating the workflow YAML fileā€¦'); try { - try { - await fs.access(filePath); - } catch { - throw new Error(`File does not exist: ${filePath}`); - } - - const fileExtension = path.extname(filePath).toLowerCase(); - - if (fileExtension !== '.yml' && fileExtension !== '.yaml') { - throw new Error('File must have a .yml or .yaml extension'); - } - - const fileContents = await fs.readFile(filePath, 'utf8'); + await checkIfFileExistsAsync(filePath); + await checkIfValidYAMLFileExtensionAsync(filePath); + await validateYAMLAsync(filePath); - try { - 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) { - throw new Error(`YAML parsing error: ${error.message}`); - } else if (error instanceof Error) { - throw new Error(`YAML parsing error: ${error.message}`); - } else { - throw new Error(`YAML parsing error: ${String(error)}`); - } - } - - spinner.succeed('Workflow configuration YAML is valid'); + spinner.succeed('Workflow configuration YAML is valid.'); } catch (error) { - spinner.fail('Workflow configuration YAML is invalid'); + spinner.fail('Workflow configuration YAML is not valid.'); if (error instanceof yaml.YAMLException) { Log.error(`YAML parsing error: ${error.message}`); } @@ -64,3 +39,35 @@ export default class WorkflowValidate extends EasCommand { } } } + +async function checkIfFileExistsAsync(filePath: string): Promise { + try { + await fs.access(filePath); + } catch { + throw new Error(`File does not exist: ${filePath}`); + } +} + +async function checkIfValidYAMLFileExtensionAsync(filePath: string): Promise { + 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 { + 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)}`); + } + } +}