Skip to content

Commit

Permalink
fix(angular): rename tsconfig.json during ng add (#4349)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Dec 18, 2020
1 parent 43f9df9 commit 9d22f74
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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": ".",
Expand Down
32 changes: 25 additions & 7 deletions packages/workspace/src/schematics/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down
47 changes: 32 additions & 15 deletions packages/workspace/src/schematics/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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(),
Expand Down Expand Up @@ -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({
Expand All @@ -483,7 +501,6 @@ function createAdditionalFiles(options: Schema): Rule {
implicitDependencies: {
'angular.json': '*',
'package.json': '*',
[tsConfigPath]: '*',
'tslint.json': '*',
'.eslintrc.json': '*',
'nx.json': '*',
Expand Down

0 comments on commit 9d22f74

Please sign in to comment.