@red-hat-developer-hub/translations-cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/bin/translations-cli +34 -0
- package/dist/commands/clean.cjs.js +130 -0
- package/dist/commands/deploy.cjs.js +53 -0
- package/dist/commands/download.cjs.js +304 -0
- package/dist/commands/generate.cjs.js +1294 -0
- package/dist/commands/index.cjs.js +146 -0
- package/dist/commands/init.cjs.js +115 -0
- package/dist/commands/list.cjs.js +178 -0
- package/dist/commands/setupMemsource.cjs.js +338 -0
- package/dist/commands/status.cjs.js +40 -0
- package/dist/commands/sync.cjs.js +226 -0
- package/dist/commands/upload.cjs.js +506 -0
- package/dist/index.cjs.js +30 -0
- package/dist/lib/errors.cjs.js +36 -0
- package/dist/lib/i18n/analyzeStatus.cjs.js +79 -0
- package/dist/lib/i18n/config.cjs.js +256 -0
- package/dist/lib/i18n/deployTranslations.cjs.js +1213 -0
- package/dist/lib/i18n/extractKeys.cjs.js +418 -0
- package/dist/lib/i18n/formatReport.cjs.js +138 -0
- package/dist/lib/i18n/generateFiles.cjs.js +94 -0
- package/dist/lib/i18n/loadFile.cjs.js +93 -0
- package/dist/lib/i18n/mergeFiles.cjs.js +126 -0
- package/dist/lib/i18n/uploadCache.cjs.js +83 -0
- package/dist/lib/i18n/validateFile.cjs.js +189 -0
- package/dist/lib/paths.cjs.js +51 -0
- package/dist/lib/utils/exec.cjs.js +41 -0
- package/dist/lib/utils/translationUtils.cjs.js +33 -0
- package/dist/lib/version.cjs.js +38 -0
- package/package.json +65 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var path = require('node:path');
|
|
4
|
+
var os = require('node:os');
|
|
5
|
+
var exec = require('../utils/exec.cjs.js');
|
|
6
|
+
var fs = require('fs-extra');
|
|
7
|
+
var paths = require('../paths.cjs.js');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
12
|
+
var os__default = /*#__PURE__*/_interopDefaultCompat(os);
|
|
13
|
+
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
14
|
+
|
|
15
|
+
const PROJECT_CONFIG_FILE_NAME = ".i18n.config.json";
|
|
16
|
+
const AUTH_CONFIG_FILE_NAME = ".i18n.auth.json";
|
|
17
|
+
const CONFIG_ENV_PREFIX = "I18N_";
|
|
18
|
+
async function loadProjectConfig() {
|
|
19
|
+
const config = {};
|
|
20
|
+
const configPath = path__default.default.join(paths.paths.targetDir, PROJECT_CONFIG_FILE_NAME);
|
|
21
|
+
if (await fs__default.default.pathExists(configPath)) {
|
|
22
|
+
try {
|
|
23
|
+
const fileConfig = await fs__default.default.readJson(configPath);
|
|
24
|
+
Object.assign(config, fileConfig);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.warn(
|
|
27
|
+
`Warning: Could not read project config file ${configPath}: ${error}`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return config;
|
|
32
|
+
}
|
|
33
|
+
async function loadAuthConfig() {
|
|
34
|
+
const config = {};
|
|
35
|
+
const authPath = path__default.default.join(os__default.default.homedir(), AUTH_CONFIG_FILE_NAME);
|
|
36
|
+
if (await fs__default.default.pathExists(authPath)) {
|
|
37
|
+
try {
|
|
38
|
+
const authConfig = await fs__default.default.readJson(authPath);
|
|
39
|
+
Object.assign(config, authConfig);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.warn(
|
|
42
|
+
`Warning: Could not read auth config file ${authPath}: ${error}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return config;
|
|
47
|
+
}
|
|
48
|
+
async function loadI18nConfig() {
|
|
49
|
+
const config = {};
|
|
50
|
+
const projectConfig = await loadProjectConfig();
|
|
51
|
+
Object.assign(config, projectConfig);
|
|
52
|
+
const authConfig = await loadAuthConfig();
|
|
53
|
+
if (Object.keys(authConfig).length > 0) {
|
|
54
|
+
config.auth = authConfig;
|
|
55
|
+
}
|
|
56
|
+
const tmsUrl = process.env[`${CONFIG_ENV_PREFIX}TMS_URL`] || process.env.MEMSOURCE_URL;
|
|
57
|
+
if (tmsUrl) {
|
|
58
|
+
config.tms = config.tms || {};
|
|
59
|
+
config.tms.url = tmsUrl;
|
|
60
|
+
}
|
|
61
|
+
if (process.env[`${CONFIG_ENV_PREFIX}TMS_PROJECT_ID`]) {
|
|
62
|
+
config.tms = config.tms || {};
|
|
63
|
+
config.tms.projectId = process.env[`${CONFIG_ENV_PREFIX}TMS_PROJECT_ID`];
|
|
64
|
+
}
|
|
65
|
+
const tmsToken = process.env[`${CONFIG_ENV_PREFIX}TMS_TOKEN`] || process.env.MEMSOURCE_TOKEN;
|
|
66
|
+
if (tmsToken) {
|
|
67
|
+
config.auth = config.auth || {};
|
|
68
|
+
config.auth.tms = config.auth.tms || {};
|
|
69
|
+
config.auth.tms.token = tmsToken;
|
|
70
|
+
}
|
|
71
|
+
const tmsUsername = process.env[`${CONFIG_ENV_PREFIX}TMS_USERNAME`] || process.env.MEMSOURCE_USERNAME;
|
|
72
|
+
if (tmsUsername) {
|
|
73
|
+
config.auth = config.auth || {};
|
|
74
|
+
config.auth.tms = config.auth.tms || {};
|
|
75
|
+
config.auth.tms.username = tmsUsername;
|
|
76
|
+
}
|
|
77
|
+
const tmsPassword = process.env[`${CONFIG_ENV_PREFIX}TMS_PASSWORD`] || process.env.MEMSOURCE_PASSWORD;
|
|
78
|
+
if (tmsPassword) {
|
|
79
|
+
config.auth = config.auth || {};
|
|
80
|
+
config.auth.tms = config.auth.tms || {};
|
|
81
|
+
config.auth.tms.password = tmsPassword;
|
|
82
|
+
}
|
|
83
|
+
const languagesEnv = process.env[`${CONFIG_ENV_PREFIX}LANGUAGES`];
|
|
84
|
+
if (languagesEnv) {
|
|
85
|
+
config.languages = languagesEnv.split(",").map((l) => l.trim());
|
|
86
|
+
}
|
|
87
|
+
if (process.env[`${CONFIG_ENV_PREFIX}FORMAT`]) {
|
|
88
|
+
config.format = process.env[`${CONFIG_ENV_PREFIX}FORMAT`];
|
|
89
|
+
}
|
|
90
|
+
if (process.env[`${CONFIG_ENV_PREFIX}SOURCE_DIR`]) {
|
|
91
|
+
config.directories = config.directories || {};
|
|
92
|
+
config.directories.sourceDir = process.env[`${CONFIG_ENV_PREFIX}SOURCE_DIR`];
|
|
93
|
+
}
|
|
94
|
+
if (process.env[`${CONFIG_ENV_PREFIX}OUTPUT_DIR`]) {
|
|
95
|
+
config.directories = config.directories || {};
|
|
96
|
+
config.directories.outputDir = process.env[`${CONFIG_ENV_PREFIX}OUTPUT_DIR`];
|
|
97
|
+
}
|
|
98
|
+
if (process.env[`${CONFIG_ENV_PREFIX}LOCALES_DIR`]) {
|
|
99
|
+
config.directories = config.directories || {};
|
|
100
|
+
config.directories.localesDir = process.env[`${CONFIG_ENV_PREFIX}LOCALES_DIR`];
|
|
101
|
+
}
|
|
102
|
+
return config;
|
|
103
|
+
}
|
|
104
|
+
function mergeDirectoryConfig(config, options, merged) {
|
|
105
|
+
if (config.directories?.sourceDir && !options.sourceDir) {
|
|
106
|
+
merged.sourceDir = config.directories.sourceDir;
|
|
107
|
+
}
|
|
108
|
+
if (config.directories?.outputDir && !options.outputDir) {
|
|
109
|
+
merged.outputDir = config.directories.outputDir;
|
|
110
|
+
}
|
|
111
|
+
if (config.directories?.localesDir && !options.targetDir && !options.localesDir) {
|
|
112
|
+
merged.targetDir = config.directories.localesDir;
|
|
113
|
+
merged.localesDir = config.directories.localesDir;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function isMemsourceSetup(config) {
|
|
117
|
+
return Boolean(process.env.MEMSOURCE_URL) || Boolean(process.env.MEMSOURCE_USERNAME) || Boolean(config.tms?.url?.includes("memsource"));
|
|
118
|
+
}
|
|
119
|
+
async function getTmsToken(config, options) {
|
|
120
|
+
let token = config.auth?.tms?.token;
|
|
121
|
+
const shouldGenerateToken = !token && Boolean(config.auth?.tms?.username) && Boolean(config.auth?.tms?.password) && !options.tmsToken;
|
|
122
|
+
if (shouldGenerateToken && isMemsourceSetup(config)) {
|
|
123
|
+
token = await generateMemsourceToken(
|
|
124
|
+
config.auth.tms.username,
|
|
125
|
+
config.auth.tms.password
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return token && !options.tmsToken ? token : void 0;
|
|
129
|
+
}
|
|
130
|
+
async function mergeAuthConfig(config, options, merged) {
|
|
131
|
+
const token = await getTmsToken(config, options);
|
|
132
|
+
if (token) {
|
|
133
|
+
merged.tmsToken = token;
|
|
134
|
+
}
|
|
135
|
+
if (config.auth?.tms?.username && !options.tmsUsername) {
|
|
136
|
+
merged.tmsUsername = config.auth.tms.username;
|
|
137
|
+
}
|
|
138
|
+
if (config.auth?.tms?.password && !options.tmsPassword) {
|
|
139
|
+
merged.tmsPassword = config.auth.tms.password;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function mergeTmsConfig(config, options, merged) {
|
|
143
|
+
if (config.tms?.url && !options.tmsUrl) {
|
|
144
|
+
merged.tmsUrl = config.tms.url;
|
|
145
|
+
}
|
|
146
|
+
if (config.tms?.projectId && !options.projectId) {
|
|
147
|
+
merged.projectId = config.tms.projectId;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function mergeLanguageAndFormatConfig(config, options, merged) {
|
|
151
|
+
if (config.languages && !options.languages && !options.targetLanguages) {
|
|
152
|
+
const languagesStr = config.languages.join(",");
|
|
153
|
+
merged.languages = languagesStr;
|
|
154
|
+
merged.targetLanguages = languagesStr;
|
|
155
|
+
}
|
|
156
|
+
if (config.format && !options.format) {
|
|
157
|
+
merged.format = config.format;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function mergePatternConfig(config, options, merged) {
|
|
161
|
+
if (config.patterns?.include && !options.includePattern) {
|
|
162
|
+
merged.includePattern = config.patterns.include;
|
|
163
|
+
}
|
|
164
|
+
if (config.patterns?.exclude && !options.excludePattern) {
|
|
165
|
+
merged.excludePattern = config.patterns.exclude;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async function mergeConfigWithOptions(config, options) {
|
|
169
|
+
const merged = {};
|
|
170
|
+
mergeDirectoryConfig(config, options, merged);
|
|
171
|
+
mergeTmsConfig(config, options, merged);
|
|
172
|
+
await mergeAuthConfig(config, options, merged);
|
|
173
|
+
mergeLanguageAndFormatConfig(config, options, merged);
|
|
174
|
+
mergePatternConfig(config, options, merged);
|
|
175
|
+
return { ...merged, ...options };
|
|
176
|
+
}
|
|
177
|
+
async function createDefaultConfigFile() {
|
|
178
|
+
const configPath = path__default.default.join(paths.paths.targetDir, PROJECT_CONFIG_FILE_NAME);
|
|
179
|
+
const defaultConfig = {
|
|
180
|
+
tms: {
|
|
181
|
+
url: "",
|
|
182
|
+
projectId: ""
|
|
183
|
+
},
|
|
184
|
+
directories: {
|
|
185
|
+
sourceDir: "src",
|
|
186
|
+
outputDir: "i18n",
|
|
187
|
+
localesDir: "src/locales"
|
|
188
|
+
},
|
|
189
|
+
languages: [],
|
|
190
|
+
format: "json",
|
|
191
|
+
patterns: {
|
|
192
|
+
include: "**/*.{ts,tsx,js,jsx}",
|
|
193
|
+
exclude: "**/node_modules/**,**/dist/**,**/build/**,**/*.test.ts,**/*.spec.ts"
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
await fs__default.default.writeJson(configPath, defaultConfig, { spaces: 2 });
|
|
197
|
+
console.log(`Created project config file: ${configPath}`);
|
|
198
|
+
console.log(` This file can be committed to git.`);
|
|
199
|
+
}
|
|
200
|
+
async function createDefaultAuthFile() {
|
|
201
|
+
const authPath = path__default.default.join(os__default.default.homedir(), AUTH_CONFIG_FILE_NAME);
|
|
202
|
+
const defaultAuth = {
|
|
203
|
+
tms: {
|
|
204
|
+
username: "",
|
|
205
|
+
password: "",
|
|
206
|
+
token: ""
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
await fs__default.default.writeJson(authPath, defaultAuth, { spaces: 2, mode: 384 });
|
|
210
|
+
console.log(`Created personal auth config file: ${authPath}`);
|
|
211
|
+
console.log(` \u26A0\uFE0F This file should NOT be committed to git.`);
|
|
212
|
+
console.log(
|
|
213
|
+
` \u26A0\uFE0F This file contains sensitive credentials - keep it secure.`
|
|
214
|
+
);
|
|
215
|
+
console.log(` Add ${AUTH_CONFIG_FILE_NAME} to your global .gitignore.`);
|
|
216
|
+
}
|
|
217
|
+
async function generateMemsourceToken(username, password) {
|
|
218
|
+
try {
|
|
219
|
+
if (!exec.commandExists("memsource")) {
|
|
220
|
+
return void 0;
|
|
221
|
+
}
|
|
222
|
+
const token = exec.safeExecSyncOrThrow(
|
|
223
|
+
"memsource",
|
|
224
|
+
[
|
|
225
|
+
"auth",
|
|
226
|
+
"login",
|
|
227
|
+
"--user-name",
|
|
228
|
+
username,
|
|
229
|
+
"--password",
|
|
230
|
+
password,
|
|
231
|
+
"-c",
|
|
232
|
+
"token",
|
|
233
|
+
"-f",
|
|
234
|
+
"value"
|
|
235
|
+
],
|
|
236
|
+
{
|
|
237
|
+
encoding: "utf-8",
|
|
238
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
239
|
+
maxBuffer: 1024 * 1024
|
|
240
|
+
}
|
|
241
|
+
).trim();
|
|
242
|
+
if (token && token.length > 0) {
|
|
243
|
+
return token;
|
|
244
|
+
}
|
|
245
|
+
} catch {
|
|
246
|
+
}
|
|
247
|
+
return void 0;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
exports.createDefaultAuthFile = createDefaultAuthFile;
|
|
251
|
+
exports.createDefaultConfigFile = createDefaultConfigFile;
|
|
252
|
+
exports.loadAuthConfig = loadAuthConfig;
|
|
253
|
+
exports.loadI18nConfig = loadI18nConfig;
|
|
254
|
+
exports.loadProjectConfig = loadProjectConfig;
|
|
255
|
+
exports.mergeConfigWithOptions = mergeConfigWithOptions;
|
|
256
|
+
//# sourceMappingURL=config.cjs.js.map
|