yaml-verify
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "yaml-verify", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"type": "module", | ||
@@ -31,3 +31,3 @@ "description": "A CLI utility to ensure proper formatting of YAML files.", | ||
"js-yaml": "^4.1.0", | ||
"ora": "^8.0.1" | ||
"listr2": "^8.0.2" | ||
}, | ||
@@ -34,0 +34,0 @@ "devDependencies": { |
@@ -8,4 +8,4 @@ #!/usr/bin/env node | ||
const fsPromises = fs.promises // Use fs.promises for async operations | ||
import ora from 'ora' | ||
import glob from 'fast-glob' | ||
import { Listr } from 'listr2' | ||
@@ -72,3 +72,3 @@ // Asynchronous function to check for duplicates in YAML data | ||
// Asynchronously process and validate YAML files with parallel processing | ||
// Asynchronously process and validate YAML files with listr2 | ||
async function processFiles(filePaths) { | ||
@@ -78,34 +78,83 @@ let allFilesPromises = filePaths.map(async (filePath) => { | ||
if (stat.isDirectory()) { | ||
return findYamlFilesAsync(filePath) // Returns a promise | ||
return findYamlFilesAsync(filePath) | ||
} | ||
return [filePath] // Wrap in array to normalize structure | ||
return [filePath] | ||
}) | ||
let allFilesArrays = await Promise.all(allFilesPromises) // Resolve all promises | ||
let allFiles = allFilesArrays.flat() // Flatten array of arrays into a single array | ||
let allFilesArrays = await Promise.all(allFilesPromises) | ||
let allFiles = allFilesArrays.flat() | ||
// Process files in parallel | ||
const validationPromises = allFiles.map(async (file) => { | ||
const spinner = ora(`Validating YAML file ${file}...`).start() | ||
// Create listr2 tasks for each file | ||
const tasks = new Listr( | ||
allFiles.map((file) => ({ | ||
title: `Validating YAML file ${file}`, | ||
task: async (ctx, task) => { | ||
const fileContents = await fsPromises.readFile(file, 'utf8') | ||
let data = null | ||
try { | ||
data = yaml.load(fileContents) | ||
} catch (error) { | ||
throw new Error( | ||
`Validation ${chalk.bgRed.whiteBright('FAILED')} for file ${file}: ${chalk.red(error.message)}`, | ||
) | ||
} | ||
const errors = checkForDuplicates(data) | ||
if (errors.length > 0) { | ||
// Generate a detailed error message including the filename | ||
throw new Error( | ||
`Validation ${chalk.bgRed.whiteBright('FAILED')} for file ${file}; Errors: ${chalk.red(errors.join('\n'))}`, | ||
) | ||
} | ||
task.title = `Validation ${chalk.bgAnsi256(22).whiteBright('PASSED')} for file ${file}` | ||
task.output = file | ||
}, | ||
})), | ||
{ | ||
concurrent: true, // Run tasks concurrently | ||
exitOnError: false, // Continue with other tasks even if some fail | ||
}, | ||
) | ||
try { | ||
const fileContents = await fsPromises.readFile(file, 'utf8') | ||
const data = yaml.load(fileContents) | ||
const errors = checkForDuplicates(data) | ||
let validationPassed = true // Track overall validation success | ||
let totalFiles = 0 | ||
let totalErrors = 0 | ||
if (errors.length > 0) { | ||
spinner.fail(`Validation failed for file ${file}.`) | ||
errors.forEach((error) => console.error(error)) | ||
} else { | ||
spinner.succeed(`Validation passed for file ${file}.`) | ||
// Run tasks | ||
try { | ||
await tasks.run() | ||
totalFiles = tasks.tasks.length | ||
// Check the state of each task after running | ||
tasks.tasks.forEach((task) => { | ||
if (task.state === 'FAILED') { | ||
validationPassed = false | ||
if (totalErrors == 0) console.log() | ||
totalErrors += 1 | ||
console.error( | ||
`Task '${task.title}' ${chalk.bgRed.whiteBright('FAILED')}`, | ||
) | ||
} | ||
} catch (e) { | ||
spinner.fail(`YAML file validation failed for file ${file}.`) | ||
console.error(e.message) | ||
} | ||
console.log() | ||
}) | ||
}) | ||
} catch (e) { | ||
validationPassed = false // Update flag if any task fails | ||
console.error( | ||
chalk.red('Validation errors found in one or more files.'), | ||
) | ||
} | ||
// Wait for all validations to complete | ||
await Promise.allSettled(validationPromises) | ||
// Check the overall validation result before printing the final message | ||
console.log() | ||
if (validationPassed) { | ||
console.log( | ||
chalk.green( | ||
`All ${totalFiles} file(s) have been successfully validated.`, | ||
), | ||
) | ||
} else { | ||
console.error( | ||
chalk.red( | ||
`${totalErrors} out of ${totalFiles} file(s) failed validation.`, | ||
), | ||
) | ||
process.exit(1) | ||
} | ||
} | ||
@@ -112,0 +161,0 @@ |
9716
18
167
+ Addedlistr2@^8.0.2
+ Addedansi-escapes@7.0.0(transitive)
+ Addedcli-truncate@4.0.0(transitive)
+ Addedcolorette@2.0.20(transitive)
+ Addedenvironment@1.1.0(transitive)
+ Addedeventemitter3@5.0.1(transitive)
+ Addedis-fullwidth-code-point@4.0.05.0.0(transitive)
+ Addedlistr2@8.2.5(transitive)
+ Addedlog-update@6.1.0(transitive)
+ Addedrfdc@1.4.1(transitive)
+ Addedslice-ansi@5.0.07.1.0(transitive)
+ Addedwrap-ansi@9.0.0(transitive)
- Removedora@^8.0.1
- Removedcli-spinners@2.9.2(transitive)
- Removedis-interactive@2.0.0(transitive)
- Removedis-unicode-supported@1.3.02.1.0(transitive)
- Removedlog-symbols@6.0.0(transitive)
- Removedora@8.2.0(transitive)
- Removedstdin-discarder@0.2.2(transitive)