nx 13.10.0 → 14.0.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.
Files changed (38) hide show
  1. package/README.md +1 -1
  2. package/executors.json +9 -0
  3. package/migrations.json +3 -0
  4. package/package.json +32 -3
  5. package/presets/core.json +21 -0
  6. package/presets/npm.json +20 -0
  7. package/src/command-line/generate.d.ts +1 -1
  8. package/src/command-line/generate.js +6 -5
  9. package/src/command-line/generate.js.map +1 -1
  10. package/src/command-line/migrate.d.ts +18 -38
  11. package/src/command-line/migrate.js +292 -272
  12. package/src/command-line/migrate.js.map +1 -1
  13. package/src/command-line/run.js +1 -1
  14. package/src/command-line/run.js.map +1 -1
  15. package/src/config/misc-interfaces.d.ts +58 -0
  16. package/src/config/workspaces.d.ts +1 -0
  17. package/src/config/workspaces.js +6 -1
  18. package/src/config/workspaces.js.map +1 -1
  19. package/src/core/dep-graph/main.esm.js +1 -1
  20. package/src/executors/run-script/run-script.impl.d.ts +7 -0
  21. package/src/executors/run-script/run-script.impl.js +29 -0
  22. package/src/executors/run-script/run-script.impl.js.map +1 -0
  23. package/src/executors/run-script/schema.json +16 -0
  24. package/src/utils/fileutils.d.ts +1 -1
  25. package/src/utils/fileutils.js +1 -1
  26. package/src/utils/fileutils.js.map +1 -1
  27. package/src/utils/package-json.d.ts +11 -1
  28. package/src/utils/package-json.js +1 -1
  29. package/src/utils/package-json.js.map +1 -1
  30. package/src/utils/package-manager.d.ts +14 -2
  31. package/src/utils/package-manager.js +95 -43
  32. package/src/utils/package-manager.js.map +1 -1
  33. package/src/utils/params.d.ts +5 -1
  34. package/src/utils/params.js.map +1 -1
  35. package/src/utils/print-help.d.ts +1 -0
  36. package/src/utils/print-help.js +205 -34
  37. package/src/utils/print-help.js.map +1 -1
  38. package/src/utils/versions.js +1 -1
@@ -6,87 +6,81 @@ const child_process_1 = require("child_process");
6
6
  const fs_extra_1 = require("fs-extra");
7
7
  const path_1 = require("path");
8
8
  const semver_1 = require("semver");
9
- const tmp_1 = require("tmp");
9
+ const util_1 = require("util");
10
10
  const tree_1 = require("../config/tree");
11
11
  const fileutils_1 = require("../utils/fileutils");
12
12
  const logger_1 = require("../utils/logger");
13
13
  const package_manager_1 = require("../utils/package-manager");
14
14
  const params_1 = require("../utils/params");
