daizong
Version:
`package.json` script runner for ES Modules
44 lines • 1.67 kB
JavaScript
import { deleteAsync } from 'del';
import chalk from 'chalk';
import pMap from 'p-map';
import * as np from 'path';
import { mkdir as nodeMkdir } from 'fs/promises';
function mkdir(path) {
return nodeMkdir(path, { recursive: true });
}
function stringToList(input) {
if (typeof input === 'string') {
return [input];
}
return input;
}
function resolvePath(path, workingDir) {
if (workingDir) {
return np.join(workingDir, path);
}
return path;
}
export async function runBTCommands(cmds, workingDir) {
const { mkdir: mkdirInput, del: delInput, parallel, mkdirDel } = cmds;
const concurrency = parallel ? undefined : 1;
await pMap(Object.keys(cmds), async (prop) => {
if (prop === 'mkdir' && mkdirInput) {
console.log(`>> ${chalk.gray(`mkdir "${mkdirInput}"`)}`);
await Promise.all(stringToList(mkdirInput).map((s) => mkdir(resolvePath(s, workingDir))));
}
else if (prop === 'del' && delInput) {
console.log(`>> ${chalk.gray(`del ${JSON.stringify(delInput)}`)}`);
const pList = typeof delInput === 'string' ? [delInput] : delInput;
await deleteAsync(pList.map((s) => resolvePath(s, workingDir)), { force: true });
}
else if (prop === 'mkdirDel' && mkdirDel) {
console.log(`>> ${chalk.gray(`mkdirDel "${mkdirDel}"`)}`);
await Promise.all(stringToList(mkdirDel).map(async (s) => {
const p = resolvePath(s, workingDir);
await deleteAsync(p);
await mkdir(p);
}));
}
}, { concurrency });
}
//# sourceMappingURL=btCmd.js.map