Skip to content

feat(@angular/cli): allow comments in .angular-cli.json #9615

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

Closed
wants to merge 1 commit into from
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
43 changes: 43 additions & 0 deletions packages/@angular/cli/models/config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ describe('Config', () => {
expect(config.numberKey).toEqual(33);
});

describe('fromConfigPath', () => {
const TEST_CONFIG_NAME = 'test-config.json';
let configContent: string;

beforeEach(() => {
spyOn(fs, 'existsSync').and.returnValue(true);
spyOn(fs, 'readFileSync').and.callFake((fileName: string) => {
if (fileName === TEST_CONFIG_NAME) {
return configContent;
} else {
return JSON.stringify(schema);
}
});
});

function callFromConfigPath(content: string) {
configContent = content;
(fs.readFileSync as jasmine.Spy).calls.reset();
const config = CliConfig.fromConfigPath<ConfigInterface>(TEST_CONFIG_NAME);
expect(fs.readFileSync).toHaveBeenCalledTimes(2);
return config;
}

it('tolerates comments in JSON', () => {
const config = callFromConfigPath(`{
// comment
"requiredKey" /* comment */ : 2 // comment
/* comment */
} // and here`);
expect(config.get('requiredKey')).toEqual(2);
});

it('tolerates unquoted keys in JSON', () => {
const config = callFromConfigPath(`{ requiredKey: 3 }`);
expect(config.get('requiredKey')).toEqual(3);
});

it('tolerates trailing commas in JSON', () => {
const config = callFromConfigPath(`{ "requiredKey": 2, }`);
expect(config.get('requiredKey')).toEqual(2);
});
});

describe('Get', () => {
it('works', () => {
const config = new CliConfig(null, schema, <ConfigInterface>{
Expand Down
18 changes: 15 additions & 3 deletions packages/@angular/cli/models/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { stripIndent } from 'common-tags';

import { parseJson, JsonParseMode } from '@angular-devkit/core';
import {SchemaClass, SchemaClassFactory} from '@ngtools/json-schema';

import { stripBom } from '../../utilities/strip-bom';
Expand Down Expand Up @@ -80,7 +81,10 @@ export class CliConfig<JsonType> {
return new CliConfig<ConfigType>(null, schema, content, global);
}

static fromConfigPath<T>(configPath: string, otherPath: string[] = []): CliConfig<T> {
static fromConfigPath<T extends object>(
configPath: string,
otherPath: string[] = []
): CliConfig<T> {
const schemaContent = fs.readFileSync(DEFAULT_CONFIG_SCHEMA_PATH, 'utf-8');
let configContent = '{}';
if (fs.existsSync(configPath)) {
Expand All @@ -105,7 +109,7 @@ export class CliConfig<JsonType> {
let others: T[];

try {
content = JSON.parse(configContent);
content = this.parseLooseJsonObject(configContent) as T;
} catch (err) {
throw new InvalidConfigError(stripIndent`
Parsing '${configPath}' failed. Ensure the file is valid JSON.
Expand All @@ -115,7 +119,7 @@ export class CliConfig<JsonType> {

others = otherContents.map(otherContent => {
try {
return JSON.parse(otherContent);
return this.parseLooseJsonObject(otherContent) as T;
} catch (err) {
throw new InvalidConfigError(stripIndent`
Parsing '${configPath}' failed. Ensure the file is valid JSON.
Expand All @@ -134,4 +138,12 @@ export class CliConfig<JsonType> {

return new CliConfig<T>(configPath, schema, content, others);
}

private static parseLooseJsonObject(json: string): object {
const result = parseJson(json, JsonParseMode.Loose);
if (result === null || typeof result !== 'object' || Array.isArray(result)) {
throw new Error('JSON is not an object');
}
return result;
}
}