UNPKG

@ipp/cli

Version:

An image build orchestrator for the modern web

108 lines (107 loc) 4.39 kB
#!/usr/bin/env node "use strict"; /** * Image Processing Pipeline - Copyright (c) Marcus Cemes * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.main = void 0; const child_process_1 = require("child_process"); const os_1 = require("os"); const process_1 = __importStar(require("process")); const constants_1 = require("./constants"); /** Environmental variable entry that signifies that process has already been forked */ const FORKED_VARIABLE = "IPP_FORKED"; const BYPASS_VARIABLE = "IPP_NO_FORK"; const CONCURRENCY_FLAGS = ["-c", "--concurrency"]; const RETURN = "\r"; const CLEAR = "\u001b[K"; async function main() { // Prevent the script from running during testing if (typeof process_1.env.ALLOW_BIN === "undefined" && process_1.env.NODE_ENV === "test") return; const concurrency = elevateUvThreads(); if (concurrency !== false) { await start(concurrency); } } exports.main = main; main().catch((err) => console.error(err)); /** * Attempts to increase the number of UV threads available by setting * the UV_THREADPOOL_SIZE variable on supported platforms, otherwise * forks a new process. * * @returns {boolean} True on success, false if the process was forked * and execution should not continue. */ function elevateUvThreads() { if (typeof process_1.env[BYPASS_VARIABLE] !== "undefined") return constants_1.DEFAULT_LIBUV_THREADPOOL; // Prevent an infinite loop of spawned processes if (typeof process_1.env[FORKED_VARIABLE] !== "undefined") { const detectedSize = parseInt(process_1.env.UV_THREADPOOL_SIZE); if (!detectedSize) return constants_1.DEFAULT_LIBUV_THREADPOOL; return Math.max(detectedSize - constants_1.DEFAULT_LIBUV_THREADPOOL, constants_1.DEFAULT_LIBUV_THREADPOOL); } const concurrency = parseConcurrency() || (0, os_1.cpus)().length; const uvThreads = concurrency + constants_1.DEFAULT_LIBUV_THREADPOOL; switch ((0, os_1.platform)()) { case "win32": // Ignore interrupts on the parent, there are no open handles apart from the // forked child process which will handle its own interrupts and exit accordingly. // The `on` method cannot be destructed during import process_1.default.on("SIGINT", () => null); (0, child_process_1.fork)(__filename, process_1.argv.slice(2), { env: { ...process_1.env, UV_THREADPOOL_SIZE: uvThreads.toString(), [FORKED_VARIABLE]: "1", }, }); return false; default: process_1.env.UV_THREADPOOL_SIZE = uvThreads.toString(); } return uvThreads; } async function start(concurrency) { process_1.stdout.write("Just a moment..."); const { init } = await Promise.resolve().then(() => __importStar(require("./init"))); process_1.stdout.write(RETURN + CLEAR); await init(concurrency); } function parseConcurrency() { for (const flag of CONCURRENCY_FLAGS) { const index = process_1.argv.indexOf(flag); if (index !== -1) return parseInt(process_1.argv[index + 1]) || null; } return null; }