-
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): enable typescript helpers in workspace
- Loading branch information
1 parent
6ca9eff
commit 0b4e91b
Showing
8 changed files
with
159 additions
and
1 deletion.
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
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
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
79 changes: 79 additions & 0 deletions
79
packages/schematics/angular/migrations/update-7/typescript-helpers.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,79 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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 { JsonParseMode, parseJsonAst } from '@angular-devkit/core'; | ||
import { Rule, Tree, chain } from '@angular-devkit/schematics'; | ||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; | ||
import { | ||
NodeDependencyType, | ||
addPackageJsonDependency, | ||
getPackageJsonDependency, | ||
} from '../../utility/dependencies'; | ||
import { | ||
findPropertyInAstObject, | ||
insertPropertyInAstObjectInOrder, | ||
} from '../../utility/json-utils'; | ||
import { latestVersions } from '../../utility/latest-versions'; | ||
|
||
export function typeScriptHelpersRule(): Rule { | ||
return chain([ | ||
_updateTsConfig(), | ||
(tree, context) => { | ||
const existing = getPackageJsonDependency(tree, 'tslib'); | ||
const type = existing ? existing.type : NodeDependencyType.Default; | ||
|
||
addPackageJsonDependency( | ||
tree, | ||
{ | ||
type, | ||
name: 'tslib', | ||
version: latestVersions.TsLib, | ||
overwrite: true, | ||
}, | ||
); | ||
|
||
context.addTask(new NodePackageInstallTask()); | ||
}, | ||
]); | ||
} | ||
|
||
function _updateTsConfig(): Rule { | ||
return (host: Tree) => { | ||
const tsConfigPath = '/tsconfig.json'; | ||
const buffer = host.read(tsConfigPath); | ||
if (!buffer) { | ||
return host; | ||
} | ||
|
||
const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose); | ||
if (tsCfgAst.kind !== 'object') { | ||
return host; | ||
} | ||
|
||
const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions'); | ||
if (!compilerOptions || compilerOptions.kind !== 'object') { | ||
return host; | ||
} | ||
|
||
const importHelpers = findPropertyInAstObject(compilerOptions, 'importHelpers'); | ||
if (importHelpers && importHelpers.value === true) { | ||
return host; | ||
} | ||
|
||
const recorder = host.beginUpdate(tsConfigPath); | ||
if (importHelpers) { | ||
const { start, end } = importHelpers; | ||
recorder.remove(start.offset, end.offset - start.offset); | ||
recorder.insertLeft(start.offset, 'true'); | ||
} else { | ||
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'importHelpers', true, 4); | ||
} | ||
host.commitUpdate(recorder); | ||
|
||
return host; | ||
}; | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/schematics/angular/migrations/update-7/typescript-helpers_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,70 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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'; | ||
|
||
const oldTsConfig = ` | ||
{ | ||
"compileOnSave": false, | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"outDir": "./dist/out-tsc", | ||
"sourceMap": true, | ||
"declaration": false, | ||
"module": "es2015", | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"importHelpers": false, | ||
"target": "es5", | ||
"typeRoots": [ | ||
"node_modules/@types" | ||
], | ||
"lib": [ | ||
"es2018", | ||
"dom" | ||
] | ||
} | ||
} | ||
`; | ||
|
||
describe('typeScriptHelpersRule', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
|
||
beforeEach(async () => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
tree = await schematicRunner.runExternalSchematicAsync( | ||
require.resolve('../../collection.json'), 'ng-new', | ||
{ | ||
name: 'migration-test', | ||
version: '1.2.3', | ||
directory: '.', | ||
}, | ||
tree, | ||
).toPromise(); | ||
}); | ||
|
||
it('should work as expected', async () => { | ||
const tsConfigPath = '/tsconfig.json'; | ||
tree.overwrite(tsConfigPath, oldTsConfig); | ||
const tree2 = await schematicRunner.runSchematicAsync('migration-04', {}, tree.branch()) | ||
.toPromise(); | ||
|
||
const { importHelpers } = JSON.parse(tree2.readContent(tsConfigPath)).compilerOptions; | ||
expect(importHelpers).toBe(true); | ||
|
||
const content = tree2.readContent('/package.json'); | ||
const pkg = JSON.parse(content); | ||
expect(pkg.dependencies.tslib).toBeDefined(); | ||
}); | ||
}); |
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
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
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