@worldware/msg-cli 0.1.3 → 0.2.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/README.md +99 -13
- package/dist/{chunk-BRZS5LYD.mjs → chunk-6A6JGF7U.mjs} +2 -2
- package/dist/chunk-BM3O7ZJW.mjs +176 -0
- package/dist/{chunk-AT7DHQF7.mjs → chunk-F7XZRKP7.mjs} +2 -2
- package/dist/chunk-KKATA5YE.mjs +324 -0
- package/dist/{chunk-T4ENP5TS.mjs → chunk-LNCUPVEF.mjs} +2 -2
- package/dist/commands/create/project.cjs +24 -12
- package/dist/commands/create/project.mjs +27 -15
- package/dist/commands/create/resource.cjs +2 -2
- package/dist/commands/create/resource.mjs +3 -3
- package/dist/commands/export.cjs +356 -0
- package/dist/commands/export.d.mts +16 -0
- package/dist/commands/export.d.ts +16 -0
- package/dist/commands/export.mjs +91 -0
- package/dist/commands/import.cjs +526 -0
- package/dist/commands/import.d.mts +17 -0
- package/dist/commands/import.d.ts +17 -0
- package/dist/commands/import.mjs +116 -0
- package/dist/commands/init.cjs +2 -2
- package/dist/commands/init.mjs +1 -1
- package/dist/lib/create-project-helpers.mjs +3 -3
- package/dist/lib/create-resource-helpers.cjs +1 -1
- package/dist/lib/create-resource-helpers.mjs +2 -2
- package/dist/lib/export-helpers.cjs +229 -0
- package/dist/lib/export-helpers.d.mts +55 -0
- package/dist/lib/export-helpers.d.ts +55 -0
- package/dist/lib/export-helpers.mjs +18 -0
- package/dist/lib/import-helpers.cjs +380 -0
- package/dist/lib/import-helpers.d.mts +92 -0
- package/dist/lib/import-helpers.d.ts +92 -0
- package/dist/lib/import-helpers.mjs +24 -0
- package/dist/lib/init-helpers.cjs +2 -2
- package/dist/lib/init-helpers.mjs +1 -1
- package/package.json +2 -2
- package/dist/lib/utilities.cjs +0 -203
- package/dist/lib/utilities.d.mts +0 -59
- package/dist/lib/utilities.d.ts +0 -59
- package/dist/lib/utilities.mjs +0 -172
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import {
|
|
2
|
+
dynamicImportFromUrl
|
|
3
|
+
} from "./chunk-LNCUPVEF.mjs";
|
|
4
|
+
|
|
5
|
+
// src/lib/import-helpers.ts
|
|
6
|
+
import { MsgResource } from "@worldware/msg";
|
|
7
|
+
import { readdir, readFile, writeFile, mkdir } from "fs/promises";
|
|
8
|
+
import { basename, extname, join } from "path";
|
|
9
|
+
import { pathToFileURL } from "url";
|
|
10
|
+
import { XMLParser } from "fast-xml-parser";
|
|
11
|
+
import { existsSync } from "fs";
|
|
12
|
+
var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
|
|
13
|
+
var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
|
|
14
|
+
async function findXliffFilePaths(directory) {
|
|
15
|
+
const result = [];
|
|
16
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
17
|
+
for (const entry of entries) {
|
|
18
|
+
const fullPath = join(directory, entry.name);
|
|
19
|
+
if (entry.isFile()) {
|
|
20
|
+
const ext = extname(entry.name).toLowerCase();
|
|
21
|
+
if (XLIFF_EXTENSIONS.includes(ext)) {
|
|
22
|
+
result.push(fullPath);
|
|
23
|
+
}
|
|
24
|
+
} else if (entry.isDirectory()) {
|
|
25
|
+
result.push(...await findXliffFilePaths(fullPath));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
function filterXliffPathsByProject(filePaths, projectName) {
|
|
31
|
+
return filePaths.filter((p) => {
|
|
32
|
+
const filename = basename(p);
|
|
33
|
+
if (filename.startsWith(projectName)) return true;
|
|
34
|
+
const pathParts = p.split(/[/\\]/);
|
|
35
|
+
return pathParts.some((part) => part === projectName);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function filterXliffPathsByLocale(filePaths, locale) {
|
|
39
|
+
return filePaths.filter((p) => {
|
|
40
|
+
const filename = basename(p);
|
|
41
|
+
if (filename.includes(`.${locale}.`)) return true;
|
|
42
|
+
const pathParts = p.split(/[/\\]/);
|
|
43
|
+
return pathParts.some((part) => part === locale);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
var LOCALE_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]+)*$/;
|
|
47
|
+
function parseXliffFilename(filePath) {
|
|
48
|
+
const filename = basename(filePath);
|
|
49
|
+
const match = filename.match(XLIFF_FILENAME_PATTERN);
|
|
50
|
+
const pathParts = filePath.split(/[/\\]/);
|
|
51
|
+
let project;
|
|
52
|
+
let locale;
|
|
53
|
+
if (match) {
|
|
54
|
+
const [, proj, loc] = match;
|
|
55
|
+
project = proj ?? basename(filename, extname(filename));
|
|
56
|
+
locale = loc;
|
|
57
|
+
} else {
|
|
58
|
+
project = basename(filename, extname(filename));
|
|
59
|
+
}
|
|
60
|
+
if (!locale) {
|
|
61
|
+
for (const part of pathParts) {
|
|
62
|
+
if (LOCALE_PATTERN.test(part) && part.length >= 2) {
|
|
63
|
+
locale = part;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (!project && pathParts.length >= 2) {
|
|
69
|
+
for (const part of pathParts) {
|
|
70
|
+
if (part && !XLIFF_EXTENSIONS.some((e) => part.endsWith(e))) {
|
|
71
|
+
project = part;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { project, locale };
|
|
77
|
+
}
|
|
78
|
+
function isMsgProjectLike(value) {
|
|
79
|
+
if (!value || typeof value !== "object") return false;
|
|
80
|
+
const loc = value.locales?.targetLocales;
|
|
81
|
+
return typeof loc === "object" && loc !== null && !Array.isArray(loc);
|
|
82
|
+
}
|
|
83
|
+
async function importMsgProject(projectsDir, projectName) {
|
|
84
|
+
const basePath = join(projectsDir, projectName);
|
|
85
|
+
const exts = [".ts", ".js"];
|
|
86
|
+
for (const ext of exts) {
|
|
87
|
+
const p = `${basePath}${ext}`;
|
|
88
|
+
if (existsSync(p)) {
|
|
89
|
+
const url = pathToFileURL(p).href;
|
|
90
|
+
const mod = await dynamicImportFromUrl(url);
|
|
91
|
+
const project = mod?.default ?? mod;
|
|
92
|
+
if (!isMsgProjectLike(project)) {
|
|
93
|
+
throw new Error(`Project file '${projectName}${ext}' does not export a valid MsgProject.`);
|
|
94
|
+
}
|
|
95
|
+
return project;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
throw new Error(`Project file not found: ${projectName}`);
|
|
99
|
+
}
|
|
100
|
+
var xmlParser = new XMLParser({
|
|
101
|
+
ignoreAttributes: false,
|
|
102
|
+
attributeNamePrefix: "@_"
|
|
103
|
+
});
|
|
104
|
+
function parseXliff20(xml) {
|
|
105
|
+
try {
|
|
106
|
+
const parsed = xmlParser.parse(xml);
|
|
107
|
+
if (!parsed || typeof parsed !== "object") {
|
|
108
|
+
throw new Error("Failed to parse XLIFF");
|
|
109
|
+
}
|
|
110
|
+
return parsed;
|
|
111
|
+
} catch (err) {
|
|
112
|
+
const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
|
|
113
|
+
throw new Error(`Malformed XLIFF 2.0: ${message}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function categoryToNoteType(category) {
|
|
117
|
+
const lower = category.toLowerCase();
|
|
118
|
+
if (lower.startsWith("x-")) return category.toUpperCase();
|
|
119
|
+
const map = {
|
|
120
|
+
description: "DESCRIPTION",
|
|
121
|
+
authorship: "AUTHORSHIP",
|
|
122
|
+
parameters: "PARAMETERS",
|
|
123
|
+
context: "CONTEXT",
|
|
124
|
+
comment: "COMMENT"
|
|
125
|
+
};
|
|
126
|
+
return map[lower] ?? category.toUpperCase();
|
|
127
|
+
}
|
|
128
|
+
function extractSegmentText(segment) {
|
|
129
|
+
if (segment == null) return "";
|
|
130
|
+
const seg = segment;
|
|
131
|
+
const target = seg.target ?? seg.source;
|
|
132
|
+
if (typeof target === "string") return target;
|
|
133
|
+
if (target && typeof target === "object") {
|
|
134
|
+
return extractInlineText(target);
|
|
135
|
+
}
|
|
136
|
+
return "";
|
|
137
|
+
}
|
|
138
|
+
function extractInlineText(node) {
|
|
139
|
+
let text = "";
|
|
140
|
+
if ("#text" in node && typeof node["#text"] === "string") {
|
|
141
|
+
text += node["#text"];
|
|
142
|
+
}
|
|
143
|
+
const children = ["pc", "sc", "ec", "ph", "mrk", "sm", "em", "cp", "target", "source"];
|
|
144
|
+
for (const tag of children) {
|
|
145
|
+
const child = node[tag];
|
|
146
|
+
if (Array.isArray(child)) {
|
|
147
|
+
for (const c of child) {
|
|
148
|
+
const childObj = typeof c === "object" && c !== null ? c : {};
|
|
149
|
+
text += extractInlineText(childObj);
|
|
150
|
+
}
|
|
151
|
+
} else if (child && typeof child === "object") {
|
|
152
|
+
text += extractInlineText(child);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return text;
|
|
156
|
+
}
|
|
157
|
+
function processUnitsFromItems(units, fileAttrs) {
|
|
158
|
+
const messages = [];
|
|
159
|
+
for (const u of units) {
|
|
160
|
+
const msg = processUnit(u, fileAttrs);
|
|
161
|
+
if (msg) messages.push(msg);
|
|
162
|
+
}
|
|
163
|
+
return messages;
|
|
164
|
+
}
|
|
165
|
+
function processGroupsForMessages(groups, fileAttrs) {
|
|
166
|
+
const messages = [];
|
|
167
|
+
for (const item of groups) {
|
|
168
|
+
const obj = item;
|
|
169
|
+
if (obj.unit) {
|
|
170
|
+
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
171
|
+
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
172
|
+
}
|
|
173
|
+
if (obj.group) {
|
|
174
|
+
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
175
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return messages;
|
|
179
|
+
}
|
|
180
|
+
function processUnit(unit, fileAttrs) {
|
|
181
|
+
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
182
|
+
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
183
|
+
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
184
|
+
const translate = (unit["@_translate"] ?? "yes").toLowerCase();
|
|
185
|
+
const dnt = translate === "no" || translate === "false";
|
|
186
|
+
const fileTranslate = (fileAttrs.translate ?? "yes").toLowerCase();
|
|
187
|
+
const fileDnt = fileTranslate === "no" || fileTranslate === "false";
|
|
188
|
+
const notes = [];
|
|
189
|
+
const notesEl = unit.notes;
|
|
190
|
+
if (notesEl?.note) {
|
|
191
|
+
const noteArr = Array.isArray(notesEl.note) ? notesEl.note : [notesEl.note];
|
|
192
|
+
for (const n of noteArr) {
|
|
193
|
+
const note = n;
|
|
194
|
+
const category = note["@_category"] ?? "comment";
|
|
195
|
+
const content = note["#text"] ?? "";
|
|
196
|
+
notes.push({ type: categoryToNoteType(category), content });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
200
|
+
const targetParts = [];
|
|
201
|
+
for (const seg of segments) {
|
|
202
|
+
targetParts.push(extractSegmentText(seg));
|
|
203
|
+
}
|
|
204
|
+
const value = targetParts.join("");
|
|
205
|
+
const attributes = {};
|
|
206
|
+
if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
|
|
207
|
+
if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
|
|
208
|
+
if (dnt !== fileDnt) attributes.dnt = dnt;
|
|
209
|
+
return {
|
|
210
|
+
key: name,
|
|
211
|
+
value,
|
|
212
|
+
attributes,
|
|
213
|
+
notes
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
217
|
+
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
218
|
+
if (!trgLang) return null;
|
|
219
|
+
if (!targetLocales.includes(trgLang)) return null;
|
|
220
|
+
const original = fileEl["@_original"] ?? "unknown.json";
|
|
221
|
+
const title = basename(original, extname(original)) || original;
|
|
222
|
+
const trgDir = fileEl["@_trgDir"];
|
|
223
|
+
const translate = (fileEl["@_translate"] ?? "yes").toLowerCase();
|
|
224
|
+
const dnt = translate === "no" || translate === "false";
|
|
225
|
+
const attributes = {
|
|
226
|
+
lang: trgLang,
|
|
227
|
+
dir: trgDir ?? "",
|
|
228
|
+
dnt
|
|
229
|
+
};
|
|
230
|
+
const notes = [];
|
|
231
|
+
const notesEl = fileEl.notes;
|
|
232
|
+
if (notesEl?.note) {
|
|
233
|
+
const noteArr = Array.isArray(notesEl.note) ? notesEl.note : [notesEl.note];
|
|
234
|
+
for (const n of noteArr) {
|
|
235
|
+
const note = n;
|
|
236
|
+
const category = note["@_category"] ?? "comment";
|
|
237
|
+
const content = note["#text"] ?? "";
|
|
238
|
+
notes.push({ type: categoryToNoteType(category), content });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const fileAttrs = {
|
|
242
|
+
trgLang,
|
|
243
|
+
trgDir,
|
|
244
|
+
translate
|
|
245
|
+
};
|
|
246
|
+
const allMessages = [
|
|
247
|
+
...processUnitsFromItems(
|
|
248
|
+
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
249
|
+
fileAttrs
|
|
250
|
+
),
|
|
251
|
+
...processGroupsForMessages(
|
|
252
|
+
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
253
|
+
fileAttrs
|
|
254
|
+
)
|
|
255
|
+
];
|
|
256
|
+
const resourceData = {
|
|
257
|
+
title,
|
|
258
|
+
attributes,
|
|
259
|
+
notes,
|
|
260
|
+
messages: allMessages.map((m) => ({
|
|
261
|
+
key: m.key,
|
|
262
|
+
value: m.value,
|
|
263
|
+
attributes: m.attributes,
|
|
264
|
+
notes: m.notes.length > 0 ? m.notes : void 0
|
|
265
|
+
}))
|
|
266
|
+
};
|
|
267
|
+
const proj = project;
|
|
268
|
+
return MsgResource.create(resourceData, proj);
|
|
269
|
+
}
|
|
270
|
+
async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
271
|
+
let project;
|
|
272
|
+
try {
|
|
273
|
+
project = await importMsgProject(projectsDir, projectName);
|
|
274
|
+
} catch {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const targetLocales = Object.keys(project.locales.targetLocales);
|
|
278
|
+
if (!targetLocales.includes(locale)) return null;
|
|
279
|
+
const xml = await readFile(xliffPath, "utf-8");
|
|
280
|
+
const parsed = parseXliff20(xml);
|
|
281
|
+
const xliffRoot = parsed.xliff;
|
|
282
|
+
if (!xliffRoot) return null;
|
|
283
|
+
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
284
|
+
if (!xliffTrgLang) return null;
|
|
285
|
+
const fileEls = xliffRoot.file;
|
|
286
|
+
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
287
|
+
const resources = [];
|
|
288
|
+
for (const fileEl of files) {
|
|
289
|
+
const resource = extractResourceFromXliffFile(
|
|
290
|
+
fileEl,
|
|
291
|
+
xliffTrgLang,
|
|
292
|
+
project,
|
|
293
|
+
targetLocales
|
|
294
|
+
);
|
|
295
|
+
if (resource) {
|
|
296
|
+
const json = resource.toJSON(true);
|
|
297
|
+
resources.push({ title: resource.title, json });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (resources.length === 0) return null;
|
|
301
|
+
return { project: projectName, locale, resources };
|
|
302
|
+
}
|
|
303
|
+
async function writeTranslationFiles(translationsDir, result) {
|
|
304
|
+
const { project, locale, resources } = result;
|
|
305
|
+
const projectDir = join(translationsDir, project);
|
|
306
|
+
const localeDir = join(projectDir, locale);
|
|
307
|
+
await mkdir(localeDir, { recursive: true });
|
|
308
|
+
for (const { title, json } of resources) {
|
|
309
|
+
const filePath = join(localeDir, `${title}.json`);
|
|
310
|
+
await writeFile(filePath, json, "utf-8");
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export {
|
|
315
|
+
findXliffFilePaths,
|
|
316
|
+
filterXliffPathsByProject,
|
|
317
|
+
filterXliffPathsByLocale,
|
|
318
|
+
parseXliffFilename,
|
|
319
|
+
importMsgProject,
|
|
320
|
+
parseXliff20,
|
|
321
|
+
extractResourceFromXliffFile,
|
|
322
|
+
processXliffFile,
|
|
323
|
+
writeTranslationFiles
|
|
324
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
loadPackageJsonForMsg
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-F7XZRKP7.mjs";
|
|
4
4
|
|
|
5
5
|
// src/lib/create-resource-helpers.ts
|
|
6
6
|
import { existsSync, mkdirSync, writeFileSync } from "fs";
|
|
@@ -79,7 +79,7 @@ function generateMsgResourceContent(params) {
|
|
|
79
79
|
key: 'example.message',
|
|
80
80
|
value: 'Example message.',
|
|
81
81
|
notes: [
|
|
82
|
-
{ type: '
|
|
82
|
+
{ type: 'DESCRIPTION', content: 'This is an example message. You can delete it.' }
|
|
83
83
|
]
|
|
84
84
|
}
|
|
85
85
|
]`;
|
|
@@ -188,11 +188,14 @@ var CreateProject = class _CreateProject extends import_core.Command {
|
|
|
188
188
|
if (!projectName?.trim()) {
|
|
189
189
|
this.error("projectName is required.", { exit: 1 });
|
|
190
190
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
191
|
+
const useExtend = Boolean(flags.extend?.trim());
|
|
192
|
+
if (!useExtend) {
|
|
193
|
+
if (!source?.trim()) {
|
|
194
|
+
this.error("source locale is required.", { exit: 1 });
|
|
195
|
+
}
|
|
196
|
+
if (!targets?.length || targets.every((t) => !t?.trim())) {
|
|
197
|
+
this.error("At least one target locale is required.", { exit: 1 });
|
|
198
|
+
}
|
|
196
199
|
}
|
|
197
200
|
const cwd = process.cwd();
|
|
198
201
|
const pkgPath = findPackageJsonPath(cwd);
|
|
@@ -212,15 +215,16 @@ var CreateProject = class _CreateProject extends import_core.Command {
|
|
|
212
215
|
const projectsDir = (0, import_path3.join)(rootDir, i18nDir, "projects");
|
|
213
216
|
const translationsDir = (0, import_path3.join)(rootDir, l10nDir, "translations");
|
|
214
217
|
const relPath = calculateRelativePath(projectsDir, translationsDir);
|
|
215
|
-
const useTypeScript = (0, import_fs3.existsSync)((0, import_path3.join)(rootDir, "tsconfig.json"));
|
|
216
218
|
const isEsm = pkg.type === "module";
|
|
217
|
-
const ext =
|
|
219
|
+
const ext = ".js";
|
|
218
220
|
const outPath = (0, import_path3.join)(projectsDir, `${projectName}${ext}`);
|
|
219
221
|
if ((0, import_fs3.existsSync)(outPath)) {
|
|
220
222
|
this.error(`A project with the name '${projectName}' already exists.`, { exit: 1 });
|
|
221
223
|
}
|
|
222
224
|
let targetLocales = {};
|
|
223
225
|
let pseudoLocale = "en-XA";
|
|
226
|
+
let resolvedSource = source?.trim();
|
|
227
|
+
const hasUserSourceAndTargets = Boolean(resolvedSource && targets?.length && targets.some((t) => t?.trim()));
|
|
224
228
|
if (flags.extend) {
|
|
225
229
|
const base = await importMsgProjectFile(projectsDir, flags.extend);
|
|
226
230
|
if (!base) {
|
|
@@ -232,10 +236,18 @@ var CreateProject = class _CreateProject extends import_core.Command {
|
|
|
232
236
|
if (base.locales?.pseudoLocale) {
|
|
233
237
|
pseudoLocale = base.locales.pseudoLocale;
|
|
234
238
|
}
|
|
239
|
+
if (!hasUserSourceAndTargets) {
|
|
240
|
+
resolvedSource = base.locales?.sourceLocale ?? "";
|
|
241
|
+
if (!resolvedSource) {
|
|
242
|
+
this.error("Base project has no sourceLocale. Provide source and targets explicitly.", { exit: 1 });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
235
245
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
246
|
+
if (hasUserSourceAndTargets) {
|
|
247
|
+
targetLocales[resolvedSource] = [resolvedSource];
|
|
248
|
+
for (const t of targets) {
|
|
249
|
+
if (t?.trim()) targetLocales[t.trim()] = [t.trim()];
|
|
250
|
+
}
|
|
239
251
|
}
|
|
240
252
|
const loaderPathLine = "const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;";
|
|
241
253
|
const loaderWarnLine = "console.warn(`Translations for locale ${language} could not be loaded.`, error);";
|
|
@@ -263,7 +275,7 @@ const loader = async (project, title, language) => {
|
|
|
263
275
|
export default MsgProject.create({
|
|
264
276
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
265
277
|
locales: {
|
|
266
|
-
sourceLocale: ${JSON.stringify(
|
|
278
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
267
279
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
268
280
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
269
281
|
},
|
|
@@ -279,7 +291,7 @@ const loader = async (project, title, language) => {
|
|
|
279
291
|
module.exports = MsgProject.create({
|
|
280
292
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
281
293
|
locales: {
|
|
282
|
-
sourceLocale: ${JSON.stringify(
|
|
294
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
283
295
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
284
296
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
285
297
|
},
|
|
@@ -3,11 +3,11 @@ import {
|
|
|
3
3
|
importMsgProjectFile,
|
|
4
4
|
loadPackageJsonForCreateProject,
|
|
5
5
|
writeMsgProjectFile
|
|
6
|
-
} from "../../chunk-
|
|
7
|
-
import "../../chunk-
|
|
6
|
+
} from "../../chunk-6A6JGF7U.mjs";
|
|
7
|
+
import "../../chunk-LNCUPVEF.mjs";
|
|
8
8
|
import {
|
|
9
9
|
findPackageJsonPath
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-F7XZRKP7.mjs";
|
|
11
11
|
|
|
12
12
|
// src/commands/create/project.ts
|
|
13
13
|
import { Args, Command, Flags } from "@oclif/core";
|
|
@@ -43,11 +43,14 @@ var CreateProject = class _CreateProject extends Command {
|
|
|
43
43
|
if (!projectName?.trim()) {
|
|
44
44
|
this.error("projectName is required.", { exit: 1 });
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
const useExtend = Boolean(flags.extend?.trim());
|
|
47
|
+
if (!useExtend) {
|
|
48
|
+
if (!source?.trim()) {
|
|
49
|
+
this.error("source locale is required.", { exit: 1 });
|
|
50
|
+
}
|
|
51
|
+
if (!targets?.length || targets.every((t) => !t?.trim())) {
|
|
52
|
+
this.error("At least one target locale is required.", { exit: 1 });
|
|
53
|
+
}
|
|
51
54
|
}
|
|
52
55
|
const cwd = process.cwd();
|
|
53
56
|
const pkgPath = findPackageJsonPath(cwd);
|
|
@@ -67,15 +70,16 @@ var CreateProject = class _CreateProject extends Command {
|
|
|
67
70
|
const projectsDir = join(rootDir, i18nDir, "projects");
|
|
68
71
|
const translationsDir = join(rootDir, l10nDir, "translations");
|
|
69
72
|
const relPath = calculateRelativePath(projectsDir, translationsDir);
|
|
70
|
-
const useTypeScript = existsSync(join(rootDir, "tsconfig.json"));
|
|
71
73
|
const isEsm = pkg.type === "module";
|
|
72
|
-
const ext =
|
|
74
|
+
const ext = ".js";
|
|
73
75
|
const outPath = join(projectsDir, `${projectName}${ext}`);
|
|
74
76
|
if (existsSync(outPath)) {
|
|
75
77
|
this.error(`A project with the name '${projectName}' already exists.`, { exit: 1 });
|
|
76
78
|
}
|
|
77
79
|
let targetLocales = {};
|
|
78
80
|
let pseudoLocale = "en-XA";
|
|
81
|
+
let resolvedSource = source?.trim();
|
|
82
|
+
const hasUserSourceAndTargets = Boolean(resolvedSource && targets?.length && targets.some((t) => t?.trim()));
|
|
79
83
|
if (flags.extend) {
|
|
80
84
|
const base = await importMsgProjectFile(projectsDir, flags.extend);
|
|
81
85
|
if (!base) {
|
|
@@ -87,10 +91,18 @@ var CreateProject = class _CreateProject extends Command {
|
|
|
87
91
|
if (base.locales?.pseudoLocale) {
|
|
88
92
|
pseudoLocale = base.locales.pseudoLocale;
|
|
89
93
|
}
|
|
94
|
+
if (!hasUserSourceAndTargets) {
|
|
95
|
+
resolvedSource = base.locales?.sourceLocale ?? "";
|
|
96
|
+
if (!resolvedSource) {
|
|
97
|
+
this.error("Base project has no sourceLocale. Provide source and targets explicitly.", { exit: 1 });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
90
100
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
101
|
+
if (hasUserSourceAndTargets) {
|
|
102
|
+
targetLocales[resolvedSource] = [resolvedSource];
|
|
103
|
+
for (const t of targets) {
|
|
104
|
+
if (t?.trim()) targetLocales[t.trim()] = [t.trim()];
|
|
105
|
+
}
|
|
94
106
|
}
|
|
95
107
|
const loaderPathLine = "const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;";
|
|
96
108
|
const loaderWarnLine = "console.warn(`Translations for locale ${language} could not be loaded.`, error);";
|
|
@@ -118,7 +130,7 @@ const loader = async (project, title, language) => {
|
|
|
118
130
|
export default MsgProject.create({
|
|
119
131
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
120
132
|
locales: {
|
|
121
|
-
sourceLocale: ${JSON.stringify(
|
|
133
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
122
134
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
123
135
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
124
136
|
},
|
|
@@ -134,7 +146,7 @@ const loader = async (project, title, language) => {
|
|
|
134
146
|
module.exports = MsgProject.create({
|
|
135
147
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
136
148
|
locales: {
|
|
137
|
-
sourceLocale: ${JSON.stringify(
|
|
149
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
138
150
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
139
151
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
140
152
|
},
|
|
@@ -180,7 +180,7 @@ function generateMsgResourceContent(params) {
|
|
|
180
180
|
key: 'example.message',
|
|
181
181
|
value: 'Example message.',
|
|
182
182
|
notes: [
|
|
183
|
-
{ type: '
|
|
183
|
+
{ type: 'DESCRIPTION', content: 'This is an example message. You can delete it.' }
|
|
184
184
|
]
|
|
185
185
|
}
|
|
186
186
|
]`;
|
|
@@ -307,7 +307,7 @@ var CreateResource = class _CreateResource extends import_core.Command {
|
|
|
307
307
|
{ exit: 1 }
|
|
308
308
|
);
|
|
309
309
|
}
|
|
310
|
-
const ext =
|
|
310
|
+
const ext = ".js";
|
|
311
311
|
const outPath = (0, import_path3.join)(resourcesDir, `${title.trim()}.msg${ext}`);
|
|
312
312
|
if ((0, import_fs3.existsSync)(outPath) && !flags.force) {
|
|
313
313
|
this.error(
|
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
importMsgProjectForResource,
|
|
5
5
|
readPackageJsonForCreateResource,
|
|
6
6
|
writeMsgResourceFile
|
|
7
|
-
} from "../../chunk-
|
|
7
|
+
} from "../../chunk-LNCUPVEF.mjs";
|
|
8
8
|
import {
|
|
9
9
|
findPackageJsonPath
|
|
10
|
-
} from "../../chunk-
|
|
10
|
+
} from "../../chunk-F7XZRKP7.mjs";
|
|
11
11
|
|
|
12
12
|
// src/commands/create/resource.ts
|
|
13
13
|
import { Args, Command, Flags } from "@oclif/core";
|
|
@@ -92,7 +92,7 @@ var CreateResource = class _CreateResource extends Command {
|
|
|
92
92
|
{ exit: 1 }
|
|
93
93
|
);
|
|
94
94
|
}
|
|
95
|
-
const ext =
|
|
95
|
+
const ext = ".js";
|
|
96
96
|
const outPath = join(resourcesDir, `${title.trim()}.msg${ext}`);
|
|
97
97
|
if (existsSync(outPath) && !flags.force) {
|
|
98
98
|
this.error(
|