Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566// npm 11.7.0's run-script.js (`npm run`, and test, start, stop and restart through// lifecycle-cmd.js): the script with its pre and post scripts, a missing one's error// with did-you-mean.js's suggestions, and the script list. Guest code.import { existsSync } from "node:fs";import { resolve } from "node:path";import process from "node:process";import { didYouMean, type Command } from "./command";import { NpmFailure } from "./output";import { exitLike, readPackage, runScript, type Pkg } from "./run-script";
const LIFECYCLE_LIST = [ "prepare", "prepublishOnly", "prepack", "postpack", "dependencies", "preinstall", "install", "postinstall", "prepublish", "publish", "postpublish", "prerestart", "restart", "postrestart", "prestart", "start", "poststart", "prestop", "stop", "poststop", "pretest", "test", "posttest", "preuninstall", "uninstall", "postuninstall", "preversion", "version", "postversion",];
/** #list, uncoloured: lifecycle scripts first, then the rest "via `npm run`". */function list(pkg: Pkg): string { const entries = Object.entries(pkg.scripts ?? {}); if (!entries.length) return ""; const lifecycle = entries.filter(([k]) => LIFECYCLE_LIST.includes(k)); const others = entries.filter(([k]) => !LIFECYCLE_LIST.includes(k)); const pkgId = `in ${pkg._id || pkg.name}`; const lines: string[] = []; const show = ([k, v]: [string, string]) => lines.push(` ${k}`, ` ${v}`); if (lifecycle.length) { lines.push(`Lifecycle scripts included ${pkgId}:`); lifecycle.forEach(show); } if (others.length) { lines.push(lifecycle.length ? "available via `npm run`:" : `Scripts available ${pkgId} via \`npm run\`:`); others.forEach(show); } return `${lines.join("\n")}\n`;}
export async function run(command: Extract<Command, { kind: "run" }>, path: string): Promise<void> { const pkg = readPackage(path); if (command.event === null) { process.stdout.write(list(pkg)); return; } const { event, args } = command; const scripts = { ...pkg.scripts }; if (event === "restart" && !scripts.restart) scripts.restart = "npm stop --if-present && npm start"; if (event === "env" && !scripts.env) scripts.env = "env"; if (event === "start" && !scripts.start && existsSync(resolve(path, "server.js"))) scripts.start = "node server.js"; if (!Object.hasOwn(scripts, event)) { if (command.ifPresent) return; throw new NpmFailure({ message: [`Missing script: "${event}"${didYouMean(pkg, event)}\n`, "To see a list of scripts, run:", " npm run"].join("\n") }); } // Positional args go to the event itself, not its pre and post scripts. const events: Array<[string, readonly string[]]> = [[event, args]]; if (!command.ignoreScripts) { if (scripts[`pre${event}`]) events.unshift([`pre${event}`, []]); if (scripts[`post${event}`]) events.push([`post${event}`, []]); } for (const [ev, evArgs] of events) { const cmd = scripts[ev]; if (!cmd) continue; const exit = await runScript({ pkg: { ...pkg, scripts }, path, event: ev, cmd, args: evArgs, banner: true }); if (exit.code || exit.signal) return exitLike(exit); }}