Deno 下命令行子進(jìn)程實(shí)操

說白了就是在 subprocess 下執(zhí)行 shell 命令

  • cmd 表示命令,用逗號(hào)來代替 shell 里的空格

下面就是我用 Deno 重寫一遍原來用 shell 來實(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 使用說明:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容