service-core/core/util.mjs

37 lines
1004 B
JavaScript
Raw Normal View History

2020-09-07 00:47:53 +00:00
import path from 'path'
import { spawn } from 'child_process'
import { fileURLToPath } from 'url'
export function getPathFromRoot(add) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
return path.join(__dirname,'../', add)
}
2020-09-07 01:25:03 +00:00
export function runCommand(command, options = [], folder = null, stream = function() {}) {
2020-09-07 00:47:53 +00:00
return new Promise(function(res, rej) {
2020-09-07 01:25:03 +00:00
let processor = spawn(command, options, {
shell: true,
cwd: folder,
})
let timeOuter = setTimeout(function() {
processor.stdin.write('n\n')
}, 250)
2020-09-07 00:47:53 +00:00
processor.stdout.on('data', function(data) {
stream(data.toString())
2020-09-07 01:25:03 +00:00
processor.stdin.write('n\n')
2020-09-07 00:47:53 +00:00
})
processor.stderr.on('data', function(data) {
stream(data.toString())
2020-09-07 01:25:03 +00:00
processor.stdin.write('n\n')
2020-09-07 00:47:53 +00:00
})
processor.on('error', function(err) {
2020-09-07 01:25:03 +00:00
clearInterval(timeOuter)
2020-09-07 00:47:53 +00:00
rej(err)
})
processor.on('exit', function (code) {
2020-09-07 01:25:03 +00:00
clearInterval(timeOuter)
2020-09-07 00:47:53 +00:00
res(code)
})
})
}