UNPKG

@ainc/script

Version:

Script compiler for typescript

50 lines (41 loc) 1.31 kB
/** ***************************************** * Created by edonet@163.com * Created on 2021-06-27 15:18:50 ***************************************** */ 'use strict'; /** ***************************************** * 加载依赖 ***************************************** */ import * as os from 'os'; import * as cp from 'child_process'; /** ***************************************** * 执行命令 ***************************************** */ export function spawn(command: string, args?: string[], options?: cp.SpawnOptions): Promise<void> { return new Promise((resolve, reject) => { const shell = os.platform() !== 'linux'; const child = cp.spawn(command, args || [], { shell, stdio: 'inherit', ...options }); // 监听事件 child.on('close', resolve); child.on('error', reject); }); } /** ***************************************** * 执行文件 ***************************************** */ export function fork(file: string, args?: string[], options?: cp.ForkOptions): Promise<void> { return new Promise((resolve, reject) => { const child = cp.fork(file, args || [], { stdio: 'inherit', detached : true, ...options }); // 监听事件 child.on('close', resolve); child.on('error', reject); }); }