@eljs/utils
Version:
Collection of nodejs utility.
117 lines (115 loc) • 4.17 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/cp/process.ts
var process_exports = {};
__export(process_exports, {
getPid: () => getPid,
sudo: () => sudo
});
module.exports = __toCommonJS(process_exports);
var import_node_child_process = __toESM(require("node:child_process"));
var import_node_os = require("node:os");
var import_read = require("read");
var import_type = require("../type");
var import_command = require("./command");
function getPid(command) {
const parse = (data, command2) => {
const reg = new RegExp("/" + command2 + "$");
const lines = data.trim().split(import_node_os.EOL);
for (const line of lines) {
const fields = line.trim().split(/\s+/, 2);
if (fields.length !== 2) {
continue;
}
const [pid, commandName] = fields;
if (commandName === command2 || reg.test(commandName)) {
return parseInt(pid, 10);
}
}
return null;
};
return new Promise((resolve, reject) => {
(0, import_command.runCommand)("ps -eo pid,comm").then((value) => {
const pid = parse(value.stdout, command);
resolve(pid);
}).catch(reject);
});
}
var cachedPassword;
async function sudo(args, options) {
if ((0, import_type.isObject)(args)) {
options = args;
args = [];
}
const NEED_PASSWORD = "#node-sudo-passwd#";
const {
spawnOptions = {},
password,
cachePassword,
prompt = "sudo requires your password"
} = options || {};
const bin = await (0, import_command.getExecutableCommand)("sudo");
args = ["-S", "-p", NEED_PASSWORD, ...args ? args : []];
spawnOptions.stdio = "pipe";
const child = import_node_child_process.default.spawn(bin, args, spawnOptions);
if (child.stdout) {
child.stdout.on("data", (chunk) => {
console.log(chunk.toString().trim());
});
}
if (child.stderr) {
child.stderr.on("data", (chunk) => {
const lines = chunk.toString().trim().split(import_node_os.EOL);
lines.forEach((line) => {
var _a, _b;
if (line === NEED_PASSWORD) {
if (password) {
(_a = child.stdin) == null ? void 0 : _a.write(password + import_node_os.EOL);
} else if (cachePassword && cachedPassword) {
(_b = child.stdin) == null ? void 0 : _b.write(cachedPassword + import_node_os.EOL);
} else {
(0, import_read.read)({ prompt, silent: true }).then((value) => {
var _a2;
(_a2 = child.stdin) == null ? void 0 : _a2.write(value + import_node_os.EOL);
if (cachePassword) {
cachedPassword = value;
}
});
}
} else {
console.log(line);
}
});
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getPid,
sudo
});