-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
3dbd3e3
commit c9f531d
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/schematics/angular/migrations/update-12/replace-prod-flag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/schematics/angular/migrations/update-12/replace-prod-flag_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |