/
migrate.ts
680 lines (628 loc) · 19.9 KB
/
migrate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import { logging, normalize, virtualFs } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { BaseWorkflow } from '@angular-devkit/schematics/src/workflow';
import { NodeModulesEngineHost } from '@angular-devkit/schematics/tools';
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
import { dirname, extname, join, resolve } from 'path';
import { gt, lte } from 'semver';
import * as stripJsonComments from 'strip-json-comments';
import { dirSync } from 'tmp';
import { getLogger } from '../shared/logger';
import { convertToCamelCase, handleErrors } from '../shared/params';
import minimist = require('minimist');
import { NodePackageName } from '@angular-devkit/schematics/tasks/node-package/options';
import { TaskExecutor } from '@angular-devkit/schematics';
import { BuiltinTaskExecutor } from '@angular-devkit/schematics/tasks/node';
export type MigrationsJson = {
version: string;
schematics?: { [name: string]: { version: string } };
packageJsonUpdates?: {
[name: string]: {
version: string;
packages: {
[p: string]: {
version: string;
ifPackageInstalled?: string;
alwaysAddToPackageJson?: boolean;
};
};
};
};
};
export class Migrator {
private readonly versions: (p: string) => string;
private readonly fetch: (p: string, v: string) => Promise<MigrationsJson>;
private readonly from: { [p: string]: string };
private readonly to: { [p: string]: string };
constructor(opts: {
versions: (p: string) => string;
fetch: (p: string, v: string) => Promise<MigrationsJson>;
from: { [p: string]: string };
to: { [p: string]: string };
}) {
this.versions = opts.versions;
this.fetch = opts.fetch;
this.from = opts.from;
this.to = opts.to;
}
async updatePackageJson(targetPackage: string, targetVersion: string) {
const packageJson = await this._updatePackageJson(
targetPackage,
{ version: targetVersion, alwaysAddToPackageJson: false },
{}
);
const migrations = await this._createMigrateJson(packageJson);
return { packageJson, migrations };
}
private async _createMigrateJson(versions: {
[k: string]: { version: string; alwaysAddToPackageJson: boolean };
}) {
const migrations = await Promise.all(
Object.keys(versions).map(async c => {
const currentVersion = this.versions(c);
if (currentVersion === null) return [];
const target = versions[c];
const migrationsJson = await this.fetch(c, target.version);
if (!migrationsJson.schematics) return [];
return Object.keys(migrationsJson.schematics)
.filter(
r =>
this.gt(migrationsJson.schematics[r].version, currentVersion) &
this.lte(migrationsJson.schematics[r].version, target.version)
)
.map(r => ({
...migrationsJson.schematics[r],
package: c,
name: r
}));
})
);
return migrations.reduce((m, c) => [...m, ...c], []);
}
private async _updatePackageJson(
targetPackage: string,
target: { version: string; alwaysAddToPackageJson: boolean },
collectedVersions: {
[k: string]: { version: string; alwaysAddToPackageJson: boolean };
}
) {
let targetVersion = target.version;
if (this.to[targetPackage]) {
targetVersion = this.to[targetPackage];
}
if (!this.versions(targetPackage)) {
return {
[targetPackage]: {
version: target.version,
alwaysAddToPackageJson: !!target.alwaysAddToPackageJson
}
};
}
let migrationsJson;
try {
migrationsJson = await this.fetch(targetPackage, targetVersion);
targetVersion = migrationsJson.version;
} catch (e) {
if (e.message.indexOf('No matching version') > -1) {
throw new Error(
`${e.message}\nRun migrate with --to="package1@version1,package2@version2"`
);
} else {
throw e;
}
}
const packages = this.collapsePackages(
targetPackage,
targetVersion,
migrationsJson
);
const childCalls = await Promise.all(
Object.keys(packages)
.filter(r => {
return (
!collectedVersions[r] ||
this.gt(packages[r].version, collectedVersions[r].version)
);
})
.map(u =>
this._updatePackageJson(u, packages[u], {
...collectedVersions,
[targetPackage]: target
})
)
);
return childCalls.reduce(
(m, c) => {
Object.keys(c).forEach(r => {
if (!m[r] || this.gt(c[r].version, m[r].version)) {
m[r] = c[r];
}
});
return m;
},
{
[targetPackage]: {
version: migrationsJson.version,
alwaysAddToPackageJson: target.alwaysAddToPackageJson || false
}
}
);
}
private collapsePackages(
packageName: string,
targetVersion: string,
m: MigrationsJson | null
) {
// this should be used to know what version to include
// we should use from everywhere we use versions
if (packageName === '@nrwl/workspace') {
if (!m.packageJsonUpdates) m.packageJsonUpdates = {};
m.packageJsonUpdates[targetVersion + '-defaultPackages'] = {
version: targetVersion,
packages: [
'@nrwl/angular',
'@nrwl/cypress',
'@nrwl/eslint-plugin-nx',
'@nrwl/express',
'@nrwl/jest',
'@nrwl/linter',
'@nrwl/nest',
'@nrwl/next',
'@nrwl/node',
'@nrwl/react',
'@nrwl/storybook',
'@nrwl/tao',
'@nrwl/web'
].reduce(
(m, c) => ({
...m,
[c]: { version: targetVersion, alwaysAddToPackageJson: false }
}),
{}
)
};
}
if (!m.packageJsonUpdates || !this.versions(packageName)) return {};
return Object.keys(m.packageJsonUpdates)
.filter(r => {
return (
this.gt(
m.packageJsonUpdates[r].version,
this.versions(packageName)
) && this.lte(m.packageJsonUpdates[r].version, targetVersion)
);
})
.map(r => m.packageJsonUpdates[r].packages)
.map(packages => {
if (!packages) return {};
return Object.keys(packages)
.filter(
p =>
!packages[p].ifPackageInstalled ||
this.versions(packages[p].ifPackageInstalled)
)
.reduce(
(m, c) => ({
...m,
[c]: {
version: packages[c].version,
alwaysAddToPackageJson: packages[c].alwaysAddToPackageJson
}
}),
{}
);
})
.reduce((m, c) => ({ ...m, ...c }), {});
}
private gt(v1: string, v2: string) {
return gt(normalizeVersion(v1), normalizeVersion(v2));
}
private lte(v1: string, v2: string) {
return lte(normalizeVersion(v1), normalizeVersion(v2));
}
}
function normalizeVersionWithTagCheck(version: string) {
if (version === 'latest' || version === 'next') return version;
return normalizeVersion(version);
}
export function normalizeVersion(version: string) {
const [v, t] = version.split('-');
const [major, minor, patch] = v.split('.');
const newV = `${major || 0}.${minor || 0}.${patch || 0}`;
const newVersion = t ? `${newV}-${t}` : newV;
try {
gt(newVersion, '0.0.0');
return newVersion;
} catch (e) {
try {
gt(newV, '0.0.0');
return newV;
} catch (e) {
const withoutPatch = `${major || 0}.${minor || 0}.0`;
try {
if (gt(withoutPatch, '0.0.0')) {
return withoutPatch;
}
} catch (e) {
const withoutPatchAndMinor = `${major || 0}.0.0`;
try {
if (gt(withoutPatchAndMinor, '0.0.0')) {
return withoutPatchAndMinor;
}
} catch (e) {
return '0.0.0';
}
}
}
}
}
type GenerateMigrations = {
type: 'generateMigrations';
targetPackage: string;
targetVersion: string;
from: { [k: string]: string };
to: { [k: string]: string };
};
type RunMigrations = { type: 'runMigrations'; runMigrations: string };
export function parseMigrationsOptions(
args: string[]
): GenerateMigrations | RunMigrations {
const options = convertToCamelCase(
minimist(args, {
string: ['runMigrations', 'from', 'to'],
alias: {
runMigrations: 'run-migrations'
}
})
);
if (!options.runMigrations) {
const from = options.from ? versionOverrides(options.from, 'from') : {};
const to = options.to ? versionOverrides(options.to, 'to') : {};
const { targetPackage, targetVersion } = parseTargetPackageAndVersion(
args[0]
);
return {
type: 'generateMigrations',
targetPackage,
targetVersion,
from,
to
};
} else {
return { type: 'runMigrations', runMigrations: options.runMigrations };
}
}
function parseTargetPackageAndVersion(args: string) {
if (!args) {
throw new Error(
`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`
);
}
if (args.indexOf('@') > -1) {
const i = args.lastIndexOf('@');
if (i === 0) {
const targetPackage = args.trim();
const targetVersion = 'latest';
return { targetPackage, targetVersion };
} else {
const targetPackage = args.substring(0, i);
const maybeVersion = args.substring(i + 1);
if (!targetPackage || !maybeVersion) {
throw new Error(
`Provide the correct package name and version. E.g., @nrwl/workspace@9.0.0.`
);
}
const targetVersion = normalizeVersionWithTagCheck(maybeVersion);
return { targetPackage, targetVersion };
}
} else {
if (args.match(/[0-9]/) || args === 'latest' || args === 'next') {
return {
targetPackage: '@nrwl/workspace',
targetVersion: normalizeVersionWithTagCheck(args)
};
} else {
return {
targetPackage: args,
targetVersion: 'latest'
};
}
}
}
function versionOverrides(overrides: string, param: string) {
const res = {};
overrides.split(',').forEach(p => {
const split = p.lastIndexOf('@');
if (split === -1 || split === 0) {
throw new Error(
`Incorrect '${param}' section. Use --${param}="package@version"`
);
}
const selectedPackage = p.substring(0, split).trim();
const selectedVersion = p.substring(split + 1).trim();
if (!selectedPackage || !selectedVersion) {
throw new Error(
`Incorrect '${param}' section. Use --${param}="package@version"`
);
}
res[selectedPackage] = normalizeVersionWithTagCheck(selectedVersion);
});
return res;
}
function versions(root: string, from: { [p: string]: string }) {
return (packageName: string) => {
try {
if (from[packageName]) {
return from[packageName];
}
const content = readFileSync(
join(root, `./node_modules/${packageName}/package.json`)
);
return JSON.parse(stripJsonComments(content.toString()))['version'];
} catch (e) {
return null;
}
};
}
// testing-fetch-start
function createFetcher(logger: logging.Logger) {
let cache = {};
return async function f(
packageName: string,
packageVersion: string
): Promise<MigrationsJson> {
if (!cache[`${packageName}-${packageVersion}`]) {
const dir = dirSync().name;
logger.info(`Fetching ${packageName}@${packageVersion}`);
execSync(`npm install ${packageName}@${packageVersion} --prefix=${dir}`, {
stdio: []
});
const json = JSON.parse(
stripJsonComments(
readFileSync(
join(dir, 'node_modules', packageName, 'package.json')
).toString()
)
);
let migrationsFile = json['nx-migrations'] || json['ng-update'];
// migrationsFile is an object
if (migrationsFile && migrationsFile.migrations) {
migrationsFile = migrationsFile.migrations;
}
// packageVersion can be a tag, resolvedVersion works with semver
const resolvedVersion = json.version;
try {
if (migrationsFile && typeof migrationsFile === 'string') {
const json = JSON.parse(
stripJsonComments(
readFileSync(
join(dir, 'node_modules', packageName, migrationsFile)
).toString()
)
);
cache[`${packageName}-${packageVersion}`] = {
version: resolvedVersion,
schematics: json.schematics,
packageJsonUpdates: json.packageJsonUpdates
};
} else {
cache[`${packageName}-${packageVersion}`] = {
version: resolvedVersion
};
}
} catch (e) {
logger.warn(
`Could not find '${migrationsFile}' in '${packageName}'. Skipping it`
);
cache[`${packageName}-${packageVersion}`] = {
version: resolvedVersion
};
}
}
return cache[`${packageName}-${packageVersion}`];
};
}
// testing-fetch-end
function createMigrationsFile(root: string, migrations: any[]) {
writeFileSync(
join(root, 'migrations.json'),
JSON.stringify({ migrations }, null, 2)
);
}
function updatePackageJson(
root: string,
updatedPackages: {
[p: string]: { version: string; alwaysAddToPackageJson: boolean };
}
) {
const packageJsonPath = join(root, 'package.json');
const json = JSON.parse(
stripJsonComments(readFileSync(packageJsonPath).toString())
);
Object.keys(updatedPackages).forEach(p => {
if (json.devDependencies && json.devDependencies[p]) {
json.devDependencies[p] = updatedPackages[p].version;
} else if (json.dependencies && json.dependencies[p]) {
json.dependencies[p] = updatedPackages[p].version;
} else if (updatedPackages[p].alwaysAddToPackageJson) {
if (!json.dependencies) json.dependencies = {};
json.dependencies[p] = updatedPackages[p].version;
}
});
writeFileSync(packageJsonPath, JSON.stringify(json, null, 2));
}
async function generateMigrationsJsonAndUpdatePackageJson(
logger: logging.Logger,
root: string,
opts: {
targetPackage: string;
targetVersion: string;
from: { [p: string]: string };
to: { [p: string]: string };
}
) {
try {
logger.info(`Fetching meta data about packages.`);
logger.info(`It may take a few minutes.`);
const migrator = new Migrator({
versions: versions(root, opts.from),
fetch: createFetcher(logger),
from: opts.from,
to: opts.to
});
const { migrations, packageJson } = await migrator.updatePackageJson(
opts.targetPackage,
opts.targetVersion
);
updatePackageJson(root, packageJson);
if (migrations.length > 0) {
createMigrationsFile(root, migrations);
logger.info(`The migrate command has run successfully.`);
logger.info(`- package.json has been updated`);
logger.info(`- migrations.json has been generated`);
logger.info(`Next steps:`);
logger.info(
`- Make sure package.json changes make sense and then run 'npm install' or 'yarn'`
);
logger.info(`- Run 'nx migrate --run-migrations=migrations.json'`);
} else {
logger.info(`The migrate command has run successfully.`);
logger.info(`- package.json has been updated`);
logger.info(
`- there are no migrations to run, so migrations.json has not been created.`
);
}
} catch (e) {
const startVersion = versions(root, {})('@nrwl/workspace');
logger.error(
`The migrate command failed. Try the following to migrate your workspace:`
);
logger.error(`> npm install --save-dev @nrwl/workspace@latest`);
logger.error(
`> nx migrate ${opts.targetPackage}@${opts.targetVersion} --from="@nrwl/workspace@${startVersion}"`
);
logger.error(
`This will use the newest version of the migrate functionality, which might have your issue resolved.`
);
logger.error(
`----------------------------------------------------------------------------------------------------`
);
throw e;
}
}
class MigrationEngineHost extends NodeModulesEngineHost {
private nodeInstallLogPrinted = false;
constructor(logger: logging.Logger) {
super();
// Overwrite the original CLI node package executor with a new one that does basically nothing
// since nx migrate doesn't do npm installs by itself
// (https://github.com/angular/angular-cli/blob/5df776780deadb6be5048b3ab006a5d3383650dc/packages/angular_devkit/schematics/tools/workflow/node-workflow.ts#L41)
this.registerTaskExecutor({
name: NodePackageName,
create: () =>
Promise.resolve<TaskExecutor>(() => {
return new Promise(res => {
if (!this.nodeInstallLogPrinted) {
logger.warn(
`An installation of node_modules has been required. Make sure to run it after the migration`
);
this.nodeInstallLogPrinted = true;
}
res();
});
})
});
this.registerTaskExecutor(BuiltinTaskExecutor.RunSchematic);
}
protected _resolveCollectionPath(name: string): string {
let collectionPath: string | undefined = undefined;
if (name.startsWith('.') || name.startsWith('/')) {
name = resolve(name);
}
if (extname(name)) {
collectionPath = require.resolve(name);
} else {
const packageJsonPath = require.resolve(join(name, 'package.json'));
const packageJson = require(packageJsonPath);
let pkgJsonSchematics = packageJson['nx-migrations'];
if (!pkgJsonSchematics) {
pkgJsonSchematics = packageJson['ng-update'];
if (!pkgJsonSchematics) {
throw new Error(`Could find migrations in package: "${name}"`);
}
}
if (typeof pkgJsonSchematics != 'string') {
pkgJsonSchematics = pkgJsonSchematics.migrations;
}
collectionPath = resolve(dirname(packageJsonPath), pkgJsonSchematics);
}
try {
if (collectionPath) {
JSON.parse(stripJsonComments(readFileSync(collectionPath).toString()));
return collectionPath;
}
} catch (e) {
throw new Error(`Invalid migration file in package: "${name}"`);
}
throw new Error(`Collection cannot be resolved: "${name}"`);
}
}
class MigrationsWorkflow extends BaseWorkflow {
constructor(host: virtualFs.Host, logger: logging.Logger) {
super({
host,
engineHost: new MigrationEngineHost(logger),
force: true,
dryRun: false
});
}
}
async function runMigrations(
logger: logging.Logger,
root: string,
opts: { runMigrations: string }
) {
const migrationsFile = JSON.parse(
stripJsonComments(readFileSync(join(root, opts.runMigrations)).toString())
);
const host = new virtualFs.ScopedHost(new NodeJsSyncHost(), normalize(root));
const workflow = new MigrationsWorkflow(host, logger);
let p = Promise.resolve(null);
migrationsFile.migrations.forEach(m => {
p = p.then(() => {
logger.info(`Running migration ${m.package}:${m.name}`);
return workflow
.execute({
collection: m.package,
schematic: m.name,
options: {},
debug: false,
logger
})
.toPromise()
.then(() => {
logger.info(`Successfully finished ${m.package}:${m.name}`);
logger.info(
`---------------------------------------------------------`
);
});
});
});
await p;
}
export async function migrate(
root: string,
args: string[],
isVerbose: boolean = false
) {
const logger = getLogger(isVerbose);
return handleErrors(logger, isVerbose, async () => {
const opts = parseMigrationsOptions(args);
if (opts.type === 'generateMigrations') {
await generateMigrationsJsonAndUpdatePackageJson(logger, root, opts);
} else {
await runMigrations(logger, root, opts);
}
});
}