UNPKG

jsr

Version:

jsr.io package manager for npm, yarn, pnpm and bun

226 lines 9.48 kB
"use strict"; // Copyright the JSR authors. MIT license. 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupNpmRc = setupNpmRc; exports.setupBunfigToml = setupBunfigToml; exports.install = install; exports.remove = remove; exports.publish = publish; exports.runScript = runScript; exports.showPackageInfo = showPackageInfo; const path = __importStar(require("node:path")); const fs = __importStar(require("node:fs")); const utils_ts_1 = require("./utils.js"); const pkg_manager_ts_1 = require("./pkg_manager.js"); const download_ts_1 = require("./download.js"); const api_ts_1 = require("./api.js"); const semiver_1 = __importDefault(require("semiver")); const NPMRC_FILE = ".npmrc"; const BUNFIG_FILE = "bunfig.toml"; const JSR_NPM_REGISTRY_URL = "https://npm.jsr.io"; const JSR_NPMRC = `@jsr:registry=${JSR_NPM_REGISTRY_URL}\n`; const JSR_BUNFIG = `[install.scopes]\n"@jsr" = "${JSR_NPM_REGISTRY_URL}"\n`; const JSR_YARN_BERRY_CONFIG_KEY = "npmScopes.jsr.npmRegistryServer"; async function wrapWithStatus(msg, fn) { process.stdout.write(msg + "..."); try { await fn(); process.stdout.write((0, utils_ts_1.styleText)("green", "ok") + "\n"); } catch (err) { process.stdout.write((0, utils_ts_1.styleText)("red", "error") + "\n"); throw err; } } async function setupNpmRc(dir) { const npmRcPath = path.join(dir, NPMRC_FILE); const msg = `Setting up ${NPMRC_FILE}`; try { let content = await fs.promises.readFile(npmRcPath, "utf-8"); if (!content.includes("@jsr:registry=")) { const nl = (0, utils_ts_1.getNewLineChars)(content); const spacer = (!content.endsWith(nl)) ? nl : ""; content += spacer + JSR_NPMRC; await wrapWithStatus(msg, async () => { await fs.promises.writeFile(npmRcPath, content); }); } } catch (err) { if (err instanceof Error && err.code === "ENOENT") { await wrapWithStatus(msg, async () => { await fs.promises.writeFile(npmRcPath, JSR_NPMRC); }); } else { throw err; } } } async function setupBunfigToml(dir) { const bunfigPath = path.join(dir, BUNFIG_FILE); const msg = `Setting up ${BUNFIG_FILE}`; try { let content = await fs.promises.readFile(bunfigPath, "utf-8"); if (!/^"@jsr"\s+=/gm.test(content)) { content += JSR_BUNFIG; await wrapWithStatus(msg, async () => { await fs.promises.writeFile(bunfigPath, content); }); } } catch (err) { if (err instanceof Error && err.code === "ENOENT") { await wrapWithStatus(msg, async () => { await fs.promises.writeFile(bunfigPath, JSR_BUNFIG); }); } else { throw err; } } } async function install(packages, options) { const { pkgManager, root } = await (0, pkg_manager_ts_1.getPkgManager)(process.cwd(), options.pkgManagerName); if (packages.length > 0) { if (pkgManager instanceof pkg_manager_ts_1.Bun && !(await pkgManager.isNpmrcSupported())) { // Bun v1.1.17 or lower doesn't support reading from .npmrc // Bun v1.1.18+ supports npmrc // https://bun.sh/blog/bun-v1.1.18#npmrc-support await setupBunfigToml(root); } else if (pkgManager instanceof pkg_manager_ts_1.YarnBerry) { // Yarn v2+ does not read from .npmrc intentionally // https://yarnpkg.com/migration/guide#update-your-configuration-to-the-new-settings await pkgManager.setConfigValue(JSR_YARN_BERRY_CONFIG_KEY, JSR_NPM_REGISTRY_URL); } else { await setupNpmRc(root); } console.log(`Installing ${(0, utils_ts_1.styleText)("cyan", packages.join(", "))}...`); } await pkgManager.install(packages, options); } async function remove(packages, options) { const { pkgManager } = await (0, pkg_manager_ts_1.getPkgManager)(process.cwd(), options.pkgManagerName); console.log(`Removing ${(0, utils_ts_1.styleText)("cyan", packages.join(", "))}...`); await pkgManager.remove(packages); } async function getOrDownloadBinPath(binFolder, canary) { const info = await (0, download_ts_1.getDenoDownloadUrl)(canary); const binPath = path.join(binFolder, info.version, // Ensure each binary has their own folder to avoid overwriting it // in case jsr gets added to a project as a dependency where // developers use multiple OSes process.platform, process.platform === "win32" ? "deno.exe" : "deno"); // Check if deno executable is available, download it if not. if (!(await (0, utils_ts_1.fileExists)(binPath))) { // Clear folder first to get rid of old download artifacts // to avoid taking up lots of disk space. try { await fs.promises.rm(binFolder, { recursive: true }); } catch (err) { if (!(err instanceof Error) || err.code !== "ENOENT") { throw err; } } await (0, download_ts_1.downloadDeno)(binPath, info); } return binPath; } async function publish(cwd, options) { const binPath = process.env.DENO_BIN_PATH ?? await getOrDownloadBinPath(options.binFolder, options.canary); // Ready to publish now! const args = [ "publish", ]; const env = { ...process.env }; // These commands should only be added for a node project, // not a Deno project. if (options.pkgJsonPath !== null) { args.push("--unstable-bare-node-builtins", "--unstable-sloppy-imports", "--unstable-byonm", "--no-check"); env.DENO_DISABLE_PEDANTIC_NODE_WARNINGS = "true"; } args.push(...options.publishArgs.filter((arg) => arg !== "--verbose")); await (0, utils_ts_1.exec)(binPath, args, cwd, env); } async function runScript(cwd, script, options) { const { pkgManager } = await (0, pkg_manager_ts_1.getPkgManager)(cwd, options.pkgManagerName); await pkgManager.runScript(script); } async function showPackageInfo(raw) { const pkg = utils_ts_1.JsrPackage.from(raw); const meta = await (0, api_ts_1.getPackageMeta)(pkg); if (pkg.version === null) { let latest = meta.latest; if (latest === undefined) { throw new Error(`Missing latest version for ${pkg}`); } else if (latest === null) { // When no stable version is published: `latest === null`. We need to // manually find the latest pre-release version const versions = Object.keys(meta.versions); if (versions.length === 0) { throw new Error(`Could not find published version for ${pkg}`); } versions.sort(semiver_1.default); pkg.version = versions[0]; } else { pkg.version = latest; } } const versionCount = Object.keys(meta.versions).length; const npmInfo = await (0, api_ts_1.getNpmPackageInfo)(pkg); const versionInfo = npmInfo.versions[pkg.version]; const time = npmInfo.time[pkg.version]; const publishTime = new Date(time).getTime(); console.log(); console.log((0, utils_ts_1.styleText)("cyan", `@${pkg.scope}/${pkg.name}@${pkg.version}`) + ` | latest: ${(0, utils_ts_1.styleText)("magenta", meta.latest ?? "-")} | versions: ${(0, utils_ts_1.styleText)("magenta", versionCount.toString())}`); console.log(npmInfo.description); console.log(); console.log(`npm tarball: ${(0, utils_ts_1.styleText)("cyan", versionInfo.dist.tarball)}`); console.log(`npm integrity: ${(0, utils_ts_1.styleText)("cyan", versionInfo.dist.integrity)}`); console.log(); console.log(`published: ${(0, utils_ts_1.styleText)("magenta", (0, utils_ts_1.timeAgo)(Date.now() - publishTime))}`); } //# sourceMappingURL=commands.js.map