yaogengzhu / daily-share

个人博客记录、内容在issues
30 stars 4 forks source link

利用node child_process 可执行任意脚本 (2023-08-03) #189

Open yaogengzhu opened 1 year ago

yaogengzhu commented 1 year ago

command 可以执行的 py, js 、直接可被执行的bin

const { exec } = require("child_process");
const p = new Promise((r) => {
  const command = "node my-script.js";
  exec(command, (error, stdout, stderr) => {
    if (error) {
      return r({
        success: false,
        error,
        msg: error.toString(),
      });
    }

    return r({
      success: true,
      error: null,
      msg: stdout,
    });
  });
});

p.then((res) => {
  console.log(res);
});