-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathshell.js
More file actions
172 lines (157 loc) · 4.11 KB
/
shell.js
File metadata and controls
172 lines (157 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const childProcess = require('node:child_process')
const os = require('node:os')
const fixPath = require('fix-path')
const PowerShell = require('node-powershell')
const log = require('../utils/util.log.core')
fixPath()
class SystemShell {
static async exec (cmds, args) {
throw new Error('You have to implement the method exec!')
}
}
class LinuxSystemShell extends SystemShell {
static async exec (cmds) {
if (typeof cmds === 'string') {
cmds = [cmds]
}
for (const cmd of cmds) {
await childExec(cmd, { shell: '/bin/bash' })
}
}
}
class DarwinSystemShell extends SystemShell {
static async exec (cmds) {
if (typeof cmds === 'string') {
cmds = [cmds]
}
let ret
for (const cmd of cmds) {
ret = await childExec(cmd)
}
return ret
}
}
class WindowsSystemShell extends SystemShell {
static async exec (cmds, args = { }) {
let { type } = args
type = type || 'ps'
if (typeof cmds === 'string') {
cmds = [cmds]
}
if (type === 'ps') {
const ps = new PowerShell({
executionPolicy: 'Bypass',
noProfile: true,
})
for (const cmd of cmds) {
ps.addCommand(cmd)
}
try {
return await ps.invoke()
} finally {
ps.dispose()
}
} else {
await childExecCmdWindows('chcp 65001', args)
let ret
for (const cmd of cmds) {
ret = await childExecCmdWindows(cmd, args)
}
return ret
}
}
}
function childExecCmdWindows (cmd, options = {}) {
return new Promise((resolve, reject) => {
const execOptions = { ...options }
delete execOptions.type
delete execOptions.printErrorLog
log.info('shell:', cmd)
childProcess.execFile('cmd.exe', ['/d', '/s', '/c', cmd], execOptions, (error, stdout, stderr) => {
if (error) {
if (options.printErrorLog !== false) {
log.error('cmd 命令执行错误:\n===>\ncommands:', cmd, '\n error:', error, '\n<===')
}
reject(new Error(stderr))
} else {
resolve(stdout.replace('Active code page: 65001\r\n', ''))
}
})
})
}
function childExec (composeCmds, options = {}) {
return new Promise((resolve, reject) => {
log.info('shell:', composeCmds)
childProcess.exec(composeCmds, options, (error, stdout, stderr) => {
if (error) {
if (options.printErrorLog !== false) {
log.error('cmd 命令执行错误:\n===>\ncommands:', composeCmds, '\n error:', error, '\n<===')
}
reject(new Error(stderr))
} else {
// log.info('cmd 命令完成:', stdout)
resolve(stdout.replace('Active code page: 65001\r\n', ''))
}
// log.info('关闭 cmd')
// ps.kill('SIGINT')
})
})
}
function getSystemShell () {
switch (getSystemPlatform(true)) {
case 'mac':
return DarwinSystemShell
case 'linux':
return LinuxSystemShell
case 'windows':
return WindowsSystemShell
default:
throw new Error(`UNKNOWN OS TYPE ${os.platform()}`)
}
}
function getSystemPlatform (throwIfUnknown = false) {
switch (os.platform()) {
case 'darwin':
return 'mac'
case 'linux':
return 'linux'
case 'win32':
return 'windows'
case 'win64':
return 'windows'
default:
log.error(`UNKNOWN OS TYPE: ${os.platform()}`)
if (throwIfUnknown) {
throw new Error(`UNKNOWN OS TYPE '${os.platform()}'`)
} else {
return 'unknown-os'
}
}
}
async function execute (executor, args) {
return executor[getSystemPlatform(true)](getSystemShell().exec, args)
}
async function execFile (file, args, options) {
return new Promise((resolve, reject) => {
try {
childProcess.execFile(file, args, options, (err, stdout) => {
if (err) {
log.error('文件执行出错:', file, err)
reject(err)
return
}
log.debug('文件执行成功:', file)
resolve(stdout)
})
} catch (e) {
log.error('文件执行出错:', file, e)
reject(e)
}
})
}
module.exports = {
getSystemShell,
getSystemPlatform,
execute,
execFile,
}