Skip to content
Closed
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
51 changes: 41 additions & 10 deletions packages/localize/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {Builders} from '@schematics/angular/utility/workspace-models';
import {Schema} from './schema';

const localizeType = `@angular/localize`;
const localizeTypeInit = '@angular/localize/init';
const typesJsonPath: JSONPath = ['compilerOptions', 'types'];

function addTypeScriptConfigTypes(projectName: string): Rule {
return async (host: Tree) => {
Expand All @@ -28,7 +30,7 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
}

// We add the root workspace tsconfig for better IDE support.
const tsConfigFiles = new Set<string>(['tsconfig.json']);
const tsConfigFiles = new Set<string>();
for (const target of project.targets.values()) {
switch (target.builder) {
case Builders.Karma:
Expand All @@ -43,28 +45,38 @@ function addTypeScriptConfigTypes(projectName: string): Rule {
}
}

const typesJsonPath: JSONPath = ['compilerOptions', 'types'];
const allTypes: string[] = [];
// All builder target tsconfigs
for (const path of tsConfigFiles) {
if (!host.exists(path)) {
continue;
}

const json = new JSONFile(host, path);
const types = json.get(typesJsonPath) ?? [];
if (!Array.isArray(types)) {
throw new SchematicsException(`TypeScript configuration file '${
path}' has an invalid 'types' property. It must be an array.`);
const builderTsConfig = getJsonFileAndTypes(host, path);
if (!builderTsConfig) {
continue;
}

const hasLocalizeType =
types.some((t) => t === localizeType || t === '@angular/localize/init');
const {json, types} = builderTsConfig;
allTypes.push(...types);

const hasLocalizeType = types.some((t) => t === localizeType || t === localizeTypeInit);
if (hasLocalizeType) {
// Skip has already localize type.
continue;
}

json.modify(typesJsonPath, [...types, localizeType]);
}

// Update workspace tsconfig
const rootTsConfigFile = getJsonFileAndTypes(host, './tsconfig.json');
if (rootTsConfigFile) {
const {json, types} = rootTsConfigFile;

const allTypesForRoot =
[...allTypes, ...types, localizeType].filter(t => t !== localizeTypeInit);
json.modify(typesJsonPath, Array.from(allTypesForRoot));
}
};
}

Expand All @@ -86,6 +98,25 @@ function moveToDependencies(host: Tree, context: SchematicContext): void {
context.addTask(new NodePackageInstallTask());
}

function getJsonFileAndTypes(host: Tree, path: string): {json: JSONFile, types: string[]}|
undefined {
if (!host.exists(path)) {
return undefined;
}

const json = new JSONFile(host, path);
const types = json.get(typesJsonPath) ?? [];
if (!Array.isArray(types)) {
throw new SchematicsException(`TypeScript configuration file '${
path}' has an invalid 'types' property. It must be an array.`);
}

return {
json,
types,
};
}

export default function(options: Schema): Rule {
return () => {
// We favor the name option because the project option has a
Expand Down
24 changes: 11 additions & 13 deletions packages/localize/schematics/ng-add/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ describe('ng-add schematic', () => {
},
},
}));

const tsConfig = JSON.stringify({
compilerOptions: {
types: ['jasmine'],
},
});

host.create('tsconfig.spec.json', tsConfig);
});

it(`should add '@angular/localize' in 'types' in the root level 'tsconfig.json'`, async () => {
it(`should add all 'types' in the root level 'tsconfig.json'`, async () => {
host.create('tsconfig.json', JSON.stringify({
compilerOptions: {
types: ['node'],
Expand All @@ -78,8 +86,7 @@ describe('ng-add schematic', () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {compilerOptions} = host.readJson('tsconfig.json') as TsConfig;
const types = compilerOptions?.types;
expect(types).toContain(localizeType);
expect(types).toHaveSize(2);
expect(types).toEqual(['jasmine', 'node', '@angular/localize']);
});

it(`should not add '@angular/localize' in 'types' tsconfig when '@angular/localize/init' is present`,
Expand All @@ -93,8 +100,7 @@ describe('ng-add schematic', () => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {compilerOptions} = host.readJson('tsconfig.json') as TsConfig;
const types = compilerOptions?.types;
expect(types).not.toContain(localizeType);
expect(types).toHaveSize(2);
expect(types).toEqual(['jasmine', 'node', '@angular/localize']);
});


Expand Down Expand Up @@ -135,14 +141,6 @@ describe('ng-add schematic', () => {

it(`should add '@angular/localize' in 'types' tsconfigs referenced in karma builder`,
async () => {
const tsConfig = JSON.stringify({
compilerOptions: {
types: ['node'],
},
});

host.create('tsconfig.spec.json', tsConfig);

host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {compilerOptions} = host.readJson('tsconfig.spec.json') as TsConfig;
const types = compilerOptions?.types;
Expand Down