@theintern/digdug
Version:
Dig Dug. A simple abstraction library for downloading and launching WebDriver service tunnels.
104 lines • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFile = exports.kill = exports.fileExists = exports.on = void 0;
var common_1 = require("@theintern/common");
var fs_1 = require("fs");
var path_1 = require("path");
var child_process_1 = require("child_process");
function on(emitter, event, listener) {
emitter.on(event, listener);
return common_1.createHandle(function () { return emitter.removeListener(event, listener); });
}
exports.on = on;
function fileExists(filename) {
try {
fs_1.statSync(filename);
return true;
}
catch (error) {
return false;
}
}
exports.fileExists = fileExists;
function kill(pid) {
var error;
if (pid === undefined) {
return;
}
getChildProcesses(pid).forEach(function (childPid) {
try {
killProcess(childPid);
}
catch (err) {
error = err;
}
});
try {
killProcess(pid);
}
catch (err) {
error = err;
}
if (error) {
throw new Error("Failed to kill " + pid + " or one of its children: " + error.message);
}
}
exports.kill = kill;
function killProcess(pid) {
try {
process.kill(pid);
}
catch (error) {
if (error.code !== 'ESRCH') {
throw error;
}
}
}
function writeFile(data, filename) {
return new Promise(function (resolve, reject) {
function mkdirp(dir) {
if (!dir) {
return;
}
try {
fs_1.mkdirSync(dir);
}
catch (error) {
if (error.code === 'ENOENT') {
mkdirp(path_1.dirname(dir));
mkdirp(dir);
}
else {
if (!fs_1.statSync(dir).isDirectory()) {
throw error;
}
}
}
}
mkdirp(path_1.dirname(filename));
fs_1.writeFile(filename, data, function (error) {
if (error) {
reject(error);
}
else {
resolve();
}
});
});
}
exports.writeFile = writeFile;
function getChildProcesses(pid) {
var command = process.platform === 'win32'
? 'wmic PROCESS GET ParentProcessId,ProcessId'
: 'ps -A -o ppid,pid';
return child_process_1.execSync(command, { encoding: 'utf8' })
.trim()
.split('\n')
.slice(1)
.map(function (line) { return line.trim(); })
.map(function (line) { return line.split(/\s+/).map(function (word) { return word.trim(); }); })
.map(function (words) { return ({ parent: Number(words[0]), child: Number(words[1]) }); })
.filter(function (entry) { return entry.parent === pid; })
.map(function (entry) { return entry.child; });
}
//# sourceMappingURL=util.js.map