Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@schematics/angular): enable typescript helpers in workspace #12598

Merged
merged 1 commit into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function addDependenciesToPackageJson() {
{
type: NodeDependencyType.Dev,
name: 'tslib',
version: '^1.9.0',
version: latestVersions.TsLib,
},
{
type: NodeDependencyType.Dev,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"version": "7.0.0-rc.0",
"factory": "./update-7/index#polyfillMetadataRule",
"description": "Update an Angular CLI project to version 7."
},
"migration-04": {
"version": "7.1.0-beta.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's feature this will be out with the next betas

"factory": "./update-7/index#typeScriptHelpersRule",
"description": "Update an Angular CLI project to version 7."
}
}
}
1 change: 1 addition & 0 deletions packages/schematics/angular/migrations/update-7/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { latestVersions } from '../../utility/latest-versions';

export { polyfillMetadataRule } from './polyfill-metadata';
export { typeScriptHelpersRule } from './typescript-helpers';

export default function(): Rule {
return (tree, context) => {
Expand Down
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;
};
}
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();
});
});
1 change: 1 addition & 0 deletions packages/schematics/angular/utility/latest-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const latestVersions = {
RxJs: '~6.3.3',
ZoneJs: '~0.8.26',
TypeScript: '~3.1.1',
TsLib: '^1.9.0',
// The versions below must be manually updated when making a new devkit release.
DevkitBuildAngular: '~0.9.0-rc.1',
DevkitBuildNgPackagr: '~0.9.0-rc.1',
alan-agius4 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/angular/workspace/files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@angular/router": "<%= experimentalAngularNext ? 'next' : latestVersions.Angular %>",
"core-js": "^2.5.4",
"rxjs": "<%= latestVersions.RxJs %>",
"tslib": "<%= latestVersions.TsLib %>",
"zone.js": "<%= latestVersions.ZoneJs %>"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/angular/workspace/files/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
Expand Down