UNPKG

@rechunk/utils

Version:

Utility functions and helpers for ReChunk packages and implementations

123 lines (121 loc) 4.82 kB
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/index.ts var index_exports = {}; __export(index_exports, { findClosestJSON: () => findClosestJSON, findWorkspaceDir: () => findWorkspaceDir, getRealPath: () => getRealPath, isRechunkDevServerRunning: () => isRechunkDevServerRunning, nonWindowsCall: () => nonWindowsCall }); module.exports = __toCommonJS(index_exports); var import_child_process = require("child_process"); var import_find_up = __toESM(require("find-up")); var import_fs = __toESM(require("fs")); var import_path = require("path"); function getRealPath(filePath) { try { return import_fs.default.realpathSync.native(filePath); } catch { return filePath; } } function findClosestJSON(filename, start = process.cwd(), level = 0) { try { const path = (0, import_path.resolve)(start, filename); const content = import_fs.default.readFileSync(path, { encoding: "utf8" }); return JSON.parse(content); } catch { return level >= 10 ? {} : findClosestJSON(filename, (0, import_path.dirname)(start), level + 1); } } function findWorkspaceDir(cwd) { if (process.env.WORKSPACE_DIR) { return process.env.WORKSPACE_DIR; } const manifestLocation = import_find_up.default.sync( ["package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lockb"], { cwd: getRealPath(cwd) } ); return manifestLocation ? (0, import_path.dirname)(manifestLocation) : void 0; } var TEN_MEGABYTES = 1e3 * 1e3 * 10; var PS_OUTPUT_REGEX = /^[ \t]*(?<pid>\d+)[ \t]+(?<ppid>\d+)[ \t]+(?<uid>[-\d]+)[ \t]+(?<cpu>\d+\.\d+)[ \t]+(?<memory>\d+\.\d+)[ \t]+(?<comm>.*)?/; function nonWindowsCall(options = {}) { const flags = options.all === false ? "wwxo" : "awwxo"; const psOutput = (0, import_child_process.execFileSync)("ps", [flags, "pid,ppid,uid,%cpu,%mem,comm"], { maxBuffer: TEN_MEGABYTES }).toString(); const psArgsOutput = (0, import_child_process.execFileSync)("ps", [flags, "pid,args"], { maxBuffer: TEN_MEGABYTES }).toString(); const psLines = psOutput.trim().split("\n"); const psArgsLines = psArgsOutput.trim().split("\n"); psLines.shift(); psArgsLines.shift(); const processCmds = {}; for (const line of psArgsLines) { const [pid, ...cmds] = line.trim().split(" "); processCmds[pid] = cmds.join(" "); } return psLines.map((line) => { const match = PS_OUTPUT_REGEX.exec(line); if (!match?.groups) throw new Error("ps output parsing failed"); const { pid, ppid, uid, cpu, memory, comm } = match.groups; return { pid: parseInt(pid, 10), ppid: parseInt(ppid, 10), uid: parseInt(uid, 10), cpu: parseFloat(cpu), memory: parseFloat(memory), name: (0, import_path.basename)(comm), cmd: processCmds[pid] }; }); } function isRechunkDevServerRunning() { const processes = nonWindowsCall(); const workspaceDir = findWorkspaceDir(process.cwd()); const NODE_IDENTIFIER = "node"; const NODE_MODULES_PATH = "./node_modules"; const DEV_SERVER_CMD = "rechunk dev-server"; return processes.some( (process2) => typeof process2.cmd === "string" && process2.cmd.includes(DEV_SERVER_CMD) && (process2.cmd.startsWith(`${NODE_IDENTIFIER} ${NODE_MODULES_PATH}`) || process2.cmd.startsWith(`${NODE_IDENTIFIER} ${workspaceDir}`)) ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { findClosestJSON, findWorkspaceDir, getRealPath, isRechunkDevServerRunning, nonWindowsCall });