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