mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-16 18:43:42 +08:00
This also will load angular-cli.json in the HOME directory as a fallback, supports more stuff from the JSON Schema (like default values) than the old one, and actually verify that what you inputs is the right thing. This will be its own NPM package at some point, as other people will probably be interested in having a JSON Schema loader that gives type safety and provides fallbacks and metadata. Closes #1763
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import * as SilentError from 'silent-error';
|
|
import * as Command from 'ember-cli/lib/models/command';
|
|
import {CliConfig} from '../models/config';
|
|
|
|
|
|
const SetCommand = Command.extend({
|
|
name: 'set',
|
|
description: 'Set a value in the configuration.',
|
|
works: 'everywhere',
|
|
|
|
availableOptions: [
|
|
{ name: 'global', type: Boolean, default: false, aliases: ['g'] },
|
|
],
|
|
|
|
asBoolean: function (raw: string): boolean {
|
|
if (raw == 'true' || raw == '1') {
|
|
return true;
|
|
} else if (raw == 'false' || raw == '' || raw == '0') {
|
|
return false;
|
|
} else {
|
|
throw new SilentError(`Invalid boolean value: "${raw}"`);
|
|
}
|
|
},
|
|
asNumber: function (raw: string): number {
|
|
if (Number.isNaN(+raw)) {
|
|
throw new SilentError(`Invalid number value: "${raw}"`);
|
|
}
|
|
return +raw;
|
|
},
|
|
|
|
run: function (commandOptions, rawArgs): Promise<void> {
|
|
return new Promise(resolve => {
|
|
const [jsonPath, rawValue] = rawArgs;
|
|
const config = CliConfig.fromProject();
|
|
const type = config.typeOf(jsonPath);
|
|
let value: any = rawValue;
|
|
|
|
switch (type) {
|
|
case 'boolean': value = this.asBoolean(rawValue); break;
|
|
case 'number': value = this.asNumber(rawValue); break;
|
|
case 'string': value = rawValue; break;
|
|
|
|
default: value = JSON.parse(rawValue);
|
|
}
|
|
|
|
config.set(jsonPath, value);
|
|
config.save();
|
|
resolve();
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = SetCommand;
|