nx 20.2.0 → 20.2.2

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/bin/nx.js CHANGED
@@ -17,6 +17,7 @@ const perf_hooks_1 = require("perf_hooks");
17
17
  const workspace_context_1 = require("../src/utils/workspace-context");
18
18
  const client_1 = require("../src/daemon/client/client");
19
19
  const db_connection_1 = require("../src/utils/db-connection");
20
+ const exit_codes_1 = require("../src/utils/exit-codes");
20
21
  function main() {
21
22
  if (process.argv[2] !== 'report' &&
22
23
  process.argv[2] !== '--version' &&
@@ -214,11 +215,25 @@ const getLatestVersionOfNx = ((fn) => {
214
215
  let cache = null;
215
216
  return () => cache || (cache = fn());
216
217
  })(_getLatestVersionOfNx);
217
- function nxCleanup() {
218
+ function nxCleanup(signal) {
218
219
  (0, db_connection_1.removeDbConnections)();
220
+ if (signal) {
221
+ process.exit((0, exit_codes_1.signalToCode)(signal));
222
+ }
223
+ else {
224
+ process.exit();
225
+ }
219
226
  }
220
- process.on('exit', nxCleanup);
221
- process.on('SIGINT', nxCleanup);
222
- process.on('SIGTERM', nxCleanup);
223
- process.on('SIGHUP', nxCleanup);
227
+ process.on('exit', () => {
228
+ nxCleanup();
229
+ });
230
+ process.on('SIGINT', () => {
231
+ nxCleanup('SIGINT');
232
+ });
233
+ process.on('SIGTERM', () => {
234
+ nxCleanup('SIGTERM');
235
+ });
236
+ process.on('SIGHUP', () => {
237
+ nxCleanup('SIGHUP');
238
+ });
224
239
  main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nx",
3
- "version": "20.2.0",
3
+ "version": "20.2.2",
4
4
  "private": false,
5
5
  "description": "The core Nx plugin contains the core functionality of Nx like the project graph, nx commands and task orchestration.",
6
6
  "repository": {
@@ -54,18 +54,19 @@
54
54
  "jsonc-parser": "3.2.0",
55
55
  "lines-and-columns": "2.0.3",
56
56
  "minimatch": "9.0.3",
57
+ "node-machine-id": "1.1.12",
57
58
  "npm-run-path": "^4.0.1",
58
59
  "open": "^8.4.0",
60
+ "ora": "5.3.0",
59
61
  "semver": "^7.5.3",
60
62
  "string-width": "^4.2.3",
61
63
  "tar-stream": "~2.2.0",
62
64
  "tmp": "~0.2.1",
63
65
  "tsconfig-paths": "^4.1.2",
64
66
  "tslib": "^2.3.0",
67
+ "yaml": "^2.6.0",
65
68
  "yargs": "^17.6.2",
66
- "yargs-parser": "21.1.1",
67
- "node-machine-id": "1.1.12",
68
- "ora": "5.3.0"
69
+ "yargs-parser": "21.1.1"
69
70
  },
70
71
  "peerDependencies": {
71
72
  "@swc-node/register": "^1.8.0",
@@ -80,16 +81,16 @@
80
81
  }
81
82
  },
82
83
  "optionalDependencies": {
83
- "@nx/nx-darwin-x64": "20.2.0",
84
- "@nx/nx-darwin-arm64": "20.2.0",
85
- "@nx/nx-linux-x64-gnu": "20.2.0",
86
- "@nx/nx-linux-x64-musl": "20.2.0",
87
- "@nx/nx-win32-x64-msvc": "20.2.0",
88
- "@nx/nx-linux-arm64-gnu": "20.2.0",
89
- "@nx/nx-linux-arm64-musl": "20.2.0",
90
- "@nx/nx-linux-arm-gnueabihf": "20.2.0",
91
- "@nx/nx-win32-arm64-msvc": "20.2.0",
92
- "@nx/nx-freebsd-x64": "20.2.0"
84
+ "@nx/nx-darwin-arm64": "20.2.2",
85
+ "@nx/nx-darwin-x64": "20.2.2",
86
+ "@nx/nx-freebsd-x64": "20.2.2",
87
+ "@nx/nx-linux-arm-gnueabihf": "20.2.2",
88
+ "@nx/nx-linux-arm64-gnu": "20.2.2",
89
+ "@nx/nx-linux-arm64-musl": "20.2.2",
90
+ "@nx/nx-linux-x64-gnu": "20.2.2",
91
+ "@nx/nx-linux-x64-musl": "20.2.2",
92
+ "@nx/nx-win32-arm64-msvc": "20.2.2",
93
+ "@nx/nx-win32-x64-msvc": "20.2.2"
93
94
  },
94
95
  "nx-migrations": {
95
96
  "migrations": "./migrations.json",
@@ -108,6 +109,7 @@
108
109
  "@nx/expo",
109
110
  "@nx/express",
110
111
  "@nx/gradle",
112
+ "@nx/module-federation",
111
113
  "@nx/nest",
112
114
  "@nx/next",
113
115
  "@nx/node",
@@ -2,10 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.importHandler = importHandler;
4
4
  const path_1 = require("path");
5
- const minimatch_1 = require("minimatch");
6
5
  const node_fs_1 = require("node:fs");
7
6
  const chalk = require("chalk");
8
- const js_yaml_1 = require("@zkochan/js-yaml");
9
7
  const git_utils_1 = require("../../utils/git-utils");
10
8
  const promises_1 = require("node:fs/promises");
11
9
  const tmp_1 = require("tmp");
@@ -22,7 +20,7 @@ const command_line_utils_1 = require("../../utils/command-line-utils");
22
20
  const prepare_source_repo_1 = require("./utils/prepare-source-repo");
23
21
  const merge_remote_source_1 = require("./utils/merge-remote-source");
24
22
  const needs_install_1 = require("./utils/needs-install");
25
- const file_utils_1 = require("../../project-graph/file-utils");
23
+ const minimatch_1 = require("minimatch");
26
24
  const importRemoteName = '__tmp_nx_import__';
27
25
  async function importHandler(options) {
28
26
  process.env.NX_RUNNING_NX_IMPORT = 'true';
@@ -166,6 +164,7 @@ async function importHandler(options) {
166
164
  ],
167
165
  });
168
166
  }
167
+ await handleMissingWorkspacesEntry(packageManager, pmc, relativeDestination, destinationGitClient);
169
168
  // If install fails, we should continue since the errors could be resolved later.
170
169
  let installFailed = false;
171
170
  if (plugins.length > 0) {
@@ -208,7 +207,6 @@ async function importHandler(options) {
208
207
  ],
209
208
  });
