diff --git a/lib/bootstrap.ts b/lib/bootstrap.ts index 8d026034d4..f2dba61abc 100644 --- a/lib/bootstrap.ts +++ b/lib/bootstrap.ts @@ -172,6 +172,9 @@ injector.requirePublicClass( injector.require("platformCommandParameter", "./platform-command-param"); injector.requireCommand("create", "./commands/create-project"); injector.requireCommand("clean", "./commands/clean"); +injector.requireCommand("config|*list", "./commands/config"); +injector.requireCommand("config|get", "./commands/config"); +injector.requireCommand("config|set", "./commands/config"); injector.requireCommand("generate", "./commands/generate"); injector.requireCommand("platform|*list", "./commands/list-platforms"); injector.requireCommand("platform|add", "./commands/add-platform"); diff --git a/lib/commands/config.ts b/lib/commands/config.ts new file mode 100644 index 0000000000..97daafb3dc --- /dev/null +++ b/lib/commands/config.ts @@ -0,0 +1,133 @@ +import { ICommand, ICommandParameter } from "../common/definitions/commands"; +import { injector } from "../common/yok"; +import { IProjectConfigService } from "../definitions/project"; +import { SupportedConfigValues } from "../tools/config-manipulation/config-transformer"; +import { IErrors } from "../common/declarations"; + +export class ConfigListCommand implements ICommand { + public allowedParameters: ICommandParameter[] = []; + + constructor( + private $projectConfigService: IProjectConfigService, + private $logger: ILogger + ) {} + + public async execute(args: string[]): Promise { + try { + const config = this.$projectConfigService.readConfig(); + this.$logger.info(this.getValueString(config as SupportedConfigValues)); + } catch (error) { + this.$logger.info("Failed to read config. Error is: ", error); + } + } + + private getValueString(value: SupportedConfigValues, depth = 0): string { + const indent = () => " ".repeat(depth); + if (typeof value === "object") { + return ( + `${depth > 0 ? "\n" : ""}` + + Object.keys(value) + .map((key) => { + return ( + `${indent()}${key}: `.green + + this.getValueString(value[key], depth + 1) + ); + }) + .join("\n") + ); + } else { + return `${value}`.yellow as string; + } + } +} + +export class ConfigGetCommand implements ICommand { + public allowedParameters: ICommandParameter[] = []; + + constructor( + private $projectConfigService: IProjectConfigService, + private $logger: ILogger, + private $errors: IErrors + ) {} + + public async execute(args: string[]): Promise { + try { + const [key] = args; + const current = this.$projectConfigService.getValue(key); + this.$logger.info(current); + } catch (err) { + // ignore + } + } + + public async canExecute(args: string[]): Promise { + if (!args[0]) { + this.$errors.failWithHelp("You must specify a key. Eg: ios.id"); + } + + return true; + } +} + +export class ConfigSetCommand implements ICommand { + public allowedParameters: ICommandParameter[] = []; + + constructor( + private $projectConfigService: IProjectConfigService, + private $logger: ILogger, + private $errors: IErrors + ) {} + + public async execute(args: string[]): Promise { + const [key, value] = args; + const current = this.$projectConfigService.getValue(key); + if (current && typeof current === "object") { + this.$errors.fail( + `Unable to change object values. Please update individual values instead.\nEg: ns config set android.codeCache true` + ); + } + const convertedValue = this.getConvertedValue(value); + const existingKey = current !== undefined; + const keyDisplay = `${key}`.green; + const currentDisplay = `${current}`.yellow; + const updatedDisplay = `${convertedValue}`.cyan; + + this.$logger.info( + `${existingKey ? "Updating" : "Setting"} ${keyDisplay}${ + existingKey ? ` from ${currentDisplay} ` : " " + }to ${updatedDisplay}` + ); + + try { + await this.$projectConfigService.setValue(key, convertedValue); + this.$logger.info("Done"); + } catch (error) { + this.$logger.info("Could not update conifg. Error is: ", error); + } + } + + public async canExecute(args: string[]): Promise { + if (!args[0]) { + this.$errors.failWithHelp("You must specify a key. Eg: ios.id"); + } + + if (!args[1]) { + this.$errors.failWithHelp("You must specify a value."); + } + + return true; + } + + private getConvertedValue(v: any): any { + try { + return JSON.parse(v); + } catch (e) { + // just treat it as a string + return `${v}`; + } + } +} + +injector.registerCommand("config|*list", ConfigListCommand); +injector.registerCommand("config|get", ConfigGetCommand); +injector.registerCommand("config|set", ConfigSetCommand);