Skip to content

Commit

Permalink
fix(core): make ensurePackage work with pnpm workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Apr 3, 2023
1 parent 90fd0c4 commit 6bd9f2e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
9 changes: 5 additions & 4 deletions docs/generated/devkit/nrwl_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions docs/generated/packages/devkit/documents/nrwl_devkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions e2e/storybook/src/storybook-nested.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions e2e/workspace-create/src/create-nx-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
22 changes: 16 additions & 6 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -449,10 +454,15 @@ export function ensurePackage<T extends any = any>(

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'));
Expand Down
6 changes: 4 additions & 2 deletions packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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: () => {
Expand All @@ -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
Expand Down

0 comments on commit 6bd9f2e

Please sign in to comment.