Skip to content

Commit

Permalink
feat: add test command
Browse files Browse the repository at this point in the history
Release-As: 0.1.1
  • Loading branch information
YieldRay committed Mar 11, 2024
1 parent 6978f10 commit 432f29d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ nrml --help
## Usage

```sh
nrm-lite v0.1.0
nrm-lite

Usage:
nrml ls List registry
nrml use [name] Use registry
nrml test Test registry speed
nrml rc Open .npmrc file
nrml help Show this help
Global Options:
Expand Down
31 changes: 28 additions & 3 deletions cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { getRegistry, setRegistry, getConfigPath } from './config.mjs'
import { REGISTRIES } from './registry.mjs'
import { REGISTRIES, speedTest } from './registry.mjs'
import c, { printRegistries } from './tty.mjs'

// https://nodejs.org/api/util.html#utilparseargsconfig
Expand Down Expand Up @@ -39,6 +39,10 @@ if (values.help) {

const command = positionals[0] || 'ls'
const { local } = values
/**
* @type {string}
*/
let name

switch (command) {
case 'h':
Expand All @@ -48,15 +52,19 @@ switch (command) {
case 'ls':
ls()
break
case 'test':
name = positionals[1]
test(name)
break
case 'rc':
rc()
break
case 'use':
const name = positionals[1]
name = positionals[1]
use(name)
break
default:
console.error(`Unknown command ${command}\n`)
console.error(`Unknown command '${command}'\n`)
process.exit(1)
}

Expand All @@ -71,6 +79,7 @@ function help() {
${c.bold('Usage:')}
nrml ls List registry
nrml use ${c.gray('[name]')} Use registry
nrml test Test registry speed
nrml rc Open .npmrc file
nrml help Show this help
${c.bold('Global Options:')}
Expand Down Expand Up @@ -103,6 +112,22 @@ async function use(name) {
printRegistries(registryUrl)
}

/**
* @param {string} name
*/
async function test(name) {
const info = await Promise.all(
Object.entries(REGISTRIES).map(async ([name, url]) => ({
name,
url,
timeSpent: await speedTest(url),
}))
)

const currentRegistry = await getRegistry(local)
printRegistries(currentRegistry, info, 2000)
}

async function rc() {
const filePath = await getConfigPath(local)
try {
Expand Down
4 changes: 2 additions & 2 deletions config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function setRegistry(local, registryUrl) {
}

/**
* @param {boolean|undefined} local
* @param {boolean=} local
*/
export async function getRegistry(local) {
const filePath = await getConfigPath(local)
Expand All @@ -42,7 +42,7 @@ export async function getRegistry(local) {

/**
* If `local` is not provided, check if local npmrc file exists
* @param {boolean|undefined} local
* @param {boolean=} local
* @noexcept
*/
export async function getConfigPath(local) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nrm-lite",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"description": "simple and lightweight replacement for nrm",
"main": "index.mjs",
Expand Down
17 changes: 17 additions & 0 deletions registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,20 @@ export async function findRegistryFromStream(stream) {
}
return { registry: null, registryLineNumber: null, lines }
}

/**
* @param {string} url
*/
export async function speedTest(url, milliseconds = 2000) {
try {
const beginTime = Date.now()
await fetch(url, {
method: 'HEAD',
signal: AbortSignal.timeout(milliseconds),
})
const timeSpent = Date.now() - beginTime
return timeSpent
} catch {
return null
}
}
55 changes: 44 additions & 11 deletions tty.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REGISTRIES } from './registry.mjs'
import { REGISTRIES, speedTest } from './registry.mjs'

/**
* @param {string} str
Expand Down Expand Up @@ -32,18 +32,51 @@ const c = {
export default c

/**
* @param {string} registryUrl
* @param {string} currentRegistryUrl
* @param {Array<{name:string,url:string,timeSpent?:number|null}>} registriesInfo
* @param {number=} timeoutLimit - milliseconds
*/
export function printRegistries(registryUrl) {
let maxNameLength = 0
export function printRegistries(
currentRegistryUrl,
registriesInfo = Object.entries(REGISTRIES).map(([name, url]) => ({
name,
url,
})),
timeoutLimit
) {
const maxNameLength = Math.max(
...registriesInfo.map(({ name }) => name.length)
)
/**
* @type {number=}
*/
let maxUrlLength = undefined

const registries = Object.entries(REGISTRIES).map(([name, url]) => {
maxNameLength = Math.max(maxNameLength, name.length)
return { name, url, highlight: url === registryUrl }
})
for (let { name, url, timeSpent } of registriesInfo) {
if (timeoutLimit) {
// lazy compute
if (!maxUrlLength)
maxUrlLength = Math.max(
...registriesInfo.map(({ url }) => url.length)
)
}

for (const { name, url, highlight } of registries) {
const row = `${name.padEnd(maxNameLength)}${url}`
console.log(highlight ? c.blue(row) : row)
let row = `${name.padEnd(maxNameLength)}${
maxUrlLength ? url.padEnd(maxUrlLength) : url
}`
if (url === currentRegistryUrl) row = c.blue(row)

if (timeoutLimit) {
timeSpent ??= Infinity

if (timeSpent >= timeoutLimit) {
row += c.red(` (>${(timeoutLimit / 1000).toFixed(1)}s)`)
} else if (timeSpent >= timeoutLimit / 2) {
row += c.yellow(` (${(timeSpent / 1000).toFixed(2)}s)`)
} else {
row += c.green(` (${(timeSpent / 1000).toFixed(2)}s)`)
}
}
console.log(row)
}
}

0 comments on commit 432f29d

Please sign in to comment.