javascript - 有没有办法让js逐行执行这个脚本

标签 javascript node.js reactjs

我正在尝试制作一个脚本,每次创建新的 React 应用程序时都可以自动化我的项目,因此首先我制作了一个运行 main.js 脚本的 bat 文件

这是bat文件

@echo off
setlocal enableextensions

REM run my main
node "C:\Windows\System32\automate\main.js" %1

这是 main.js 文件

const { exec } = require("child_process");
const { replace, rename } = require("./file.js");
const axios = require("axios").default;

let state = {
  cwd: "E:/WORK/Projects",
  token: "[[my github token]]",
  project: {
    name: process.argv[2],
    path: "E:/WORK/Projects/" + process.argv[2],
    src: "E:/WORK/Projects/" + process.argv[2] + "/src",
  },
};

// init
let { cwd, project, token } = state;

//
//
// main
// create the app
exec("create-react-app " + project.name, { cwd });

// install debs
exec("npm i node-sass", { cwd: project.path });

// use sass
rename(project.src + "/index.css", "index.scss");
rename(project.src + "/App.css", "App.scss");
replace(project.src + "/index.js", "./index.css", "./index.scss");
replace(project.src + "/App.js", "./App.css", "./App.scss");

// structure my app
let code = [
  'mkdir "' + project.src + '/App"',
  'mkdir "' + project.src + '/App/Elements"',
  'mkdir "' + project.src + '/App/nav"',
  'touch "' + project.src + '/App/nav.jsx"',
  'touch "' + project.src + '/App/nav/nav.scss"',
];
code = code.join(" && ");
exec(code);

// push to github
axios({
  method: "post",
  url: "https://api.github.com/user/repos?access_token=" + token,
  data: {
    name: project.name,
  },
})
  .then((res) => {
    let repo = res.data.clone_url;
    let c = [
      "git remote add origin " + repo,
      "git push --set-upstream origin master",
      "git push",
    ].join(" && ");
    exec(c);
  })
  .catch((err) => {
    console.log(err.response.data.errors);
  });

// open in code
exec("code " + project.path);

// final message
console.log("Have Fun!");

现在一切都准备好了,但我唯一的问题是每行都异步执行
例如 create-react-app 命令,这需要很多时间,并且接下来的每一行都依赖于它来首先完成

最佳答案

您可以使用execSync代替exec:

const {execSync} = require('child_process');

以下是有关此功能的主要文档:NodeJs Docs

具体说明:

The child_process.execSync() method is generally identical to child_process.exec() with the exception that the method will not return until the child process has fully closed. When a timeout has been encountered and killSignal is sent, the method won't return until the process has completely exited. If the child process intercepts and handles the SIGTERM signal and doesn't exit, the parent process will wait until the child process has exited.

关于javascript - 有没有办法让js逐行执行这个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61043152/

相关文章:

javascript - 根据 CSS 类更改 SVG 填充动画高度

javascript - Bootstrap 表跟随 div 内的滚动

javascript - 将虚拟参数添加到 Google 表格的自定义函数

javascript - MongoDB:从用户文档中删除技能

javascript - 如何使用 JavaScript/Node.js 创建图像?

ReactJS - 使用 ES6 生成器的嵌套元素的递归方法

JavaScript document.getElementById (“id” ) 和元素 id 属性

javascript - 使用 Express NodeJS Rest api 样板创建新路线

javascript - 将变量从 react-router 传递给子组件

javascript - JSX 如何与 HTML 文件交互?