From 9d22f746e10bf232c0aea441dc54f270f5d662ab Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 18 Dec 2020 17:37:13 -0500 Subject: [PATCH] fix(angular): rename tsconfig.json during ng add (#4349) --- .../init/__snapshots__/init.spec.ts.snap | 27 ++++++++++- .../src/schematics/init/init.spec.ts | 32 ++++++++++--- .../workspace/src/schematics/init/init.ts | 47 +++++++++++++------ 3 files changed, 83 insertions(+), 23 deletions(-) diff --git a/packages/workspace/src/schematics/init/__snapshots__/init.spec.ts.snap b/packages/workspace/src/schematics/init/__snapshots__/init.spec.ts.snap index 5725897b56a62..f9b3fb3edfca8 100644 --- a/packages/workspace/src/schematics/init/__snapshots__/init.spec.ts.snap +++ b/packages/workspace/src/schematics/init/__snapshots__/init.spec.ts.snap @@ -1,5 +1,30 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`workspace move to nx layout should create nx.json 1`] = ` +Object { + "affected": Object { + "defaultBase": "master", + }, + "implicitDependencies": Object { + ".eslintrc.json": "*", + "angular.json": "*", + "nx.json": "*", + "package.json": "*", + "tsconfig.base.json": "*", + "tslint.json": "*", + }, + "npmScope": "my-app", + "projects": Object { + "myApp": Object { + "tags": Array [], + }, + "myApp-e2e": Object { + "tags": Array [], + }, + }, +} +`; + exports[`workspace move to nx layout should update tsconfig.base.json if present 1`] = ` Object { "compilerOptions": Object { @@ -10,7 +35,7 @@ Object { } `; -exports[`workspace move to nx layout should update tsconfig.json if tsconfig.base.json if present 1`] = ` +exports[`workspace move to nx layout should work if angular-cli workspace had tsconfig.base.json 1`] = ` Object { "compilerOptions": Object { "baseUrl": ".", diff --git a/packages/workspace/src/schematics/init/init.spec.ts b/packages/workspace/src/schematics/init/init.spec.ts index 50227cc899eb6..6cfaf07090964 100644 --- a/packages/workspace/src/schematics/init/init.spec.ts +++ b/packages/workspace/src/schematics/init/init.spec.ts @@ -57,7 +57,7 @@ describe('workspace', () => { '/tsconfig.spec.json', '{"extends": "../tsconfig.json", "compilerOptions": {}}' ); - appTree.create('/tsconfig.base.json', '{"compilerOptions": {}}'); + appTree.create('/tsconfig.json', '{"compilerOptions": {}}'); appTree.create('/tslint.json', '{"rules": {}}'); appTree.create('/e2e/protractor.conf.js', '// content'); appTree.create('/src/app/app.module.ts', '// content'); @@ -133,18 +133,28 @@ describe('workspace', () => { } }); - it('should update tsconfig.base.json if present', async () => { + it('should set the default collection to @nrwl/angular', async () => { const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); - expect(readJsonInTree(tree, 'tsconfig.base.json')).toMatchSnapshot(); + expect(readJsonInTree(tree, 'angular.json').cli.defaultCollection).toBe( + '@nrwl/angular' + ); }); - it('should update tsconfig.json if tsconfig.base.json if present', async () => { - appTree.rename('tsconfig.base.json', 'tsconfig.json'); + it('should create nx.json', async () => { const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); - expect(readJsonInTree(tree, 'tsconfig.json')).toMatchSnapshot(); + expect(readJsonInTree(tree, 'nx.json')).toMatchSnapshot(); }); - it('should add paths to the tsconfig.base.json if present', () => {}); + it('should work if angular-cli workspace had tsconfig.base.json', async () => { + appTree.rename('tsconfig.json', 'tsconfig.base.json'); + const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); + expect(readJsonInTree(tree, 'tsconfig.base.json')).toMatchSnapshot(); + }); + + it('should update tsconfig.base.json if present', async () => { + const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); + expect(readJsonInTree(tree, 'tsconfig.base.json')).toMatchSnapshot(); + }); it('should work without nested tsconfig files', async () => { const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); @@ -292,6 +302,14 @@ describe('workspace', () => { const prettierIgnore = tree.read('/.prettierignore').toString(); expect(prettierIgnore).toBe('# existing ignore rules'); }); + + it('should update tsconfigs', async () => { + appTree.create('/.prettierignore', '# existing ignore rules'); + const tree = await runSchematic('ng-add', { name: 'myApp' }, appTree); + + const prettierIgnore = tree.read('/.prettierignore').toString(); + expect(prettierIgnore).toBe('# existing ignore rules'); + }); }); describe('preserve angular cli layout', () => { diff --git a/packages/workspace/src/schematics/init/init.ts b/packages/workspace/src/schematics/init/init.ts index 6072f418866a3..bc62d421a6df5 100755 --- a/packages/workspace/src/schematics/init/init.ts +++ b/packages/workspace/src/schematics/init/init.ts @@ -10,7 +10,7 @@ import { noop, filter, } from '@angular-devkit/schematics'; -import { join, normalize } from '@angular-devkit/core'; +import { join, JsonObject, normalize, relative } from '@angular-devkit/core'; import { Schema } from './schema'; import { angularCliVersion, @@ -231,16 +231,34 @@ function updateAngularCLIJson(options: Schema): Rule { ); defaultProject.targets.delete('e2e'); } + + workspace.extensions.cli = (workspace.extensions.cli || {}) as JsonObject; + if (!workspace.extensions.cli.defaultCollection) { + workspace.extensions.cli.defaultCollection = '@nrwl/angular'; + } }); } function updateTsConfig(options: Schema): Rule { - return (host: Tree) => { - const tsConfigPath = getRootTsConfigPath(host); - return updateJsonInTree(tsConfigPath, (tsConfigJson) => - setUpCompilerOptions(tsConfigJson, options.npmScope, '') - ); - }; + return (host) => + chain([ + updateJsonInTree('nx.json', (json) => { + json.implicitDependencies['tsconfig.base.json'] = '*'; + return json; + }), + updateJsonInTree('tsconfig.base.json', () => + setUpCompilerOptions( + readJsonInTree(host, getRootTsConfigPath(host)), + options.npmScope, + '' + ) + ), + (host) => { + if (host.exists('tsconfig.json')) { + host.delete('tsconfig.json'); + } + }, + ]); } function updateTsConfigsJson(options: Schema) { @@ -249,29 +267,30 @@ function updateTsConfigsJson(options: Schema) { const app = workspaceJson.projects[options.name]; const e2eProject = getE2eProject(workspaceJson); const tsConfigPath = getRootTsConfigPath(host); - const offset = '../../'; + const appOffset = offsetFromRoot(app.root); return chain([ updateJsonInTree(app.architect.build.options.tsConfig, (json) => { - json.extends = `${offset}${tsConfigPath}`; + json.extends = `${appOffset}${tsConfigPath}`; json.compilerOptions = json.compilerOptions || {}; - json.compilerOptions.outDir = `${offset}dist/out-tsc`; + json.compilerOptions.outDir = `${appOffset}dist/out-tsc`; return json; }), app.architect.test ? updateJsonInTree(app.architect.test.options.tsConfig, (json) => { - json.extends = `${offset}${tsConfigPath}`; + json.extends = `${appOffset}${tsConfigPath}`; json.compilerOptions = json.compilerOptions || {}; - json.compilerOptions.outDir = `${offset}dist/out-tsc`; + json.compilerOptions.outDir = `${appOffset}dist/out-tsc`; return json; }) : noop(), app.architect.server ? updateJsonInTree(app.architect.server.options.tsConfig, (json) => { + json.extends = `${appOffset}${tsConfigPath}`; json.compilerOptions = json.compilerOptions || {}; - json.compilerOptions.outDir = `${offset}dist/out-tsc`; + json.compilerOptions.outDir = `${appOffset}dist/out-tsc`; return json; }) : noop(), @@ -472,7 +491,6 @@ function moveExistingFiles(options: Schema) { function createAdditionalFiles(options: Schema): Rule { return (host: Tree, _context: SchematicContext) => { const workspaceJson = readJsonInTree(host, 'angular.json'); - const tsConfigPath = getRootTsConfigPath(host); host.create( 'nx.json', serializeJson({ @@ -483,7 +501,6 @@ function createAdditionalFiles(options: Schema): Rule { implicitDependencies: { 'angular.json': '*', 'package.json': '*', - [tsConfigPath]: '*', 'tslint.json': '*', '.eslintrc.json': '*', 'nx.json': '*',