-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec9acac
commit 69adedc
Showing
7 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,53 @@ | ||
export const foo = 'bar' | ||
import {encode} from 'ini' | ||
|
||
export interface INpmrcData { | ||
format?: string, | ||
root?: Record<string, string>, | ||
scopes?: Record<string, { | ||
registry: string | ||
auth?: string | ||
authToken?: string | ||
username?: string | ||
password?: string | ||
[i: string]: string | undefined | ||
}> | ||
} | ||
|
||
const credKeys = new Set(['_auth', '_authToken', 'username', '_password']) | ||
const credAliases = new Set(['auth', 'authToken', 'password']) | ||
|
||
// https://docs.npmjs.com/cli/v9/configuring-npm/npmrc | ||
export const formatNpmrc = ({scopes = {}, root = {}, format}: INpmrcData): string => { | ||
const config: Record<string, string> = {} | ||
const registry = normalizeRegistry(root.registry || 'registry.npmjs.org') | ||
|
||
for (const [name, scope] of Object.entries(scopes)) { | ||
for (const [k, v] of Object.entries(scope)) { | ||
if (k === 'registry') { | ||
config[`@${normalizeScope(name)}:${k}`] = v as string | ||
continue | ||
} | ||
config[`${normalizeRegistry(scope.registry)}:${normalizeKey(k)}`] = v || 'true' | ||
} | ||
} | ||
|
||
for (const [_k, v, k = normalizeKey(_k)] of Object.entries(root)) { | ||
if (format === 'npm9' && credKeys.has(k)) { | ||
config[`${registry}:${k}`] = v || 'true' | ||
continue | ||
} | ||
config[k] = v || 'true' | ||
} | ||
|
||
return encode(config) | ||
} | ||
|
||
const normalizeRegistry = (value: string): string => value | ||
.replace(/^(https?:\/\/)?/, '//') | ||
.replace(/\/*$/, '/') | ||
|
||
const normalizeScope = (value: string): string => value.replace(/^@/, '') | ||
const normalizeKey = (value: string): string => | ||
credAliases.has(value) | ||
? `_${value}` | ||
: value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
const { test } = require('uvu') | ||
const assert = require('node:assert') | ||
const toposource = require('@semrel-extra/npmrc') | ||
const npmrc = require('@semrel-extra/npmrc') | ||
|
||
test('index (cjs)', () => { | ||
assert.ok(typeof toposource.foo === 'string') | ||
assert.ok(typeof npmrc.formatNpmrc === 'function') | ||
}) | ||
|
||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { test } from 'uvu' | ||
import assert from 'node:assert' | ||
import {foo} from '@semrel-extra/npmrc' | ||
import { formatNpmrc } from '@semrel-extra/npmrc' | ||
|
||
test('index (mjs)', () => { | ||
assert.ok(typeof foo === 'string') | ||
assert.ok(typeof formatNpmrc === 'function') | ||
}) | ||
|
||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,63 @@ | ||
import { test } from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
|
||
import { foo } from '../../main/ts' | ||
import {formatNpmrc, INpmrcData} from '../../main/ts' | ||
|
||
test('index has proper index', () => { | ||
assert.ok(typeof foo === 'string') | ||
assert.ok(typeof formatNpmrc === 'function') | ||
}) | ||
|
||
test('`formatNpmrc()` returns proper contents', () => { | ||
const cases: [INpmrcData, string][] = [ | ||
[ | ||
{}, | ||
'' | ||
], | ||
[ | ||
{ | ||
scopes: { | ||
test: { | ||
registry: 'https://registry.example.com', | ||
'always-auth': '', | ||
auth: 'basic', | ||
_authToken: 'bearer' | ||
} | ||
} | ||
}, | ||
`@test:registry=https://registry.example.com | ||
//registry.example.com/:always-auth=true | ||
//registry.example.com/:_auth=basic | ||
//registry.example.com/:_authToken=bearer | ||
` | ||
], | ||
[ | ||
{ | ||
format: 'npm9', | ||
root: { | ||
'always-auth': '', | ||
'auth': 'basic' | ||
} | ||
}, | ||
`always-auth=true | ||
//registry.npmjs.org/:_auth=basic | ||
` | ||
], | ||
[ | ||
{ | ||
root: { | ||
'always-auth': '', | ||
'auth': 'basic' | ||
} | ||
}, | ||
`always-auth=true | ||
_auth=basic | ||
` | ||
] | ||
]; | ||
|
||
for (const [data, expected] of cases) { | ||
assert.equal(formatNpmrc(data), expected) | ||
} | ||
}) | ||
|
||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters