pretty-parallel
Version:
Concurrent prettier runner
87 lines • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processParallel = void 0;
const tslib_1 = require("tslib");
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const ora_1 = tslib_1.__importDefault(require("ora"));
const piscina_1 = tslib_1.__importDefault(require("piscina"));
const prettier = tslib_1.__importStar(require("prettier"));
const pretty_ms_1 = tslib_1.__importDefault(require("pretty-ms"));
const files_resolver_1 = require("./files-resolver");
const progress_reporter_1 = require("./progress-reporter");
class WorkerError extends Error {
constructor(filePath, error) {
super();
this.filePath = filePath;
this.error = error;
Object.setPrototypeOf(this, WorkerError.prototype);
}
}
async function processParallel(type, { filePatterns, workingDir, maxWorkers, config }) {
const resolveFilesStart = Date.now();
const prettierIgnorePath = node_path_1.default.resolve(node_path_1.default.join(workingDir, '.prettierignore'));
const packageJsonPath = node_path_1.default.join(workingDir, 'package.json');
const prettierConfigPath = config || (await prettier.resolveConfigFile(packageJsonPath));
const prettierOptions = prettierConfigPath &&
(await prettier.resolveConfig(packageJsonPath, {
config: prettierConfigPath,
editorconfig: true,
}));
const fileStructureLoadingSpinner = (0, ora_1.default)('Loading file structure').start();
const { filePaths } = await (0, files_resolver_1.resolveFilePaths)(filePatterns, prettierIgnorePath);
fileStructureLoadingSpinner.info(`Found ${chalk_1.default.bold(filePaths.length)} ${filePaths.length === 1 ? 'file' : 'files'} [${(0, pretty_ms_1.default)(Date.now() - resolveFilesStart)}]`);
const prettierStart = Date.now();
const piscina = new piscina_1.default({
filename: node_path_1.default.resolve(__dirname, 'worker.js'),
maxThreads: maxWorkers,
workerData: prettierOptions,
});
const reporter = new progress_reporter_1.ProgressReporter(filePaths.length, type);
try {
const tasks = [];
for (const filePath of filePaths) {
tasks.push(piscina
.run(filePath, { name: type })
.then((result) => ({
result,
filePath,
}))
.catch((err) => {
throw new WorkerError(filePath, err);
})
.finally(() => reporter.update(piscina.completed)));
}
const result = await Promise.all(tasks);
if (type === 'check') {
const checkFailures = result.filter((c) => c.result === false);
if (checkFailures.length > 0) {
checkFailures.forEach(({ filePath }) => reporter.warn(`Check failed: ${chalk_1.default.bold(filePath)}`));
reporter.fail(`Code style issues found in the ${chalk_1.default.bold(checkFailures.length)} ${checkFailures.length === 1 ? 'file' : 'files'} above. Forgot to run Prettier? [${(0, pretty_ms_1.default)(Date.now() - prettierStart)}]`);
process.exit(1);
}
else {
reporter.succeed(`Successfully checked ${chalk_1.default.bold(filePaths.length)} ${filePaths.length === 1 ? 'file' : 'files'}! [${(0, pretty_ms_1.default)(Date.now() - prettierStart)}]`);
process.exit(0);
}
}
else {
result.forEach(({ filePath, result }) => result && reporter.succeed(`Fixed up ${chalk_1.default.bold(filePath)}`));
reporter.succeed(`Successfully formatted ${chalk_1.default.bold(filePaths.length)} ${filePaths.length === 1 ? 'file' : 'files'}! [${(0, pretty_ms_1.default)(Date.now() - prettierStart)}]`);
process.exit(0);
}
}
catch (err) {
if (err instanceof WorkerError) {
reporter.fail(`An error occurred while parsing file '${err.filePath}'!`);
console.error(err.error);
process.exit(1);
}
else {
throw err;
}
}
console.log(`Total time ${(Date.now() - resolveFilesStart) / 1000}s `);
}
exports.processParallel = processParallel;
//# sourceMappingURL=process.js.map