nx 20.2.0 → 20.2.1

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.
@@ -1 +1 @@
1
- "use strict";(self.webpackChunk=self.webpackChunk||[]).push([[532],{24258:()=>{}},s=>{var e;e=24258,s(s.s=e)}]);
1
+ "use strict";(self.webpackChunk=self.webpackChunk||[]).push([[532],{9916:()=>{}},s=>{var e;e=9916,s(s.s=e)}]);
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
@@ -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
+ }