Skip to content

Commit

Permalink
fix: fix the --version flag
Browse files Browse the repository at this point in the history
Release-As: 0.1.3
  • Loading branch information
YieldRay committed Mar 12, 2024
1 parent 0cef1ff commit 9e059ef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <name> Use registry
nrml test [<timeout>] 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)
```
18 changes: 14 additions & 4 deletions cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (values.help) {
help()
}

const command = positionals[0] || 'ls'
const command = positionals[0] || ''
const { local } = values
/**
* @type {string}
Expand All @@ -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
Expand All @@ -72,24 +75,31 @@ 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('<name>')} Use registry
nrml test ${c.gray(
'[<timeout>]'
)} Test registry speed, optional timeout in second (default: 2)
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)
}

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -21,7 +22,7 @@
"nrml": "cli.mjs"
},
"devDependencies": {
"@types/node": "^20.11.25",
"@types/node": "^20.11.26",
"typescript": "^5.4.2"
},
"publishConfig": {
Expand Down
9 changes: 5 additions & 4 deletions registry.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as readline from 'node:readline'

/**
* @type { Record<string,string>}
* @type {Record<string,string>}
*/
export const REGISTRIES = {
npm: 'https://registry.npmjs.org/',
Expand All @@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tty.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REGISTRIES, speedTest } from './registry.mjs'
import { REGISTRIES } from './registry.mjs'

/**
* @param {string} str
Expand Down

0 comments on commit 9e059ef

Please sign in to comment.