From 37582994616f87270ef1dd8fb8b29f64578c3504 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Mon, 3 Apr 2023 14:47:41 +0200 Subject: [PATCH] fix(core): make ensurePackage work with pnpm workspaces --- docs/generated/devkit/nrwl_devkit.md | 9 ++++---- .../packages/devkit/documents/nrwl_devkit.md | 9 ++++---- e2e/storybook/src/storybook-nested.test.ts | 1 + .../src/create-nx-workspace.test.ts | 2 ++ packages/devkit/src/utils/package-json.ts | 22 ++++++++++++++----- packages/nx/src/utils/package-manager.ts | 6 +++-- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/docs/generated/devkit/nrwl_devkit.md b/docs/generated/devkit/nrwl_devkit.md index a253317955679..71c85e0c724a1 100644 --- a/docs/generated/devkit/nrwl_devkit.md +++ b/docs/generated/devkit/nrwl_devkit.md @@ -1433,7 +1433,7 @@ Returns the list of outputs that will be cached. ### getPackageManagerCommand -▸ **getPackageManagerCommand**(`packageManager?`): `PackageManagerCommands` +▸ **getPackageManagerCommand**(`packageManager?`, `root?`): `PackageManagerCommands` Returns commands for the package manager used in the workspace. By default, the package manager is derived based on the lock file, @@ -1447,9 +1447,10 @@ execSync(`${getPackageManagerCommand().addDev} my-dev-package`); #### Parameters -| Name | Type | -| :--------------- | :-------------------------------------------------------------------- | -| `packageManager` | [`PackageManager`](../../devkit/documents/nrwl_devkit#packagemanager) | +| Name | Type | Default value | +| :--------------- | :-------------------------------------------------------------------- | :-------------- | +| `packageManager` | [`PackageManager`](../../devkit/documents/nrwl_devkit#packagemanager) | `undefined` | +| `root` | `string` | `workspaceRoot` | #### Returns diff --git a/docs/generated/packages/devkit/documents/nrwl_devkit.md b/docs/generated/packages/devkit/documents/nrwl_devkit.md index a253317955679..71c85e0c724a1 100644 --- a/docs/generated/packages/devkit/documents/nrwl_devkit.md +++ b/docs/generated/packages/devkit/documents/nrwl_devkit.md @@ -1433,7 +1433,7 @@ Returns the list of outputs that will be cached. ### getPackageManagerCommand -▸ **getPackageManagerCommand**(`packageManager?`): `PackageManagerCommands` +▸ **getPackageManagerCommand**(`packageManager?`, `root?`): `PackageManagerCommands` Returns commands for the package manager used in the workspace. By default, the package manager is derived based on the lock file, @@ -1447,9 +1447,10 @@ execSync(`${getPackageManagerCommand().addDev} my-dev-package`); #### Parameters -| Name | Type | -| :--------------- | :-------------------------------------------------------------------- | -| `packageManager` | [`PackageManager`](../../devkit/documents/nrwl_devkit#packagemanager) | +| Name | Type | Default value | +| :--------------- | :-------------------------------------------------------------------- | :-------------- | +| `packageManager` | [`PackageManager`](../../devkit/documents/nrwl_devkit#packagemanager) | `undefined` | +| `root` | `string` | `workspaceRoot` | #### Returns diff --git a/e2e/storybook/src/storybook-nested.test.ts b/e2e/storybook/src/storybook-nested.test.ts index 51e93312212fd..812b5f1ac81ab 100644 --- a/e2e/storybook/src/storybook-nested.test.ts +++ b/e2e/storybook/src/storybook-nested.test.ts @@ -17,6 +17,7 @@ describe('Storybook generators and executors for standalone workspaces - using R beforeAll(() => { // create a workspace with a single react app at the root + // TODO @meeroslav: we always need to pass in `packageManager` here, otherwise `runCreateWorkspace` will use `npm` by default runCreateWorkspace(wsName, { preset: 'react-standalone', appName, diff --git a/e2e/workspace-create/src/create-nx-workspace.test.ts b/e2e/workspace-create/src/create-nx-workspace.test.ts index cd9a09a3763cc..398654a0f870b 100644 --- a/e2e/workspace-create/src/create-nx-workspace.test.ts +++ b/e2e/workspace-create/src/create-nx-workspace.test.ts @@ -247,6 +247,7 @@ describe('create-nx-workspace', () => { it('should be able to create react-native workspace', () => { const wsName = uniq('react-native'); const appName = uniq('app'); + // TODO @meeroslav: let's run those only on macos or only if the `packageManager` is `npm` runCreateWorkspace(wsName, { preset: 'react-native', appName, @@ -260,6 +261,7 @@ describe('create-nx-workspace', () => { it('should be able to create an expo workspace', () => { const wsName = uniq('expo'); const appName = uniq('app'); + // TODO @meeroslav: let's run those only on macos or only if the `packageManager` is `npm` runCreateWorkspace(wsName, { preset: 'expo', appName, diff --git a/packages/devkit/src/utils/package-json.ts b/packages/devkit/src/utils/package-json.ts index a5fbceba8fda9..8a427e627975c 100644 --- a/packages/devkit/src/utils/package-json.ts +++ b/packages/devkit/src/utils/package-json.ts @@ -11,8 +11,13 @@ import { requireNx } from '../../nx'; import { dirSync } from 'tmp'; import { join } from 'path'; -const { readJson, updateJson, getPackageManagerCommand, workspaceRoot } = - requireNx(); +const { + readJson, + updateJson, + getPackageManagerCommand, + workspaceRoot, + detectPackageManager, +} = requireNx(); const UNIDENTIFIED_VERSION = 'UNIDENTIFIED_VERSION'; const NON_SEMVER_TAGS = { @@ -449,10 +454,15 @@ export function ensurePackage( console.log(`Fetching ${pkg}...`); const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true'; - execSync(`${getPackageManagerCommand().addDev} ${pkg}@${requiredVersion}`, { - cwd: tempDir, - stdio: isVerbose ? 'inherit' : 'ignore', - }); + execSync( + `${ + getPackageManagerCommand(detectPackageManager(), tempDir).addDev + } ${pkg}@${requiredVersion}`, + { + cwd: tempDir, + stdio: isVerbose ? 'inherit' : 'ignore', + } + ); addToNodePath(join(workspaceRoot, 'node_modules')); addToNodePath(join(tempDir, 'node_modules')); diff --git a/packages/nx/src/utils/package-manager.ts b/packages/nx/src/utils/package-manager.ts index c41165b979aee..6156b422de490 100644 --- a/packages/nx/src/utils/package-manager.ts +++ b/packages/nx/src/utils/package-manager.ts @@ -7,6 +7,7 @@ import { promisify } from 'util'; import { writeJsonFile } from './fileutils'; import { readModulePackageJson } from './package-json'; import { gte, lt } from 'semver'; +import { workspaceRoot } from './workspace-root'; const execAsync = promisify(exec); @@ -46,7 +47,8 @@ export function detectPackageManager(dir: string = ''): PackageManager { * ``` */ export function getPackageManagerCommand( - packageManager: PackageManager = detectPackageManager() + packageManager: PackageManager = detectPackageManager(), + root: string = workspaceRoot ): PackageManagerCommands { const commands: { [pm in PackageManager]: () => PackageManagerCommands } = { yarn: () => { @@ -70,7 +72,7 @@ export function getPackageManagerCommand( const pnpmVersion = getPackageManagerVersion('pnpm'); const useExec = gte(pnpmVersion, '6.13.0'); const includeDoubleDashBeforeArgs = lt(pnpmVersion, '7.0.0'); - const isPnpmWorkspace = existsSync('pnpm-workspace.yaml'); + const isPnpmWorkspace = existsSync(join(root, 'pnpm-workspace.yaml')); return { install: 'pnpm install --no-frozen-lockfile', // explicitly disable in case of CI