智能照明系统APP-本地串口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
LightingSystemApp-serial/.svn/pristine/4c/4c5c40a0930b11ab107691d34ef...

47 lines
962 B

'use strict'
const childProcess = require('child_process')
function shellExec (cmd = '', opts = {}) {
if (Array.isArray(cmd)) {
cmd = cmd.join(';')
}
opts = Object.assign({ stdio: 'pipe', cwd: process.cwd() }, opts)
let child
const shell = process.platform === 'win32' ? { cmd: 'cmd', arg: '/C' } : { cmd: 'sh', arg: '-c' }
try {
child = childProcess.spawn(shell.cmd, [shell.arg, cmd], opts)
} catch (error) {
return Promise.reject(error)
}
return new Promise(resolve => {
let stdout = ''
let stderr = ''
if (child.stdout) {
child.stdout.on('data', data => {
stdout += data
})
}
if (child.stderr) {
child.stderr.on('data', data => {
stderr += data
})
}
child.on('error', error => {
resolve({ error, stdout, stderr, cmd })
})
child.on('close', code => {
resolve({ stdout, stderr, cmd, code })
})
})
}
module.exports = shellExec