Skip to content

Commit

Permalink
fix: fallback when rc file not exists
Browse files Browse the repository at this point in the history
Release-As: 0.2.0
  • Loading branch information
YieldRay committed Jun 11, 2024
1 parent 6cc8a09 commit 7335b03
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 166 deletions.
11 changes: 4 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { getRegistry, setRegistry, getConfigPath } from './config.mjs'
import { REGISTRIES, speedTest } from './registry.mjs'
import c, { printRegistries } from './tty.mjs'
import c, { printRegistries } from './utils.mjs'

// https://nodejs.org/api/util.html#utilparseargsconfig
const { values, positionals } = parseArgs({
Expand Down
39 changes: 18 additions & 21 deletions config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as fs from 'node:fs'
import { writeFile } from 'node:fs/promises'
import { homedir } from 'node:os'
import { REGISTRIES, findRegistryFromStream } from './registry.mjs'
import {
REGISTRIES,
getRegistryFromStream,
setRegistryFromStream,
} from './registry.mjs'
import { isFile } from './utils.mjs'

/**
Expand All @@ -10,34 +14,27 @@ import { isFile } from './utils.mjs'
*/
export async function setRegistry(local, registryUrl) {
const filePath = await getConfigPath(local)

const fileStream = fs.createReadStream(filePath)
const { registry, lines, registryLineNumber } =
await findRegistryFromStream(fileStream)
const newRegistryLine = `registry=${registryUrl}`

if (!registry) {
lines.push(newRegistryLine)
} else if (registry === registryUrl) {
// same, do nothing
return
} else {
// number-1 is index
lines[registryLineNumber - 1] = newRegistryLine
try {
const fileStream = fs.createReadStream(filePath)
const result = await setRegistryFromStream(fileStream, registryUrl)
return writeFile(filePath, result)
} catch {
return writeFile(filePath, `registry=${registryUrl}`)
}

const result = lines.join('\n')
return writeFile(filePath, result)
}

/**
* @param {boolean=} local
*/
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
return (await getRegistryFromStream(fileStream)) || REGISTRIES['npm']
} catch {
// when rc file not found, fallback registry to default
return REGISTRIES['npm']
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './config.mjs'
export * from './registry.mjs'
export { getRegistry, setRegistry, getConfigPath } from './config.mjs'
export { speedTest } from './registry.mjs'
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nrm-lite",
"version": "0.1.3",
"version": "0.2.0",
"type": "module",
"description": "Fast and lightweight NpmRegistryManager, for the minimalists.",
"main": "index.mjs",
Expand All @@ -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"
Expand Down
64 changes: 37 additions & 27 deletions registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,49 @@ export const REGISTRIES = {
}

/**
*
* Note that `registryLineNumber` is index + 1
*
* Returns undefined when line does not contain registry, or registry as string
* @param {string} line
*/
function checkLine(line) {
let currLine = line.trim()
const keyName = 'registry'
if (!currLine.startsWith(keyName)) return
currLine = currLine.slice(keyName.length).trimStart()
if (!currLine.startsWith('=')) return
return currLine.slice(1).trimStart()
}

/**
* @param {NodeJS.ReadableStream} stream
* @returns {Promise<{registry:string,lines:string[],registryLineNumber:number}
* |{registry:null,lines:string[],registryLineNumber:null}>}
* @returns {Promise<string|undefined>}
* @see https://docs.npmjs.com/cli/configuring-npm/npmrc
*
* @example
* const fileStream = fs.createReadStream(path)
* findRegistryFromStream(fileStream)
*/
export async function findRegistryFromStream(stream) {
export async function getRegistryFromStream(stream) {
const rl = readline.createInterface(stream)
/**
* @type {string[]}
*/
const lines = []

for await (const entireLine of rl) {
lines.push(entireLine)

let currLine = entireLine.trim()
const keyName = 'registry'
if (!currLine.startsWith(keyName)) continue
currLine = currLine.slice(keyName.length).trimStart()
if (!currLine.startsWith('=')) continue
currLine = currLine.slice(1).trimStart()
for await (const line of rl) {
const r = checkLine(line)
if (r) return r
}
}

// now that current line is the registry url
return { registry: currLine, lines, registryLineNumber: lines.length }
/**
* Returns the proceed rc content
* @param {NodeJS.ReadableStream} stream
* @param {string} registryUrl
* @returns {Promise<string>}
*/
export async function setRegistryFromStream(stream, registryUrl) {
const rl = readline.createInterface(stream)
const lines = []
for await (const line of rl) {
const r = checkLine(line)
if (r) {
lines.push(`registry=${registryUrl}`)
} else {
lines.push(line)
}
}
return { registry: null, registryLineNumber: null, lines }
return lines.join('\n')
}

/**
Expand Down
41 changes: 19 additions & 22 deletions registry.test.mjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import { test } from 'node:test'
import * as assert from 'node:assert'
import { Readable } from 'node:stream'
import { findRegistryFromStream } from './registry.mjs'
import { getRegistryFromStream } from './registry.mjs'

test('test findRegistryFromStream()', async () => {
assert.deepStrictEqual(await findRegistryFromStream(Readable.from('')), {
lines: [],
registry: null,
registryLineNumber: null,
})
test('test getRegistryFromStream()', async () => {
assert.deepStrictEqual(
await getRegistryFromStream(Readable.from('')),
undefined
)

const text2 = `registry=https://registry.npmmirror.com/`
assert.deepStrictEqual(
await getRegistryFromStream(
Readable.from(`registry=https://registry.npmmirror.com/`)
),
'https://registry.npmmirror.com/'
)

assert.deepStrictEqual(await findRegistryFromStream(Readable.from(text2)), {
registry: 'https://registry.npmmirror.com/',
registryLineNumber: 1,
lines: [text2],
})

const text3 = `//registry.npmjs.org/:_authToken=npm_123456
assert.deepStrictEqual(
await getRegistryFromStream(
Readable.from(`//registry.npmjs.org/:_authToken=npm_123456
# strict-ssl=false
registry= https://registry.npmmirror.com/`

assert.deepStrictEqual(await findRegistryFromStream(Readable.from(text3)), {
registry: 'https://registry.npmmirror.com/',
registryLineNumber: 4,
lines: text3.split(/\n/g),
})
registry= https://registry.npmmirror.com/`)
),
'https://registry.npmmirror.com/'
)
})
82 changes: 0 additions & 82 deletions tty.mjs

This file was deleted.

Loading

0 comments on commit 7335b03

Please sign in to comment.