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.

64 lines
2.9 KiB

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getRunningProcess = getRunningProcess;
function _child_process() {
const data = require("child_process");
_child_process = function () {
return data;
};
return data;
}
function path() {
const data = _interopRequireWildcard(require("path"));
path = function () {
return data;
};
return data;
}
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const defaultOptions = {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore']
};
function getPID(port) {
return (0, _child_process().execFileSync)('lsof', [`-i:${port}`, '-P', '-t', '-sTCP:LISTEN'], defaultOptions).split('\n')[0].trim();
}
function getPackageName(packageRoot) {
const packageJson = path().join(packageRoot.trim(), 'package.json');
try {
return require(packageJson).name || null;
} catch {
return null;
}
}
function getProcessCommand(pid, procDirectory) {
const results = (0, _child_process().execSync)(`ps -o command -p ${pid} | sed -n 2p`, defaultOptions).replace(/\n$/, '').trim();
const name = getPackageName(procDirectory);
return name ? name : results;
}
function getDirectoryOfProcessById(processId) {
return (0, _child_process().execSync)('lsof -p ' + processId + ' | awk \'$4=="cwd" {for (i=9; i<=NF; i++) printf "%s ", $i}\'', defaultOptions).trim();
}
function getRunningProcess(port) {
try {
// 63828
const pid = getPID(port);
// /Users/evanbacon/Documents/GitHub/lab/myapp
const directory = getDirectoryOfProcessById(pid);
// /Users/evanbacon/Documents/GitHub/lab/myapp/package.json
const command = getProcessCommand(pid, directory);
// TODO: Have a better message for reusing another process.
return {
pid,
directory,
command
};
} catch {
return null;
}
}
//# sourceMappingURL=getRunningProcess.js.map