如何在节点应用程序中执行节点命令?

How to execute node command in a node app?

所以基本上,我正在尝试制作一个 cli 来自动执行我一直重复的一些小任务。在这里,我想将 console.log 替换为 运行 命令的内容。我尝试使用 'child_process' 的 exec,但它没有用,我以无限加载器结束

switch (arg) {
    case "React App":
      console.log(`npx create-react-app ${projectName}`);
      console.log(`cd ${projectName}`);
      console.log(`npm start`);
      break;
    case "Next App":
      console.log(`npx create-next-app@latest ${projectName}`);
      console.log(`cd ${projectName}`);
      console.log(`npm dev`);
      break;

这是带exec的版本

 switch (arg) {
case "React App":
  () => exec(`npx create-react-app ${projectName}`);
  () => exec(`cd ${projectName}`);
  () => exec(`npm start`);
  break;
case "Next App":
  () => exec(`npx create-next-app@latest ${projectName}`);
  () => exec(`cd ${projectName}`);
  () => exec(`npm dev`);
  break;

我在许多论坛和堆栈溢出上进行了搜索,我认为我不是以英语为母语的人这一事实从来都不是那么明显,因为该死的我没有找到我想要的东西

运行它依次与execSync

const execSync = require('child_process').execSync;

//...
switch (arg) {
    case "React App":
        console.log(`npx create-react-app ${projectName}`);
        execSync(`npx create-react-app ${projectName}`);
        console.log(`cd ${projectName}`);
        execSync(`cd ${projectName}`);
        console.log(`npm start`);
        execSync(`npm start`);
        break;
    case "Next App":
        console.log(`npx create-next-app@latest ${projectName}`);
        execSync(`npx create-next-app@latest ${projectName}`);
        console.log(`cd ${projectName}`);
        execSync(`cd ${projectName}`);
        console.log(`npm dev`);
        execSync(`npm dev`);
        break;
}