mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-31 19:18:19 -05:00
feat(find-workspace-dir): add env var support for setting the workspace dir (#3464)
This commit is contained in:
5
.changeset/six-files-drive.md
Normal file
5
.changeset/six-files-drive.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/find-workspace-dir": patch
|
||||
---
|
||||
|
||||
Add support for NPM_CONFIG_WORKSPACE_DIR override environment variable when finding the workspace directory
|
||||
1
packages/find-workspace-dir/jest.config.js
Normal file
1
packages/find-workspace-dir/jest.config.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require('../../jest.config.js')
|
||||
@@ -12,8 +12,9 @@
|
||||
"node": ">=12.17"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint -c ../../eslint.json src/**/*.ts",
|
||||
"test": "pnpm run compile",
|
||||
"lint": "eslint -c ../../eslint.json src/**/*.ts test/**/*.ts",
|
||||
"_test": "jest",
|
||||
"test": "pnpm run compile && pnpm run _test",
|
||||
"prepublishOnly": "pnpm run compile",
|
||||
"compile": "rimraf lib tsconfig.tsbuildinfo && tsc --build && pnpm run lint -- --fix"
|
||||
},
|
||||
|
||||
@@ -2,10 +2,14 @@ import path from 'path'
|
||||
import PnpmError from '@pnpm/error'
|
||||
import findUp from 'find-up'
|
||||
|
||||
const WORKSPACE_DIR_ENV_VAR = 'NPM_CONFIG_WORKSPACE_DIR'
|
||||
const WORKSPACE_MANIFEST_FILENAME = 'pnpm-workspace.yaml'
|
||||
|
||||
export default async function findWorkspaceDir (cwd: string) {
|
||||
const workspaceManifestLocation = await findUp([WORKSPACE_MANIFEST_FILENAME, 'pnpm-workspace.yml'], { cwd })
|
||||
const workspaceManifestDirEnvVar = process.env[WORKSPACE_DIR_ENV_VAR] ?? process.env[WORKSPACE_DIR_ENV_VAR.toLowerCase()]
|
||||
const workspaceManifestLocation = workspaceManifestDirEnvVar
|
||||
? path.join(workspaceManifestDirEnvVar, 'pnpm-workspace.yaml')
|
||||
: await findUp([WORKSPACE_MANIFEST_FILENAME, 'pnpm-workspace.yml'], { cwd })
|
||||
if (workspaceManifestLocation?.endsWith('.yml')) {
|
||||
throw new PnpmError('BAD_WORKSPACE_MANIFEST_NAME', `The workspace manifest file should be named "pnpm-workspace.yaml". File found: ${workspaceManifestLocation}`)
|
||||
}
|
||||
|
||||
21
packages/find-workspace-dir/test/index.ts
Normal file
21
packages/find-workspace-dir/test/index.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/// <reference path="../../../typings/index.d.ts"/>
|
||||
import path from 'path'
|
||||
import findWorkspaceDir from '@pnpm/find-workspace-dir'
|
||||
|
||||
const NPM_CONFIG_WORKSPACE_DIR_ENV_VAR = 'NPM_CONFIG_WORKSPACE_DIR'
|
||||
const FAKE_PATH = 'FAKE_PATH'
|
||||
|
||||
test('finds actual workspace dir', async () => {
|
||||
const workspaceDir = await findWorkspaceDir(process.cwd())
|
||||
|
||||
expect(workspaceDir).toBe(path.resolve(__dirname, '..', '..', '..'))
|
||||
})
|
||||
|
||||
test('finds overriden workspace dir', async () => {
|
||||
const oldValue = process.env[NPM_CONFIG_WORKSPACE_DIR_ENV_VAR]
|
||||
process.env[NPM_CONFIG_WORKSPACE_DIR_ENV_VAR] = FAKE_PATH
|
||||
const workspaceDir = await findWorkspaceDir(process.cwd())
|
||||
process.env[NPM_CONFIG_WORKSPACE_DIR_ENV_VAR] = oldValue
|
||||
|
||||
expect(workspaceDir).toBe(FAKE_PATH)
|
||||
})
|
||||
Reference in New Issue
Block a user