UNPKG

@darkobits/re-pack

Version:

Utility for managing NPM package contents.

63 lines (62 loc) 2.01 kB
import chex from "@darkobits/chex"; import Arborist from "@npmcli/arborist"; import packlist from "npm-packlist"; import log from "./log.js"; import { getPackageInfo, temporarilyRemoveProblematicPackageScripts } from "./utils.js"; const npm = chex.sync("npm >=6.0.0"); async function getPackList(cwd) { const arborist = new Arborist({ path: cwd }); const tree = await arborist.loadActual(); return packlist(tree); } async function linkPackage(cwd) { const pkgInfo = await getPackageInfo(cwd); const prefix = log.chalk.cyan.dim("link"); log.info(prefix, `${log.chalk.bold("Linking package:")} ${log.chalk.green(pkgInfo.json.name)}`); const restorePackageJson = await temporarilyRemoveProblematicPackageScripts(pkgInfo); await npm(["link", "--ignore-scripts"], { cwd, stdio: "inherit" }); await restorePackageJson(); log.info(prefix, log.chalk.bold("Done.")); } async function runLifecycleScript(opts) { const args = ["run", opts.scriptName]; const prefix = log.chalk.cyan.dim(opts.scriptName); log.verbose(prefix, `Running ${log.chalk.bold(`\`npm ${args.join(" ")}\``)}.`); await npm(args, { cwd: opts.cwd, stdio: "inherit", env: { FORCE_COLOR: "3" } }); } async function publishPackage({ cwd, tag, dryRun }) { const pkgInfo = await getPackageInfo(cwd); const args = ["publish", "--ignore-scripts"]; if (dryRun) args.push("--dry-run"); if (tag) args.push(`--tag=${tag}`); log.verbose(log.chalk.cyan.dim("publishPackage"), `Running ${log.chalk.bold(`\`npm ${args.join(" ")}\``)}.`); const restorePackageJson = await temporarilyRemoveProblematicPackageScripts(pkgInfo); await npm(args, { cwd, stdio: "inherit", env: { // Note: Unsure why this wasn't getting populated in the child process' // environment. NPM_TOKEN: process.env.NPM_TOKEN } }); await restorePackageJson(); } export { getPackList, linkPackage, publishPackage, runLifecycleScript }; //# sourceMappingURL=npm.js.map