|
| 1 | +/* |
| 2 | + * @Date: 2022-08-09 11:24:59 |
| 3 | + * @Author: Yao guan shou |
| 4 | + * @LastEditors: Yao guan shou |
| 5 | + * @LastEditTime: 2022-08-09 13:22:27 |
| 6 | + * @FilePath: /react-loading-ssr/bin/cmd.js |
| 7 | + * @Description: |
| 8 | + */ |
| 9 | +import os from 'os' |
| 10 | +import { spawn, SpawnOptions, exec, execSync } from 'child_process' |
| 11 | +import moment from 'moment' |
| 12 | + |
| 13 | + |
| 14 | +export default class Cmd { |
| 15 | + text = '' |
| 16 | + |
| 17 | + runNodeModule(moduleName, params, options) { |
| 18 | + if (os.type() == 'Windows_NT' && !moduleName.match(/\.cmd$/)) { |
| 19 | + moduleName += '.cmd' |
| 20 | + } |
| 21 | + return this.run(moduleName, params, options) |
| 22 | + } |
| 23 | + |
| 24 | + run(command, params, options) { |
| 25 | + this.text = '' |
| 26 | + // options = Object.assign(options || {}, { cwd: this.cfg.cwd }); |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + console.log(`run command: ${command}, params:`, params, options) |
| 29 | + |
| 30 | + if (!options) { |
| 31 | + options = { |
| 32 | + stdio: 'inherit', |
| 33 | + } |
| 34 | + } |
| 35 | + if (!params) params = [] |
| 36 | + options.stdio = 'pipe' |
| 37 | + |
| 38 | + let proc = spawn(command, params, options) |
| 39 | + // console.log('proc===', proc) |
| 40 | + |
| 41 | + proc.stdout.on('data', (data) => { |
| 42 | + let dataStr = String(data) |
| 43 | + if (options.logPrefix) { |
| 44 | + dataStr = options.logPrefix + dataStr |
| 45 | + } |
| 46 | + this.text += dataStr |
| 47 | + if (!options?.silent) |
| 48 | + process.stdout.write(moment().format('HH:mm:ss:SSS ') + dataStr) |
| 49 | + }) |
| 50 | + |
| 51 | + proc.stderr.on('data', (data) => { |
| 52 | + // 不一定代表进程exitcode != 0,可能只是进程调用了console.error |
| 53 | + let dataStr = String(data) |
| 54 | + if (options?.logPrefix) { |
| 55 | + dataStr = options.logPrefix + dataStr |
| 56 | + } |
| 57 | + if (!options?.silent) |
| 58 | + process.stderr.write(moment().format('HH:mm:ss:SSS ') + dataStr) |
| 59 | + }) |
| 60 | + |
| 61 | + // 进程错误 |
| 62 | + proc.on('error', (error) => { |
| 63 | + if (!options?.silent) console.error(error) |
| 64 | + reject(error) |
| 65 | + }) |
| 66 | + |
| 67 | + // 进程关闭 |
| 68 | + proc.on('close', (code) => { |
| 69 | + console.log(`process closed with exit code: ${code}`) |
| 70 | + if (code == 0) { |
| 71 | + resolve(this.text || '') |
| 72 | + } else { |
| 73 | + let errMsg = `process closed with exit code: ${code}` |
| 74 | + if (options?.logPrefix) { |
| 75 | + errMsg = options.logPrefix + errMsg |
| 76 | + } |
| 77 | + reject(new Error(errMsg)) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + proc.on('exit', (code, signal) => { |
| 82 | + console.log(`process exits`) |
| 83 | + }) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// let cmd = new Cmd().runNodeModule( |
| 89 | +// process.platform === 'win32' ? 'npm.cmd' : 'npm', |
| 90 | +// ['run', 'ssr:dev', '--progress', 'bar:force'], |
| 91 | +// ) |
| 92 | + |
| 93 | +export const execute = (command, args = [], options = { stdio: 'inherit' }) => { |
| 94 | + const proc = spawn(command, args, options) |
| 95 | + |
| 96 | + // 进程错误 |
| 97 | + proc.on('error', (error) => { |
| 98 | + if (error) console.error(error) |
| 99 | + }) |
| 100 | + |
| 101 | + // 进程关闭 |
| 102 | + proc.on('close', (code) => { |
| 103 | + console.log(`process closed with exit code: ${code}`) |
| 104 | + }) |
| 105 | + |
| 106 | + // 退出 |
| 107 | + proc.on('exit', (code, signal) => { |
| 108 | + console.log(`process exits`) |
| 109 | + }) |
| 110 | + |
| 111 | + return proc |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * 判断端口是否被占用 |
| 116 | + * @param port 端口号 |
| 117 | + * @returns 该端口是否被占用 |
| 118 | + */ |
| 119 | +export const iSportTake = (port) => { |
| 120 | + const cmd = process.platform === 'win32' ? 'netstat -aon|findstr' : 'lsof -i:' |
| 121 | + |
| 122 | + let order = `${cmd}${port}` |
| 123 | + |
| 124 | + try { |
| 125 | + const res = execSync(order) |
| 126 | + return true |
| 127 | + } catch (error) { |
| 128 | + console.log('error:', error) |
| 129 | + } |
| 130 | + return false |
| 131 | +} |
0 commit comments