jsr
Version:
jsr.io package manager for node
285 lines (280 loc) • 10.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
// Copyright 2024 the JSR authors. MIT license.
const kl = __importStar(require("kolorist"));
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const node_util_1 = require("node:util");
const commands_1 = require("./commands");
const utils_1 = require("./utils");
const args = process.argv.slice(2);
function prettyPrintRow(rows) {
let max = 0;
for (let i = 0; i < rows.length; i++) {
const len = rows[i][0].length;
max = len > max ? len : max;
}
return rows
.map((row) => ` ${kl.green(row[0].padStart(max))} ${row[1]}`)
.join("\n");
}
function printHelp() {
console.log(`jsr.io cli for node
Usage:
${prettyPrintRow([
["jsr add @std/log", 'Install the "@std/log" package from jsr.io.'],
[
"jsr remove @std/log",
'Remove the "@std/log" package from the project.',
],
])}
Commands:
${prettyPrintRow([
["<script>", "Run a script from the package.json file"],
["run <script>", "Run a script from the package.json file"],
["i, install, add", "Install one or more JSR packages."],
["r, uninstall, remove", "Remove one or more JSR packages."],
["publish", "Publish a package to the JSR registry."],
["info, show, view", "Show package information."],
])}
Options:
${prettyPrintRow([
[
"-P, --save-prod",
"Package will be added to dependencies. This is the default.",
],
["-D, --save-dev", "Package will be added to devDependencies."],
["-O, --save-optional", "Package will be added to optionalDependencies."],
["--npm", "Use npm to remove and install packages."],
["--yarn", "Use yarn to remove and install packages."],
["--pnpm", "Use pnpm to remove and install packages."],
["--bun", "Use bun to remove and install packages."],
["--verbose", "Show additional debugging information."],
["-h, --help", "Show this help text."],
["-v, --version", "Print the version number."],
])}
Publish Options:
${prettyPrintRow([
[
"--token <Token>",
"The API token to use when publishing. If unset, interactive authentication will be used.",
],
[
"--dry-run",
"Prepare the package for publishing performing all checks and validations without uploading.",
],
["--allow-slow-types", "Allow publishing with slow types."],
[
"--provenance",
"From CI/CD system, publicly links the package to where it was built and published from.",
],
])}
Environment variables:
${prettyPrintRow([
["JSR_URL", "Use a different registry URL for the publish command."],
[
"DENO_BIN_PATH",
"Use specified Deno binary instead of local downloaded one.",
],
[
"DENO_BIN_CANARY",
"Use the canary Deno binary instead of latest for publishing.",
],
])}
`);
}
function getPackages(positionals, allowEmpty) {
const pkgArgs = positionals.slice(1);
const packages = pkgArgs.map((p) => utils_1.JsrPackage.from(p));
if (!allowEmpty && pkgArgs.length === 0) {
console.error(kl.red(`Missing packages argument.`));
console.log();
printHelp();
process.exit(1);
}
return packages;
}
if (args.length === 0) {
printHelp();
process.exit(0);
}
else if (args.some((arg) => arg === "-h" || arg === "--help")) {
printHelp();
process.exit(0);
}
else if (args.some((arg) => arg === "-v" || arg === "--version")) {
const version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf-8")).version;
console.log(version);
process.exit(0);
}
else {
const cmd = args[0];
// Bypass cli argument validation for publish command. The underlying
// `deno publish` cli is under active development and args may change
// frequently.
if (cmd === "publish") {
const binFolder = path.join(__dirname, "..", ".download");
run(async () => {
const projectInfo = await (0, utils_1.findProjectDir)(process.cwd());
return (0, commands_1.publish)(process.cwd(), {
canary: process.env.DENO_BIN_CANARY !== undefined,
binFolder,
publishArgs: args.slice(1),
pkgJsonPath: projectInfo.pkgJsonPath,
});
});
}
else if (cmd === "view" || cmd === "show" || cmd === "info") {
const pkgName = args[1];
if (pkgName === undefined) {
console.log(kl.red(`Missing package name.`));
printHelp();
process.exit(1);
}
run(async () => {
await (0, commands_1.showPackageInfo)(pkgName);
});
}
else {
const options = (0, node_util_1.parseArgs)({
args,
allowPositionals: true,
options: {
"save-prod": { type: "boolean", default: true, short: "P" },
"save-dev": { type: "boolean", default: false, short: "D" },
"save-optional": { type: "boolean", default: false, short: "O" },
"dry-run": { type: "boolean", default: false },
"allow-slow-types": { type: "boolean", default: false },
token: { type: "string" },
config: { type: "string", short: "c" },
"no-config": { type: "boolean" },
check: { type: "string" },
"no-check": { type: "string" },
quiet: { type: "boolean", short: "q" },
npm: { type: "boolean", default: false },
yarn: { type: "boolean", default: false },
pnpm: { type: "boolean", default: false },
bun: { type: "boolean", default: false },
debug: { type: "boolean", default: false },
canary: { type: "boolean", default: false },
help: { type: "boolean", default: false, short: "h" },
version: { type: "boolean", default: false, short: "v" },
},
});
if (options.values.debug || process.env.DEBUG) {
(0, utils_1.setDebug)(true);
}
if (options.positionals.length === 0) {
printHelp();
process.exit(0);
}
const pkgManagerName = options.values.pnpm
? "pnpm"
: options.values.yarn
? "yarn"
: options.values.bun
? "bun"
: options.values.npm
? "npm"
: null;
if (cmd === "i" || cmd === "install" || cmd === "add") {
run(async () => {
const packages = getPackages(options.positionals, true);
await (0, commands_1.install)(packages, {
mode: options.values["save-dev"]
? "dev"
: options.values["save-optional"]
? "optional"
: "prod",
pkgManagerName,
});
});
}
else if (cmd === "r" || cmd === "uninstall" || cmd === "remove") {
run(async () => {
const packages = getPackages(options.positionals, false);
await (0, commands_1.remove)(packages, { pkgManagerName });
});
}
else if (cmd === "run") {
const script = options.positionals[1];
if (!script) {
console.error(kl.red(`Missing script argument.`));
console.log();
printHelp();
process.exit(1);
}
run(async () => {
await (0, commands_1.runScript)(process.cwd(), script, { pkgManagerName });
});
}
else {
const packageJsonPath = path.join(process.cwd(), "package.json");
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
if (packageJson.scripts && packageJson.scripts[cmd]) {
run(async () => {
await (0, commands_1.runScript)(process.cwd(), cmd, { pkgManagerName });
});
}
else {
throwUnknownCommand(cmd);
}
}
else {
throwUnknownCommand(cmd);
}
}
}
}
async function run(fn) {
const start = Date.now();
try {
await fn();
const time = Date.now() - start;
console.log();
console.log(`${kl.green("Completed")} in ${(0, utils_1.prettyTime)(time)}`);
}
catch (err) {
if (err instanceof utils_1.JsrPackageNameError) {
console.log(kl.red(err.message));
process.exit(1);
}
else if (err instanceof utils_1.ExecError) {
console.log(kl.red(err.message));
process.exit(err.code);
}
throw err;
}
}
function throwUnknownCommand(cmd) {
console.error(kl.red(`Unknown command: ${cmd}`));
console.log();
printHelp();
process.exit(1);
}
//# sourceMappingURL=bin.js.map