Skip to content
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

fix(CliConfig): allow set on array item properties #1800

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
19 changes: 13 additions & 6 deletions addon/ng2/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ export class CliConfig {
let properties: any;
let additionalProperties: boolean;

jsonPath = jsonPath.replace(/\[(\d+)\]/g, '.$1');
const checkPath = jsonPath.split('.').reduce((o, i) => {
if (!o || !o.properties) {
if (!o || (o.type === 'array' && !o.items) || (o.type === 'object' && !o.properties)) {
throw new Error(`Invalid config path.`);
}
properties = o.properties;

additionalProperties = o.additionalProperties;

return o.properties[i];
if (o.type === 'array') {
return o.items;
}

properties = o.properties;

return properties[i];
}, schema);
const configPath = jsonPath.split('.').reduce((o, i) => o[i], this._config);

Expand All @@ -84,18 +91,18 @@ export class CliConfig {
}
}

if (typeof checkPath.type === 'string' && isNaN(value)) {
if (checkPath.type === 'string' && isNaN(value)) {
parent[name] = value;
return true;
}

if (typeof checkPath.type === 'number' && !isNaN(value)) {
if (checkPath.type === 'number' && !isNaN(value)) {
parent[name] = value;
return true;
}

if (typeof value != checkPath.type) {
throw new Error(`Invalid value type. Trying to set ${typeof value} to ${path.type}`);
throw new Error(`Invalid value type. Trying to set ${typeof value} to ${checkPath.type}`);
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/models/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe.skip('Config Tests', () => {
expect(contents.project.name).to.be.equal('new-project-name');
});

it('Updates properties of array items successfully', () => {
let c = new CliConfig(configCopy);
c.set('apps.0.main', 'new-main.ts');
c.set('apps[0].test', 'new-test.ts');
c.save();

let contents = getContents();

expect(contents.apps[0].main).to.be.equal('new-main.ts');
expect(contents.apps[0].test).to.be.equal('new-test.ts');
});

it('Throws an error if try to assign property that does not exists', () => {
let c = new CliConfig(configCopy);

Expand Down