Skip to content

Commit

Permalink
fix(@angular/cli): Fixing set prefix issue (#5301)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora authored and filipesilva committed Mar 9, 2017
1 parent afbddfe commit 1f8363a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/@angular/cli/commands/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const SetCommand = Command.extend({
case 'number': value = this.asNumber(rawValue); break;
case 'string': value = rawValue; break;

default: value = JSON.parse(rawValue);
default: value = parseValue(rawValue, jsonPath);
}

config.set(jsonPath, value);
Expand All @@ -74,4 +74,12 @@ const SetCommand = Command.extend({
}
});

function parseValue(rawValue: string, path: string) {
try {
return JSON.parse(rawValue);
} catch (error) {
throw new SilentError(`No node found at path ${path}`);
}
}

export default SetCommand;
8 changes: 7 additions & 1 deletion packages/@ngtools/json-schema/src/schema-class-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ function _getSchemaNodeForPath<T>(rootMetaData: SchemaTreeNode<T>,
let fragments = _parseJsonPath(path);
// TODO: make this work with union (oneOf) schemas
return fragments.reduce((md: SchemaTreeNode<any>, current: string) => {
return md && md.children && md.children[current];
if (md && md.children) {
return md.children[current];
} else if (md && md.items) {
return md.items[parseInt(current, 10)];
} else {
return md;
}
}, rootMetaData);
}

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

export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('get', 'apps.zzz.prefix')))
.then(() => ng('get', 'apps.0.prefix'))
.then(({ stdout }) => {
if (!stdout.match(/app/)) {
throw new Error(`Expected "app", received "${JSON.stringify(stdout)}".`);
}
});
}
14 changes: 14 additions & 0 deletions tests/e2e/tests/commands/set/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';

export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('set', 'apps.zzz.prefix')))
.then(() => ng('set', 'apps.0.prefix' , 'new-prefix'))
.then(() => ng('get', 'apps.0.prefix'))
.then(({ stdout }) => {
if (!stdout.match(/new-prefix/)) {
throw new Error(`Expected "new-prefix", received "${JSON.stringify(stdout)}".`);
}
});
}

0 comments on commit 1f8363a

Please sign in to comment.