@nx/workspace 20.5.0-rc.4 → 20.6.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/workspace",
3
- "version": "20.5.0-rc.4",
3
+ "version": "20.6.0-beta.0",
4
4
  "private": false,
5
5
  "description": "The Workspace plugin contains executors and generators that are useful for any Nx workspace. It should be present in every Nx workspace and other plugins build on it.",
6
6
  "repository": {
@@ -38,12 +38,14 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@nx/devkit": "20.5.0-rc.4",
41
+ "@nx/devkit": "20.6.0-beta.0",
42
+ "@zkochan/js-yaml": "0.0.7",
42
43
  "chalk": "^4.1.0",
43
44
  "enquirer": "~2.3.6",
45
+ "picomatch": "4.0.2",
44
46
  "tslib": "^2.3.0",
45
47
  "yargs-parser": "21.1.1",
46
- "nx": "20.5.0-rc.4"
48
+ "nx": "20.6.0-beta.0"
47
49
  },
48
50
  "publishConfig": {
49
51
  "access": "public"
@@ -1,4 +1,4 @@
1
- import { Tree } from '@nx/devkit';
1
+ import { type GeneratorCallback, type Tree } from '@nx/devkit';
2
2
  import { Schema } from './schema';
3
- export declare function moveGenerator(tree: Tree, rawSchema: Schema): Promise<void>;
3
+ export declare function moveGenerator(tree: Tree, rawSchema: Schema): Promise<GeneratorCallback>;
4
4
  export default moveGenerator;
@@ -2,8 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.moveGenerator = moveGenerator;
4
4
  const devkit_1 = require("@nx/devkit");
5
+ const package_manager_workspaces_1 = require("../../utilities/package-manager-workspaces");
6
+ const ts_solution_setup_1 = require("../../utilities/typescript/ts-solution-setup");
5
7
  const check_destination_1 = require("./lib/check-destination");
6
8
  const create_project_configuration_in_new_destination_1 = require("./lib/create-project-configuration-in-new-destination");
9
+ const extract_base_configs_1 = require("./lib/extract-base-configs");
7
10
  const move_project_files_1 = require("./lib/move-project-files");
8
11
  const normalize_schema_1 = require("./lib/normalize-schema");
9
12
  const run_angular_plugin_1 = require("./lib/run-angular-plugin");
@@ -18,9 +21,9 @@ const update_package_json_1 = require("./lib/update-package-json");
18
21
  const update_project_root_files_1 = require("./lib/update-project-root-files");
19
22
  const update_readme_1 = require("./lib/update-readme");
20
23
  const update_storybook_config_1 = require("./lib/update-storybook-config");
21
- const extract_base_configs_1 = require("./lib/extract-base-configs");
22
24
  async function moveGenerator(tree, rawSchema) {
23
25
  let projectConfig = (0, devkit_1.readProjectConfiguration)(tree, rawSchema.projectName);
26
+ const wasIncludedInWorkspaces = (0, package_manager_workspaces_1.isProjectIncludedInPackageManagerWorkspaces)(tree, projectConfig.root);
24
27
  const schema = await (0, normalize_schema_1.normalizeSchema)(tree, rawSchema, projectConfig);
25
28
  (0, check_destination_1.checkDestination)(tree, schema, rawSchema.destination);
26
29
  if (projectConfig.root === '.') {
@@ -48,8 +51,23 @@ async function moveGenerator(tree, rawSchema) {
48
51
  (0, extract_base_configs_1.maybeMigrateEslintConfigIfRootProject)(tree, projectConfig);
49
52
  }
50
53
  await (0, run_angular_plugin_1.runAngularPlugin)(tree, schema);
54
+ let task;
55
+ if (wasIncludedInWorkspaces) {
56
+ // check if the new destination is included in the package manager workspaces
57
+ const isIncludedInWorkspaces = (0, package_manager_workspaces_1.isProjectIncludedInPackageManagerWorkspaces)(tree, schema.destination);
58
+ if (!isIncludedInWorkspaces) {
59
+ // the new destination is not included in the package manager workspaces
60
+ // so we need to add it and run a package install to ensure the symlink
61
+ // is created
62
+ await (0, ts_solution_setup_1.addProjectToTsSolutionWorkspace)(tree, schema.destination);
63
+ task = () => (0, devkit_1.installPackagesTask)(tree, true);
64
+ }
65
+ }
51
66
  if (!schema.skipFormat) {
52
67
  await (0, devkit_1.formatFiles)(tree);
53
68
  }
69
+ if (task) {
70
+ return task;
71
+ }
54
72
  }
55
73
  exports.default = moveGenerator;
@@ -73,6 +73,7 @@ async function createPreset(tree, options) {
73
73
  nxCloudToken: options.nxCloudToken,
74
74
  useTsSolution: options.workspaces,
75
75
  formatter: options.formatter,
76
+ useProjectJson: !options.workspaces,
76
77
  });
77
78
  }
