Skip to content

Commit 1f8363a

Browse files
sumitarorafilipesilva
authored andcommitted
fix(@angular/cli): Fixing set prefix issue (angular#5301)
1 parent afbddfe commit 1f8363a

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const SetCommand = Command.extend({
6464
case 'number': value = this.asNumber(rawValue); break;
6565
case 'string': value = rawValue; break;
6666

67-
default: value = JSON.parse(rawValue);
67+
default: value = parseValue(rawValue, jsonPath);
6868
}
6969

7070
config.set(jsonPath, value);
@@ -74,4 +74,12 @@ const SetCommand = Command.extend({
7474
}
7575
});
7676

77+
function parseValue(rawValue: string, path: string) {
78+
try {
79+
return JSON.parse(rawValue);
80+
} catch (error) {
81+
throw new SilentError(`No node found at path ${path}`);
82+
}
83+
}
84+
7785
export default SetCommand;

packages/@ngtools/json-schema/src/schema-class-factory.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ function _getSchemaNodeForPath<T>(rootMetaData: SchemaTreeNode<T>,
5151
let fragments = _parseJsonPath(path);
5252
// TODO: make this work with union (oneOf) schemas
5353
return fragments.reduce((md: SchemaTreeNode<any>, current: string) => {
54-
return md && md.children && md.children[current];
54+
if (md && md.children) {
55+
return md.children[current];
56+
} else if (md && md.items) {
57+
return md.items[parseInt(current, 10)];
58+
} else {
59+
return md;
60+
}
5561
}, rootMetaData);
5662
}
5763

tests/e2e/tests/commands/get/get.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {ng} from '../../../utils/process';
2+
import {expectToFail} from '../../../utils/utils';
3+
4+
export default function() {
5+
return Promise.resolve()
6+
.then(() => expectToFail(() => ng('get', 'apps.zzz.prefix')))
7+
.then(() => ng('get', 'apps.0.prefix'))
8+
.then(({ stdout }) => {
9+
if (!stdout.match(/app/)) {
10+
throw new Error(`Expected "app", received "${JSON.stringify(stdout)}".`);
11+
}
12+
});
13+
}

tests/e2e/tests/commands/set/set.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {ng} from '../../../utils/process';
2+
import {expectToFail} from '../../../utils/utils';
3+
4+
export default function() {
5+
return Promise.resolve()
6+
.then(() => expectToFail(() => ng('set', 'apps.zzz.prefix')))
7+
.then(() => ng('set', 'apps.0.prefix' , 'new-prefix'))
8+
.then(() => ng('get', 'apps.0.prefix'))
9+
.then(({ stdout }) => {
10+
if (!stdout.match(/new-prefix/)) {
11+
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
12+
}
13+
});
14+
}

0 commit comments

Comments
 (0)