210
209
  }
211
- await warnOnMissingWorkspacesEntry(packageManager, pmc, relativeDestination);
212
210
  if (source != destination) {
213
211
  output_1.output.warn({
214
212
  title: `Check configuration files`,
@@ -259,9 +257,10 @@ async function createTemporaryRemote(destinationGitClient, sourceRemoteUrl, remo
259
257
  await destinationGitClient.addGitRemote(remoteName, sourceRemoteUrl);
260
258
  await destinationGitClient.fetch(remoteName);
261
259
  }
262
- // If the user imports a project that isn't in NPM/Yarn/PNPM workspaces, then its dependencies
263
- // will not be installed. We should warn users and provide instructions on how to fix this.
264
- async function warnOnMissingWorkspacesEntry(pm, pmc, pkgPath) {
260
+ /**
261
+ * If the user imports a project that isn't in the workspaces entry, we should add that path to the workspaces entry.
262
+ */
263
+ async function handleMissingWorkspacesEntry(pm, pmc, pkgPath, destinationGitClient) {
265
264
  if (!(0, package_manager_1.isWorkspacesEnabled)(pm, workspace_root_1.workspaceRoot)) {
266
265
  output_1.output.warn({
267
266
  title: `Missing workspaces in package.json`,
@@ -292,37 +291,24 @@ async function warnOnMissingWorkspacesEntry(pm, pmc, pkgPath) {
292
291
  });
293
292
  }
294
293
  else {
295
- // Check if the new package is included in existing workspaces entries. If not, warn the user.
296
- let workspaces = null;
297
- if (pm === 'npm' || pm === 'yarn' || pm === 'bun') {
298
- const packageJson = (0, file_utils_1.readPackageJson)();
299
- workspaces = packageJson.workspaces;
300
- }
301
- else if (pm === 'pnpm') {
302
- const yamlPath = (0, path_1.join)(workspace_root_1.workspaceRoot, 'pnpm-workspace.yaml');
303
- if ((0, node_fs_1.existsSync)(yamlPath)) {
304
- const yamlContent = await node_fs_1.promises.readFile(yamlPath, 'utf-8');
305
- const yaml = (0, js_yaml_1.load)(yamlContent);
306
- workspaces = yaml.packages;
307
- }
308
- }
309
- if (workspaces) {
310
- const isPkgIncluded = workspaces.some((w) => (0, minimatch_1.minimatch)(pkgPath, w));
311
- if (!isPkgIncluded) {
312
- const pkgsDir = (0, path_1.dirname)(pkgPath);
313
- output_1.output.warn({
314
- title: `Project missing in workspaces`,
315
- bodyLines: pm === 'npm' || pm === 'yarn' || pm === 'bun'
316
- ? [
317
- `The imported project (${pkgPath}) is missing the "workspaces" field in package.json.`,
318
- `Add "${pkgsDir}/*" to workspaces run "${pmc.install}".`,
319
- ]
320
- : [
321
- `The imported project (${pkgPath}) is missing the "packages" field in pnpm-workspaces.yaml.`,
322
- `Add "${pkgsDir}/*" to packages run "${pmc.install}".`,
323
- ],
324
- });
325
- }
294
+ let workspaces = (0, package_manager_1.getPackageWorkspaces)(pm, workspace_root_1.workspaceRoot);
295
+ const isPkgIncluded = workspaces.some((w) => (0, minimatch_1.minimatch)(pkgPath, w));
296
+ if (isPkgIncluded) {
297
+ return;
326
298
  }
299
+ (0, package_manager_1.addPackagePathToWorkspaces)(pkgPath, pm, workspaces, workspace_root_1.workspaceRoot);
300
+ await destinationGitClient.amendCommit();
301
+ output_1.output.success({
302
+ title: `Project added in workspaces`,
303
+ bodyLines: pm === 'npm' || pm === 'yarn' || pm === 'bun'
304
+ ? [
305
+ `The imported project (${chalk.bold(pkgPath)}) is missing the "workspaces" field in package.json.`,
306
+ `Added "${chalk.bold(pkgPath)}" to workspaces.`,
307
+ ]
308
+ : [
309
+ `The imported project (${chalk.bold(pkgPath)}) is missing the "packages" field in pnpm-workspaces.yaml.`,
310
+ `Added "${chalk.bold(pkgPath)}" to packages.`,
311
+ ],
312
+ });
327
313
  }
328
314
  }
@@ -84,14 +84,15 @@ async function getGitDiff(from, to = 'HEAD') {
84
84
  else {
85
85
  range = `${from}..${to}`;
86
86
  }
87
- // Use a unique enough separator that we can be relatively certain will not occur within the commit message itself
88
- const separator = '§§§';
87
+ // Use unique enough separators that we can be relatively certain will not occur within the commit message itself
88
+ const commitMetadataSeparator = '§§§';
89
+ const commitsSeparator = '|@-------@|';
89
90
  // https://git-scm.com/docs/pretty-formats
90
91
  const args = [
91
92
  '--no-pager',
92
93
  'log',
93
94
  range,
94
- `--pretty="----%n%s${separator}%h${separator}%an${separator}%ae%n%b"`,
95
+ `--pretty="${commitsSeparator}%n%s${commitMetadataSeparator}%h${commitMetadataSeparator}%an${commitMetadataSeparator}%ae%n%b"`,
95
96
  '--name-status',
96
97
  ];
97
98
  // Support cases where the nx workspace root is located at a nested path within the git repo
@@ -101,11 +102,11 @@ async function getGitDiff(from, to = 'HEAD') {
101
102
  }
102
103
  const r = await (0, exec_command_1.execCommand)('git', args);
103
104
  return r
104
- .split('----\n')
105
+ .split(`${commitsSeparator}\n`)
105
106
  .splice(1)
106
107
  .map((line) => {
107
108
  const [firstLine, ..._body] = line.split('\n');
108
- const [message, shortHash, authorName, authorEmail] = firstLine.split(separator);
109
+ const [message, shortHash, authorName, authorEmail] = firstLine.split(commitMetadataSeparator);
109
110
  const r = {
110
111
  message,
111
112
  shortHash,
Binary file
@@ -155,7 +155,7 @@ function getGlobPatternsFromPackageManagerWorkspaces(root, readJson = (path) =>
155
155
  : packageJson.workspaces?.packages ?? []));
156
156
  if ((0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'pnpm-workspace.yaml'))) {
157
157
  try {
158
- const { packages } = (0, fileutils_1.readYamlFile)((0, node_path_1.join)(root, 'pnpm-workspace.yaml'));
158
+ const { packages } = (0, fileutils_1.readYamlFile)((0, node_path_1.join)(root, 'pnpm-workspace.yaml')) ?? {};
159
159
  patterns.push(...normalizePatterns(packages || []));
160
160
  }
161
161
  catch (e) {
@@ -167,7 +167,7 @@ function getGlobPatternsFromPackageManagerWorkspaces(root, readJson = (path) =>
167
167
  }
168
168
  if ((0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'lerna.json'))) {
169
169
  try {
170
- const { packages } = readJson('lerna.json');
170
+ const { packages } = readJson('lerna.json') ?? {};
171
171
  patterns.push(...normalizePatterns(packages?.length > 0 ? packages : ['packages/*']));
172
172
  }
173
173
  catch (e) {
@@ -26,5 +26,5 @@ export declare function readWorkspaceConfig(opts: {
26
26
  path?: string;
27
27
  }): ProjectsConfigurations;
28
28
  export declare function defaultFileRead(filePath: string): string | null;
29
- export declare function readPackageJson(): any;
29
+ export declare function readPackageJson(root?: string): any;
30
30
  export { FileData };
@@ -131,9 +131,9 @@ function readWorkspaceConfig(opts) {
131
131
  function defaultFileRead(filePath) {
132
132
  return (0, fs_1.readFileSync)((0, path_1.join)(workspace_root_1.workspaceRoot, filePath), 'utf-8');
133
133
  }
134
- function readPackageJson() {
134
+ function readPackageJson(root = workspace_root_1.workspaceRoot) {
135
135
  try {
136
- return (0, fileutils_1.readJsonFile)(`${workspace_root_1.workspaceRoot}/package.json`);
136
+ return (0, fileutils_1.readJsonFile)(`${root}/package.json`);
137
137
  }
138
138
  catch {
139
139
  return {}; // if package.json doesn't exist
@@ -21,6 +21,7 @@ async function getPlugins() {
21
21
  }
22
22
  // Cleanup current plugins before loading new ones
23
23
  if (cleanup) {
24
+ pendingPluginsPromise = undefined;
24
25
  cleanup();
25
26
  }
26
27
  pendingPluginsPromise ??= (0, internal_api_1.loadNxPlugins)(pluginsConfiguration, workspace_root_1.workspaceRoot);
@@ -40,6 +41,7 @@ async function getOnlyDefaultPlugins() {
40
41
  }
41
42
  // Cleanup current plugins before loading new ones
42
43
  if (cleanupDefaultPlugins) {
44
+ pendingDefaultPluginPromise = undefined;
43
45
  cleanupDefaultPlugins();
44
46
  }
45
47
  pendingDefaultPluginPromise ??= (0, internal_api_1.loadNxPlugins)([], workspace_root_1.workspaceRoot);
@@ -81,8 +81,8 @@ function readTargetsFromPackageJson(packageJson, nxJson) {
81
81
  const { scripts, nx, private: isPrivate } = packageJson ?? {};
82
82
  const res = {};
83
83
  const includedScripts = nx?.includedScripts || Object.keys(scripts ?? {});
84
- packageManagerCommand ??= (0, package_manager_1.getPackageManagerCommand)();
85
84
  for (const script of includedScripts) {
85
+ packageManagerCommand ??= (0, package_manager_1.getPackageManagerCommand)();
86
86
  res[script] = buildTargetFromScript(script, scripts, packageManagerCommand);
87
87
  }
88
88
  for (const targetName in nx?.targets) {
@@ -98,3 +98,17 @@ export declare function packageRegistryView(pkg: string, version: string, args:
98
98
  export declare function packageRegistryPack(cwd: string, pkg: string, version: string): Promise<{
99
99
  tarballPath: string;
100
100
  }>;
101
+ /**
102
+ * Gets the workspaces defined in the package manager configuration.
103
+ * @returns workspaces defined in the package manager configuration, empty array if none are defined
104
+ */
105
+ export declare function getPackageWorkspaces(packageManager?: PackageManager, root?: string): string[];
106
+ /**
107
+ * Adds a package to the workspaces defined in the package manager configuration.
108
+ * If the package is already included in the workspaces, it will not be added again.
109
+ * @param packageManager The package manager to use. If not provided, it will be detected based on the lock file.
110
+ * @param workspaces The workspaces to add the package to. Defaults to the workspaces defined in the package manager configuration.
111
+ * @param root The directory the commands will be ran inside of. Defaults to the current workspace's root.
112
+ * @param packagePath The path of the package to add to the workspaces
113
+ */
114
+ export declare function addPackagePathToWorkspaces(packagePath: string, packageManager?: PackageManager, workspaces?: string[], root?: string): void;
@@ -13,8 +13,11 @@ exports.resolvePackageVersionUsingRegistry = resolvePackageVersionUsingRegistry;
13
13
  exports.resolvePackageVersionUsingInstallation = resolvePackageVersionUsingInstallation;
14
14
  exports.packageRegistryView = packageRegistryView;
15
15
  exports.packageRegistryPack = packageRegistryPack;
16
+ exports.getPackageWorkspaces = getPackageWorkspaces;
17
+ exports.addPackagePathToWorkspaces = addPackagePathToWorkspaces;
16
18
  const child_process_1 = require("child_process");
17
19
  const fs_1 = require("fs");
20
+ const yaml_1 = require("yaml");
18
21
  const promises_1 = require("node:fs/promises");
19
22
  const path_1 = require("path");
20
23
  const semver_1 = require("semver");
@@ -50,7 +53,7 @@ function isWorkspacesEnabled(packageManager = detectPackageManager(), root = wor
50
53
  return (0, fs_1.existsSync)((0, path_1.join)(root, 'pnpm-workspace.yaml'));
51
54
  }
52
55
  // yarn and npm both use the same 'workspaces' property in package.json
53
- const packageJson = (0, file_utils_1.readPackageJson)();
56
+ const packageJson = (0, file_utils_1.readPackageJson)(root);
54
57
  return !!packageJson?.workspaces;
55
58
  }
56
59
  /**
@@ -402,3 +405,86 @@ async function packageRegistryPack(cwd, pkg, version) {
402
405
  const tarballPath = stdout.trim();
403
406
  return { tarballPath };
404
407
  }
408
+ /**
409
+ * Gets the workspaces defined in the package manager configuration.
410
+ * @returns workspaces defined in the package manager configuration, empty array if none are defined
411
+ */
412
+ function getPackageWorkspaces(packageManager = detectPackageManager(), root = workspace_root_1.workspaceRoot) {
413
+ let workspaces;
414
+ if (packageManager === 'npm' ||
415
+ packageManager === 'yarn' ||
416
+ packageManager === 'bun') {
417
+ const packageJson = (0, file_utils_1.readPackageJson)(root);
418
+ workspaces = packageJson.workspaces;
419
+ }
420
+ else if (packageManager === 'pnpm') {
421
+ const pnpmWorkspacePath = (0, path_1.join)(root, 'pnpm-workspace.yaml');
422
+ if ((0, fs_1.existsSync)(pnpmWorkspacePath)) {
423
+ const { packages } = (0, fileutils_1.readYamlFile)(pnpmWorkspacePath) ?? {};
424
+ workspaces = packages;
425
+ }
426
+ }
427
+ return workspaces ?? [];
428
+ }
429
+ /**
430
+ * Adds a package to the workspaces defined in the package manager configuration.
431
+ * If the package is already included in the workspaces, it will not be added again.
432
+ * @param packageManager The package manager to use. If not provided, it will be detected based on the lock file.
433
+ * @param workspaces The workspaces to add the package to. Defaults to the workspaces defined in the package manager configuration.
434
+ * @param root The directory the commands will be ran inside of. Defaults to the current workspace's root.
435
+ * @param packagePath The path of the package to add to the workspaces
436
+ */
437
+ function addPackagePathToWorkspaces(packagePath, packageManager = detectPackageManager(), workspaces = getPackageWorkspaces(packageManager), root = workspace_root_1.workspaceRoot) {
438
+ if (packageManager === 'npm' ||
439
+ packageManager === 'yarn' ||
440
+ packageManager === 'bun') {
441
+ workspaces.push(packagePath);
442
+ const packageJson = (0, file_utils_1.readPackageJson)(root);
443
+ const updatedPackageJson = {
444
+ ...packageJson,
445
+ workspaces,
446
+ };
447
+ const packageJsonPath = (0, path_1.join)(root, 'package.json');
448
+ (0, fileutils_1.writeJsonFile)(packageJsonPath, updatedPackageJson);
449
+ }
450
+ else if (packageManager === 'pnpm') {
451
+ const pnpmWorkspacePath = (0, path_1.join)(root, 'pnpm-workspace.yaml');
452
+ if ((0, fs_1.existsSync)(pnpmWorkspacePath)) {
453
+ const pnpmWorkspaceDocument = (0, yaml_1.parseDocument)((0, fileutils_1.readFileIfExisting)(pnpmWorkspacePath));
454
+ const pnpmWorkspaceContents = pnpmWorkspaceDocument.contents;
455
+ if (!pnpmWorkspaceContents) {
456
+ (0, fs_1.writeFileSync)(pnpmWorkspacePath, (0, yaml_1.stringify)({
457
+ packages: [packagePath],
458
+ }));
459
+ }
460
+ else if (pnpmWorkspaceContents instanceof yaml_1.YAMLMap) {
461
+ const packages = pnpmWorkspaceContents.items.find((item) => {
462
+ return item.key instanceof yaml_1.Scalar
463
+ ? item.key?.value === 'packages'
464
+ : item.key === 'packages';
465
+ });
466
+ if (packages) {
467
+ if (packages.value instanceof yaml_1.YAMLSeq === false) {
468
+ packages.value = new yaml_1.YAMLSeq();
469
+ }
470
+ packages.value.items ??= [];
471
+ packages.value.items.push(packagePath);
472
+ }
473
+ else {
474
+ // if the 'packages' key doesn't exist, create it
475
+ const packagesSeq = new yaml_1.YAMLSeq();
476
+ packagesSeq.items ??= [];
477
+ packagesSeq.items.push(packagePath);
478
+ pnpmWorkspaceDocument.add(pnpmWorkspaceDocument.createPair('packages', packagesSeq));
479
+ }
480
+ (0, fs_1.writeFileSync)(pnpmWorkspacePath, (0, yaml_1.stringify)(pnpmWorkspaceContents));
481
+ }
482
+ }
483
+ else {
484
+ // If the file doesn't exist, create it
485
+ (0, fs_1.writeFileSync)(pnpmWorkspacePath, (0, yaml_1.stringify)({
486
+ packages: [packagePath],
487
+ }));
488
+ }
489
+ }
490
+ }