@darkobits/re-pack
Version:
Utility for managing NPM package contents.
144 lines (143 loc) • 4.39 kB
JavaScript
#!/usr/bin/env node
import adeiu from "@darkobits/adeiu";
import * as cli from "@darkobits/saffron";
import { DEFAULT_OPTIONS, DEFAULT_PUBLISH_OPTIONS } from "../etc/constants.js";
import config from "../lib/config.js";
import log from "../lib/log.js";
import publish from "../lib/publish.js";
import publishGuard from "../lib/publish-guard.js";
import rePack from "../lib/re-pack.js";
const DESCRIPTIONS = {
HOIST_DIR: "Directory to hoist to the re-pack root.",
PACK_DIR: "Directory where the package will be re-packed."
};
cli.command({
command: "* [cwd]",
description: "Re-pack the host package.",
config: { auto: false },
builder: ({ command }) => {
command.positional("cwd", {
description: "Root directory of the package to re-pack. [default: cwd]",
type: "string",
required: false
});
command.option("hoist-dir", {
description: DESCRIPTIONS.HOIST_DIR,
type: "string",
required: false,
default: DEFAULT_OPTIONS.hoistDir
});
command.option("pack-dir", {
description: DESCRIPTIONS.PACK_DIR,
type: "string",
default: DEFAULT_OPTIONS.packDir
});
command.option("watch", {
description: `Continuously watches ${log.chalk.bold("hoist-dir")} and re-packs to ${log.chalk.bold("pack-dir")}.`,
type: "boolean",
required: false,
default: DEFAULT_OPTIONS.watch
});
command.option("link", {
description: `After re-packing, runs ${log.chalk.bold("npm link")} from ${log.chalk.bold("pack-dir")}.`,
type: "boolean",
required: false,
default: DEFAULT_OPTIONS.link
});
},
handler: async ({ argv, config: config2, configPath }) => {
const prefix = log.chalk.cyan.dim("config");
try {
if (configPath) {
log.debug(prefix, `Loaded configuration from: ${log.chalk.green(configPath)}`);
log.debug(prefix, config2);
}
adeiu((signal) => {
if (argv.watch) {
log.info(`Got signal ${log.chalk.yellow(signal)}; closing watcher.`);
}
});
await rePack({
...argv,
...config2
});
} catch (err) {
log.error(err.message);
log.verbose(err.stack.split("\n").slice(1).join("\n"));
process.exit(1);
}
}
});
cli.command({
command: "publish",
description: "Re-pack and publish the host package.",
config: { auto: false },
builder: ({ command }) => {
command.positional("cwd", {
description: "Root directory of the package to re-pack and publish. [default: cwd]",
type: "string",
required: false
});
command.option("hoist-dir", {
description: DESCRIPTIONS.HOIST_DIR,
type: "string",
required: false,
default: DEFAULT_PUBLISH_OPTIONS.hoistDir
});
command.option("pack-dir", {
description: DESCRIPTIONS.PACK_DIR,
type: "string",
default: DEFAULT_PUBLISH_OPTIONS.packDir
});
command.option("tag", {
description: [
"Distribution tag to publish the package under. If the current",
"package version contains a pre-release token (ex: beta), it",
`will be used.
Forwards to the --tag argument of ${log.chalk.bold("npm publish")}.`
].join("\n"),
type: "boolean",
required: false
});
command.option("access", {
description: `Access to set on the published package.
Forwards to the --access argument of ${log.chalk.bold("npm publish")}.`,
choices: ["public", "restricted"],
type: "string",
required: false
});
command.option("dry-run", {
description: `Forwards to the --dry-run argument of ${log.chalk.bold("npm publish")}.`,
type: "boolean",
required: false,
default: DEFAULT_PUBLISH_OPTIONS.dryRun
});
},
handler: async ({ argv, config: config2 }) => {
try {
await publish({
...argv,
...config2
});
} catch (err) {
log.error(err.message);
process.exit(1);
}
}
});
cli.command({
command: "guard",
config: { auto: false },
description: 'Guards against accidental invocations of `npm publish`. This command should be run as a "prepublishOnly" script.',
handler: () => {
try {
publishGuard();
} catch (err) {
log.error(err.message);
config.set("isPublishing", false);
process.exit(1);
}
}
});
cli.init();
//# sourceMappingURL=cli.js.map