Skip to content

Commit

Permalink
fix: replacing env variables to values in .npmrc (#5623)
Browse files Browse the repository at this point in the history
close #5604
close #2570

Co-authored-by: Zoltan Kochan <z@kochan.io>
  • Loading branch information
DudaGod and zkochan committed Nov 11, 2022
1 parent f61de1d commit e6cd45d
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 397 deletions.
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).
3 changes: 3 additions & 0 deletions packages/config/package.json
Expand Up @@ -32,6 +32,7 @@
},
"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/global-bin-dir": "workspace:*",
Expand All @@ -40,10 +41,12 @@
"@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",
"normalize-registry-url": "2.0.0",
"ramda": "^0.27.2",
"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 @@ -24,6 +24,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": "^0.27.2",
"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 @@ -28,12 +28,10 @@ import {
MutatedProject,
mutateModules,
} 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 @@ -449,24 +447,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
29 changes: 29 additions & 0 deletions packages/plugin-commands-installation/test/miscRecursive.ts
Expand Up @@ -607,6 +607,35 @@ 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, selectedProjectsGraph } = await readProjects(process.cwd(), [])
await install.handler({
...DEFAULT_OPTS,
allProjects,
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,14 +66,12 @@
"@pnpm/store-controller-types": "workspace:*",
"@pnpm/types": "workspace:*",
"@zkochan/npm-package-arg": "^2.0.1",
"camelcase-keys": "^6.2.2",
"dependency-path": "workspace:*",
"graph-sequencer": "2.0.0",
"load-json-file": "^6.2.0",
"mem": "^8.1.1",
"p-limit": "^3.1.0",
"ramda": "^0.27.2",
"read-ini-file": "^3.1.0",
"render-help": "^1.0.2",
"run-groups": "^3.0.1",
"semver": "^7.3.7"
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 { rebuild as rebuildAll, RebuildOptions, rebuildPkgs } from './implementation'

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

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 {}
}
}

0 comments on commit e6cd45d

Please sign in to comment.