-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathconfig.ts
More file actions
136 lines (117 loc) · 3.73 KB
/
config.ts
File metadata and controls
136 lines (117 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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";
import { color } from "../color";
export class ConfigListCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(
private $projectConfigService: IProjectConfigService,
private $logger: ILogger,
) {}
public async execute(args: string[]): Promise<void> {
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 (
color.green(`${indent()}${key}: `) +
// @ts-ignore
this.getValueString(value[key], depth + 1)
);
})
.join("\n")
);
} else {
return color.yellow(value.toString());
}
}
}
export class ConfigGetCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];
constructor(
private $projectConfigService: IProjectConfigService,
private $logger: ILogger,
private $errors: IErrors,
) {}
public async execute(args: string[]): Promise<void> {
try {
const [key] = args;
const current = this.$projectConfigService.getValue(key);
this.$logger.info(current);
} catch (err) {
// ignore
}
}
public async canExecute(args: string[]): Promise<boolean> {
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<void> {
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 = color.green(key);
// when current is undefined, return empty string to avoid throw
const currentDisplay = current ? color.yellow(current) : "";
const updatedDisplay = color.cyan(convertedValue);
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<boolean> {
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);