UNPKG

isolate-package

Version:

Isolate monorepo packages to form a self-contained deployable unit

148 lines (142 loc) 4.87 kB
#!/usr/bin/env node import { a as loadConfigFromFile, t as isolate, u as filterObjectUndefined } from "./isolate-B0zQOSqE.mjs"; import { outdent } from "outdent"; import console from "node:console"; import meow from "meow"; import sourceMaps from "source-map-support"; //#region src/lib/cli.ts const validLogLevels = [ "info", "debug", "warn", "error" ]; /** * Check if a boolean flag was explicitly passed on the command line. meow * returns `false` for unset boolean flags, making it impossible to distinguish * "not passed" from "explicitly set to false" via `--no-<flag>`. */ function wasFlagExplicitlyPassed(flagName, argv, shortFlag) { const kebab = flagName.replace(/[A-Z]/g, (l) => `-${l.toLowerCase()}`); return argv.some((arg) => arg === `--${kebab}` || arg === `--no-${kebab}` || shortFlag !== void 0 && arg === `-${shortFlag}`); } /** * Validate a log level string against the allowed values. Returns undefined if * the input is undefined, throws if the value is not a valid log level. */ function parseLogLevel(value) { if (value === void 0) return; if (!validLogLevels.includes(value)) throw new Error(`Invalid log level: "${value}". Must be one of: ${validLogLevels.join(", ")}`); return value; } /** * Build CLI overrides from parsed meow flags. Only includes values that were * actually provided by the user, so they can cleanly override config file * values via spread. */ function buildCliOverrides(flags, argv) { const logLevel = parseLogLevel(flags.logLevel); return filterObjectUndefined({ buildDirName: flags.buildDirName, isolateDirName: flags.isolateDirName, logLevel, targetPackagePath: flags.targetPackagePath, tsconfigPath: flags.tsconfigPath, workspaceRoot: flags.workspaceRoot, workspacePackages: flags.workspacePackages?.length ? flags.workspacePackages : void 0, pickFromScripts: flags.pickFromScripts?.length ? flags.pickFromScripts : void 0, omitFromScripts: flags.omitFromScripts?.length ? flags.omitFromScripts : void 0, ...wasFlagExplicitlyPassed("forceNpm", argv) && { forceNpm: flags.forceNpm }, ...wasFlagExplicitlyPassed("includeDevDependencies", argv, "d") && { includeDevDependencies: flags.includeDevDependencies }, ...wasFlagExplicitlyPassed("omitPackageManager", argv) && { omitPackageManager: flags.omitPackageManager } }); } //#endregion //#region src/isolate-bin.ts sourceMaps.install(); const cli = meow(outdent` Isolate a monorepo workspace package into a self-contained directory. Usage $ isolate [options] Options -b, --build-dir-name <name> Build output directory name -d, --include-dev-dependencies Include devDependencies in the isolated package -o, --isolate-dir-name <name> Name of the isolate output directory (default: isolate) -l, --log-level <level> Log level: info, debug, warn, error (default: info) -t, --target-package-path <path> Path to the target package -c, --tsconfig-path <path> Path to tsconfig.json (default: ./tsconfig.json) -w, --workspace-packages <glob> Workspace package globs (repeatable) -r, --workspace-root <path> Path to the workspace root (default: ../..) --force-npm Force npm lockfile generation -p, --pick-from-scripts <name> Scripts to include (repeatable) --omit-from-scripts <name> Scripts to exclude (repeatable) --omit-package-manager Omit the packageManager field from the manifest Examples $ isolate --log-level debug $ isolate --force-npm --workspace-root ../.. $ isolate --pick-from-scripts build --pick-from-scripts start `, { importMeta: import.meta, flags: { buildDirName: { type: "string", shortFlag: "b" }, includeDevDependencies: { type: "boolean", shortFlag: "d" }, isolateDirName: { type: "string", shortFlag: "o" }, logLevel: { type: "string", shortFlag: "l" }, targetPackagePath: { type: "string", shortFlag: "t" }, tsconfigPath: { type: "string", shortFlag: "c" }, workspacePackages: { type: "string", shortFlag: "w", isMultiple: true }, workspaceRoot: { type: "string", shortFlag: "r" }, forceNpm: { type: "boolean" }, pickFromScripts: { type: "string", shortFlag: "p", isMultiple: true }, omitFromScripts: { type: "string", isMultiple: true }, omitPackageManager: { type: "boolean" } } }); async function run() { const cliOverrides = buildCliOverrides(cli.flags, process.argv); await isolate({ ...loadConfigFromFile(), ...cliOverrides }); } run().catch((err) => { if (err instanceof Error) { console.error(err.stack); process.exit(1); } else console.error(err); }); //#endregion export { }; //# sourceMappingURL=isolate-bin.mjs.map