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

fix(@schematics/angular): older projects are not migrated to support … #14328

Merged
merged 1 commit into from
May 7, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {
JsonAstObject,
JsonParseMode,
isJsonObject,
join,
Expand All @@ -17,6 +18,7 @@ import { Rule, Tree } from '@angular-devkit/schematics';
import {
findPropertyInAstObject,
insertPropertyInAstObjectInOrder,
removePropertyInAstObject,
} from '../../utility/json-utils';

// tslint:disable-next-line:max-line-length
Expand All @@ -40,24 +42,12 @@ not IE 9-11 # For IE 9-11 support, remove 'not'.`;
export function updateES5Projects(): 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') {
const compilerOptions = getCompilerOptionsAstObject(host, tsConfigPath);
if (!compilerOptions) {
return host;
}

const recorder = host.beginUpdate(tsConfigPath);

const scriptTarget = findPropertyInAstObject(compilerOptions, 'target');
if (!scriptTarget) {
insertPropertyInAstObjectInOrder(recorder, compilerOptions, 'target', 'es2015', 4);
Expand All @@ -78,11 +68,11 @@ export function updateES5Projects(): Rule {

host.commitUpdate(recorder);

return updateBrowserlist;
return updateProjects;
};
}

function updateBrowserlist(): Rule {
function updateProjects(): Rule {
return (tree) => {
const angularConfigContent = tree.read('angular.json') || tree.read('.angular.json');

Expand Down Expand Up @@ -110,6 +100,34 @@ function updateBrowserlist(): Rule {
continue;
}

// Older projects app and spec ts configs had script and module set in them.
const tsConfigs = [];
const architect = project.architect;
if (isJsonObject(architect)
&& isJsonObject(architect.build)
&& isJsonObject(architect.build.options)
&& typeof architect.build.options.tsConfig === 'string') {
tsConfigs.push(architect.build.options.tsConfig);
}

if (isJsonObject(architect)
&& isJsonObject(architect.test)
&& isJsonObject(architect.test.options)
&& typeof architect.test.options.tsConfig === 'string') {
tsConfigs.push(architect.test.options.tsConfig);
}

for (const tsConfig of tsConfigs) {
const compilerOptions = getCompilerOptionsAstObject(tree, tsConfig);
if (!compilerOptions) {
continue;
}
const recorder = tree.beginUpdate(tsConfig);
removePropertyInAstObject(recorder, compilerOptions, 'target');
removePropertyInAstObject(recorder, compilerOptions, 'module');
tree.commitUpdate(recorder);
}

const browserslistPath = join(normalize(project.root), 'browserslist');
if (typeof project.sourceRoot === 'string') {
// Move the CLI 7 style browserlist to root if it's there.
Expand Down Expand Up @@ -143,3 +161,23 @@ function updateBrowserlist(): Rule {
return tree;
};
}

function getCompilerOptionsAstObject(host: Tree, tsConfigPath: string): JsonAstObject | undefined {
const buffer = host.read(tsConfigPath);
if (!buffer) {
return;
}

const tsCfgAst = parseJsonAst(buffer.toString(), JsonParseMode.Loose);

if (tsCfgAst.kind !== 'object') {
return;
}

const compilerOptions = findPropertyInAstObject(tsCfgAst, 'compilerOptions');
if (!compilerOptions || compilerOptions.kind !== 'object') {
return;
}

return compilerOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,26 @@ describe('Migration to version 8', () => {
expect(content).toContain('Chrome 41');
expect(content).toContain('last 2 Chrome versions');
});

it(`should remove 'target' and 'module' from non workspace tsconfig.json`, () => {
const appTsConfig = '/tsconfig.app.json';
const specsTsConfig = '/tsconfig.spec.json';
const compilerOptions = {
...oldTsConfig.compilerOptions,
target: 'es2015',
module: 'es2015',
};

tree.overwrite(appTsConfig, JSON.stringify({ compilerOptions }, null, 2));
const tree2 = schematicRunner.runSchematic('migration-07', {}, tree.branch());
const { compilerOptions: appCompilerOptions } = JSON.parse(tree2.readContent(appTsConfig));
expect(appCompilerOptions.target).toBeUndefined();
expect(appCompilerOptions.module).toBeUndefined();

const { compilerOptions: specsCompilerOptions }
= JSON.parse(tree2.readContent(specsTsConfig));
expect(specsCompilerOptions.target).toBeUndefined();
expect(specsCompilerOptions.module).toBeUndefined();
});
});
});