UNPKG

rugby

Version:

A Node.js CLI to collect npm package tarballs and assets (including those fetched by pre/postinstall scripts) for offline Artifactory use.

89 lines (76 loc) 2.51 kB
#!/usr/bin/env node "use strict"; const { Command } = require("commander"); const path = require("path"); const fs = require("fs-extra"); const prompts = require("prompts"); const chalk = require("chalk"); const { run } = require("../src/index.js"); async function main() { const program = new Command(); program .name("rugby") .description( "Collect npm package tarballs and assets (including those fetched by pre/postinstall scripts) for offline Artifactory use." ) .option("-d, --dir <path>", "Directory containing package.json", process.cwd()) .option("-o, --out <path>", "Output directory", path.join(process.cwd(), "out")) .option("--list-only", "Only list dependency tree, including assets from install scripts") .option("--npm <path>", "Path to npm executable (defaults to resolved npm)") .option("--npm-args <args>", "Extra args passed to npm when simulating install scripts", "") .option("--no-color", "Disable colors in output") .option("-v, --verbose", "Verbose logs") .version("0.1.0"); program.parse(process.argv); const opts = program.opts(); const dir = path.resolve(opts.dir); const listOnly = Boolean(opts.listOnly); const verbose = Boolean(opts.verbose); const outDir = path.resolve(opts.out); const pkgPath = path.join(dir, "package.json"); const lockPath = path.join(dir, "package-lock.json"); if (!(await fs.pathExists(pkgPath))) { console.error(chalk.red(`No package.json found at ${pkgPath}`)); process.exit(1); } if (!(await fs.pathExists(lockPath))) { console.error( chalk.red( `No package-lock.json found at ${lockPath}. Please run npm install to generate a lockfile.` ) ); process.exit(1); } if (!listOnly) { if (!(await fs.pathExists(outDir))) { const response = await prompts({ type: "confirm", name: "create", message: `Output directory ${outDir} does not exist. Create it?`, initial: true, }); if (!response.create) { console.error(chalk.yellow("Aborting - output directory does not exist.")); process.exit(1); } await fs.mkdirp(outDir); } } try { await run({ projectDir: dir, outDir, listOnly, verbose, npmPath: opts.npm, npmArgs: opts["npmArgs"], }); } catch (err) { if (verbose) { console.error(err); } console.error(chalk.red(err.message || String(err))); process.exit(1); } } main();