From 9032e57cd8de3f432c1e31bfc28ee42f0be68b95 Mon Sep 17 00:00:00 2001 From: YieldRay Date: Tue, 11 Jun 2024 16:59:36 +0800 Subject: [PATCH] fix when rc file not exists --- CHANGELOG.md | 11 ++++------- README.md | 2 +- cli.mjs | 8 ++++---- config.mjs | 11 ++++++++--- package.json | 6 +++--- registry.mjs | 1 - tty.mjs | 6 +++--- 7 files changed, 23 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2598560..34800fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,22 +2,19 @@ ## [0.1.3](https://github.com/YieldRay/nrm-lite/compare/v0.1.2...v0.1.3) (2024-03-12) - ### Bug Fixes -* fix the --version flag ([9e059ef](https://github.com/YieldRay/nrm-lite/commit/9e059efaf24198123e01307a71e59dfdad09bba2)) +- fix the --version flag ([9e059ef](https://github.com/YieldRay/nrm-lite/commit/9e059efaf24198123e01307a71e59dfdad09bba2)) ## [0.1.2](https://github.com/YieldRay/nrm-lite/compare/v0.1.1...v0.1.2) (2024-03-11) - ### Features -* support timeout for test command ([d14c87d](https://github.com/YieldRay/nrm-lite/commit/d14c87d70a5990dc60c855f5c42883d1d9e8da3e)) +- support timeout for test command ([d14c87d](https://github.com/YieldRay/nrm-lite/commit/d14c87d70a5990dc60c855f5c42883d1d9e8da3e)) ## 0.1.1 (2024-03-11) - ### Features -* add test command ([432f29d](https://github.com/YieldRay/nrm-lite/commit/432f29db4524f64951056970ab44b0917302efa2)) -* initial commit ([ff2c04b](https://github.com/YieldRay/nrm-lite/commit/ff2c04b774b81b849c60a46c18dde2555c51b61e)) +- add test command ([432f29d](https://github.com/YieldRay/nrm-lite/commit/432f29db4524f64951056970ab44b0917302efa2)) +- initial commit ([ff2c04b](https://github.com/YieldRay/nrm-lite/commit/ff2c04b774b81b849c60a46c18dde2555c51b61e)) diff --git a/README.md b/README.md index 1aa07ee..beb0951 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![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) -Fast and lightweight NpmRegistryManager, for the minimalists. +Fast and lightweight Npm [Registry](https://docs.npmjs.com/cli/using-npm/registry) Manager, for the minimalists. Like [dnrm](https://github.com/markthree/dnrm), but in pure Node.js. [![asciicast](https://asciinema.org/a/646571.svg)](https://asciinema.org/a/646571) diff --git a/cli.mjs b/cli.mjs index 81da6c1..8c6cc29 100644 --- a/cli.mjs +++ b/cli.mjs @@ -94,7 +94,7 @@ ${c.bold('Usage:')} nrml ls List registries nrml use ${c.gray('')} Use registry nrml test ${c.gray( - '[]' + '[]', )} Test registry speed, optional timeout in second (default: 2) nrml rc Open .npmrc file nrml help Show this help @@ -137,7 +137,7 @@ async function test(timeoutLimit) { name, url, timeSpent: await speedTest(url, timeoutLimit), - })) + })), ) const currentRegistry = await getRegistry(local) @@ -152,8 +152,8 @@ async function rc() { } catch { console.error( `You do not have vscode installed!\nPlease open ${c.gray( - filePath - )} manually.` + filePath, + )} manually.`, ) process.exit(-1) } diff --git a/config.mjs b/config.mjs index edad94f..ec29c98 100644 --- a/config.mjs +++ b/config.mjs @@ -35,9 +35,14 @@ export async function setRegistry(local, registryUrl) { */ export async function getRegistry(local) { const filePath = await getConfigPath(local) - const fileStream = fs.createReadStream(filePath) - const { registry } = await findRegistryFromStream(fileStream) - return registry || REGISTRIES['npm'] + try { + const fileStream = fs.createReadStream(filePath) // the file may not exists + const { registry } = await findRegistryFromStream(fileStream) + return registry || REGISTRIES['npm'] + } catch { + // when rc file not found, fallback registry to default + return REGISTRIES['npm'] + } } /** diff --git a/package.json b/package.json index f5a5519..b011814 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nrm-lite", - "version": "0.1.3", + "version": "0.1.4", "type": "module", "description": "Fast and lightweight NpmRegistryManager, for the minimalists.", "main": "index.mjs", @@ -22,8 +22,8 @@ "nrml": "cli.mjs" }, "devDependencies": { - "@types/node": "^20.11.26", - "typescript": "^5.4.2" + "@types/node": "^20.14.2", + "typescript": "^5.4.5" }, "publishConfig": { "registry": "https://registry.npmjs.org" diff --git a/registry.mjs b/registry.mjs index 217ab19..6ef68fd 100644 --- a/registry.mjs +++ b/registry.mjs @@ -13,7 +13,6 @@ export const REGISTRIES = { } /** - * * Note that `registryLineNumber` is index + 1 * * @param {NodeJS.ReadableStream} stream diff --git a/tty.mjs b/tty.mjs index 1713243..042644d 100644 --- a/tty.mjs +++ b/tty.mjs @@ -42,10 +42,10 @@ export function printRegistries( name, url, })), - timeoutLimit + timeoutLimit, ) { const maxNameLength = Math.max( - ...registriesInfo.map(({ name }) => name.length) + ...registriesInfo.map(({ name }) => name.length), ) /** * @type {number=} @@ -57,7 +57,7 @@ export function printRegistries( // lazy compute if (!maxUrlLength) maxUrlLength = Math.max( - ...registriesInfo.map(({ url }) => url.length) + ...registriesInfo.map(({ url }) => url.length), ) }