mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-19 14:20:36 -04:00
This is consistent with #9358, but implements support for the GitHub Packages npm registry and, more broadly, for vlt-style https://docs.vlt.sh/cli/registries for any registry. This PR adds a built-in gh: specifier that resolves against the GitHub Packages npm registry, plus a namedRegistries config key so a project can map its own aliases to arbitrary registries. A project can mix public npm packages and private GitHub Packages (or self-hosted) ones without applying a scope-wide registry override to every @scope/* package. - pnpm add gh:@acme/private writes "@acme/private": "gh:^1.0.0" and resolves from https://npm.pkg.github.com/. - pnpm add gh:@acme/private@^1.0.0 (with or without an alias) is also supported. Aliased form writes "my-alias": "gh:@acme/private@^1.0.0". - Auth comes from the existing per-URL .npmrc mechanism, e.g. //npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}. No new auth surface. - @github is intentionally not defaulted to https://npm.pkg.github.com/ - hardcoding that would hijack installs of the public @github/* packages on npmjs.org (e.g. @github/relative-time-element) for users without a scope-wide override. Use gh: to install from GitHub Packages, or configure @github:registry=... yourself if that's really what you want. - Additional named registries (a self-hosted proxy, GitHub Enterprise Server, etc.) can be configured in pnpm-workspace.yaml: ```yml namedRegistries: gh: https://npm.pkg.github.example.com/ # optional: overrides the built-in `gh` alias for GHES work: https://npm.work.example.com/ ``` - Then work:@corp/lib@^2.0.0 resolves against https://npm.work.example.com/, and the built-in gh alias can be redirected to a GHES host. - Env-var substitution (${VAR}) is supported in namedRegistries values, mirroring the .npmrc convention. - Reserved alias names (npm, jsr, github, workspace, catalog, file, git, http, https, link, patch, and related git host shorthands) cannot be redefined as user-named registries - the resolver throws ERR_PNPM_RESERVED_NAMED_REGISTRY_ALIAS at startup rather than silently shadowing another protocol. Malformed URLs throw ERR_PNPM_INVALID_NAMED_REGISTRY_URL at startup too, instead of failing as a confusing 404 during resolution. - On publish, createExportableManifest strips any named-registry prefix (both the built-in gh: and any user-configured alias) so npm and yarn consumers can still resolve the dependency via their own scope-registry configuration - mirroring the user-facing requirement when installing such a dep without the prefix. The prefix is gh: rather than github: because github: is reserved by npm-package-arg / hosted-git-info as a git host shorthand (e.g. github:owner/repo) - reusing it would be a deviation from the specs used by the npm CLI. gh: is shorter, matches vlt's convention, and cannot collide with any existing npm scheme. Unlike jsr:, gh: (and any other named-registry alias) does not rewrite the package name - gh:@acme/foo resolves @acme/foo from the GitHub Packages registry as-is. This also means npm/yarn consumers see the original name after the prefix is stripped on publish. --------- Co-authored-by: Zoltan Kochan <z@kochan.io>
57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { afterEach, expect, test } from '@jest/globals'
|
|
|
|
import { getOptionsFromPnpmSettings } from '../lib/getOptionsFromRootManifest.js'
|
|
|
|
const ORIGINAL_ENV = process.env
|
|
|
|
afterEach(() => {
|
|
process.env = { ...ORIGINAL_ENV }
|
|
})
|
|
|
|
test('getOptionsFromPnpmSettings() replaces env variables in settings', () => {
|
|
process.env.PNPM_TEST_KEY = 'foo'
|
|
process.env.PNPM_TEST_VALUE = 'bar'
|
|
const options = getOptionsFromPnpmSettings(process.cwd(), {
|
|
'${PNPM_TEST_KEY}': '${PNPM_TEST_VALUE}',
|
|
} as any) as any // eslint-disable-line
|
|
expect(options.foo).toBe('bar')
|
|
})
|
|
|
|
test('getOptionsFromPnpmSettings() expands env variables inside registries values', () => {
|
|
process.env.PNPM_TEST_TOKEN = 'secret'
|
|
const options = getOptionsFromPnpmSettings(process.cwd(), {
|
|
registries: {
|
|
default: 'https://registry.npmjs.org/',
|
|
'@scope': 'https://registry.example.com/${PNPM_TEST_TOKEN}/',
|
|
},
|
|
}) as any // eslint-disable-line
|
|
expect(options.registries['@scope']).toBe('https://registry.example.com/secret/')
|
|
})
|
|
|
|
test('getOptionsFromPnpmSettings() expands env variables inside namedRegistries values', () => {
|
|
process.env.PNPM_TEST_HOST = 'work.example.com'
|
|
const options = getOptionsFromPnpmSettings(process.cwd(), {
|
|
namedRegistries: {
|
|
work: 'https://${PNPM_TEST_HOST}/npm/',
|
|
},
|
|
} as any) as any // eslint-disable-line
|
|
expect(options.namedRegistries.work).toBe('https://work.example.com/npm/')
|
|
})
|
|
|
|
test('getOptionsFromPnpmSettings() converts allowBuilds', () => {
|
|
const options = getOptionsFromPnpmSettings(process.cwd(), {
|
|
allowBuilds: {
|
|
foo: true,
|
|
bar: false,
|
|
qar: 'warn',
|
|
},
|
|
})
|
|
expect(options).toStrictEqual({
|
|
allowBuilds: {
|
|
foo: true,
|
|
bar: false,
|
|
qar: 'warn',
|
|
},
|
|
})
|
|
})
|