Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replacing env variables to values in .npmrc #5623

Merged
merged 4 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-garlics-joke.md
@@ -0,0 +1,5 @@
---
"@pnpm/config": minor
---

New function added: `readLocalConfig(dir: string)`.
7 changes: 7 additions & 0 deletions .changeset/grumpy-bats-drum.md
@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-installation": patch
"@pnpm/plugin-commands-rebuild": patch
"pnpm": patch
---

Replace environment variable placeholders with their values, when reading `.npmrc` files in subdirectories inside a workspace [#2570](https://github.com/pnpm/pnpm/issues/2570).
5 changes: 4 additions & 1 deletion packages/config/package.json
Expand Up @@ -32,22 +32,25 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/packages/config#readme",
"dependencies": {
"@pnpm/config.env-replace": "1.0.0",
"@pnpm/constants": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/git-utils": "workspace:*",
"@pnpm/matcher": "workspace:*",
"@pnpm/npm-conf": "2.0.2",
"@pnpm/npm-conf": "2.0.4",
"@pnpm/pnpmfile": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/types": "workspace:*",
"camelcase": "^6.3.0",
"camelcase-keys": "^6.2.2",
"can-write-to-dir": "^1.1.1",
"is-subdir": "^1.2.0",
"is-windows": "^1.0.2",
"normalize-registry-url": "2.0.0",
"path-absolute": "^1.0.1",
"path-name": "^1.0.0",
"ramda": "npm:@pnpm/ramda@0.28.1",
"read-ini-file": "^3.1.0",
"realpath-missing": "^1.1.0",
"which": "^2.0.2"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/index.ts
Expand Up @@ -25,6 +25,8 @@ import {
} from './Config'
import { getWorkspaceConcurrency } from './concurrency'

export * from './readLocalConfig'

export { Config, UniversalOptions }

const npmDefaults = loadNpmConf.defaults
Expand Down
31 changes: 31 additions & 0 deletions packages/config/src/readLocalConfig.ts
@@ -0,0 +1,31 @@
import path from 'path'
import camelcaseKeys from 'camelcase-keys'
import { envReplace } from '@pnpm/config.env-replace'
import readIniFile from 'read-ini-file'

export async function readLocalConfig (prefix: string) {
try {
const ini = await readIniFile(path.join(prefix, '.npmrc')) as Record<string, string>
const config = camelcaseKeys(ini) as (Record<string, string> & { hoist?: boolean })
if (config.shamefullyFlatten) {
config.hoistPattern = '*'
// TODO: print a warning
}
if (config.hoist === false) {
config.hoistPattern = ''
}
for (const [key, val] of Object.entries(config)) {
if (typeof val === 'string') {
try {
config[key] = envReplace(val, process.env)
} catch (err) {
// ignore
}
}
}
return config
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
return {}
}
}
2 changes: 0 additions & 2 deletions packages/plugin-commands-installation/package.json
Expand Up @@ -89,7 +89,6 @@
"@zkochan/rimraf": "^2.1.2",
"@zkochan/table": "^1.0.0",
"@zkochan/which": "^2.0.3",
"camelcase-keys": "^6.2.2",
"chalk": "^4.1.2",
"enquirer": "^2.3.6",
"is-ci": "^3.0.1",
Expand All @@ -101,7 +100,6 @@
"path-absolute": "^1.0.1",
"path-exists": "^4.0.0",
"ramda": "npm:@pnpm/ramda@0.28.1",
"read-ini-file": "^3.1.0",
"render-help": "^1.0.2",
"version-selector-type": "^3.0.0"
},
Expand Down
22 changes: 1 addition & 21 deletions packages/plugin-commands-installation/src/recursive.ts
Expand Up @@ -4,7 +4,7 @@ import {
RecursiveSummary,
throwOnCommandFail,
} from '@pnpm/cli-utils'
import { Config } from '@pnpm/config'
import { Config, readLocalConfig } from '@pnpm/config'
import { PnpmError } from '@pnpm/error'
import { arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages'
import { logger } from '@pnpm/logger'
Expand All @@ -29,12 +29,10 @@ import {
mutateModules,
ProjectOptions,
} from '@pnpm/core'
import camelcaseKeys from 'camelcase-keys'
import isSubdir from 'is-subdir'
import mem from 'mem'
import pFilter from 'p-filter'
import pLimit from 'p-limit'
import readIniFile from 'read-ini-file'
import { getOptionsFromRootManifest } from './getOptionsFromRootManifest'
import { createWorkspaceSpecs, updateToWorkspacePackagesFromManifest } from './updateWorkspaceDependencies'
import { updateToLatestSpecsFromManifest, createLatestSpecs } from './updateToLatestSpecsFromManifest'
Expand Down Expand Up @@ -425,24 +423,6 @@ async function unlinkPkgs (dependencyNames: string[], manifest: ProjectManifest,
)
}

async function readLocalConfig (prefix: string) {
try {
const ini = await readIniFile(path.join(prefix, '.npmrc')) as Record<string, string>
const config = camelcaseKeys(ini) as (Record<string, string> & { hoist?: boolean })
if (config.shamefullyFlatten) {
config.hoistPattern = '*'
// TODO: print a warning
}
if (config.hoist === false) {
config.hoistPattern = ''
}
return config
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
return {}
}
}

function calculateRepositoryRoot (
workspaceDir: string,
projectDirs: string[]
Expand Down
30 changes: 30 additions & 0 deletions packages/plugin-commands-installation/test/miscRecursive.ts
Expand Up @@ -658,6 +658,36 @@ test('recursive install in a monorepo with different modules directories', async
await projects['project-2'].has('is-positive', 'modules_2')
})

test('recursive install in a monorepo with parsing env variables', async () => {
const projects = preparePackages([
{
name: 'project',
version: '1.0.0',

dependencies: {
'is-positive': '1.0.0',
},
},
])

process.env['SOME_NAME'] = 'some_name'
// eslint-disable-next-line no-template-curly-in-string
await fs.writeFile('project/.npmrc', 'modules-dir=${SOME_NAME}_modules', 'utf8')

const { allProjects, allProjectsGraph, selectedProjectsGraph } = await readProjects(process.cwd(), [])
await install.handler({
...DEFAULT_OPTS,
allProjects,
allProjectsGraph,
dir: process.cwd(),
recursive: true,
selectedProjectsGraph,
workspaceDir: process.cwd(),
})

await projects['project'].has('is-positive', `${process.env['SOME_NAME']}_modules`)
})

test('prefer-workspace-package', async () => {
await addDistTag({
distTag: 'latest',
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-commands-rebuild/package.json
Expand Up @@ -66,13 +66,11 @@
"@pnpm/store-connection-manager": "workspace:*",
"@pnpm/store-controller-types": "workspace:*",
"@pnpm/types": "workspace:*",
"camelcase-keys": "^6.2.2",
"dependency-path": "workspace:*",
"load-json-file": "^6.2.0",
"mem": "^8.1.1",
"p-limit": "^3.1.0",
"ramda": "npm:@pnpm/ramda@0.28.1",
"read-ini-file": "^3.1.0",
"render-help": "^1.0.2",
"run-groups": "^3.0.1",
"semver": "^7.3.8"
Expand Down
22 changes: 1 addition & 21 deletions packages/plugin-commands-rebuild/src/recursive.ts
@@ -1,20 +1,18 @@
import path from 'path'
import {
RecursiveSummary,
throwOnCommandFail,
} from '@pnpm/cli-utils'
import {
Config,
readLocalConfig,
} from '@pnpm/config'
import { arrayOfWorkspacePackagesToMap } from '@pnpm/find-workspace-packages'
import { logger } from '@pnpm/logger'
import { sortPackages } from '@pnpm/sort-packages'
import { createOrConnectStoreController, CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { Project, ProjectManifest } from '@pnpm/types'
import camelcaseKeys from 'camelcase-keys'
import mem from 'mem'
import pLimit from 'p-limit'
import readIniFile from 'read-ini-file'
import { rebuildProjects as rebuildAll, RebuildOptions, rebuildSelectedPkgs } from './implementation'

type RecursiveRebuildOpts = CreateStoreControllerOptions & Pick<Config,
Expand Down Expand Up @@ -163,21 +161,3 @@ export async function recursiveRebuild (

throwOnFail(result)
}

async function readLocalConfig (prefix: string) {
try {
const ini = await readIniFile(path.join(prefix, '.npmrc')) as Record<string, string>
const config = camelcaseKeys(ini) as (Record<string, string> & { hoist?: boolean })
if (config.shamefullyFlatten) {
config.hoistPattern = '*'
// TODO: print a warning
}
if (config.hoist === false) {
config.hoistPattern = ''
}
return config
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
return {}
}
}
38 changes: 21 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.