Skip to content

Commit

Permalink
fix(@schematics/angular): add links to generated tsconfig files
Browse files Browse the repository at this point in the history
We now add a link to generated tsconfig.json to make is easier for users to find relevant information.

(cherry picked from commit d800b25)
  • Loading branch information
alan-agius4 committed Jun 16, 2020
1 parent f5e126f commit b6b0718
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
37 changes: 22 additions & 15 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { getFileContent } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';

// tslint:disable-next-line: no-any
function readJsonFile(tree: UnitTestTree, path: string): any {
return parseJson(tree.readContent(path).toString(), JsonParseMode.Loose);
}

describe('Application Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
Expand Down Expand Up @@ -79,8 +84,7 @@ describe('Application Schematic', () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();

// tslint:disable-next-line:no-any
const { references } = parseJson(tree.readContent('/tsconfig.json').toString(), JsonParseMode.Loose) as any;
const { references } = readJsonFile(tree, '/tsconfig.json');
expect(references).toEqual([
{ path: './projects/foo/tsconfig.app.json' },
{ path: './projects/foo/tsconfig.spec.json' },
Expand Down Expand Up @@ -147,17 +151,20 @@ describe('Application Schematic', () => {
expect(content).toContain(`import { enableProdMode, ViewEncapsulation } from '@angular/core'`);
});

it('should set the right paths in the tsconfig files', async () => {
it('should set the right paths in the tsconfig.app.json', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(_extends).toBe('../../tsconfig.base.json');
});

it('should set the right paths in the tsconfig.spec.json', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
let path = '/projects/foo/tsconfig.app.json';
let content = tree.readContent(path);
expect(content).toMatch('../../tsconfig.base.json');
path = '/projects/foo/tsconfig.spec.json';
content = tree.readContent(path);
expect(content).toMatch('../../tsconfig.base.json');
const specTsConfig = JSON.parse(content);
expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']);
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json');
expect(files).toEqual(['src/test.ts', 'src/polyfills.ts']);
expect(_extends).toBe('../../tsconfig.base.json');
});

it('should set the right path and prefix in the tslint file', async () => {
Expand Down Expand Up @@ -384,9 +391,9 @@ describe('Application Schematic', () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const appTsConfig = JSON.parse(tree.readContent('/tsconfig.app.json'));
const appTsConfig = readJsonFile(tree, '/tsconfig.app.json');
expect(appTsConfig.extends).toEqual('./tsconfig.base.json');
const specTsConfig = JSON.parse(tree.readContent('/tsconfig.spec.json'));
const specTsConfig = readJsonFile(tree, '/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('./tsconfig.base.json');
expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']);
});
Expand Down Expand Up @@ -427,9 +434,9 @@ describe('Application Schematic', () => {
expect(buildOpt.polyfills).toEqual('foo/src/polyfills.ts');
expect(buildOpt.tsConfig).toEqual('foo/tsconfig.app.json');

const appTsConfig = JSON.parse(tree.readContent('/foo/tsconfig.app.json'));
const appTsConfig = readJsonFile(tree, '/foo/tsconfig.app.json');
expect(appTsConfig.extends).toEqual('../tsconfig.base.json');
const specTsConfig = JSON.parse(tree.readContent('/foo/tsconfig.spec.json'));
const specTsConfig = readJsonFile(tree, '/foo/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('../tsconfig.base.json');
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.lib.json",
"angularCompilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
17 changes: 9 additions & 8 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { latestVersions } from '../utility/latest-versions';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as GenerateLibrarySchema } from './schema';

function getJsonFileContent(tree: UnitTestTree, path: string) {
return JSON.parse(tree.readContent(path));
// tslint:disable-next-line: no-any
function getJsonFileContent(tree: UnitTestTree, path: string): any {
return parseJson(tree.readContent(path).toString(), JsonParseMode.Loose);
}

describe('Library Schematic', () => {
Expand Down Expand Up @@ -264,13 +265,13 @@ describe('Library Schematic', () => {
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
expect(pkgJson.name).toEqual(scopedName);

const tsConfigJson = JSON.parse(tree.readContent('/projects/myscope/mylib/tsconfig.spec.json'));
const tsConfigJson = getJsonFileContent(tree, '/projects/myscope/mylib/tsconfig.spec.json');
expect(tsConfigJson.extends).toEqual('../../../tsconfig.base.json');

const cfg = JSON.parse(tree.readContent('/angular.json'));
expect(cfg.projects['@myscope/mylib']).toBeDefined();

const rootTsCfg = JSON.parse(tree.readContent('/tsconfig.base.json'));
const rootTsCfg = getJsonFileContent(tree, '/tsconfig.base.json');
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib/myscope-mylib', 'dist/myscope/mylib']);

const karmaConf = getFileContent(tree, '/projects/myscope/mylib/karma.conf.js');
Expand Down Expand Up @@ -307,16 +308,16 @@ describe('Library Schematic', () => {
const workspaceTree = await schematicRunner.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' }).toPromise();
const tree = await schematicRunner.runSchematicAsync('library', defaultOptions, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const config = getJsonFileContent(tree, '/angular.json');
const project = config.projects.foo;
expect(project.root).toEqual('foo');
const buildOpt = project.architect.build.options;
expect(buildOpt.project).toEqual('foo/ng-package.json');
expect(buildOpt.tsConfig).toEqual('foo/tsconfig.lib.json');

const appTsConfig = JSON.parse(tree.readContent('/foo/tsconfig.lib.json'));
const appTsConfig = getJsonFileContent(tree, '/foo/tsconfig.lib.json');
expect(appTsConfig.extends).toEqual('../tsconfig.base.json');
const specTsConfig = JSON.parse(tree.readContent('/foo/tsconfig.spec.json'));
const specTsConfig = getJsonFileContent(tree, '/foo/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('../tsconfig.base.json');
});

Expand All @@ -333,7 +334,7 @@ describe('Library Schematic', () => {
.toPromise();

// tslint:disable-next-line:no-any
const { references } = parseJson(tree.readContent('/tsconfig.json').toString(), JsonParseMode.Loose) as any;
const { references } = getJsonFileContent(tree, '/tsconfig.json');
expect(references).toEqual([
{ path: './projects/foo/tsconfig.lib.json' },
{ path: './projects/foo/tsconfig.spec.json' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import { DirEntry, Rule, chain } from '@angular-devkit/schematics';
import { findPropertyInAstObject } from '../../utility/json-utils';
import { getWorkspace } from '../../utility/workspace';

const SOLUTIONS_TS_CONFIG_HEADER = '// This is a "Solution Style" tsconfig.json file, and is used by editors and TypeScript’s' +
'language server to improve development experience.\n' +
'// It is not intended to be used to perform a compilation.\n';
const SOLUTIONS_TS_CONFIG_HEADER = `/*
This is a "Solution Style" tsconfig.json file, and is used by editors and TypeScript’s language server to improve development experience.
It is not intended to be used to perform a compilation.
To learn more about this file see: https://angular.io/config/solution-tsconfig.
*/
`;

function* visitExtendedJsonFiles(directory: DirEntry): IterableIterator<[string, JsonAstString]> {
for (const path of directory.subfiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/

import { JsonParseMode, JsonValue, parseJson } from '@angular-devkit/core';
import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getWorkspaceTargets, updateWorkspaceTargets } from './update-workspace-config_spec';

// tslint:disable-next-line: no-any
function readJsonFile(tree: UnitTestTree, path: string): any {
return parseJson(tree.readContent(path).toString(), JsonParseMode.Loose);
}

function overrideJsonFile(tree: UnitTestTree, path: string, newContent: object) {
tree.overwrite(path, JSON.stringify(newContent, undefined, 2));
}
Expand Down Expand Up @@ -63,7 +68,7 @@ describe('Migration to version 9', () => {
it('should update apps tsConfig with stricter files inclusions', async () => {
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { exclude, files, include } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { exclude, files, include } = readJsonFile(tree2 , 'tsconfig.app.json');
expect(exclude).toBeUndefined();
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['src/**/*.d.ts']);
Expand All @@ -90,7 +95,7 @@ describe('Migration to version 9', () => {
updateWorkspaceTargets(tree2, config, 'another-app');

const tree3 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree2.branch()).toPromise();
const { exclude, files } = JSON.parse(tree3.readContent(tsCfgPath));
const { exclude, files } = readJsonFile(tree3, tsCfgPath);
expect(exclude).toBeUndefined();
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
});
Expand All @@ -104,7 +109,7 @@ describe('Migration to version 9', () => {
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);

const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { files, include } = readJsonFile(tree2, 'tsconfig.app.json');
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['foo.ts']);
});
Expand All @@ -119,7 +124,7 @@ describe('Migration to version 9', () => {
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);

const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { files, include, exclude } = readJsonFile(tree2, 'tsconfig.app.json');
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['src/**/*.d.ts']);
expect(exclude).toBeUndefined();
Expand All @@ -134,7 +139,7 @@ describe('Migration to version 9', () => {
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);

const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { files, include, exclude } = readJsonFile(tree2, 'tsconfig.app.json');
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['foo.ts', 'src/**/*.d.ts']);
expect(exclude).toBeUndefined();
Expand All @@ -143,7 +148,7 @@ describe('Migration to version 9', () => {
it(`should remove angularCompilerOptions when enableIvy is true and it's the only option`, async () => {
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { angularCompilerOptions } = readJsonFile(tree2, 'tsconfig.app.json');
expect(angularCompilerOptions).toBeUndefined();
});

Expand All @@ -158,7 +163,7 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { angularCompilerOptions } = readJsonFile(tree2, 'tsconfig.app.json');
expect(angularCompilerOptions.enableIvy).toBeUndefined();
expect(angularCompilerOptions.fullTemplateTypeCheck).toBe(true);
});
Expand All @@ -174,7 +179,7 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { angularCompilerOptions } = readJsonFile(tree2, 'tsconfig.app.json');
expect(angularCompilerOptions.enableIvy).toBe(false);
expect(angularCompilerOptions.fullTemplateTypeCheck).toBe(true);
});
Expand All @@ -190,10 +195,10 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { compilerOptions } = readJsonFile(tree2, 'tsconfig.app.json');
expect(compilerOptions.module).toBeUndefined();

const { compilerOptions: workspaceCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.json'));
const { compilerOptions: workspaceCompilerOptions } = readJsonFile(tree2, 'tsconfig.json');
expect(workspaceCompilerOptions.module).toBe('esnext');
});

Expand All @@ -209,7 +214,7 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
const { compilerOptions } = readJsonFile(tree2, 'tsconfig.app.json');
expect(compilerOptions.module).toBe('esnext');
});

Expand Down Expand Up @@ -238,7 +243,7 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.server.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.server.json'));
const { compilerOptions } = readJsonFile(tree2, 'tsconfig.server.json');
expect(compilerOptions.module).toBe('commonjs');
});

Expand All @@ -250,7 +255,7 @@ describe('Migration to version 9', () => {

overrideJsonFile(tree, 'tsconfig.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.json'));
const { compilerOptions } = readJsonFile(tree2, 'tsconfig.json');
expect(compilerOptions.module).toBe('esnext');
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./<%= tsConfigExtends %>",
"compilerOptions": {
Expand Down
10 changes: 6 additions & 4 deletions packages/schematics/angular/universal/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ describe('Universal Schematic', () => {
.toPromise();
const filePath = '/tsconfig.server.json';
expect(tree.exists(filePath)).toEqual(true);
const contents = tree.readContent(filePath);
expect(JSON.parse(contents)).toEqual({
// tslint:disable-next-line: no-any
const contents = parseJson(tree.readContent(filePath).toString(), JsonParseMode.Loose) as any;
expect(contents).toEqual({
extends: './tsconfig.app.json',
compilerOptions: {
outDir: './out-tsc/server',
Expand All @@ -112,8 +113,9 @@ describe('Universal Schematic', () => {
.toPromise();
const filePath = '/projects/bar/tsconfig.server.json';
expect(tree.exists(filePath)).toEqual(true);
const contents = tree.readContent(filePath);
expect(JSON.parse(contents)).toEqual({
// tslint:disable-next-line: no-any
const contents = parseJson(tree.readContent(filePath).toString(), JsonParseMode.Loose) as any;
expect(contents).toEqual({
extends: './tsconfig.app.json',
compilerOptions: {
outDir: '../../out-tsc/server',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "<%= relativePathToWorkspaceRoot %>/tsconfig.base.json",
"compilerOptions": {
Expand Down
6 changes: 4 additions & 2 deletions packages/schematics/angular/web-worker/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ describe('Web Worker Schematic', () => {
const path = '/projects/bar/tsconfig.worker.json';
expect(tree.exists(path)).toEqual(true);

const { compilerOptions } = JSON.parse(tree.readContent(path));
// tslint:disable-next-line: no-any
const { compilerOptions } = parseJson(tree.readContent(path).toString(), JsonParseMode.Loose) as any;
expect(compilerOptions.outDir).toBe('../../out-tsc/worker');
});

Expand Down Expand Up @@ -123,7 +124,8 @@ describe('Web Worker Schematic', () => {
const path = '/tsconfig.worker.json';
expect(tree.exists(path)).toEqual(true);

const { compilerOptions } = JSON.parse(tree.readContent(path));
// tslint:disable-next-line: no-any
const { compilerOptions } = parseJson(tree.readContent(path).toString(), JsonParseMode.Loose) as any;
expect(compilerOptions.outDir).toBe('./out-tsc/worker');
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// This is a "Solution Style" tsconfig.json file, and is used by editors and TypeScript’s language server to improve development experience.
// It is not intended to be used to perform a compilation.
/*
This is a "Solution Style" tsconfig.json file, and is used by editors and TypeScript’s language server to improve development experience.
It is not intended to be used to perform a compilation.

To learn more about this file see: https://angular.io/config/solution-tsconfig.
*/
{
"files": [],
"references": []
Expand Down

0 comments on commit b6b0718

Please sign in to comment.