diff --git a/README.md b/README.md index 8d82f17..5480ba3 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,16 @@ # nrm-lite -[![npm](https://img.shields.io/npm/v/nrm-lite)](https://www.npmjs.com/package/nrm-lite)  [![install size](https://packagephobia.com/badge?p=nrm-lite)](https://packagephobia.com/result?p=nrm-lite) +[![npm](https://img.shields.io/npm/v/nrm-lite)](https://www.npmjs.com/package/nrm-lite) +[![node-current](https://img.shields.io/node/v/nrm-lite)](https://nodejs.dev/) +[![install size](https://packagephobia.com/badge?p=nrm-lite)](https://packagephobia.com/result?p=nrm-lite) -Simple and lightweight replacement for nrm. -Like [dnrm](https://github.com/markthree/dnrm), but in pure Node.js +Fast and lightweight NpmRegistryManager, for the minimalists. +Like [dnrm](https://github.com/markthree/dnrm), but in pure Node.js. ## Features - Super lightweight, pure Node.js with NO dependency -- Fast, DO NOT parse the `.npmrc` file (slightly slower that `dnrm`, due to the runtime) +- Fast, DO NOT parse the `.npmrc` file - Correct, follow the rules of the `.npmrc` file ## Install @@ -26,17 +28,28 @@ nrml use taobao nrml test ``` +It is well-known that [Deno](https://deno.com/)'s cold start is faster than Node.js. +Hence you can install `nrm-lite` in deno, so it will be as fast as `dnrm`. + +```sh +# install `nrml` in deno +deno install -Arf npm:nrm-lite -n nrml + +# test if it's installed +nrml --help +``` + ## Usage ```sh nrm-lite Usage: - nrml ls List registry + nrml ls List registries nrml use Use registry nrml test [] Test registry speed, optional timeout in second (default: 2) nrml rc Open .npmrc file nrml help Show this help Global Options: - --local Use local .npmrc file, rather than the global one (default: false) + --local Use local .npmrc file, rather than the global one (default: false) ``` diff --git a/cli.mjs b/cli.mjs index d437c20..81da6c1 100644 --- a/cli.mjs +++ b/cli.mjs @@ -37,7 +37,7 @@ if (values.help) { help() } -const command = positionals[0] || 'ls' +const command = positionals[0] || '' const { local } = values /** * @type {string} @@ -53,6 +53,9 @@ switch (command) { case 'help': help() break + // @ts-ignore intentional fallthrough here + case '': + if (values.version) help(true) case 'ls': ls() break @@ -72,16 +75,23 @@ switch (command) { process.exit(1) } -function help() { +/** + * @param {boolean=} v - only print version number + */ +function help(v) { // assert json will cause ExperimentalWarning. so we use this instead // import pkg from './package.json' assert { type: 'json' } const __filename = fileURLToPath(import.meta.url) const __dirname = dirname(__filename) const pkg = JSON.parse(readFileSync(`${__dirname}/package.json`, 'utf-8')) + if (v) { + console.error('v' + pkg.version) + process.exit(1) + } console.error(`${c.green(pkg.name)} v${pkg.version} ${c.bold('Usage:')} - nrml ls List registry + nrml ls List registries nrml use ${c.gray('')} Use registry nrml test ${c.gray( '[]' @@ -89,7 +99,7 @@ ${c.bold('Usage:')} nrml rc Open .npmrc file nrml help Show this help ${c.bold('Global Options:')} - --local Use local .npmrc file, rather than the global one (default: false)`) + --local Use local .npmrc file, rather than the global one (default: false)`) process.exit(1) } diff --git a/package.json b/package.json index fa03082..f5a5519 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "nrm-lite", - "version": "0.1.2", + "version": "0.1.3", "type": "module", - "description": "simple and lightweight replacement for nrm", + "description": "Fast and lightweight NpmRegistryManager, for the minimalists.", "main": "index.mjs", + "sideEffects": false, "scripts": { "test": "node --test", "watch": "tsc --watch", @@ -21,7 +22,7 @@ "nrml": "cli.mjs" }, "devDependencies": { - "@types/node": "^20.11.25", + "@types/node": "^20.11.26", "typescript": "^5.4.2" }, "publishConfig": { diff --git a/registry.mjs b/registry.mjs index 838bb27..217ab19 100644 --- a/registry.mjs +++ b/registry.mjs @@ -1,7 +1,7 @@ import * as readline from 'node:readline' /** - * @type { Record} + * @type {Record} */ export const REGISTRIES = { npm: 'https://registry.npmjs.org/', @@ -17,8 +17,8 @@ export const REGISTRIES = { * Note that `registryLineNumber` is index + 1 * * @param {NodeJS.ReadableStream} stream - * @returns {Promise<{registry:string,lines:string[],registryLineNumber:number}| - * {registry:null,lines:string[],registryLineNumber:null}>} + * @returns {Promise<{registry:string,lines:string[],registryLineNumber:number} + * |{registry:null,lines:string[],registryLineNumber:null}>} * @see https://docs.npmjs.com/cli/configuring-npm/npmrc * * @example @@ -49,6 +49,7 @@ export async function findRegistryFromStream(stream) { } /** + * Returns `Infinity` when execeed timeout, and `null` when network error * @param {string} url * @param {number} timeoutLimit - in milliseconds */ @@ -60,7 +61,7 @@ export async function speedTest(url, timeoutLimit) { signal: AbortSignal.timeout(timeoutLimit), }) const timeSpent = Date.now() - beginTime - return timeSpent + return timeSpent > timeoutLimit ? Infinity : timeSpent } catch (e) { if (e instanceof DOMException) { return Infinity diff --git a/tty.mjs b/tty.mjs index 64b0a88..1713243 100644 --- a/tty.mjs +++ b/tty.mjs @@ -1,4 +1,4 @@ -import { REGISTRIES, speedTest } from './registry.mjs' +import { REGISTRIES } from './registry.mjs' /** * @param {string} str