2019-01-17 18:22:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.spawn = spawn;
|
|
|
|
exports.spawnReg = spawnReg;
|
|
|
|
exports.addToRegistry = addToRegistry;
|
|
|
|
|
|
|
|
var _child_process = require('child_process');
|
|
|
|
|
|
|
|
var _child_process2 = _interopRequireDefault(_child_process);
|
|
|
|
|
|
|
|
var _path = require('path');
|
|
|
|
|
|
|
|
var _path2 = _interopRequireDefault(_path);
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const regExe = process.env.SystemRoot ? _path2.default.join(process.env.SystemRoot, 'System32', 'reg.exe') : 'reg.exe';
|
2019-01-17 18:22:05 +00:00
|
|
|
|
|
|
|
// Spawn a command and invoke the callback when it completes with an error
|
|
|
|
// and the output from standard out.
|
|
|
|
function spawn(command, args, callback) {
|
2019-03-12 17:01:00 +00:00
|
|
|
let stdout = '';
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
let spawnedProcess;
|
2019-01-17 18:22:05 +00:00
|
|
|
try {
|
|
|
|
// TODO: contrary to below, it should not throw any error
|
|
|
|
spawnedProcess = _child_process2.default.spawn(command, args);
|
|
|
|
} catch (err) {
|
|
|
|
// Spawn can throw an error
|
2019-03-12 17:01:00 +00:00
|
|
|
process.nextTick(() => {
|
2019-01-17 18:22:05 +00:00
|
|
|
if (callback != null) {
|
|
|
|
callback(err, stdout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we need to specify the encoding for the data if we're going to concat it as a string
|
2019-03-12 17:01:00 +00:00
|
|
|
spawnedProcess.stdout.on('data', data => {
|
2019-01-17 18:22:05 +00:00
|
|
|
stdout += data;
|
|
|
|
});
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
let err = null;
|
2019-01-17 18:22:05 +00:00
|
|
|
// TODO: close event might not get called, we should
|
|
|
|
// callback on error https://nodejs.org/api/child_process.html#child_process_event_error
|
2019-03-12 17:01:00 +00:00
|
|
|
spawnedProcess.on('error', err => {
|
2019-01-17 18:22:05 +00:00
|
|
|
// TODO: there should always be an error
|
|
|
|
if (err != null) {
|
|
|
|
err = err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: don't listen to close, but listen to exit instead
|
2019-03-12 17:01:00 +00:00
|
|
|
spawnedProcess.on('close', (code, signal) => {
|
2019-01-17 18:22:05 +00:00
|
|
|
if (err === null && code !== 0) {
|
|
|
|
err = new Error('Command failed: ' + (signal || code));
|
|
|
|
}
|
|
|
|
if (err != null) {
|
|
|
|
err.code = err.code || code;
|
|
|
|
err.stdout = err.stdout || stdout;
|
|
|
|
}
|
|
|
|
if (callback != null) {
|
|
|
|
callback(err, stdout);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spawn reg.exe and callback when it completes
|
|
|
|
function spawnReg(args, callback) {
|
|
|
|
return spawn(regExe, args, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: since we're doing this one by one, we could have a more graceful way of processing the queue
|
|
|
|
// rather than mutating the array
|
|
|
|
function addToRegistry(queue, callback) {
|
|
|
|
if (queue.length === 0) {
|
|
|
|
return callback && callback();
|
|
|
|
}
|
|
|
|
|
2019-03-12 17:01:00 +00:00
|
|
|
const args = queue.shift();
|
2019-01-17 18:22:05 +00:00
|
|
|
args.unshift('add');
|
|
|
|
args.push('/f');
|
2019-03-12 17:01:00 +00:00
|
|
|
return spawnReg(args, () => addToRegistry(queue, callback));
|
2019-01-17 18:22:05 +00:00
|
|
|
}
|