Skip to content

Commit ecfa266

Browse files
committed
fix(@schematics/update): respect semver rules for migration from & to
This implements the logic provided in the following link with the exception that the lower range is exclusive instead of inclusive. https://github.com/npm/node-semver#hyphen-ranges-xyz---abc Fixes angular#14559
1 parent 3afdab2 commit ecfa266

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

packages/schematics/update/migrate/index.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ export default function(options: PostUpdateSchema): Rule {
5151
return (tree: Tree, context: SchematicContext) => {
5252
const schematicsToRun: { name: string; version: string; }[] = [];
5353

54+
const from = _coerceVersionNumber(options.from);
55+
if (!from) {
56+
throw new SchematicsException(
57+
`Invalid from option: ${JSON.stringify(options.from)}`,
58+
);
59+
}
60+
61+
const to = semver.validRange('<=' + options.to);
62+
if (!to) {
63+
throw new SchematicsException(
64+
`Invalid to option: ${JSON.stringify(options.to)}`,
65+
);
66+
}
67+
5468
// Create the collection for the package.
5569
const collection = context.engine.createCollection(options.collection);
5670
for (const name of collection.listSchematicNames()) {
@@ -68,7 +82,8 @@ export default function(options: PostUpdateSchema): Rule {
6882
);
6983
}
7084

71-
if (semver.gt(version, options.from) && semver.lte(version, options.to)) {
85+
if (semver.gt(version, from) &&
86+
semver.satisfies(version, to, { includePrerelease: true })) {
7287
schematicsToRun.push({ name, version });
7388
}
7489
}

packages/schematics/update/migrate/index_spec.ts

+69
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,75 @@ describe('@schematics/update:migrate', () => {
5757
}),
5858
).toPromise().then(done, done.fail);
5959
});
60+
61+
it('supports partial version ranges with only major', done => {
62+
// Since we cannot run tasks in unit tests, we need to validate that the default
63+
// update schematic updates the package.json appropriately, AND validate that the
64+
// migrate schematic actually do work appropriately, in a separate test.
65+
schematicRunner.runSchematicAsync('migrate', {
66+
package: 'test',
67+
collection: require.resolve('./test/migration.json'),
68+
from: '1',
69+
to: '2',
70+
}, appTree).pipe(
71+
map(tree => {
72+
const resultJson = JSON.parse(tree.readContent('/migrations'));
73+
74+
expect(resultJson).toEqual([
75+
'migration-03', // "1.0.5"
76+
'migration-05', // "1.1.0-beta.0"
77+
'migration-04', // "1.1.0-beta.1"
78+
'migration-02', // "1.1.0"
79+
'migration-13', // "1.1.0"
80+
'migration-19', // "1.1"
81+
'migration-06', // "1.4.0"
82+
'migration-17', // "2.0.0-alpha"
83+
'migration-16', // "2.0.0-alpha.5"
84+
'migration-08', // "2.0.0-beta.0"
85+
'migration-07', // "2.0.0-rc.0"
86+
'migration-12', // "2.0.0-rc.4"
87+
'migration-14', // "2.0.0"
88+
'migration-20', // "2"
89+
'migration-15', // "2.0.1"
90+
'migration-11', // "2.1.0"
91+
]);
92+
}),
93+
).toPromise().then(done, done.fail);
94+
});
95+
96+
it('supports partial version ranges with major and minor', done => {
97+
// Since we cannot run tasks in unit tests, we need to validate that the default
98+
// update schematic updates the package.json appropriately, AND validate that the
99+
// migrate schematic actually do work appropriately, in a separate test.
100+
schematicRunner.runSchematicAsync('migrate', {
101+
package: 'test',
102+
collection: require.resolve('./test/migration.json'),
103+
from: '1.0',
104+
to: '2.0',
105+
}, appTree).pipe(
106+
map(tree => {
107+
const resultJson = JSON.parse(tree.readContent('/migrations'));
108+
109+
expect(resultJson).toEqual([
110+
'migration-03', // "1.0.5"
111+
'migration-05', // "1.1.0-beta.0"
112+
'migration-04', // "1.1.0-beta.1"
113+
'migration-02', // "1.1.0"
114+
'migration-13', // "1.1.0"
115+
'migration-19', // "1.1"
116+
'migration-06', // "1.4.0"
117+
'migration-17', // "2.0.0-alpha"
118+
'migration-16', // "2.0.0-alpha.5"
119+
'migration-08', // "2.0.0-beta.0"
120+
'migration-07', // "2.0.0-rc.0"
121+
'migration-12', // "2.0.0-rc.4"
122+
'migration-14', // "2.0.0"
123+
'migration-20', // "2"
124+
'migration-15', // "2.0.1"
125+
]);
126+
}),
127+
).toPromise().then(done, done.fail);
128+
});
60129
});
61130

62131

0 commit comments

Comments
 (0)