powerShell optimizatio error handling
This commit is contained in:
parent
78f4ef5046
commit
8df7d8a93a
31
lib/util.js
31
lib/util.js
@ -218,24 +218,10 @@ function getWmic() {
|
|||||||
|
|
||||||
function powerShell(cmd) {
|
function powerShell(cmd) {
|
||||||
|
|
||||||
// helper function
|
|
||||||
function dispose(child) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
child.stdin.write('exit' + os.EOL);
|
|
||||||
process.nextTick(() => {
|
|
||||||
child.stdin.end()
|
|
||||||
child.kill();
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = '';
|
let result = '';
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
|
const child = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-ExecutionPolicy', 'Unrestricted', '-Command', '-'], {
|
||||||
stdio: 'pipe'
|
stdio: 'pipe'
|
||||||
@ -246,20 +232,24 @@ function powerShell(cmd) {
|
|||||||
result = result + data.toString('utf8');
|
result = result + data.toString('utf8');
|
||||||
});
|
});
|
||||||
child.stderr.on('data', function (data) {
|
child.stderr.on('data', function (data) {
|
||||||
dispose(child).then(resolve(result));
|
child.kill();
|
||||||
|
resolve(result);
|
||||||
});
|
});
|
||||||
child.on('close', function () {
|
child.on('close', function () {
|
||||||
dispose(child).then(resolve(result));
|
child.kill();
|
||||||
;
|
resolve(result);
|
||||||
});
|
});
|
||||||
child.on('error', function () {
|
child.on('error', function () {
|
||||||
dispose(child).then(resolve(result));
|
child.kill();
|
||||||
|
resolve(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
child.stdin.write(cmd + os.EOL);
|
child.stdin.write(cmd + os.EOL);
|
||||||
|
child.stdin.write('exit' + os.EOL);
|
||||||
|
child.stdin.end()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
dispose(child).then(resolve(result));
|
child.kill();
|
||||||
|
resolve(result);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
resolve(result);
|
resolve(result);
|
||||||
@ -270,7 +260,6 @@ function powerShell(cmd) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCodepage() {
|
function getCodepage() {
|
||||||
if (_windows) {
|
if (_windows) {
|
||||||
if (!codepage) {
|
if (!codepage) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user