Skip to content

Commit

Permalink
fix(@angular/cli): handle raw strings in config command
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin committed May 29, 2018
1 parent 6d80c49 commit 9269905
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
29 changes: 19 additions & 10 deletions packages/@angular/cli/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { writeFileSync } from 'fs';
import { Command, Option } from '../models/command';
import {
getWorkspace,
getWorkspaceRaw,
migrateLegacyGlobalConfig,
validateWorkspace,
} from '../utilities/config';
import {
JsonValue,
InvalidJsonCharacterException,
JsonArray,
JsonObject,
JsonParseMode,
JsonValue,
experimental,
parseJson,
tags,
} from '@angular-devkit/core';
import { writeFileSync } from 'fs';
import { Command, Option } from '../models/command';
import {
getWorkspace,
getWorkspaceRaw,
migrateLegacyGlobalConfig,
validateWorkspace,
} from '../utilities/config';

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

Expand Down Expand Up @@ -152,7 +153,15 @@ function normalizeValue(value: string, path: string): JsonValue {
}

if (typeof value === 'string') {
return parseJson(value, JsonParseMode.Loose);
try {
return parseJson(value, JsonParseMode.Loose);
} catch (e) {
if (e instanceof InvalidJsonCharacterException && !value.startsWith('{')) {
return value;
} else {
throw e;
}
}
}

return value;
Expand Down
7 changes: 2 additions & 5 deletions tests/e2e/tests/commands/config/config-get.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';
import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';


export default function() {
Expand All @@ -12,9 +12,6 @@ export default function() {
throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`);
}
})
.then(() => expectToFail(() => {
return ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
}))
.then(() => ng('config', 'schematics.@schematics/angular.component.inlineStyle', 'true'))
.then(() => ng('config', 'schematics.@schematics/angular.component.inlineStyle'))
.then(({ stdout }) => {
Expand Down
16 changes: 8 additions & 8 deletions tests/e2e/tests/commands/config/config-global.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';
import { expectFileToExist } from '../../../utils/fs';
import * as path from 'path';
import { homedir } from 'os';
import { deleteFile } from '../../../utils/fs';
import * as path from 'path';
import { deleteFile, expectFileToExist } from '../../../utils/fs';
import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';


export default function() {
Expand All @@ -16,9 +15,10 @@ export default function() {
throw new Error(`Expected "false", received "${JSON.stringify(stdout)}".`);
}
})
.then(() => expectToFail(() => {
return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
}))
// This test requires schema querying capabilities
// .then(() => expectToFail(() => {
// return ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'INVALID_BOOLEAN');
// }))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle', 'true'))
.then(() => ng('config', '--global', 'schematics.@schematics/angular.component.inlineStyle'))
.then(({ stdout }) => {
Expand Down
30 changes: 19 additions & 11 deletions tests/e2e/tests/commands/config/config-set-enum-check.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';
import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';

export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('config', 'schematics.@schematics/angular.component.aaa', 'bbb')))
.then(() => expectToFail(() => ng(
'config',
'schematics.@schematics/angular.component.viewEncapsulation',
'bbb'
)))
.then(() => ng('config', 'schematics.@schematics/angular.component.viewEncapsulation', '"Emulated"'));
export default async function() {

// These tests require schema querying capabilities
// .then(() => expectToFail(
// () => ng('config', 'schematics.@schematics/angular.component.aaa', 'bbb')),
// )
// .then(() => expectToFail(() => ng(
// 'config',
// 'schematics.@schematics/angular.component.viewEncapsulation',
// 'bbb',
// )))

await ng(
'config',
'schematics.@schematics/angular.component.viewEncapsulation',
'Emulated',
);
}
19 changes: 5 additions & 14 deletions tests/e2e/tests/commands/config/config-set-prefix.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';
import * as fs from 'fs';
import { ng } from '../../../utils/process';
import { expectToFail } from '../../../utils/utils';

export default function() {
return;

return Promise.resolve()
.then(() => expectToFail(() => ng('config', 'apps.zzz.prefix')))
.then(() => ng('config', 'apps.0.prefix' , 'new-prefix'))
.then(() => ng('config', 'apps.0.prefix'))
.then(() => expectToFail(() => ng('config', 'schematics.@schematics/angular.component.prefix')))
.then(() => ng('config', 'schematics.@schematics/angular.component.prefix' , 'new-prefix'))
.then(() => ng('config', 'schematics.@schematics/angular.component.prefix'))
.then(({ stdout }) => {
if (!stdout.match(/new-prefix/)) {
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
}
})
.then(() => {
const tsLint = JSON.parse(fs.readFileSync(process.cwd() + '/tslint.json', 'utf8'));
if (tsLint.rules['component-selector'][2] !== 'new-prefix') {
throw new Error(`Expected "new-prefix" Found: ${tsLint.rules['component-selector'][2]}.`);
}
});
}

0 comments on commit 9269905

Please sign in to comment.