-
Notifications
You must be signed in to change notification settings - Fork 1
/
replace.ts
76 lines (61 loc) · 2.12 KB
/
replace.ts
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
import {Args, Flags} from '@oclif/core'
import chalk from "chalk";
import {Helper} from "../../shared/helper.js";
import {LabelBaseCommand} from "../../shared/label-base.command.js";
import {UTIL} from "../../shared/util.js";
/**
* node --loader ts-node/esm --no-warnings=ExperimentalWarning ./bin/dev label:replace hello.world
*/
export default class LabelReplace extends LabelBaseCommand<typeof LabelReplace> {
static args = {
label: Args.string({
description: 'A label key',
required: true
}),
}
static description = 'Replace the label'
static examples = [
'<%= config.bin %> <%= command.id %> --help',
'<%= config.bin %> <%= command.id %> hello.world -t="Hello world!!!"',
'<%= config.bin %> <%= command.id %> hello.world -t="Hello world!!!" -fen',
]
static flags = {
langCode: Flags.string({
char: 'f',
default: null,
description: 'The language code of source text.',
multiple: false,
requiredOrDefaulted: true,
}),
translation: Flags.string({
char: 't',
description: 'The translation text.',
required: true,
}),
}
private label: string;
private langCode: string;
private translation: string;
public async run(): Promise<void> {
const {args, flags} = await this.parse(LabelReplace)
await this.readCliConfig();
this.label = await this.getLabelValidationOrInput({
label: args.label,
labelValidation: this.cliConfig.labelValidation
});
this.langCode = await this.getLangCode(this.cliConfig.languages, flags.langCode);
this.translation = await this.getTranslation(flags.translation, this.langCode);
const mapLang = await this.getLangTranslation(this.langCode);
let isChange = false;
if (this.label in mapLang.translateFlatten) {
mapLang.translate = UTIL.replacePropertyPath(mapLang.translate, this.label, flags.translation);
isChange = true;
} else {
this.log(chalk.red(`Label not found.`));
}
if (isChange) {
await Helper.writeOrCreateJsonFile(mapLang.translate, mapLang.file);
this.log(chalk.green(`Label changed successfully.`));
}
}
}