Validate that input files exist before attempting to process them

This commit is contained in:
Peter Fernandes
2020-12-09 17:14:43 -05:00
parent 5d691af8a1
commit 15248bf85a

View File

@@ -4,6 +4,7 @@ import { isMainThread } from 'worker_threads';
import { cpus } from 'os';
import { extname, join, basename } from 'path';
import { promises as fsp } from 'fs';
import { resolve as resolvePath } from 'path';
import { version } from 'json:../package.json';
import ora from 'ora';
import kleur from 'kleur';
@@ -182,7 +183,30 @@ function progressTracker(results) {
return tracker;
}
async function checkInputFilesValid(files) {
const validFiles = [];
for (const file of files) {
try {
await fsp.stat(file);
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(
`Warning: Input file does not exist: ${resolvePath(file)}`,
);
continue;
} else throw err;
}
validFiles.push(file);
}
return validFiles;
}
async function processFiles(files) {
files = await checkInputFilesValid(files);
const parallelism = cpus().length;
const results = new Map();