78
79
  else if (options.preset === presets_1.Preset.ReactStandalone) {
@@ -106,6 +107,7 @@ async function createPreset(tree, options) {
106
107
  nxCloudToken: options.nxCloudToken,
107
108
  useTsSolution: options.workspaces,
108
109
  formatter: options.formatter,
110
+ useProjectJson: !options.workspaces,
109
111
  });
110
112
  }
111
113
  else if (options.preset === presets_1.Preset.RemixStandalone) {
@@ -137,6 +139,7 @@ async function createPreset(tree, options) {
137
139
  nxCloudToken: options.nxCloudToken,
138
140
  useTsSolution: options.workspaces,
139
141
  formatter: options.formatter,
142
+ useProjectJson: !options.workspaces,
140
143
  });
141
144
  }
142
145
  else if (options.preset === presets_1.Preset.VueStandalone) {
@@ -168,6 +171,7 @@ async function createPreset(tree, options) {
168
171
  nxCloudToken: options.nxCloudToken,
169
172
  useTsSolution: options.workspaces,
170
173
  formatter: options.formatter,
174
+ useProjectJson: !options.workspaces,
171
175
  });
172
176
  }
173
177
  else if (options.preset === presets_1.Preset.NuxtStandalone) {
@@ -200,6 +204,7 @@ async function createPreset(tree, options) {
200
204
  addPlugin,
201
205
  useTsSolution: options.workspaces,
202
206
  formatter: options.formatter,
207
+ useProjectJson: !options.workspaces,
203
208
  });
204
209
  }
205
210
  else if (options.preset === presets_1.Preset.NextJsStandalone) {
@@ -245,6 +250,7 @@ async function createPreset(tree, options) {
245
250
  addPlugin,
246
251
  useTsSolution: options.workspaces,
247
252
  formatter: options.formatter,
253
+ useProjectJson: !options.workspaces,
248
254
  });
249
255
  }
250
256
  else if (options.preset === presets_1.Preset.Express) {
@@ -258,6 +264,7 @@ async function createPreset(tree, options) {
258
264
  addPlugin,
259
265
  useTsSolution: options.workspaces,
260
266
  formatter: options.formatter,
267
+ useProjectJson: !options.workspaces,
261
268
  });
262
269
  }
263
270
  else if (options.preset === presets_1.Preset.ReactNative) {
@@ -274,6 +281,7 @@ async function createPreset(tree, options) {
274
281
  bundler: options.bundler ?? 'webpack',
275
282
  useTsSolution: options.workspaces,
276
283
  formatter: options.formatter,
284
+ useProjectJson: !options.workspaces,
277
285
  });
278
286
  }
279
287
  else if (options.preset === presets_1.Preset.Expo) {
@@ -288,6 +296,7 @@ async function createPreset(tree, options) {
288
296
  nxCloudToken: options.nxCloudToken,
289
297
  useTsSolution: options.workspaces,
290
298
  formatter: options.formatter,
299
+ useProjectJson: !options.workspaces,
291
300
  });
292
301
  }
293
302
  else if (options.preset === presets_1.Preset.TS) {
@@ -346,6 +355,7 @@ async function createPreset(tree, options) {
346
355
  addPlugin,
347
356
  useTsSolution: options.workspaces,
348
357
  formatter: options.formatter,
358
+ useProjectJson: !options.workspaces,
349
359
  });
350
360
  }
