說(shuō)白了就是在 subprocess 下執(zhí)行 shell 命令
- cmd 表示命令惭载,用逗號(hào)來(lái)代替 shell 里的空格
下面就是我用 Deno 重寫(xiě)一遍原來(lái)用 shell 來(lái)實(shí)現(xiàn)打包功能(主要是不會(huì)在命令行生成帶時(shí)間戳的文件名,逃)
import { moment } from 'https://deno.land/x/moment/moment.ts';
const { run } = Deno;
const timeStamp = moment().format('YYYYMMDD-hhmmss');
const removeOld = async () => {
const rimraf = run({
cmd: ['rimraf', 'tool/*.zip'],
});
await rimraf.status();
rimraf.close();
const newFiles = 'dist,.DS_Store,package.json,package-lock.json'.split(',');
for (const file of newFiles) {
const filePath = `tool/${file}`;
const runCmd = run({
cmd: ['rm', '-rf', filePath],
});
await runCmd.status();
runCmd.close();
}
};
const copyNew = async () => {
const newFiles = 'dist,.npmrc,package.json,package-lock.json'.split(',');
for (const file of newFiles) {
const runCmd = run({
cmd: ['cp', file === 'dist' ? '-Rf' : '-f', file, 'tool'],
});
await runCmd.status();
runCmd.close();
}
};
const zip = async () => {
const runCmd = run({
cmd: ['zip', '-r', `tool/導(dǎo)出小工具-${timeStamp}.zip`, 'tool'],
});
await runCmd.status();
runCmd.close();
};
const main = async () => {
await removeOld();
await copyNew();
await zip();
};
main();
為什么不用 copyFile readFile 之類的內(nèi)置命令
答: 這些命令不支持目錄的遍歷操作,也不支持*.*
通配符
API 使用說(shuō)明: