@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
143 lines (141 loc) • 4.11 kB
JavaScript
import { ps } from "./ps.mjs";
import { wmic } from "./wmic.mjs";
import * as os$1 from "node:os";
//#region src/utils/runParallel/pidTree.ts
const platformToMethod = {
darwin: "ps",
sunos: "ps",
freebsd: "ps",
netbsd: "ps",
win: "wmic",
linux: "ps",
aix: "ps"
};
const methodToFn = {
ps,
wmic
};
let platform = os$1.platform();
if (platform.startsWith("win")) platform = "win";
const method = platformToMethod[platform];
/**
* Gets the list of all the pids of the system.
*/
const getAll = (callback) => {
if (method === void 0) {
callback(/* @__PURE__ */ new Error(os$1.platform() + " is not supported yet, please open an issue (https://github.com/simonepri/pidtree)"), void 0);
return;
}
const listFn = methodToFn[method];
listFn(callback);
};
/**
* Get the list of children and grandchildren pids of the given PID.
* @param PID A PID. If -1 will return all the pids.
* @param options Optional options object.
* @param callback Called when the list is ready.
*/
const pidtree = (PID, options, callback) => {
let normalizedOptions;
let normalizedCallback;
if (typeof options === "function") {
normalizedCallback = options;
normalizedOptions = {};
} else {
normalizedCallback = callback;
normalizedOptions = typeof options === "object" ? options : {};
}
const parsedPID = parseInt(String(PID), 10);
if (Number.isNaN(parsedPID) || parsedPID < -1) {
normalizedCallback(/* @__PURE__ */ new TypeError("The pid provided is invalid"), void 0);
return;
}
getAll((err, processList) => {
if (err) {
normalizedCallback(err, void 0);
return;
}
if (!processList) {
normalizedCallback(/* @__PURE__ */ new Error("Failed to get process list"), void 0);
return;
}
if (parsedPID === -1) {
const result = processList.map(([ppid, pid]) => normalizedOptions.advanced ? {
ppid,
pid
} : pid);
normalizedCallback(null, result);
return;
}
let root;
for (let l = 0; l < processList.length; l++) {
if (processList[l][1] === parsedPID) {
root = normalizedOptions.advanced ? {
ppid: processList[l][0],
pid: parsedPID
} : parsedPID;
break;
}
if (processList[l][0] === parsedPID) root = normalizedOptions.advanced ? { pid: parsedPID } : parsedPID;
}
if (root === void 0) {
normalizedCallback(/* @__PURE__ */ new Error("No matching pid found"), void 0);
return;
}
const tree = {};
const listCopy = [...processList];
while (listCopy.length > 0) {
const element = listCopy.pop();
if (tree[element[0]]) tree[element[0]].push(element[1]);
else tree[element[0]] = [element[1]];
}
let idx = 0;
const pids = [root];
while (idx < pids.length) {
const curpid = normalizedOptions.advanced ? pids[idx++].pid : pids[idx++];
if (!tree[curpid]) continue;
const length = tree[curpid].length;
for (let j = 0; j < length; j++) pids.push(normalizedOptions.advanced ? {
ppid: curpid,
pid: tree[curpid][j]
} : tree[curpid][j]);
delete tree[curpid];
}
if (!normalizedOptions.root) pids.shift();
normalizedCallback(null, pids);
});
};
const pify = (fn, arg1, arg2) => {
return new Promise((resolve, reject) => {
fn(arg1, arg2, (err, data) => {
if (err) return reject(err);
if (data === void 0) return reject(/* @__PURE__ */ new Error("No data returned"));
resolve(data);
});
});
};
/* istanbul ignore if */
if (!String.prototype.startsWith) String.prototype.startsWith = function(suffix) {
return this.substring(0, suffix.length) === suffix;
};
/**
* Get the list of children pids of the given pid.
* @param pid A PID. If -1 will return all the pids.
* @param options Optional options object.
* @param callback Called when the list is ready. If not provided a promise is returned instead.
* @returns Only when the callback is not provided.
*/
const list = (pid, options, callback) => {
if (typeof options === "function") {
pidtree(pid, void 0, options);
return;
}
if (typeof callback === "function") {
pidtree(pid, options, callback);
return;
}
return pify(pidtree, pid, options);
};
//#endregion
export { list };
//# sourceMappingURL=pidTree.mjs.map