UNPKG

@darkobits/re-pack

Version:

Utility for managing NPM package contents.

64 lines (63 loc) 2.8 kB
import path from "path"; import * as R from "ramda"; import { DEFAULT_PUBLISH_OPTIONS } from "../etc/constants.js"; import config from "./config.js"; import log from "./log.js"; import { runLifecycleScript, publishPackage } from "./npm.js"; import rePack from "./re-pack.js"; import { getPackageInfo, inferPublishTag } from "./utils.js"; async function publish(userOptions) { var _a, _b, _c; try { const runTime = log.chronograph(); const prefix = log.chalk.cyan.dim("publish"); config.set("isPublishing", true); const opts = R.mergeAll([DEFAULT_PUBLISH_OPTIONS, userOptions]); const resolvedCwd = path.resolve(opts.cwd); log.verbose(prefix, `cwd: ${log.chalk.green(resolvedCwd)}`); const pkg = await getPackageInfo(resolvedCwd); log.info(prefix, [ `Preparing to publish package: ${log.chalk.green(`${pkg.json.name}@${pkg.json.version}`)}`, opts.dryRun ? log.chalk.gray("(dry run)") : false ].filter(Boolean).join(" ")); const tag = opts.tag ?? inferPublishTag(pkg.json.version); if (tag) log.info(prefix, `Using dist-tag: ${log.chalk.yellow(tag)}`); if ((_a = pkg.json.scripts) == null ? void 0 : _a.prepublishOnly) { log.verbose(prefix, `Running ${log.chalk.green('"prepublishOnly"')} lifecycle script.`); await runLifecycleScript({ cwd: pkg.root, scriptName: "prepublishOnly" }); } else if (!config.get("hasSeenPrepublishWarning")) { log.warn(prefix, `Consider adding a ${log.chalk.green('"prepublishOnly"')} package script that runs ${log.chalk.bold("re-pack guard")}.`); log.warn(prefix, log.chalk.gray("This warning will not be displayed again.")); config.set("hasSeenPrepublishWarning", true); } if ((_b = pkg.json.scripts) == null ? void 0 : _b.prepare) { log.verbose(prefix, `Running ${log.chalk.green('"prepare"')} lifecycle script.`); await runLifecycleScript({ cwd: pkg.root, scriptName: "prepare" }); } await rePack({ cwd: opts.cwd, // N.B. We want to pass the plain hoistDir here, not the resolved one, as // this function will be working with relative paths within the package. hoistDir: opts.hoistDir, packDir: opts.packDir, afterRepack: opts.afterRepack }); await publishPackage({ cwd: opts.packDir, dryRun: opts.dryRun, tag }); if ((_c = pkg.json.scripts) == null ? void 0 : _c.postpublish) { log.verbose(prefix, `Running ${log.chalk.green('"postpublish"')} lifecycle script.`); await runLifecycleScript({ cwd: pkg.root, scriptName: "postpublish" }); } log.info(prefix, log.chalk.bold(`Done in ${log.chalk.yellow(runTime)}.`)); } finally { config.set("isPublishing", false); } } export { publish as default }; //# sourceMappingURL=publish.js.map