@worldware/msg-cli 0.1.1 → 0.2.0
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-4Q2252JQ.mjs → chunk-6A6JGF7U.mjs} +2 -2
- package/dist/chunk-BM3O7ZJW.mjs +176 -0
- package/dist/{chunk-DBC2Y6VQ.mjs → chunk-F7XZRKP7.mjs} +6 -6
- package/dist/chunk-KKATA5YE.mjs +324 -0
- package/dist/{chunk-KKYCVD5M.mjs → chunk-LNCUPVEF.mjs} +2 -2
- package/dist/commands/create/project.cjs +23 -10
- package/dist/commands/create/project.mjs +26 -13
- package/dist/commands/create/resource.cjs +1 -1
- package/dist/commands/create/resource.mjs +2 -2
- 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 +6 -6
- 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 +6 -6
- 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);
|
|
@@ -221,6 +224,8 @@ var CreateProject = class _CreateProject extends import_core.Command {
|
|
|
221
224
|
}
|
|
222
225
|
let targetLocales = {};
|
|
223
226
|
let pseudoLocale = "en-XA";
|
|
227
|
+
let resolvedSource = source?.trim();
|
|
228
|
+
const hasUserSourceAndTargets = Boolean(resolvedSource && targets?.length && targets.some((t) => t?.trim()));
|
|
224
229
|
if (flags.extend) {
|
|
225
230
|
const base = await importMsgProjectFile(projectsDir, flags.extend);
|
|
226
231
|
if (!base) {
|
|
@@ -232,10 +237,18 @@ var CreateProject = class _CreateProject extends import_core.Command {
|
|
|
232
237
|
if (base.locales?.pseudoLocale) {
|
|
233
238
|
pseudoLocale = base.locales.pseudoLocale;
|
|
234
239
|
}
|
|
240
|
+
if (!hasUserSourceAndTargets) {
|
|
241
|
+
resolvedSource = base.locales?.sourceLocale ?? "";
|
|
242
|
+
if (!resolvedSource) {
|
|
243
|
+
this.error("Base project has no sourceLocale. Provide source and targets explicitly.", { exit: 1 });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
235
246
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
247
|
+
if (hasUserSourceAndTargets) {
|
|
248
|
+
targetLocales[resolvedSource] = [resolvedSource];
|
|
249
|
+
for (const t of targets) {
|
|
250
|
+
if (t?.trim()) targetLocales[t.trim()] = [t.trim()];
|
|
251
|
+
}
|
|
239
252
|
}
|
|
240
253
|
const loaderPathLine = "const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;";
|
|
241
254
|
const loaderWarnLine = "console.warn(`Translations for locale ${language} could not be loaded.`, error);";
|
|
@@ -263,7 +276,7 @@ const loader = async (project, title, language) => {
|
|
|
263
276
|
export default MsgProject.create({
|
|
264
277
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
265
278
|
locales: {
|
|
266
|
-
sourceLocale: ${JSON.stringify(
|
|
279
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
267
280
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
268
281
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
269
282
|
},
|
|
@@ -279,7 +292,7 @@ const loader = async (project, title, language) => {
|
|
|
279
292
|
module.exports = MsgProject.create({
|
|
280
293
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
281
294
|
locales: {
|
|
282
|
-
sourceLocale: ${JSON.stringify(
|
|
295
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
283
296
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
284
297
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
285
298
|
},
|
|
@@ -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);
|
|
@@ -76,6 +79,8 @@ var CreateProject = class _CreateProject extends Command {
|
|
|
76
79
|
}
|
|
77
80
|
let targetLocales = {};
|
|
78
81
|
let pseudoLocale = "en-XA";
|
|
82
|
+
let resolvedSource = source?.trim();
|
|
83
|
+
const hasUserSourceAndTargets = Boolean(resolvedSource && targets?.length && targets.some((t) => t?.trim()));
|
|
79
84
|
if (flags.extend) {
|
|
80
85
|
const base = await importMsgProjectFile(projectsDir, flags.extend);
|
|
81
86
|
if (!base) {
|
|
@@ -87,10 +92,18 @@ var CreateProject = class _CreateProject extends Command {
|
|
|
87
92
|
if (base.locales?.pseudoLocale) {
|
|
88
93
|
pseudoLocale = base.locales.pseudoLocale;
|
|
89
94
|
}
|
|
95
|
+
if (!hasUserSourceAndTargets) {
|
|
96
|
+
resolvedSource = base.locales?.sourceLocale ?? "";
|
|
97
|
+
if (!resolvedSource) {
|
|
98
|
+
this.error("Base project has no sourceLocale. Provide source and targets explicitly.", { exit: 1 });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
90
101
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
if (hasUserSourceAndTargets) {
|
|
103
|
+
targetLocales[resolvedSource] = [resolvedSource];
|
|
104
|
+
for (const t of targets) {
|
|
105
|
+
if (t?.trim()) targetLocales[t.trim()] = [t.trim()];
|
|
106
|
+
}
|
|
94
107
|
}
|
|
95
108
|
const loaderPathLine = "const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;";
|
|
96
109
|
const loaderWarnLine = "console.warn(`Translations for locale ${language} could not be loaded.`, error);";
|
|
@@ -118,7 +131,7 @@ const loader = async (project, title, language) => {
|
|
|
118
131
|
export default MsgProject.create({
|
|
119
132
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
120
133
|
locales: {
|
|
121
|
-
sourceLocale: ${JSON.stringify(
|
|
134
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
122
135
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
123
136
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
124
137
|
},
|
|
@@ -134,7 +147,7 @@ const loader = async (project, title, language) => {
|
|
|
134
147
|
module.exports = MsgProject.create({
|
|
135
148
|
project: { name: ${JSON.stringify(projectName)}, version: 1 },
|
|
136
149
|
locales: {
|
|
137
|
-
sourceLocale: ${JSON.stringify(
|
|
150
|
+
sourceLocale: ${JSON.stringify(resolvedSource)},
|
|
138
151
|
pseudoLocale: ${JSON.stringify(pseudoLocale)},
|
|
139
152
|
targetLocales: ${JSON.stringify(targetLocales)}
|
|
140
153
|
},
|
|
@@ -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
|
]`;
|
|
@@ -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";
|