Skip to content

Commit

Permalink
feat(@schematics/angular): add migration to replace deprecated --prod
Browse files Browse the repository at this point in the history
With this change we add migration to replace the deprecated `--prod` with `--configuration production` in the scripts section of the package.json.

Closes #21036
  • Loading branch information
alan-agius4 authored and clydin committed Jun 16, 2021
1 parent 3dbd3e3 commit c9f531d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"factory": "./update-12/schematic-options",
"description": "Remove invalid 'skipTests' option in '@schematics/angular:module' Angular schematic options."
},
"replace-deprecated-prod-flag": {
"version": "12.1.0",
"factory": "./update-12/replace-prod-flag",
"description": "Replace the deprecated '--prod' in package.json scripts."
},
"production-by-default": {
"version": "9999.0.0",
"factory": "./update-12/production-default-config",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { Rule } from '@angular-devkit/schematics';
import { JSONFile } from '../../utility/json-file';

export default function (): Rule {
return (tree) => {
const file = new JSONFile(tree, 'package.json');
const scripts = file.get(['scripts']);
if (!scripts || typeof scripts !== 'object') {
return;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const updatedScripts = Object.entries(scripts!).map(([key, value]) => [
key,
typeof value === 'string'
? value.replace(/ --prod(?!\w)/g, ' --configuration production')
: value,
]);

file.modify(['scripts'], Object.fromEntries(updatedScripts));
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';

describe(`Migration to replace '--prod' flag from package.json scripts`, () => {
const pkgJsonPath = '/package.json';
const schematicName = 'replace-deprecated-prod-flag';

const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);

let tree: UnitTestTree;

beforeEach(async () => {
tree = new UnitTestTree(new EmptyTree());
});

it(`should replace '--prod' with '--configuration production'`, async () => {
tree.create(
pkgJsonPath,
JSON.stringify(
{
scripts: {
build: 'ng build --prod',
test: 'ng test --prod && ng e2e --prod',
},
},
undefined,
2,
),
);
const tree2 = await schematicRunner
.runSchematicAsync(schematicName, {}, tree.branch())
.toPromise();

const { scripts } = JSON.parse(tree2.readContent(pkgJsonPath));
expect(scripts).toEqual({
build: 'ng build --configuration production',
test: 'ng test --configuration production && ng e2e --configuration production',
});
});

it(`should not replace flags that start with '--prod...'`, async () => {
tree.create(
pkgJsonPath,
JSON.stringify(
{
scripts: {
test: 'npx test --production && ng e2e --prod',
},
},
undefined,
2,
),
);
const tree2 = await schematicRunner
.runSchematicAsync(schematicName, {}, tree.branch())
.toPromise();

const { scripts } = JSON.parse(tree2.readContent(pkgJsonPath));
expect(scripts).toEqual({
test: 'npx test --production && ng e2e --configuration production',
});
});
});

0 comments on commit c9f531d

Please sign in to comment.