Files
pnpm/workspace/commands/test/init.test.ts
Zoltan Kochan 96704a1c58 refactor(config): rename rawConfig to authConfig, add nodeDownloadMirrors, simplify config reader (#11194)
Major cleanup of the config system after migrating settings from `.npmrc` to `pnpm-workspace.yaml`.

### Config reader simplification
- Remove `checkUnknownSetting` (dead code, always `false`)
- Trim `npmConfigTypes` from ~127 to ~67 keys (remove unused npm config keys)
- Replace `rcOptions` iteration over all type keys with direct construction from defaults + auth overlay
- Remove `rcOptionsTypes` parameter from `getConfig()` and its assembly chain

### Rename `rawConfig` to `authConfig`
- `rawConfig` was a confusing mix of auth data and general settings
- Non-auth settings are already on the typed `Config` object — stop duplicating them in `rawConfig`
- Rename `rawConfig` → `authConfig` across the codebase to clarify it only contains auth/registry data from `.npmrc`

### Remove `rawConfig` from non-auth consumers
- **Lifecycle hooks**: replace `rawConfig: object` with `userAgent?: string` — only user-agent was read
- **Fetchers**: remove unused `rawConfig` from git fetcher, binary fetcher, tarball fetcher, prepare-package
- **Update command**: use `opts.production/dev/optional` instead of `rawConfig.*`
- **`pnpm init`**: accept typed init properties instead of parsing `rawConfig`

### Add `nodeDownloadMirrors` setting
- New `nodeDownloadMirrors?: Record<string, string>` on `PnpmSettings` and `Config`
- Replaces the `node-mirror:<channel>` pattern that was stored in `rawConfig`
- Configured in `pnpm-workspace.yaml`:
  ```yaml
  nodeDownloadMirrors:
    release: https://my-mirror.example.com/download/release/
  ```
- Remove unused `rawConfig` from deno-resolver and bun-resolver

### Refactor `pnpm config get/list`
- New `configToRecord()` builds display data from typed Config properties on the fly
- Excludes sensitive internals (`authInfos`, `sslConfigs`, etc.)
- Non-types keys (e.g., `package-extensions`) resolve through `configToRecord` instead of direct property access
- Delete `processConfig.ts` (replaced by `configToRecord.ts`)

### Pre-push hook improvement
- Add `compile-only` (`tsgo --build`) to pre-push hook to catch type errors before push
2026-04-04 20:33:43 +02:00

106 lines
3.6 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { prepare, prepareEmpty } from '@pnpm/prepare'
import type { ProjectManifest } from '@pnpm/types'
import { init } from '@pnpm/workspace.commands'
import { loadJsonFileSync } from 'load-json-file'
test('init a new package.json', async () => {
prepareEmpty()
await init.handler({ cliOptions: {} })
const manifest = loadJsonFileSync(path.resolve('package.json'))
expect(manifest).toBeTruthy()
})
test('throws an error if a package.json exists in the current directory', async () => {
prepare({})
await expect(
init.handler({ cliOptions: {} })
).rejects.toThrow('package.json already exists')
})
test('init a new package.json with author and license settings', async () => {
prepareEmpty()
await init.handler({
cliOptions: {},
initAuthorEmail: 'xxxxxx@pnpm.com',
initAuthorName: 'pnpm',
initAuthorUrl: 'https://www.github.com/pnpm',
initLicense: 'MIT',
initVersion: '2.0.0',
})
const manifest: Record<string, string> = loadJsonFileSync(path.resolve('package.json'))
expect(manifest.version).toBe('2.0.0')
expect(manifest.author).toBe('pnpm <xxxxxx@pnpm.com> (https://www.github.com/pnpm)')
expect(manifest.license).toBe('MIT')
})
test('throw an error if params are passed to the init command', async () => {
prepare({})
await expect(
init.handler({ cliOptions: {} }, ['react-app'])
).rejects.toThrow('init command does not accept any arguments')
})
test('init a new package.json if a package.json exists in the parent directory', async () => {
prepare({})
fs.mkdirSync('empty-dir1')
process.chdir('./empty-dir1')
await init.handler({ cliOptions: {} })
const manifest = loadJsonFileSync(path.resolve('package.json'))
expect(manifest).toBeTruthy()
})
test('init a new package.json if a package.json exists in the current directory but specifies --dir option', async () => {
prepare({})
fs.mkdirSync('empty-dir2')
await init.handler({
cliOptions: {
dir: './empty-dir2',
},
})
const manifest = loadJsonFileSync(path.resolve('empty-dir2/package.json'))
expect(manifest).toBeTruthy()
})
test('init a new package.json with init-package-manager=true', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initPackageManager: true })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest.packageManager).toBeTruthy()
})
test('init a new package.json with init-package-manager=false', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initPackageManager: false })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).toBeTruthy()
expect(manifest).not.toHaveProperty('packageManager')
})
test('init a new package.json with init-type=module', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, initType: 'module' })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest.type).toBe('module')
})
test('init a new package.json with --bare', async () => {
prepareEmpty()
await init.handler({ cliOptions: {}, bare: true })
const manifest = loadJsonFileSync<ProjectManifest>(path.resolve('package.json'))
expect(manifest).not.toHaveProperty(['name'])
expect(manifest).not.toHaveProperty(['version'])
expect(manifest).not.toHaveProperty(['description'])
expect(manifest).not.toHaveProperty(['main'])
expect(manifest).not.toHaveProperty(['keywords'])
expect(manifest).not.toHaveProperty(['author'])
expect(manifest).not.toHaveProperty(['license'])
})