|
1 | 1 | import {
|
2 | 2 | detectPackageManager,
|
| 3 | + getPackageManagerVersion, |
3 | 4 | isWorkspacesEnabled,
|
| 5 | + output, |
4 | 6 | readJson,
|
| 7 | + type GeneratorCallback, |
5 | 8 | type Tree,
|
6 | 9 | } from '@nx/devkit';
|
7 | 10 | import { minimatch } from 'minimatch';
|
8 | 11 | import { join } from 'node:path/posix';
|
9 | 12 | import { getGlobPatternsFromPackageManagerWorkspaces } from 'nx/src/plugins/package-json';
|
| 13 | +import { lt } from 'semver'; |
10 | 14 |
|
11 | 15 | export type ProjectPackageManagerWorkspaceState =
|
12 | 16 | | 'included'
|
@@ -35,3 +39,51 @@ export function getProjectPackageManagerWorkspaceState(
|
35 | 39 | export function isUsingPackageManagerWorkspaces(tree: Tree): boolean {
|
36 | 40 | return isWorkspacesEnabled(detectPackageManager(tree.root), tree.root);
|
37 | 41 | }
|
| 42 | + |
| 43 | +export function getProjectPackageManagerWorkspaceStateWarningTask( |
| 44 | + projectPackageManagerWorkspaceState: ProjectPackageManagerWorkspaceState, |
| 45 | + workspaceRoot: string |
| 46 | +): GeneratorCallback { |
| 47 | + return (): void => { |
| 48 | + if (projectPackageManagerWorkspaceState !== 'excluded') { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const packageManager = detectPackageManager(workspaceRoot); |
| 53 | + let adviseMessage = |
| 54 | + 'updating the "workspaces" option in the workspace root "package.json" file with the project root or pattern that includes it'; |
| 55 | + let packageManagerWorkspaceSetupDocs: string; |
| 56 | + if (packageManager === 'pnpm') { |
| 57 | + adviseMessage = |
| 58 | + 'updating the "pnpm-workspace.yaml" file with the project root or pattern that includes it'; |
| 59 | + packageManagerWorkspaceSetupDocs = |
| 60 | + 'https://pnpm.io/workspaces and https://pnpm.io/pnpm-workspace_yaml'; |
| 61 | + } else if (packageManager === 'yarn') { |
| 62 | + const yarnVersion = getPackageManagerVersion( |
| 63 | + packageManager, |
| 64 | + workspaceRoot |
| 65 | + ); |
| 66 | + if (lt(yarnVersion, '2.0.0')) { |
| 67 | + packageManagerWorkspaceSetupDocs = |
| 68 | + 'https://classic.yarnpkg.com/lang/en/docs/workspaces/'; |
| 69 | + } else { |
| 70 | + packageManagerWorkspaceSetupDocs = |
| 71 | + 'https://yarnpkg.com/features/workspaces'; |
| 72 | + } |
| 73 | + } else if (packageManager === 'npm') { |
| 74 | + packageManagerWorkspaceSetupDocs = |
| 75 | + 'https://docs.npmjs.com/cli/v10/using-npm/workspaces'; |
| 76 | + } else if (packageManager === 'bun') { |
| 77 | + packageManagerWorkspaceSetupDocs = |
| 78 | + 'https://bun.sh/docs/install/workspaces'; |
| 79 | + } |
| 80 | + |
| 81 | + output.warn({ |
| 82 | + title: `The project is not included in the package manager workspaces configuration`, |
| 83 | + bodyLines: [ |
| 84 | + `Please add the project to the package manager workspaces configuration by ${adviseMessage}.`, |
| 85 | + `Read more about the ${packageManager} workspaces feature and how to set it up at ${packageManagerWorkspaceSetupDocs}.`, |
| 86 | + ], |
| 87 | + }); |
| 88 | + }; |
| 89 | +} |
0 commit comments