15
+ const execAsync = (0, util_1.promisify)(child_process_1.exec);
15
16
  function normalizeVersion(version) {
16
- const [v, t] = version.split('-');
17
- const [major, minor, patch] = v.split('.');
18
- const newV = `${major || 0}.${minor || 0}.${patch || 0}`;
19
- const newVersion = t ? `${newV}-${t}` : newV;
20
- try {
21
- (0, semver_1.gt)(newVersion, '0.0.0');
22
- return newVersion;
23
- }
24
- catch (e) {
17
+ const [semver, prereleaseTag] = version.split('-');
18
+ const [major, minor, patch] = semver.split('.');
19
+ const newSemver = `${major || 0}.${minor || 0}.${patch || 0}`;
20
+ const newVersion = prereleaseTag
21
+ ? `${newSemver}-${prereleaseTag}`
22
+ : newSemver;
23
+ const withoutPatch = `${major || 0}.${minor || 0}.0`;
24
+ const withoutPatchAndMinor = `${major || 0}.0.0`;
25
+ const variationsToCheck = [
26
+ newVersion,
27
+ newSemver,
28
+ withoutPatch,
29
+ withoutPatchAndMinor,
30
+ ];
31
+ for (const variation of variationsToCheck) {
25
32
  try {
26
- (0, semver_1.gt)(newV, '0.0.0');
27
- return newV;
28
- }
29
- catch (e) {
30
- const withoutPatch = `${major || 0}.${minor || 0}.0`;
31
- try {
32
- if ((0, semver_1.gt)(withoutPatch, '0.0.0')) {
33
- return withoutPatch;
34
- }
35
- }
36
- catch (e) {
37
- const withoutPatchAndMinor = `${major || 0}.0.0`;
38
- try {
39
- if ((0, semver_1.gt)(withoutPatchAndMinor, '0.0.0')) {
40
- return withoutPatchAndMinor;
41
- }
42
- }
43
- catch (e) {
44
- return '0.0.0';
45
- }
33
+ if ((0, semver_1.gt)(variation, '0.0.0')) {
34
+ return variation;
46
35
  }
47
36
  }
37
+ catch (_a) { }
48
38
  }
39
+ return '0.0.0';
49
40
  }
50
41
  exports.normalizeVersion = normalizeVersion;
51
- function slash(packageName) {
42
+ function normalizeSlashes(packageName) {
52
43
  return packageName.replace(/\\/g, '/');
53
44
  }
54
45
  class Migrator {
55
46
  constructor(opts) {
47
+ this.collectedVersions = {};
56
48
  this.packageJson = opts.packageJson;
57
49
  this.versions = opts.versions;
58
50
  this.fetch = opts.fetch;
59
- this.from = opts.from;
60
51
  this.to = opts.to;
61
52
  }
62
53
  updatePackageJson(targetPackage, targetVersion) {
63
54
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
64
- const packageJson = yield this._updatePackageJson(targetPackage, { version: targetVersion, addToPackageJson: false }, {});
55
+ const packageJson = yield this._updatePackageJson(targetPackage, {
56
+ version: targetVersion,
57
+ addToPackageJson: false,
58
+ });
65
59
  const migrations = yield this._createMigrateJson(packageJson);
66
60
  return { packageJson, migrations };
67
61
  });
68
62
  }
69
63
  _createMigrateJson(versions) {
70
64
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
71
- const migrations = yield Promise.all(Object.keys(versions).map((c) => tslib_1.__awaiter(this, void 0, void 0, function* () {
72
- const currentVersion = this.versions(c);
65
+ const migrations = yield Promise.all(Object.keys(versions).map((packageName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
66
+ const currentVersion = this.versions(packageName);
73
67
  if (currentVersion === null)
74
68
  return [];
75
- const target = versions[c];
76
- const migrationsJson = yield this.fetch(c, target.version);
77
- const generators = migrationsJson.generators;
69
+ const { version } = versions[packageName];
70
+ const { generators } = yield this.fetch(packageName, version);
78
71
  if (!generators)
79
72
  return [];
80
- return Object.keys(generators)
81
- .filter((r) => generators[r].version &&
82
- this.gt(generators[r].version, currentVersion) &&
83
- this.lte(generators[r].version, target.version))
84
- .map((r) => (Object.assign(Object.assign({}, migrationsJson.generators[r]), { package: c, name: r })));
73
+ return Object.entries(generators)
74
+ .filter(([, migration]) => migration.version &&
75
+ this.gt(migration.version, currentVersion) &&
76
+ this.lte(migration.version, version))
77
+ .map(([migrationName, migration]) => (Object.assign(Object.assign({}, migration), { package: packageName, name: migrationName })));
85
78
  })));
86
- return migrations.reduce((m, c) => [...m, ...c], []);
79
+ return migrations.flat();
87
80
  });
88
81
  }
89
- _updatePackageJson(targetPackage, target, collectedVersions) {
82
+ _updatePackageJson(targetPackage, target) {
83
+ var _a;
90
84
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
91
85
  let targetVersion = target.version;
92
86
  if (this.to[targetPackage]) {
@@ -104,9 +98,10 @@ class Migrator {
104
98
  try {
105
99
  migrationsJson = yield this.fetch(targetPackage, targetVersion);
106
100
  targetVersion = migrationsJson.version;
101
+ this.collectedVersions[targetPackage] = targetVersion;
107
102
  }
108
103
  catch (e) {
109
- if (e.message.indexOf('No matching version') > -1) {
104
+ if ((_a = e === null || e === void 0 ? void 0 : e.message) === null || _a === void 0 ? void 0 : _a.includes('No matching version')) {
110
105
  throw new Error(`${e.message}\nRun migrate with --to="package1@version1,package2@version2"`);
111
106
  }
112
107
  else {
@@ -114,19 +109,18 @@ class Migrator {
114
109
  }
115
110
  }
116
111
  const packages = this.collapsePackages(targetPackage, targetVersion, migrationsJson);
117
- const childCalls = yield Promise.all(Object.keys(packages)
118
- .filter((r) => {
119
- return (!collectedVersions[r] ||
120
- this.gt(packages[r].version, collectedVersions[r].version));
121
- })
122
- .map((u) => this._updatePackageJson(u, packages[u], Object.assign(Object.assign({}, collectedVersions), { [targetPackage]: target }))));
123
- return childCalls.reduce((m, c) => {
124
- Object.keys(c).forEach((r) => {
125
- if (!m[r] || this.gt(c[r].version, m[r].version)) {
126
- m[r] = c[r];
112
+ const childPackageMigrations = yield Promise.all(Object.keys(packages)
113
+ .filter((packageName) => !this.collectedVersions[packageName] ||
114
+ this.gt(packages[packageName].version, this.collectedVersions[packageName]))
115
+ .map((packageName) => this._updatePackageJson(packageName, packages[packageName])));
116
+ return childPackageMigrations.reduce((migrations, childMigrations) => {
117
+ for (const migrationName of Object.keys(childMigrations)) {
118
+ if (!migrations[migrationName] ||
119
+ this.gt(childMigrations[migrationName].version, migrations[migrationName].version)) {
120
+ migrations[migrationName] = childMigrations[migrationName];
127
121
  }
128
- });
129
- return m;
122
+ }
123
+ return migrations;
130
124
  }, {
131
125
  [targetPackage]: {
132
126
  version: migrationsJson.version,
@@ -135,66 +129,77 @@ class Migrator {
135
129
  });
136
130
  });
137
131
  }
138
- collapsePackages(packageName, targetVersion, m) {
132
+ collapsePackages(packageName, targetVersion, migration) {
139
133
  // this should be used to know what version to include
140
134
  // we should use from everywhere we use versions
141
- if (packageName === '@nrwl/workspace') {
142
- if (!m.packageJsonUpdates)
143
- m.packageJsonUpdates = {};
144
- m.packageJsonUpdates[`${targetVersion}-defaultPackages`] = {
135
+ var _a;
136
+ // Support Migrating to older versions of Nx
137
+ // Use the packageGroup of the latest version of Nx instead of the one from the target version which could be older.
138
+ if (packageName === '@nrwl/workspace' &&
139
+ (0, semver_1.lt)(targetVersion, '14.0.0-beta.0')) {
140
+ migration.packageGroup = {
141
+ '@nrwl/workspace': targetVersion,
142
+ '@nrwl/angular': targetVersion,
143
+ '@nrwl/cypress': targetVersion,
144
+ '@nrwl/devkit': targetVersion,
145
+ '@nrwl/eslint-plugin-nx': targetVersion,
146
+ '@nrwl/express': targetVersion,
147
+ '@nrwl/jest': targetVersion,
148
+ '@nrwl/linter': targetVersion,
149
+ '@nrwl/nest': targetVersion,
150
+ '@nrwl/next': targetVersion,
151
+ '@nrwl/node': targetVersion,
152
+ '@nrwl/nx-plugin': targetVersion,
153
+ '@nrwl/react': targetVersion,
154
+ '@nrwl/storybook': targetVersion,
155
+ '@nrwl/web': targetVersion,
156
+ '@nrwl/js': targetVersion,
157
+ '@nrwl/cli': targetVersion,
158
+ '@nrwl/nx-cloud': 'latest',
159
+ '@nrwl/react-native': targetVersion,
160
+ '@nrwl/detox': targetVersion,
161
+ };
162
+ }
163
+ if (migration.packageGroup) {
164
+ (_a = migration.packageJsonUpdates) !== null && _a !== void 0 ? _a : (migration.packageJsonUpdates = {});
165
+ const packageGroup = normalizePackageGroup(migration.packageGroup);
166
+ migration.packageJsonUpdates[targetVersion + '--PackageGroup'] = {
145
167
  version: targetVersion,
146
- packages: [
147
- 'nx',
148
- '@nrwl/angular',
149
- '@nrwl/cypress',
150
- '@nrwl/devkit',
151
- '@nrwl/eslint-plugin-nx',
152
- '@nrwl/express',
153
- '@nrwl/jest',
154
- '@nrwl/js',
155
- '@nrwl/cli',
156
- '@nrwl/linter',
157
- '@nrwl/nest',
158
- '@nrwl/next',
159
- '@nrwl/node',
160
- '@nrwl/nx-cloud',
161
- '@nrwl/nx-plugin',
162
- '@nrwl/react',
163
- '@nrwl/storybook',
164
- '@nrwl/web',
165
- '@nrwl/react-native',
166
- '@nrwl/detox',
167
- ].reduce((m, c) => (Object.assign(Object.assign({}, m), { [c]: {
168
- version: c === '@nrwl/nx-cloud' ? 'latest' : targetVersion,
169
- alwaysAddToPackageJson: false,
170
- } })), {}),
168
+ packages: packageGroup.reduce((acc, packageConfig) => {
169
+ const { package: pkg, version } = typeof packageConfig === 'string'
170
+ ? { package: packageConfig, version: targetVersion }
171
+ : packageConfig;
172
+ return Object.assign(Object.assign({}, acc), { [pkg]: {
173
+ version,
174
+ alwaysAddToPackageJson: false,
175
+ } });
176
+ }, {}),
171
177
  };
172
178
  }
173
- if (!m.packageJsonUpdates || !this.versions(packageName))
179
+ if (!migration.packageJsonUpdates || !this.versions(packageName))
174
180
  return {};
175
- return Object.keys(m.packageJsonUpdates)
176
- .filter((r) => {
177
- return (this.gt(m.packageJsonUpdates[r].version, this.versions(packageName)) && this.lte(m.packageJsonUpdates[r].version, targetVersion));
181
+ return Object.values(migration.packageJsonUpdates)
182
+ .filter(({ version, packages }) => {
183
+ return (packages &&
184
+ this.gt(version, this.versions(packageName)) &&
185
+ this.lte(version, targetVersion));
178
186
  })
179
- .map((r) => m.packageJsonUpdates[r].packages)
180
- .map((packages) => {
181
- if (!packages)
182
- return {};
183
- return Object.keys(packages)
184
- .filter((pkg) => {
185
- const { dependencies, devDependencies } = this.packageJson;
186
- return ((!packages[pkg].ifPackageInstalled ||
187
- this.versions(packages[pkg].ifPackageInstalled)) &&
188
- (packages[pkg].alwaysAddToPackageJson ||
189
- packages[pkg].addToPackageJson ||
190
- !!(dependencies === null || dependencies === void 0 ? void 0 : dependencies[pkg]) ||
191
- !!(devDependencies === null || devDependencies === void 0 ? void 0 : devDependencies[pkg])));
187
+ .map(({ packages }) => {
188
+ const { dependencies, devDependencies } = this.packageJson;
189
+ return Object.entries(packages)
190
+ .filter(([packageName, packageUpdate]) => {
191
+ return ((!packageUpdate.ifPackageInstalled ||
192
+ this.versions(packageUpdate.ifPackageInstalled)) &&
193
+ (packageUpdate.alwaysAddToPackageJson ||
194
+ packageUpdate.addToPackageJson ||
195
+ !!(dependencies === null || dependencies === void 0 ? void 0 : dependencies[packageName]) ||
196
+ !!(devDependencies === null || devDependencies === void 0 ? void 0 : devDependencies[packageName])));
192
197
  })
193
- .reduce((m, c) => (Object.assign(Object.assign({}, m), { [c]: {
194
- version: packages[c].version,
195
- addToPackageJson: packages[c].alwaysAddToPackageJson
198
+ .reduce((acc, [packageName, packageUpdate]) => (Object.assign(Object.assign({}, acc), { [packageName]: {
199
+ version: packageUpdate.version,
200
+ addToPackageJson: packageUpdate.alwaysAddToPackageJson
196
201
  ? 'dependencies'
197
- : packages[c].addToPackageJson || false,
202
+ : packageUpdate.addToPackageJson || false,
198
203
  } })), {});
199
204
  })
200
205
  .reduce((m, c) => (Object.assign(Object.assign({}, m), c)), {});
@@ -207,6 +212,15 @@ class Migrator {
207
212
  }
208
213
  }
209
214
  exports.Migrator = Migrator;
215
+ function normalizePackageGroup(packageGroup) {
216
+ if (!Array.isArray(packageGroup)) {
217
+ return Object.entries(packageGroup).map(([pkg, version]) => ({
218
+ package: pkg,
219
+ version,
220
+ }));
221
+ }
222
+ return packageGroup;
223
+ }
210
224
  function normalizeVersionWithTagCheck(version) {
211
225
  if (version === 'latest' || version === 'next')
212
226
  return version;
@@ -224,13 +238,14 @@ function versionOverrides(overrides, param) {
224
238
  if (!selectedPackage || !selectedVersion) {
225
239
  throw new Error(`Incorrect '${param}' section. Use --${param}="package@version"`);
226
240
  }
227
- res[slash(selectedPackage)] = normalizeVersionWithTagCheck(selectedVersion);
241
+ res[normalizeSlashes(selectedPackage)] =
242
+ normalizeVersionWithTagCheck(selectedVersion);
228
243
  });
229
244
  return res;
230
245
  }
231
246
  function parseTargetPackageAndVersion(args) {
232
247
  if (!args) {
233
- throw new Error(`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`);
248
+ throw new Error(`Provide the correct package name and version. E.g., my-package@9.0.0.`);
234
249
  }
235
250
  if (args.indexOf('@') > -1) {
236
251
  const i = args.lastIndexOf('@');
@@ -243,7 +258,7 @@ function parseTargetPackageAndVersion(args) {
243
258
  const targetPackage = args.substring(0, i);
244
259
  const maybeVersion = args.substring(i + 1);
245
260
  if (!targetPackage || !maybeVersion) {
246
- throw new Error(`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`);
261
+ throw new Error(`Provide the correct package name and version. E.g., my-package@9.0.0.`);
247
262
  }
248
263
  const targetVersion = normalizeVersionWithTagCheck(maybeVersion);
249
264
  return { targetPackage, targetVersion };
@@ -253,9 +268,14 @@ function parseTargetPackageAndVersion(args) {
253
268
  if (args.match(/^\d+(?:\.\d+)?(?:\.\d+)?$/) ||
254
269
  args === 'latest' ||
255
270
  args === 'next') {
271
+ const targetVersion = normalizeVersionWithTagCheck(args);
272
+ const targetPackage = args.match(/^\d+(?:\.\d+)?(?:\.\d+)?$/) &&
273
+ (0, semver_1.lt)(targetVersion, '14.0.0-beta.0')
274
+ ? '@nrwl/workspace'
275
+ : 'nx';
256
276
  return {
257
- targetPackage: '@nrwl/workspace',
258
- targetVersion: normalizeVersionWithTagCheck(args),
277
+ targetPackage,
278
+ targetVersion,
259
279
  };
260
280
  }
261
281
  else {
@@ -278,7 +298,7 @@ function parseMigrationsOptions(options) {
278
298
  const { targetPackage, targetVersion } = parseTargetPackageAndVersion(options['packageAndVersion']);
279
299
  return {
280
300
  type: 'generateMigrations',
281
- targetPackage: slash(targetPackage),
301
+ targetPackage: normalizeSlashes(targetPackage),
282
302
  targetVersion,
283
303
  from,
284
304
  to,
@@ -293,15 +313,19 @@ function parseMigrationsOptions(options) {
293
313
  }
294
314
  exports.parseMigrationsOptions = parseMigrationsOptions;
295
315
  function versions(root, from) {
316
+ const cache = {};
296
317
  return (packageName) => {
297
318
  try {
298
319
  if (from[packageName]) {
299
320
  return from[packageName];
300
321
  }
301
- const packageJsonPath = require.resolve(`${packageName}/package.json`, {
302
- paths: [root],
303
- });
304
- return (0, fileutils_1.readJsonFile)(packageJsonPath).version;
322
+ if (!cache[packageName]) {
323
+ const packageJsonPath = require.resolve(`${packageName}/package.json`, {
324
+ paths: [root],
325
+ });
326
+ cache[packageName] = (0, fileutils_1.readJsonFile)(packageJsonPath).version;
327
+ }
328
+ return cache[packageName];
305
329
  }
306
330
  catch (_a) {
307
331
  return null;
@@ -310,177 +334,177 @@ function versions(root, from) {
310
334
  }
311
335
  // testing-fetch-start
312
336
  function createFetcher() {
313
- const cache = {};
314
- return function f(packageName, packageVersion) {
315
- var _a;
316
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
317
- if (cache[`${packageName}-${packageVersion}`]) {
318
- return cache[`${packageName}-${packageVersion}`];
337
+ const migrationsCache = {};
338
+ const resolvedVersionCache = {};
339
+ function fetchMigrations(packageName, packageVersion, setCache) {
340
+ const cacheKey = packageName + '-' + packageVersion;
341
+ return Promise.resolve(resolvedVersionCache[cacheKey])
342
+ .then((cachedResolvedVersion) => {
343
+ if (cachedResolvedVersion) {
344
+ return cachedResolvedVersion;
319
345
  }
320
- let resolvedVersion;
321
- let migrations;
322
- try {
323
- resolvedVersion = (0, package_manager_1.resolvePackageVersionUsingRegistry)(packageName, packageVersion);
324
- if (cache[`${packageName}-${resolvedVersion}`]) {
325
- return cache[`${packageName}-${resolvedVersion}`];
326
- }
327
- logger_1.logger.info(`Fetching ${packageName}@${packageVersion}`);
328
- migrations = yield getPackageMigrations(packageName, resolvedVersion);
329
- }
330
- catch (_b) {
331
- logger_1.logger.info(`Fetching ${packageName}@${packageVersion}`);
332
- const result = yield installPackageAndGetVersionAngMigrations(packageName, packageVersion);
333
- resolvedVersion = result.resolvedVersion;
334
- migrations = result.migrations;
335
- }
336
- if (migrations) {
337
- cache[`${packageName}-${packageVersion}`] = cache[`${packageName}-${resolvedVersion}`] = {
338
- version: resolvedVersion,
339
- generators: (_a = migrations.generators) !== null && _a !== void 0 ? _a : migrations.schematics,
340
- packageJsonUpdates: migrations.packageJsonUpdates,
341
- };
346
+ resolvedVersionCache[cacheKey] = (0, package_manager_1.resolvePackageVersionUsingRegistry)(packageName, packageVersion);
347
+ return resolvedVersionCache[cacheKey];
348
+ })
349
+ .then((resolvedVersion) => {
350
+ if (resolvedVersion !== packageVersion &&
351
+ migrationsCache[`${packageName}-${resolvedVersion}`]) {
352
+ return migrationsCache[`${packageName}-${resolvedVersion}`];
342
353
  }
343
- else {
344
- cache[`${packageName}-${packageVersion}`] = cache[`${packageName}-${resolvedVersion}`] = {
345
- version: resolvedVersion,
346
- };
354
+ setCache(packageName, resolvedVersion);
355
+ return getPackageMigrationsUsingRegistry(packageName, resolvedVersion);
356
+ })
357
+ .catch(() => {
358
+ logger_1.logger.info(`Fetching ${packageName}@${packageVersion}`);
359
+ return getPackageMigrationsUsingInstall(packageName, packageVersion);
360
+ });
361
+ }
362
+ return function nxMigrateFetcher(packageName, packageVersion) {
363
+ if (migrationsCache[`${packageName}-${packageVersion}`]) {
364
+ return migrationsCache[`${packageName}-${packageVersion}`];
365
+ }
366
+ let resolvedVersion = packageVersion;
367
+ let migrations;
368
+ function setCache(packageName, packageVersion) {
369
+ migrationsCache[packageName + '-' + packageVersion] = migrations;
370
+ }
371
+ migrations = fetchMigrations(packageName, packageVersion, setCache).then((result) => {
372
+ if (result.schematics) {
373
+ result.generators = result.schematics;
374
+ delete result.schematics;
347
375
  }
348
- return cache[`${packageName}-${packageVersion}`];
376
+ resolvedVersion = result.version;
377
+ return result;
349
378
  });
379
+ setCache(packageName, packageVersion);
380
+ return migrations;
350
381
  };
351
382
  }
352
383
  // testing-fetch-end
353
- function getPackageMigrations(packageName, packageVersion) {
384
+ function getPackageMigrationsUsingRegistry(packageName, packageVersion) {
354
385
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
355
- try {
356
- // check if there are migrations in the packages by looking at the
357
- // registry directly
358
- const migrationsPath = getPackageMigrationsPathFromRegistry(packageName, packageVersion);
359
- if (!migrationsPath) {
360
- return null;
361
- }
362
- // try to obtain the migrations from the registry directly
363
- return yield getPackageMigrationsUsingRegistry(packageName, packageVersion, migrationsPath);
386
+ // check if there are migrations in the packages by looking at the
387
+ // registry directly
388
+ const migrationsConfig = yield getPackageMigrationsConfigFromRegistry(packageName, packageVersion);
389
+ if (!migrationsConfig) {
390
+ return {
391
+ version: packageVersion,
392
+ };
364
393
  }
365
- catch (_a) {
366
- // fall back to installing the package
367
- const { migrations } = yield installPackageAndGetVersionAngMigrations(packageName, packageVersion);
368
- return migrations;
394
+ if (!migrationsConfig.migrations) {
395
+ return {
396
+ version: packageVersion,
397
+ packageGroup: migrationsConfig.packageGroup,
398
+ };
369
399
  }
400
+ logger_1.logger.info(`Fetching ${packageName}@${packageVersion}`);
401
+ // try to obtain the migrations from the registry directly
402
+ return yield downloadPackageMigrationsFromRegistry(packageName, packageVersion, migrationsConfig);
370
403
  });
371
404
  }
372
- function getPackageMigrationsPathFromRegistry(packageName, packageVersion) {
373
- var _a, _b;
374
- let pm = (0, package_manager_1.detectPackageManager)();
375
- if (pm === 'yarn') {
376
- pm = 'npm';
377
- }
378
- const result = (0, child_process_1.execSync)(`${pm} view ${packageName}@${packageVersion} nx-migrations ng-update --json`, {
379
- stdio: [],
380
- })
381
- .toString()
382
- .trim();
383
- if (!result) {
384
- return null;
385
- }
386
- const json = JSON.parse(result);
387
- let migrationsFilePath = (_b = (_a = json['nx-migrations']) !== null && _a !== void 0 ? _a : json['ng-update']) !== null && _b !== void 0 ? _b : json;
388
- if (typeof json === 'object') {
389
- migrationsFilePath = migrationsFilePath.migrations;
390
- }
391
- return migrationsFilePath;
405
+ function resolveNxMigrationConfig(json) {
406
+ const parseNxMigrationsConfig = (fromJson) => {
407
+ if (!fromJson) {
408
+ return {};
409
+ }
410
+ if (typeof fromJson === 'string') {
411
+ return { migrations: fromJson, packageGroup: [] };
412
+ }
413
+ return Object.assign(Object.assign({}, (fromJson.migrations ? { migrations: fromJson.migrations } : {})), (fromJson.packageGroup ? { packageGroup: fromJson.packageGroup } : {}));
414
+ };
415
+ const config = Object.assign(Object.assign(Object.assign({}, parseNxMigrationsConfig(json['ng-update'])), parseNxMigrationsConfig(json['nx-migrations'])), parseNxMigrationsConfig(json));
416
+ return config;
392
417
  }
393
- function getPackageMigrationsUsingRegistry(packageName, packageVersion, migrationsFilePath) {
418
+ function getPackageMigrationsConfigFromRegistry(packageName, packageVersion) {
394
419
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
395
- const dir = (0, tmp_1.dirSync)().name;
396
- createNPMRC(dir);
397
- let pm = (0, package_manager_1.detectPackageManager)();
398
- if (pm === 'yarn') {
399
- pm = 'npm';
400
- }
401
- const tarballPath = (0, child_process_1.execSync)(`${pm} pack ${packageName}@${packageVersion}`, {
402
- cwd: dir,
403
- stdio: [],
404
- })
405
- .toString()
406
- .trim();
407
- let migrations = null;
408
- migrationsFilePath = (0, path_1.join)('package', migrationsFilePath);
409
- const migrationDestinationPath = (0, path_1.join)(dir, migrationsFilePath);
420
+ const result = yield (0, package_manager_1.packageRegistryView)(packageName, packageVersion, 'nx-migrations ng-update --json');
421
+ if (!result) {
422
+ return null;
423
+ }
424
+ return resolveNxMigrationConfig(JSON.parse(result));
425
+ });
426
+ }
427
+ function downloadPackageMigrationsFromRegistry(packageName, packageVersion, { migrations: migrationsFilePath, packageGroup }) {
428
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
429
+ const dir = (0, package_manager_1.createTempNpmDirectory)();
430
+ let result;
410
431
  try {
411
- yield (0, fileutils_1.extractFileFromTarball)((0, path_1.join)(dir, tarballPath), migrationsFilePath, migrationDestinationPath);
412
- migrations = (0, fileutils_1.readJsonFile)(migrationDestinationPath);
432
+ const { tarballPath } = yield (0, package_manager_1.packageRegistryPack)(dir, packageName, packageVersion);
433
+ const migrations = yield (0, fileutils_1.extractFileFromTarball)((0, path_1.join)(dir, tarballPath), (0, path_1.join)('package', migrationsFilePath), (0, path_1.join)(dir, migrationsFilePath)).then((path) => (0, fileutils_1.readJsonFile)(path));
434
+ result = Object.assign(Object.assign({}, migrations), { packageGroup, version: packageVersion });
413
435
  }
414
436
  catch (_a) {
415
437
  throw new Error(`Failed to find migrations file "${migrationsFilePath}" in package "${packageName}@${packageVersion}".`);
416
438
  }
417
- try {
418
- (0, fs_extra_1.removeSync)(dir);
419
- }
420
- catch (_b) {
421
- // It's okay if this fails, the OS will clean it up eventually
439
+ finally {
440
+ try {
441
+ yield (0, fs_extra_1.remove)(dir);
442
+ }
443
+ catch (_b) {
444
+ // It's okay if this fails, the OS will clean it up eventually
445
+ }
422
446
  }
423
- return migrations;
447
+ return result;
424
448
  });
425
449
  }
426
- function installPackageAndGetVersionAngMigrations(packageName, packageVersion) {
450
+ function getPackageMigrationsUsingInstall(packageName, packageVersion) {
427
451
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
428
- const dir = (0, tmp_1.dirSync)().name;
429
- createNPMRC(dir);
430
- const pmc = (0, package_manager_1.getPackageManagerCommand)();
431
- (0, child_process_1.execSync)(`${pmc.add} ${packageName}@${packageVersion}`, {
432
- stdio: [],
433
- cwd: dir,
434
- });
435
- const packageJsonPath = require.resolve(`${packageName}/package.json`, {
436
- paths: [dir],
437
- });
438
- const { version: resolvedVersion } = (0, fileutils_1.readJsonFile)(packageJsonPath);
439
- const migrationsFilePath = packageToMigrationsFilePath(packageName, dir);
440
- let migrations = null;
441
- if (migrationsFilePath) {
442
- migrations = (0, fileutils_1.readJsonFile)(migrationsFilePath);
443
- }
452
+ const dir = (0, package_manager_1.createTempNpmDirectory)();
453
+ let result;
444
454
  try {
445
- (0, fs_extra_1.removeSync)(dir);
455
+ const pmc = (0, package_manager_1.getPackageManagerCommand)();
456
+ yield execAsync(`${pmc.add} ${packageName}@${packageVersion}`, {
457
+ cwd: dir,
458
+ });
459
+ const { migrations: migrationsFilePath, packageGroup, packageJson, } = readPackageMigrationConfig(packageName, dir);
460
+ let migrations = undefined;
461
+ if (migrationsFilePath) {
462
+ migrations = (0, fileutils_1.readJsonFile)(migrationsFilePath);
463
+ }
464
+ result = Object.assign(Object.assign({}, migrations), { packageGroup, version: packageJson.version });
446
465
  }
447
- catch (_a) {
448
- // It's okay if this fails, the OS will clean it up eventually
466
+ finally {
467
+ try {
468
+ yield (0, fs_extra_1.remove)(dir);
469
+ }
470
+ catch (_a) {
471
+ // It's okay if this fails, the OS will clean it up eventually
472
+ }
449
473
  }
450
- return { migrations, resolvedVersion };
474
+ return result;
451
475
  });
452
476
  }
453
- function createNPMRC(dir) {
454
- // A package.json is needed for pnpm pack and for .npmrc to resolve
455
- (0, fileutils_1.writeJsonFile)(`${dir}/package.json`, {});
456
- const npmrc = (0, package_manager_1.checkForNPMRC)();
457
- if (npmrc) {
458
- // Copy npmrc if it exists, so that npm still follows it.
459
- (0, fs_extra_1.copyFileSync)(npmrc, `${dir}/.npmrc`);
460
- }
461
- }
462
- function packageToMigrationsFilePath(packageName, dir) {
477
+ function readPackageMigrationConfig(packageName, dir) {
463
478
  const packageJsonPath = require.resolve(`${packageName}/package.json`, {
464
479
  paths: [dir],
465
480
  });
466
481
  const json = (0, fileutils_1.readJsonFile)(packageJsonPath);
467
- let migrationsFile = json['nx-migrations'] || json['ng-update'];
468
- // migrationsFile is an object
469
- if (migrationsFile && migrationsFile.migrations) {
470
- migrationsFile = migrationsFile.migrations;
482
+ const migrationConfigOrFile = json['nx-migrations'] || json['ng-update'];
483
+ if (!migrationConfigOrFile) {
484
+ return { packageJson: json, migrations: null, packageGroup: [] };
471
485
  }
472
- try {
473
- if (migrationsFile && typeof migrationsFile === 'string') {
474
- return require.resolve(migrationsFile, {
475
- paths: [(0, path_1.dirname)(packageJsonPath)],
476
- });
477
- }
478
- else {
479
- return null;
486
+ const migrationsConfig = typeof migrationConfigOrFile === 'string'
487
+ ? {
488
+ migrations: migrationConfigOrFile,
489
+ packageGroup: [],
480
490
  }
491
+ : migrationConfigOrFile;
492
+ try {
493
+ const migrationFile = require.resolve(migrationsConfig.migrations, {
494
+ paths: [(0, path_1.dirname)(packageJsonPath)],
495
+ });
496
+ return {
497
+ packageJson: json,
498
+ migrations: migrationFile,
499
+ packageGroup: migrationsConfig.packageGroup,
500
+ };
481
501
  }
482
502
  catch (_a) {
483
- return null;
503
+ return {
504
+ packageJson: json,
505
+ migrations: null,
506
+ packageGroup: migrationsConfig.packageGroup,
507
+ };
484
508
  }
485
509
  }
486
510
  function createMigrationsFile(root, migrations) {
@@ -491,23 +515,19 @@ function updatePackageJson(root, updatedPackages) {
491
515
  const parseOptions = {};
492
516
  const json = (0, fileutils_1.readJsonFile)(packageJsonPath, parseOptions);
493
517
  Object.keys(updatedPackages).forEach((p) => {
494
- if (json.devDependencies && json.devDependencies[p]) {
518
+ var _a, _b, _c;
519
+ if ((_a = json.devDependencies) === null || _a === void 0 ? void 0 : _a[p]) {
495
520
  json.devDependencies[p] = updatedPackages[p].version;
521
+ return;
496
522
  }
497
- else if (json.dependencies && json.dependencies[p]) {
523
+ if ((_b = json.dependencies) === null || _b === void 0 ? void 0 : _b[p]) {
498
524
  json.dependencies[p] = updatedPackages[p].version;
525
+ return;
499
526
  }
500
- else if (updatedPackages[p].addToPackageJson) {
501
- if (updatedPackages[p].addToPackageJson === 'dependencies') {
502
- if (!json.dependencies)
503
- json.dependencies = {};
504
- json.dependencies[p] = updatedPackages[p].version;
505
- }
506
- else if (updatedPackages[p].addToPackageJson === 'devDependencies') {
507
- if (!json.devDependencies)
508
- json.devDependencies = {};
509
- json.devDependencies[p] = updatedPackages[p].version;
510
- }
527
+ const dependencyType = updatedPackages[p].addToPackageJson;
528
+ if (typeof dependencyType === 'string') {
529
+ (_c = json[dependencyType]) !== null && _c !== void 0 ? _c : (json[dependencyType] = {});
530
+ json[dependencyType][p] = updatedPackages[p].version;
511
531
  }
512
532
  });
513
533
  (0, fileutils_1.writeJsonFile)(packageJsonPath, json, {
@@ -525,7 +545,6 @@ function generateMigrationsJsonAndUpdatePackageJson(root, opts) {
525
545
  packageJson: originalPackageJson,
526
546
  versions: versions(root, opts.from),
527
547
  fetch: createFetcher(),
528
- from: opts.from,
529
548
  to: opts.to,
530
549
  });
531
550
  const { migrations, packageJson } = yield migrator.updatePackageJson(opts.targetPackage, opts.targetVersion);
@@ -544,11 +563,12 @@ function generateMigrationsJsonAndUpdatePackageJson(root, opts) {
544
563
  logger_1.logger.info(`NX Next steps:`);
545
564
  logger_1.logger.info(`- Make sure package.json changes make sense and then run '${pmc.install}'`);
546
565
  if (migrations.length > 0) {
547
- logger_1.logger.info(`- Run 'nx migrate --run-migrations'`);
566
+ logger_1.logger.info(`- Run '${pmc.run('nx', 'migrate --run-migrations')}'`);
548
567
  }
549
568
  logger_1.logger.info(`- To learn more go to https://nx.dev/using-nx/updating-nx`);
550
569
  if (showConnectToCloudMessage()) {
551
- logger_1.logger.info(`- You may run "nx connect-to-nx-cloud" to get faster builds, GitHub integration, and more. Check out https://nx.app`);
570
+ const cmd = pmc.run('nx', 'connect-to-nx-cloud');
571
+ logger_1.logger.info(`- You may run '${cmd}' to get faster builds, GitHub integration, and more. Check out https://nx.app`);
552
572
  }
553
573
  }
554
574
  catch (e) {
@@ -580,7 +600,7 @@ function runMigrations(root, opts, isVerbose) {
580
600
  }
581
601
  logger_1.logger.info(`NX Running migrations from '${opts.runMigrations}'`);
582
602
  const migrations = (0, fileutils_1.readJsonFile)((0, path_1.join)(root, opts.runMigrations)).migrations;
583
- for (let m of migrations) {
603
+ for (const m of migrations) {
584
604
  logger_1.logger.info(`Running migration ${m.name}`);
585
605
  if (m.cli === 'nx') {
586
606
  yield runNxMigration(root, m.package, m.name);
@@ -596,7 +616,7 @@ function runMigrations(root, opts, isVerbose) {
596
616
  }
597
617
  function runNxMigration(root, packageName, name) {
598
618
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
599
- const collectionPath = packageToMigrationsFilePath(packageName, root);
619
+ const collectionPath = readPackageMigrationConfig(packageName, root).migrations;
600
620
  const collection = (0, fileutils_1.readJsonFile)(collectionPath);
601
621
  const g = collection.generators || collection.schematics;
602
622
  const implRelativePath = g[name].implementation || g[name].factory;