|
| 1 | +import { ICommand, ICommandParameter } from "../common/definitions/commands"; |
| 2 | +import { injector } from "../common/yok"; |
| 3 | +import { IProjectConfigService } from "../definitions/project"; |
| 4 | +import { SupportedConfigValues } from "../tools/config-manipulation/config-transformer"; |
| 5 | +import { IErrors } from "../common/declarations"; |
| 6 | + |
| 7 | +export class ConfigListCommand implements ICommand { |
| 8 | + public allowedParameters: ICommandParameter[] = []; |
| 9 | + |
| 10 | + constructor( |
| 11 | + private $projectConfigService: IProjectConfigService, |
| 12 | + private $logger: ILogger |
| 13 | + ) {} |
| 14 | + |
| 15 | + public async execute(args: string[]): Promise<void> { |
| 16 | + try { |
| 17 | + const config = this.$projectConfigService.readConfig(); |
| 18 | + this.$logger.info(this.getValueString(config as SupportedConfigValues)); |
| 19 | + } catch (error) { |
| 20 | + this.$logger.info("Failed to read config. Error is: ", error); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private getValueString(value: SupportedConfigValues, depth = 0): string { |
| 25 | + const indent = () => " ".repeat(depth); |
| 26 | + if (typeof value === "object") { |
| 27 | + return ( |
| 28 | + `${depth > 0 ? "\n" : ""}` + |
| 29 | + Object.keys(value) |
| 30 | + .map((key) => { |
| 31 | + return ( |
| 32 | + `${indent()}${key}: `.green + |
| 33 | + this.getValueString(value[key], depth + 1) |
| 34 | + ); |
| 35 | + }) |
| 36 | + .join("\n") |
| 37 | + ); |
| 38 | + } else { |
| 39 | + return `${value}`.yellow as string; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export class ConfigGetCommand implements ICommand { |
| 45 | + public allowedParameters: ICommandParameter[] = []; |
| 46 | + |
| 47 | + constructor( |
| 48 | + private $projectConfigService: IProjectConfigService, |
| 49 | + private $logger: ILogger, |
| 50 | + private $errors: IErrors |
| 51 | + ) {} |
| 52 | + |
| 53 | + public async execute(args: string[]): Promise<void> { |
| 54 | + try { |
| 55 | + const [key] = args; |
| 56 | + const current = this.$projectConfigService.getValue(key); |
| 57 | + this.$logger.info(current); |
| 58 | + } catch (err) { |
| 59 | + // ignore |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public async canExecute(args: string[]): Promise<boolean> { |
| 64 | + if (!args[0]) { |
| 65 | + this.$errors.failWithHelp("You must specify a key. Eg: ios.id"); |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export class ConfigSetCommand implements ICommand { |
| 73 | + public allowedParameters: ICommandParameter[] = []; |
| 74 | + |
| 75 | + constructor( |
| 76 | + private $projectConfigService: IProjectConfigService, |
| 77 | + private $logger: ILogger, |
| 78 | + private $errors: IErrors |
| 79 | + ) {} |
| 80 | + |
| 81 | + public async execute(args: string[]): Promise<void> { |
| 82 | + const [key, value] = args; |
| 83 | + const current = this.$projectConfigService.getValue(key); |
| 84 | + if (current && typeof current === "object") { |
| 85 | + this.$errors.fail( |
| 86 | + `Unable to change object values. Please update individual values instead.\nEg: ns config set android.codeCache true` |
| 87 | + ); |
| 88 | + } |
| 89 | + const convertedValue = this.getConvertedValue(value); |
| 90 | + const existingKey = current !== undefined; |
| 91 | + const keyDisplay = `${key}`.green; |
| 92 | + const currentDisplay = `${current}`.yellow; |
| 93 | + const updatedDisplay = `${convertedValue}`.cyan; |
| 94 | + |
| 95 | + this.$logger.info( |
| 96 | + `${existingKey ? "Updating" : "Setting"} ${keyDisplay}${ |
| 97 | + existingKey ? ` from ${currentDisplay} ` : " " |
| 98 | + }to ${updatedDisplay}` |
| 99 | + ); |
| 100 | + |
| 101 | + try { |
| 102 | + await this.$projectConfigService.setValue(key, convertedValue); |
| 103 | + this.$logger.info("Done"); |
| 104 | + } catch (error) { |
| 105 | + this.$logger.info("Could not update conifg. Error is: ", error); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public async canExecute(args: string[]): Promise<boolean> { |
| 110 | + if (!args[0]) { |
| 111 | + this.$errors.failWithHelp("You must specify a key. Eg: ios.id"); |
| 112 | + } |
| 113 | + |
| 114 | + if (!args[1]) { |
| 115 | + this.$errors.failWithHelp("You must specify a value."); |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + private getConvertedValue(v: any): any { |
| 122 | + try { |
| 123 | + return JSON.parse(v); |
| 124 | + } catch (e) { |
| 125 | + // just treat it as a string |
| 126 | + return `${v}`; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +injector.registerCommand("config|*list", ConfigListCommand); |
| 132 | +injector.registerCommand("config|get", ConfigGetCommand); |
| 133 | +injector.registerCommand("config|set", ConfigSetCommand); |
0 commit comments