351
361
  else {
@@ -0,0 +1,5 @@
1
+ import { type Tree } from '@nx/devkit';
2
+ export declare function isProjectIncludedInPackageManagerWorkspaces(tree: Tree, projectRoot: string): boolean;
3
+ export declare function getPackageManagerWorkspacesPatterns(tree: Tree): string[];
4
+ export declare function isUsingPackageManagerWorkspaces(tree: Tree): boolean;
5
+ export declare function isWorkspacesEnabled(tree: Tree): boolean;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isProjectIncludedInPackageManagerWorkspaces = isProjectIncludedInPackageManagerWorkspaces;
4
+ exports.getPackageManagerWorkspacesPatterns = getPackageManagerWorkspacesPatterns;
5
+ exports.isUsingPackageManagerWorkspaces = isUsingPackageManagerWorkspaces;
6
+ exports.isWorkspacesEnabled = isWorkspacesEnabled;
7
+ const devkit_1 = require("@nx/devkit");
8
+ const posix_1 = require("node:path/posix");
9
+ const package_json_1 = require("nx/src/plugins/package-json");
10
+ const picomatch = require("picomatch");
11
+ function isProjectIncludedInPackageManagerWorkspaces(tree, projectRoot) {
12
+ if (!isUsingPackageManagerWorkspaces(tree)) {
13
+ return false;
14
+ }
15
+ const patterns = getPackageManagerWorkspacesPatterns(tree);
16
+ return patterns.some((p) => picomatch(p)((0, posix_1.join)(projectRoot, 'package.json')));
17
+ }
18
+ function getPackageManagerWorkspacesPatterns(tree) {
19
+ return (0, package_json_1.getGlobPatternsFromPackageManagerWorkspaces)(tree.root, (path) => (0, devkit_1.readJson)(tree, path, { expectComments: true }), (path) => {
20
+ const content = tree.read(path, 'utf-8');
21
+ const { load } = require('@zkochan/js-yaml');
22
+ return load(content, { filename: path });
23
+ }, (path) => tree.exists(path));
24
+ }
25
+ function isUsingPackageManagerWorkspaces(tree) {
26
+ return isWorkspacesEnabled(tree);
27
+ }
28
+ function isWorkspacesEnabled(tree) {
29
+ const packageManager = (0, devkit_1.detectPackageManager)(tree.root);
30
+ if (packageManager === 'pnpm') {
31
+ return tree.exists('pnpm-workspace.yaml');
32
+ }
33
+ // yarn and npm both use the same 'workspaces' property in package.json
34
+ if (tree.exists('package.json')) {
35
+ const packageJson = (0, devkit_1.readJson)(tree, 'package.json');
36
+ return !!packageJson?.workspaces;
37
+ }
38
+ return false;
39
+ }
@@ -1,2 +1,3 @@
1
1
  import { type Tree } from '@nx/devkit';
2
2
  export declare function isUsingTsSolutionSetup(tree?: Tree): boolean;
3
+ export declare function addProjectToTsSolutionWorkspace(tree: Tree, projectDir: string): Promise<void>;
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isUsingTsSolutionSetup = isUsingTsSolutionSetup;
4
+ exports.addProjectToTsSolutionWorkspace = addProjectToTsSolutionWorkspace;
4
5
  const devkit_1 = require("@nx/devkit");
6
+ const posix_1 = require("node:path/posix");
5
7
  const tree_1 = require("nx/src/generators/tree");
8
+ const package_manager_workspaces_1 = require("../package-manager-workspaces");
6
9
  function isUsingPackageManagerWorkspaces(tree) {
7
10
  return isWorkspacesEnabled(tree);
8
11
  }
@@ -55,3 +58,48 @@ function isUsingTsSolutionSetup(tree) {
55
58
  return (isUsingPackageManagerWorkspaces(tree) &&
56
59
  isWorkspaceSetupWithTsSolution(tree));
57
60
  }
61
+ async function addProjectToTsSolutionWorkspace(tree, projectDir) {
62
+ const isIncluded = (0, package_manager_workspaces_1.isProjectIncludedInPackageManagerWorkspaces)(tree, projectDir);
63
+ if (isIncluded) {
64
+ return;
65
+ }
66
+ // If dir is "libs/foo", we try to use "libs/*" but we only do it if it's
67
+ // safe to do so. So, we first check if adding that pattern doesn't result
68
+ // in extra projects being matched. If extra projects are matched, or the
69
+ // dir is just "foo" then we add it as is.
70
+ const baseDir = (0, posix_1.dirname)(projectDir);
71
+ let pattern = projectDir;
72
+ if (baseDir !== '.') {
73
+ const patterns = (0, package_manager_workspaces_1.getPackageManagerWorkspacesPatterns)(tree);
74
+ const projectsBefore = await (0, devkit_1.globAsync)(tree, patterns);
75
+ patterns.push(`${baseDir}/*/package.json`);
76
+ const projectsAfter = await (0, devkit_1.globAsync)(tree, patterns);
77
+ if (projectsBefore.length + 1 === projectsAfter.length) {
78
+ // Adding the pattern to the parent directory only results in one extra
79
+ // project being matched, which is the project we're adding. It's safe
80
+ // to add the pattern to the parent directory.
81
+ pattern = `${baseDir}/*`;
82
+ }
83
+ }
84
+ if (tree.exists('pnpm-workspace.yaml')) {
85
+ const { load, dump } = require('@zkochan/js-yaml');
86
+ const workspaceFile = tree.read('pnpm-workspace.yaml', 'utf-8');
87
+ const yamlData = load(workspaceFile) ?? {};
88
+ yamlData.packages ??= [];
89
+ if (!yamlData.packages.includes(pattern)) {
90
+ yamlData.packages.push(pattern);
91
+ tree.write('pnpm-workspace.yaml', dump(yamlData, { indent: 2, quotingType: '"', forceQuotes: true }));
92
+ }
93
+ }
94
+ else {
95
+ // Update package.json
96
+ const packageJson = (0, devkit_1.readJson)(tree, 'package.json');
97
+ if (!packageJson.workspaces) {
98
+ packageJson.workspaces = [];
99
+ }
100
+ if (!packageJson.workspaces.includes(pattern)) {
101
+ packageJson.workspaces.push(pattern);
102
+ tree.write('package.json', JSON.stringify(packageJson, null, 2));
103
+ }
104
+ }
105
+ }