UNPKG

@cliz/gpm

Version:
75 lines (74 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runInBackground = exports.runInShell = void 0; const child_process_1 = require("child_process"); const kill = require("tree-kill"); async function runInShell(command, options) { if (options === null || options === void 0 ? void 0 : options.env) { for (const key in options.env) { process.env[key] = '' + options.env[key]; } } return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, { shell: true, stdio: 'inherit', cwd: options === null || options === void 0 ? void 0 : options.cwd, }); // child.on('data', e => console.log('daa:', e)); child.on('exit', (code) => { if (code !== 0) return reject(`run command error (command: ${command}, code: ${code})`); resolve(); }); child.on('error', (error) => { return reject(`run command error (command: ${command}, error: ${error === null || error === void 0 ? void 0 : error.message})`); }); child.kill = () => { return new Promise((resolve, reject) => { kill(child.pid, (error) => { if (error) { return reject(error); } return resolve(); }); }); }; }); } exports.runInShell = runInShell; async function runInBackground(command, options) { if (options === null || options === void 0 ? void 0 : options.env) { for (const key in options.env) { process.env[key] = '' + options.env[key]; } } return new Promise((resolve, reject) => { // console.log('command context:', options?.cwd, command); const child = (0, child_process_1.spawn)(command, { shell: true, stdio: 'inherit', cwd: options === null || options === void 0 ? void 0 : options.cwd, }); child.on('exit', (code) => { if (code !== 0) return reject(`run command error (command: ${command}, code: ${code})`); resolve(child); }); child.on('error', (error) => { return reject(`run command error (command: ${command}, error: ${error === null || error === void 0 ? void 0 : error.message})`); }); child.kill = () => { return new Promise((resolve, reject) => { kill(child.pid, (error) => { if (error) { return reject(error); } return resolve(); }); }); }; resolve(child); }); } exports.runInBackground = runInBackground;