/
get.ts
102 lines (86 loc) · 2.58 KB
/
get.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
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
import { Args, Flags, ux } from "@oclif/core";
import * as ApiV1 from "@/lib/api-v1";
import BaseCommand from "@/lib/base-command";
import { formatDate } from "@/lib/helpers/date";
import { withSpinner } from "@/lib/helpers/request";
import * as Translation from "@/lib/marshal/translation";
export default class TranslationGet extends BaseCommand<typeof TranslationGet> {
static summary = "Display a single translation from an environment.";
static flags = {
environment: Flags.string({
default: "development",
summary: "The environment to use.",
}),
"hide-uncommitted-changes": Flags.boolean({
summary: "Hide any uncommitted changes.",
}),
};
static args = {
translationRef: Args.string({
description: Translation.translationRefDescription,
required: true,
}),
};
static enableJsonFlag = true;
async run(): Promise<ApiV1.GetTranslationResp | void> {
const { args, flags } = this.props;
const parsedRef = Translation.parseTranslationRef(args.translationRef);
if (!parsedRef) {
return this.error(
`Invalid translation ref \`${args.translationRef}\`, use valid <locale> or <namespace>.<locale> for namespaced translations`,
);
}
const resp = await withSpinner<ApiV1.GetTranslationResp>(() =>
this.apiV1.getTranslation(this.props, parsedRef),
);
if (flags.json) return resp.data;
this.render(resp.data);
}
render(translation: ApiV1.GetTranslationResp): void {
const { translationRef } = this.props.args;
const { environment: env, "hide-uncommitted-changes": commitedOnly } =
this.props.flags;
const qualifier =
env === "development" && !commitedOnly ? "(including uncommitted)" : "";
this.log(
`‣ Showing translation \`${translationRef}\` in \`${env}\` environment ${qualifier}\n`,
);
/*
* Translation table
*/
const rows = [
{
key: "Language",
value: Translation.formatLanguage(translation),
},
{
key: "Locale",
value: translation.locale_code,
},
{
key: "Namespace",
value: translation.namespace || "-",
},
{
key: "Updated at",
value: formatDate(translation.updated_at),
},
{
key: "Created at",
value: formatDate(translation.created_at),
},
];
ux.table(rows, {
key: {
header: "Translation",
minWidth: 24,
},
value: {
header: "",
minWidth: 16,
},
});
this.log("");
ux.styledJSON(JSON.parse(translation.content));
}
}