Skip to content

Commit 9269905

Browse files
committed
fix(@angular/cli): handle raw strings in config command
1 parent 6d80c49 commit 9269905

File tree

5 files changed

+53
-48
lines changed

5 files changed

+53
-48
lines changed

packages/@angular/cli/commands/config.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { writeFileSync } from 'fs';
2-
import { Command, Option } from '../models/command';
3-
import {
4-
getWorkspace,
5-
getWorkspaceRaw,
6-
migrateLegacyGlobalConfig,
7-
validateWorkspace,
8-
} from '../utilities/config';
91
import {
10-
JsonValue,
2+
InvalidJsonCharacterException,
113
JsonArray,
124
JsonObject,
135
JsonParseMode,
6+
JsonValue,
147
experimental,
158
parseJson,
169
tags,
1710
} from '@angular-devkit/core';
11+
import { writeFileSync } from 'fs';
12+
import { Command, Option } from '../models/command';
13+
import {
14+
getWorkspace,
15+
getWorkspaceRaw,
16+
migrateLegacyGlobalConfig,
17+
validateWorkspace,
18+
} from '../utilities/config';
1819

1920
const SilentError = require('silent-error');
2021

@@ -152,7 +153,15 @@ function normalizeValue(value: string, path: string): JsonValue {
152153
}
153154

154155
if (typeof value === 'string') {
155-
return parseJson(value, JsonParseMode.Loose);
156+
try {
157+
return parseJson(value, JsonParseMode.Loose);
158+
} catch (e) {
159+
if (e instanceof InvalidJsonCharacterException && !value.startsWith('{')) {
160+
return value;
161+
} else {
162+
throw e;
163+
}
164+
}
156165
}
157166

158167
return value;

tests/e2e/tests/commands/config/config-get.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {ng} from '../../../utils/process';
2-
import {expectToFail} from '../../../utils/utils';
1+
import { ng } from '../../../utils/process';
2+
import { expectToFail } from '../../../utils/utils';
33

44

55
export default function() {
@@ -12,9 +12,6 @@ export default function() {
1212
throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`);
1313
}
1414
})
15-
.then(() => expectToFail(() => {
16-
return ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
17-
}))
1815
.then(() => ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'true'))
1916
.then(() => ng('config', 'schematics.@schematics/angular.component.inlineStyle'))
2017
.then(({ stdout }) => {

tests/e2e/tests/commands/config/config-global.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {ng} from '../../../utils/process';
2-
import {expectToFail} from '../../../utils/utils';
3-
import { expectFileToExist } from '../../../utils/fs';
4-
import * as path from 'path';
51
import { homedir } from 'os';
6-
import { deleteFile } from '../../../utils/fs';
2+
import * as path from 'path';
3+
import { deleteFile, expectFileToExist } from '../../../utils/fs';
4+
import { ng } from '../../../utils/process';
5+
import { expectToFail } from '../../../utils/utils';
76

87

98
export default function() {
@@ -16,9 +15,10 @@ export default function() {
1615
throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`);
1716
}
1817
})
19-
.then(() => expectToFail(() => {
20-
return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
21-
}))
18+
// This test requires schema querying capabilities
19+
// .then(() => expectToFail(() => {
20+
// return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
21+
// }))
2222
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'true'))
2323
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle'))
2424
.then(({ stdout }) => {
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import {ng} from '../../../utils/process';
2-
import {expectToFail} from '../../../utils/utils';
1+
import { ng } from '../../../utils/process';
2+
import { expectToFail } from '../../../utils/utils';
33

4-
export default function() {
5-
return Promise.resolve()
6-
.then(() => expectToFail(() => ng('config', 'schematics.@schematics/angular.component.aaa', 'bbb')))
7-
.then(() => expectToFail(() => ng(
8-
'config',
9-
'schematics.@schematics/angular.component.viewEncapsulation',
10-
'bbb'
11-
)))
12-
.then(() => ng('config', 'schematics.@schematics/angular.component.viewEncapsulation', '"Emulated"'));
4+
export default async function() {
5+
6+
// These tests require schema querying capabilities
7+
// .then(() => expectToFail(
8+
// () => ng('config', 'schematics.@schematics/angular.component.aaa', 'bbb')),
9+
// )
10+
// .then(() => expectToFail(() => ng(
11+
// 'config',
12+
// 'schematics.@schematics/angular.component.viewEncapsulation',
13+
// 'bbb',
14+
// )))
15+
16+
await ng(
17+
'config',
18+
'schematics.@schematics/angular.component.viewEncapsulation',
19+
'Emulated',
20+
);
1321
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
import {ng} from '../../../utils/process';
2-
import {expectToFail} from '../../../utils/utils';
3-
import * as fs from 'fs';
1+
import { ng } from '../../../utils/process';
2+
import { expectToFail } from '../../../utils/utils';
43

54
export default function() {
6-
return;
7-
85
return Promise.resolve()
9-
.then(() => expectToFail(() => ng('config', 'apps.zzz.prefix')))
10-
.then(() => ng('config', 'apps.0.prefix' , 'new-prefix'))
11-
.then(() => ng('config', 'apps.0.prefix'))
6+
.then(() => expectToFail(() => ng('config', 'schematics.@schematics/angular.component.prefix')))
7+
.then(() => ng('config', 'schematics.@schematics/angular.component.prefix' , 'new-prefix'))
8+
.then(() => ng('config', 'schematics.@schematics/angular.component.prefix'))
129
.then(({ stdout }) => {
1310
if (!stdout.match(/new-prefix/)) {
1411
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
1512
}
16-
})
17-
.then(() => {
18-
const tsLint = JSON.parse(fs.readFileSync(process.cwd() + '/tslint.json', 'utf8'));
19-
if (tsLint.rules['component-selector'][2] !== 'new-prefix') {
20-
throw new Error(`Expected "new-prefix" Found: ${tsLint.rules['component-selector'][2]}.`);
21-
}
2213
});
2314
}

0 commit comments

Comments
 (0)