just-scripts
Version:
Just Stack Scripts
65 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettierCheckTask = exports.prettierTask = void 0;
const just_task_1 = require("just-task");
const just_scripts_utils_1 = require("just-scripts-utils");
const splitArrayIntoChunks_1 = require("../arrayUtils/splitArrayIntoChunks");
const path = require("path");
const arrayify_1 = require("../arrayUtils/arrayify");
function prettierTask(options = {}) {
const prettierBin = just_task_1.resolve('prettier/bin-prettier.js');
if (prettierBin) {
return function prettier() {
return runPrettierAsync({
prettierBin,
...{ configPath: options.configPath || undefined },
...{ ignorePath: options.ignorePath || undefined },
...{
files: arrayify_1.arrayify(options.files || path.resolve(process.cwd(), '**', '*.{ts,tsx,js,jsx,json,scss,html,yml,md}')),
},
check: false,
});
};
}
return function () {
just_task_1.logger.warn('Prettier is not available, ignoring this task');
};
}
exports.prettierTask = prettierTask;
function prettierCheckTask(options = {}) {
const prettierBin = just_task_1.resolve('prettier/bin-prettier.js');
if (prettierBin) {
return function prettierCheck() {
return runPrettierAsync({
prettierBin,
...{ configPath: options.configPath || undefined },
...{ ignorePath: options.ignorePath || undefined },
...{
files: arrayify_1.arrayify(options.files || path.resolve(process.cwd(), '**', '*.{ts,tsx,js,jsx,json,scss,html,yml,md}')),
},
check: true,
});
};
}
return function () {
just_task_1.logger.warn('Prettier is not available, ignoring this task');
};
}
exports.prettierCheckTask = prettierCheckTask;
function runPrettierAsync(context) {
const MaxFileEntriesPerChunk = 20;
const { prettierBin, configPath, ignorePath, files, check } = context;
const chunks = splitArrayIntoChunks_1.splitArrayIntoChunks(files, MaxFileEntriesPerChunk);
return chunks.reduce((finishPromise, chunk) => {
const prettierArgs = [
prettierBin,
...(configPath ? ['--config', configPath] : []),
...(ignorePath ? ['--ignore-path', ignorePath] : []),
...(check ? ['--check'] : ['--write']),
...chunk,
];
just_task_1.logger.info(process.execPath + ' ' + prettierArgs.join(' '));
return finishPromise.then(() => just_scripts_utils_1.spawn(process.execPath, prettierArgs, { stdio: 'inherit' }));
}, Promise.resolve());
}
//# sourceMappingURL=prettierTask.js.map