Skip to content

Commit c9f531d

Browse files
alan-agius4clydin
authored andcommitted
feat(@schematics/angular): add migration to replace deprecated --prod
With this change we add migration to replace the deprecated `--prod` with `--configuration production` in the scripts section of the package.json. Closes #21036
1 parent 3dbd3e3 commit c9f531d

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

packages/schematics/angular/migrations/migration-collection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
"factory": "./update-12/schematic-options",
121121
"description": "Remove invalid 'skipTests' option in '@schematics/angular:module' Angular schematic options."
122122
},
123+
"replace-deprecated-prod-flag": {
124+
"version": "12.1.0",
125+
"factory": "./update-12/replace-prod-flag",
126+
"description": "Replace the deprecated '--prod' in package.json scripts."
127+
},
123128
"production-by-default": {
124129
"version": "9999.0.0",
125130
"factory": "./update-12/production-default-config",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { Rule } from '@angular-devkit/schematics';
10+
import { JSONFile } from '../../utility/json-file';
11+
12+
export default function (): Rule {
13+
return (tree) => {
14+
const file = new JSONFile(tree, 'package.json');
15+
const scripts = file.get(['scripts']);
16+
if (!scripts || typeof scripts !== 'object') {
17+
return;
18+
}
19+
20+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
21+
const updatedScripts = Object.entries(scripts!).map(([key, value]) => [
22+
key,
23+
typeof value === 'string'
24+
? value.replace(/ --prod(?!\w)/g, ' --configuration production')
25+
: value,
26+
]);
27+
28+
file.modify(['scripts'], Object.fromEntries(updatedScripts));
29+
};
30+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { EmptyTree } from '@angular-devkit/schematics';
10+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
11+
12+
describe(`Migration to replace '--prod' flag from package.json scripts`, () => {
13+
const pkgJsonPath = '/package.json';
14+
const schematicName = 'replace-deprecated-prod-flag';
15+
16+
const schematicRunner = new SchematicTestRunner(
17+
'migrations',
18+
require.resolve('../migration-collection.json'),
19+
);
20+
21+
let tree: UnitTestTree;
22+
23+
beforeEach(async () => {
24+
tree = new UnitTestTree(new EmptyTree());
25+
});
26+
27+
it(`should replace '--prod' with '--configuration production'`, async () => {
28+
tree.create(
29+
pkgJsonPath,
30+
JSON.stringify(
31+
{
32+
scripts: {
33+
build: 'ng build --prod',
34+
test: 'ng test --prod && ng e2e --prod',
35+
},
36+
},
37+
undefined,
38+
2,
39+
),
40+
);
41+
const tree2 = await schematicRunner
42+
.runSchematicAsync(schematicName, {}, tree.branch())
43+
.toPromise();
44+
45+
const { scripts } = JSON.parse(tree2.readContent(pkgJsonPath));
46+
expect(scripts).toEqual({
47+
build: 'ng build --configuration production',
48+
test: 'ng test --configuration production && ng e2e --configuration production',
49+
});
50+
});
51+
52+
it(`should not replace flags that start with '--prod...'`, async () => {
53+
tree.create(
54+
pkgJsonPath,
55+
JSON.stringify(
56+
{
57+
scripts: {
58+
test: 'npx test --production && ng e2e --prod',
59+
},
60+
},
61+
undefined,
62+
2,
63+
),
64+
);
65+
const tree2 = await schematicRunner
66+
.runSchematicAsync(schematicName, {}, tree.branch())
67+
.toPromise();
68+
69+
const { scripts } = JSON.parse(tree2.readContent(pkgJsonPath));
70+
expect(scripts).toEqual({
71+
test: 'npx test --production && ng e2e --configuration production',
72+
});
73+
});
74+
});

0 commit comments

Comments
 (0)