@spyglassmc/mcdoc-cli 0.1.6 → 0.1.8
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/lib/generate/index.d.ts +17 -0
- package/lib/generate/index.js +632 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +31 -154
- package/lib/update_locales/index.d.ts +2 -0
- package/lib/update_locales/index.js +105 -0
- package/package.json +5 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AstNode } from '@spyglassmc/core';
|
|
2
|
+
import type { Service } from '@spyglassmc/core';
|
|
3
|
+
import type walk from 'klaw';
|
|
4
|
+
import type { Logger } from '../index.js';
|
|
5
|
+
type Args = {
|
|
6
|
+
locale?: boolean;
|
|
7
|
+
module?: boolean;
|
|
8
|
+
pretty?: boolean;
|
|
9
|
+
verbose?: boolean;
|
|
10
|
+
dry?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function generate(project_path: string, generated_path: string, args: Args, doc_file: walk.Item, service: Service, logger: Logger): Promise<[{
|
|
13
|
+
resource: string;
|
|
14
|
+
children: AstNode[];
|
|
15
|
+
}[], Record<string, string>, number]>;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { join, parse } from 'path';
|
|
2
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import lineColumn from 'line-column';
|
|
5
|
+
export async function generate(project_path, generated_path, args, doc_file, service, logger) {
|
|
6
|
+
const symbols = [];
|
|
7
|
+
let errors = 0;
|
|
8
|
+
const internal_locales = {};
|
|
9
|
+
const locales = {};
|
|
10
|
+
const DocumentUri = pathToFileURL(doc_file.path).toString();
|
|
11
|
+
const doc_contents = await fs.readFile(doc_file.path, 'utf-8');
|
|
12
|
+
await service.project.onDidOpen(DocumentUri, 'mcdoc', 0, doc_contents);
|
|
13
|
+
const check = await service.project.ensureClientManagedChecked(DocumentUri);
|
|
14
|
+
if (check && check.doc && check.node) {
|
|
15
|
+
const { doc, node } = check;
|
|
16
|
+
const path = parse(fileURLToPath(doc.uri));
|
|
17
|
+
let resource = join(path.dir.replace(`${project_path}`, ''), path.name).replace(/^[\/\\]/, '');
|
|
18
|
+
// remove windows cruft
|
|
19
|
+
if (resource.includes('\\'))
|
|
20
|
+
resource = resource.replaceAll('\\', '/');
|
|
21
|
+
logger.info(`parsing ${resource}\n`);
|
|
22
|
+
if (node.children[0]) {
|
|
23
|
+
const children = node.children;
|
|
24
|
+
function flattenChild(parent, self, _parent, _child) {
|
|
25
|
+
const child = {};
|
|
26
|
+
const known_error = false;
|
|
27
|
+
/* @ts-ignore */
|
|
28
|
+
child.self = self;
|
|
29
|
+
/* @ts-ignore */
|
|
30
|
+
if (_child.parent)
|
|
31
|
+
child.parent = parent;
|
|
32
|
+
/* @ts-ignore */
|
|
33
|
+
if (_child.parentMap)
|
|
34
|
+
child.parentMap = parent;
|
|
35
|
+
if (_child.children) {
|
|
36
|
+
child.children = [];
|
|
37
|
+
for (const [i, __child] of Object.entries(_child.children)) {
|
|
38
|
+
/* @ts-ignore */
|
|
39
|
+
child.children[Number(i)] = flattenChild(self, `${self}[${i}]`, _child, __child);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
child.type = _child.type;
|
|
43
|
+
if (child.type === 'resource_location') {
|
|
44
|
+
/* @ts-ignore */
|
|
45
|
+
child.namespace = _child.namespace;
|
|
46
|
+
/* @ts-ignore */
|
|
47
|
+
child.path = _child.path;
|
|
48
|
+
}
|
|
49
|
+
if (Object.hasOwn(_child, 'isOptional')) {
|
|
50
|
+
/* @ts-ignore */
|
|
51
|
+
child.isOptional = _child.isOptional;
|
|
52
|
+
}
|
|
53
|
+
if (Object.hasOwn(_child, 'colorTokenType')) {
|
|
54
|
+
/* @ts-ignore */
|
|
55
|
+
child.colorTokenType = _child.colorTokenType;
|
|
56
|
+
}
|
|
57
|
+
// if you want to keep your sanity, avoid looking at this function
|
|
58
|
+
function setLocale(end) {
|
|
59
|
+
let container;
|
|
60
|
+
if (doc_file.path.endsWith(`${resource.split('/').slice(-1)[0]}.mcdoc`)) {
|
|
61
|
+
const threeBack = _parent?.parent?.parent;
|
|
62
|
+
if (threeBack?.type === 'mcdoc:struct') {
|
|
63
|
+
const identifierIndex = threeBack?.children?.findIndex(child => child.type === 'mcdoc:identifier');
|
|
64
|
+
if (identifierIndex && identifierIndex !== -1) {
|
|
65
|
+
/* @ts-ignore */
|
|
66
|
+
container = threeBack?.children?.[identifierIndex].value;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
/* @ts-ignore */
|
|
70
|
+
const fourBack = threeBack?.parent;
|
|
71
|
+
switch (fourBack?.type) {
|
|
72
|
+
case 'mcdoc:struct/field/pair':
|
|
73
|
+
{
|
|
74
|
+
const key = fourBack.children?.find(child => child.type === 'mcdoc:identifier'
|
|
75
|
+
/* @ts-ignore */
|
|
76
|
+
)?.value;
|
|
77
|
+
const sixBack = fourBack.parent?.parent;
|
|
78
|
+
const foundRoot = sixBack?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
79
|
+
if (key) {
|
|
80
|
+
if (foundRoot) {
|
|
81
|
+
container = `${foundRoot}.${key}`;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// This is another nested anonymous struct
|
|
85
|
+
// TODO: Yeah this should be recursive and smarter but I'm lazy
|
|
86
|
+
const parentKey = sixBack?.parent
|
|
87
|
+
?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
88
|
+
/* @ts-ignore */
|
|
89
|
+
)?.value;
|
|
90
|
+
const actualRoot = sixBack?.parent
|
|
91
|
+
?.parent?.parent?.children?.find(child => child.type ===
|
|
92
|
+
'mcdoc:identifier')?.value;
|
|
93
|
+
container =
|
|
94
|
+
`${actualRoot}.${parentKey}.${key}`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
// This is another nested anonymous struct
|
|
99
|
+
// TODO: Yeah this should be recursive and smarter but I'm lazy
|
|
100
|
+
const parentKey = sixBack?.parent?.children
|
|
101
|
+
?.find(child => child.type === 'mcdoc:identifier'
|
|
102
|
+
/* @ts-ignore */
|
|
103
|
+
)?.value;
|
|
104
|
+
const actualRoot = sixBack?.parent?.parent
|
|
105
|
+
?.parent?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
106
|
+
/* @ts-ignore */
|
|
107
|
+
)?.value;
|
|
108
|
+
container =
|
|
109
|
+
`${actualRoot}.${parentKey}.__map_key.__struct`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
case 'mcdoc:type/union':
|
|
114
|
+
{
|
|
115
|
+
switch (fourBack?.parent?.type) {
|
|
116
|
+
case 'mcdoc:struct/field/spread':
|
|
117
|
+
{
|
|
118
|
+
const root = fourBack?.parent?.parent
|
|
119
|
+
?.parent?.children?.find(child => child.type ===
|
|
120
|
+
'mcdoc:identifier'
|
|
121
|
+
/* @ts-ignore */
|
|
122
|
+
)?.value;
|
|
123
|
+
// java/server/world/entity/mob/breedable/llama.[0][2][4][2][9][0][0][1][0][0][0]
|
|
124
|
+
// java/server/world/entity/mob/breedable/llama.[0][2][4][2][9][0][1][1][0][0][0]
|
|
125
|
+
if (root) {
|
|
126
|
+
container =
|
|
127
|
+
`${root}.__spread.__union.__struct_${self.split('][').slice(-4)[0]}`; // THIS IS REALLY REALLY BAD
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
logger.warn('Could not find root for union spread');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case 'mcdoc:struct/field/pair':
|
|
135
|
+
{
|
|
136
|
+
const parentKey = fourBack?.parent
|
|
137
|
+
?.children?.find(child => child.type ===
|
|
138
|
+
'mcdoc:identifier'
|
|
139
|
+
/* @ts-ignore */
|
|
140
|
+
)?.value;
|
|
141
|
+
const root = fourBack?.parent?.parent
|
|
142
|
+
?.parent?.children?.find(child => child.type ===
|
|
143
|
+
'mcdoc:identifier'
|
|
144
|
+
/* @ts-ignore */
|
|
145
|
+
)?.value;
|
|
146
|
+
container =
|
|
147
|
+
`${root}.${parentKey}.__union.__struct_${self.split('][').slice(-2)[0]}`; // THIS IS REALLY REALLY BAD
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
case 'mcdoc:dispatch_statement':
|
|
151
|
+
{
|
|
152
|
+
const registry = fourBack.parent
|
|
153
|
+
.children?.find(child => child.type ===
|
|
154
|
+
'resource_location'
|
|
155
|
+
/* @ts-ignore */
|
|
156
|
+
)?.path?.join('_');
|
|
157
|
+
const key = fourBack.parent.children
|
|
158
|
+
?.find(child => child.type ===
|
|
159
|
+
'mcdoc:index_body'
|
|
160
|
+
/* @ts-ignore */
|
|
161
|
+
)?.children?.[0]?.value;
|
|
162
|
+
const indexGuess = self.split('][').slice(-6)[0]; // THIS IS REALLY REALLY BAD
|
|
163
|
+
if (indexGuess === '45') {
|
|
164
|
+
container =
|
|
165
|
+
`__dispatch.${registry}.${key}.__union.__struct_${self.split('][').slice(-4)[0]}`;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
container =
|
|
169
|
+
`__dispatch.${registry}.${key}.__union.__struct_${indexGuess}`;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
case 'mcdoc:type_alias':
|
|
174
|
+
{
|
|
175
|
+
container = fourBack?.parent?.children
|
|
176
|
+
?.find(child => child.type ===
|
|
177
|
+
'mcdoc:identifier'
|
|
178
|
+
/* @ts-ignore */
|
|
179
|
+
)?.value;
|
|
180
|
+
}
|
|
181
|
+
break;
|
|
182
|
+
default: {
|
|
183
|
+
// TODO: there's more cases here, but I'm done for now
|
|
184
|
+
// console.log('aa ', fourBack?.parent?.type)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
break;
|
|
189
|
+
case 'mcdoc:type/list':
|
|
190
|
+
{
|
|
191
|
+
const fiveBack = fourBack?.parent;
|
|
192
|
+
switch (fiveBack?.type) {
|
|
193
|
+
case 'mcdoc:struct/field/pair':
|
|
194
|
+
{
|
|
195
|
+
const key = fiveBack.children?.find(child => child.type ===
|
|
196
|
+
'mcdoc:identifier')?.value;
|
|
197
|
+
const sevenBack = fiveBack.parent
|
|
198
|
+
?.parent;
|
|
199
|
+
const foundRoot = sevenBack?.children
|
|
200
|
+
?.find(child => child.type ===
|
|
201
|
+
'mcdoc:identifier'
|
|
202
|
+
/* @ts-ignore */
|
|
203
|
+
)?.value;
|
|
204
|
+
if (foundRoot) {
|
|
205
|
+
container =
|
|
206
|
+
`${foundRoot}.${key}.__struct_list`;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
// This is another nested anonymous struct
|
|
210
|
+
switch (sevenBack?.parent?.type) {
|
|
211
|
+
case 'mcdoc:type/list':
|
|
212
|
+
{
|
|
213
|
+
const nineBack = sevenBack
|
|
214
|
+
.parent?.parent;
|
|
215
|
+
/* @ts-ignore */
|
|
216
|
+
const parentKey = nineBack
|
|
217
|
+
?.children?.find(child => child.type ===
|
|
218
|
+
'mcdoc:identifier')?.value;
|
|
219
|
+
// Credits husk
|
|
220
|
+
const actualRoot = nineBack?.parent
|
|
221
|
+
?.parent?.parent
|
|
222
|
+
?.parent?.children
|
|
223
|
+
?.find(child => child.type ===
|
|
224
|
+
'mcdoc:identifier'
|
|
225
|
+
/* @ts-ignore */
|
|
226
|
+
)?.value;
|
|
227
|
+
if (actualRoot) {
|
|
228
|
+
container =
|
|
229
|
+
`${actualRoot}.__struct_list.${parentKey}.__struct_list.${key}.__struct_list`;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
logger.warn("Could not find root for a bunch of nested struct lists, probably don't do this");
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
case 'mcdoc:type/union': {
|
|
237
|
+
const actualRoot = sevenBack
|
|
238
|
+
.parent?.parent?.children
|
|
239
|
+
?.find(child => child.type ===
|
|
240
|
+
'mcdoc:identifier'
|
|
241
|
+
/* @ts-ignore */
|
|
242
|
+
)?.value;
|
|
243
|
+
// block state definition, this is fragile
|
|
244
|
+
if (actualRoot) {
|
|
245
|
+
container =
|
|
246
|
+
`${actualRoot}.__struct_union.${key}.__struct_list`;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
logger.warn('Could not find root for struct list within struct union, this should be fixed');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
case 'mcdoc:type_alias':
|
|
257
|
+
{
|
|
258
|
+
const root = fiveBack?.children?.find(child => child.type ===
|
|
259
|
+
'mcdoc:identifier')?.value;
|
|
260
|
+
container = `${root}.__struct_list`;
|
|
261
|
+
}
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
break;
|
|
266
|
+
case 'mcdoc:type_alias':
|
|
267
|
+
{
|
|
268
|
+
const root = fourBack?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
269
|
+
/* @ts-ignore */
|
|
270
|
+
)?.value;
|
|
271
|
+
if (root) {
|
|
272
|
+
container = root;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
logger.warn('Could not find root for type alias, hint:' +
|
|
276
|
+
self);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case 'mcdoc:dispatch_statement':
|
|
281
|
+
{
|
|
282
|
+
// anonymous struct
|
|
283
|
+
container = `__dispatch.__struct${self.split('][').slice(-5)[0]}`; // THIS IS REALLY REALLY BAD
|
|
284
|
+
}
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
// anonymous struct
|
|
288
|
+
switch (fourBack?.type) {
|
|
289
|
+
case 'mcdoc:struct/field/pair':
|
|
290
|
+
{
|
|
291
|
+
const key = fourBack.children?.find(child => child.type === 'mcdoc:identifier'
|
|
292
|
+
/* @ts-ignore */
|
|
293
|
+
)?.value;
|
|
294
|
+
const sixBack = fourBack.parent?.parent;
|
|
295
|
+
const parentKey = sixBack?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
296
|
+
if (key) {
|
|
297
|
+
if (parentKey) {
|
|
298
|
+
container = `${parentKey}.${key}`;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
// memories
|
|
302
|
+
const actualParentKey = sixBack?.parent
|
|
303
|
+
?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
304
|
+
/* @ts-ignore */
|
|
305
|
+
)?.value;
|
|
306
|
+
const root = sixBack?.parent?.parent
|
|
307
|
+
?.parent?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
308
|
+
/* @ts-ignore */
|
|
309
|
+
)?.value;
|
|
310
|
+
container =
|
|
311
|
+
`${root}.${actualParentKey}.${key}`;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
// advancement criteria trigger
|
|
316
|
+
const sevenBack = fourBack?.parent?.parent
|
|
317
|
+
?.parent;
|
|
318
|
+
const parentKey = sevenBack?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
319
|
+
const root = sevenBack?.parent?.parent
|
|
320
|
+
?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
321
|
+
/* @ts-ignore */
|
|
322
|
+
)?.value;
|
|
323
|
+
container =
|
|
324
|
+
`${root}.${parentKey}.__map_key.__struct`;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
break;
|
|
328
|
+
case 'mcdoc:type/union':
|
|
329
|
+
{
|
|
330
|
+
if (!container) { // yes
|
|
331
|
+
switch (fourBack?.parent?.type) {
|
|
332
|
+
case 'mcdoc:type_alias':
|
|
333
|
+
{
|
|
334
|
+
const root = fourBack?.parent
|
|
335
|
+
?.children?.find(child => child.type ===
|
|
336
|
+
'mcdoc:identifier'
|
|
337
|
+
/* @ts-ignore */
|
|
338
|
+
)?.value;
|
|
339
|
+
container =
|
|
340
|
+
`${root}.__union.__struct_${self.split('][').slice(-4)[0]}`; // THIS IS REALLY REALLY BAD
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
case 'mcdoc:struct/field/pair':
|
|
344
|
+
{
|
|
345
|
+
const parentKey = fourBack?.parent
|
|
346
|
+
.children?.find(child => child.type ===
|
|
347
|
+
'mcdoc:identifier'
|
|
348
|
+
/* @ts-ignore */
|
|
349
|
+
)?.value;
|
|
350
|
+
const root = fourBack?.parent
|
|
351
|
+
?.parent?.parent?.children
|
|
352
|
+
?.find(child => child.type ===
|
|
353
|
+
'mcdoc:identifier'
|
|
354
|
+
/* @ts-ignore */
|
|
355
|
+
)?.value;
|
|
356
|
+
container = `${root}.${parentKey}`;
|
|
357
|
+
}
|
|
358
|
+
break;
|
|
359
|
+
case 'mcdoc:dispatch_statement':
|
|
360
|
+
{
|
|
361
|
+
// kill
|
|
362
|
+
const indexGuess = self.split('][').slice(-6)[0];
|
|
363
|
+
if (indexGuess === '45') {
|
|
364
|
+
container =
|
|
365
|
+
`__dispatch.__struct_${self.split('][').slice(-4)[0]}`;
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
container =
|
|
369
|
+
`__dispatch.__struct_${indexGuess}`; // THIS IS REALLY REALLY BAD
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
break;
|
|
373
|
+
case 'mcdoc:type/union':
|
|
374
|
+
{
|
|
375
|
+
// book lines
|
|
376
|
+
const sevenBack = fourBack?.parent
|
|
377
|
+
?.parent?.parent;
|
|
378
|
+
const parentKey = sevenBack
|
|
379
|
+
?.children?.find(child => child.type ===
|
|
380
|
+
'mcdoc:identifier'
|
|
381
|
+
/* @ts-ignore */
|
|
382
|
+
)?.value;
|
|
383
|
+
const root = sevenBack?.parent
|
|
384
|
+
?.parent?.children?.find(child => child.type ===
|
|
385
|
+
'mcdoc:identifier')?.value;
|
|
386
|
+
container =
|
|
387
|
+
`${root}.${parentKey}.__union_list.__struct`;
|
|
388
|
+
}
|
|
389
|
+
break;
|
|
390
|
+
case 'mcdoc:type/list':
|
|
391
|
+
{
|
|
392
|
+
// texture meta
|
|
393
|
+
const parentKey = fourBack?.parent
|
|
394
|
+
.parent?.children?.find(child => child.type ===
|
|
395
|
+
'mcdoc:identifier'
|
|
396
|
+
/* @ts-ignore */
|
|
397
|
+
)?.value;
|
|
398
|
+
const rootKey = fourBack?.parent
|
|
399
|
+
?.parent?.parent?.parent?.parent
|
|
400
|
+
?.children?.find(child => child.type ===
|
|
401
|
+
'mcdoc:identifier'
|
|
402
|
+
/* @ts-ignore */
|
|
403
|
+
)?.value;
|
|
404
|
+
const root = fourBack?.parent
|
|
405
|
+
?.parent?.parent?.parent?.parent
|
|
406
|
+
?.parent?.parent?.children
|
|
407
|
+
?.find(child => child.type ===
|
|
408
|
+
'mcdoc:identifier'
|
|
409
|
+
/* @ts-ignore */
|
|
410
|
+
)?.value;
|
|
411
|
+
container =
|
|
412
|
+
`${root}.${rootKey}.${parentKey}.__union_list.__struct`;
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
const threeBack = _parent?.parent?.parent;
|
|
424
|
+
switch (threeBack?.type) {
|
|
425
|
+
case 'mcdoc:enum':
|
|
426
|
+
{
|
|
427
|
+
const root = threeBack?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
428
|
+
/* @ts-ignore */
|
|
429
|
+
)?.value;
|
|
430
|
+
if (root) {
|
|
431
|
+
container = root;
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
// inline enum
|
|
435
|
+
const parentKey = threeBack?.parent?.children
|
|
436
|
+
?.find(child => child.type === 'mcdoc:identifier'
|
|
437
|
+
/* @ts-ignore */
|
|
438
|
+
)?.value;
|
|
439
|
+
const root = threeBack?.parent?.parent?.parent
|
|
440
|
+
?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
441
|
+
/* @ts-ignore */
|
|
442
|
+
)?.value;
|
|
443
|
+
if (root) {
|
|
444
|
+
container = `${root}.${parentKey}`;
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
const rootKey = threeBack?.parent?.parent
|
|
448
|
+
?.parent?.parent?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
449
|
+
/* @ts-ignore */
|
|
450
|
+
)?.value;
|
|
451
|
+
const root = threeBack?.parent?.parent
|
|
452
|
+
?.parent?.parent?.parent?.parent
|
|
453
|
+
?.children?.find(child => child.type === 'mcdoc:identifier'
|
|
454
|
+
/* @ts-ignore */
|
|
455
|
+
)?.value;
|
|
456
|
+
container =
|
|
457
|
+
`${root}.${rootKey}.${parentKey}`;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
case 'file':
|
|
463
|
+
{
|
|
464
|
+
switch (_parent?.type) {
|
|
465
|
+
case 'mcdoc:struct':
|
|
466
|
+
{
|
|
467
|
+
container = _parent?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
case 'mcdoc:dispatch_statement':
|
|
471
|
+
{
|
|
472
|
+
if (_parent?.children) {
|
|
473
|
+
container = _parent?.children?.find(child => child.type ===
|
|
474
|
+
'resource_location')?.path?.join('_');
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
break;
|
|
478
|
+
case 'mcdoc:enum':
|
|
479
|
+
{
|
|
480
|
+
container = _parent?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
481
|
+
}
|
|
482
|
+
break;
|
|
483
|
+
case 'mcdoc:type_alias':
|
|
484
|
+
{
|
|
485
|
+
container = _parent?.children?.find(child => child.type === 'mcdoc:identifier')?.value;
|
|
486
|
+
}
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
break;
|
|
491
|
+
case 'mcdoc:struct/field/pair': {
|
|
492
|
+
const key = threeBack.children?.find(child => child.type === 'mcdoc:identifier'
|
|
493
|
+
/* @ts-ignore */
|
|
494
|
+
)?.value;
|
|
495
|
+
const root = threeBack.parent?.parent?.children
|
|
496
|
+
?.find(child => child.type === 'mcdoc:identifier')
|
|
497
|
+
/* @ts-ignore */
|
|
498
|
+
?.value;
|
|
499
|
+
container = `${root}.${key}`;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const key = `mcdoc.${resource.replace(/\//g, '.')}${container ? `.${container}` : ''}.${end}`;
|
|
505
|
+
let value = internal_locales[parent].join('').trimEnd();
|
|
506
|
+
/// remove windows cruft
|
|
507
|
+
if (value.includes('\r'))
|
|
508
|
+
value = value.replaceAll('\r', '');
|
|
509
|
+
locales[key] = value;
|
|
510
|
+
}
|
|
511
|
+
/* @ts-ignore */
|
|
512
|
+
if (Object.hasOwn(_child, 'value')) {
|
|
513
|
+
/* @ts-ignore */
|
|
514
|
+
child.value = _child.value;
|
|
515
|
+
if (internal_locales[parent]) {
|
|
516
|
+
if (child.value === 'dispatch') {
|
|
517
|
+
const dispatchValue = _parent?.children?.slice(-1)[0];
|
|
518
|
+
if (dispatchValue) {
|
|
519
|
+
if (dispatchValue.type === 'mcdoc:struct') {
|
|
520
|
+
/* @ts-ignore */
|
|
521
|
+
const key = dispatchValue.children?.[1]?.value;
|
|
522
|
+
if (key) {
|
|
523
|
+
setLocale(`__dispatch.${key}`);
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
/// anonymous struct
|
|
527
|
+
setLocale(`__dispatch_${self.split('][').slice(-2)[0]}`); // THIS IS REALLY REALLY BAD
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
const key = `${
|
|
532
|
+
/* @ts-ignore */
|
|
533
|
+
_parent?.children?.[3]?.children?.[0].value}`
|
|
534
|
+
.replace(/[\%\, ]/, '__'); // should be sanitized enough
|
|
535
|
+
setLocale(`__dispatch.${key}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
setLocale(child.value);
|
|
541
|
+
}
|
|
542
|
+
delete internal_locales[parent];
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
if (child.type === 'mcdoc:struct/map_key' &&
|
|
546
|
+
internal_locales[parent]) {
|
|
547
|
+
const attributes = _child.children?.[0]?.children?.[0]?.children;
|
|
548
|
+
if (attributes && attributes.length === 1 &&
|
|
549
|
+
/* @ts-ignore */
|
|
550
|
+
!attributes[0].children && attributes[0].value === 'id') {
|
|
551
|
+
/* @ts-ignore */
|
|
552
|
+
setLocale(_child.children?.[0].children?.[1].value);
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
logger.warn('Could not find good path, using __map_key. hint: ' + self);
|
|
556
|
+
setLocale('__map_key');
|
|
557
|
+
}
|
|
558
|
+
delete internal_locales[parent];
|
|
559
|
+
}
|
|
560
|
+
if (Object.hasOwn(_child, 'comment')) {
|
|
561
|
+
/* @ts-ignore */
|
|
562
|
+
const comment = _child.comment;
|
|
563
|
+
child.comment = comment;
|
|
564
|
+
if (!args.dry && args.locale &&
|
|
565
|
+
_parent?.type === 'mcdoc:doc_comments') {
|
|
566
|
+
const key = parent.replace(/\[\d+\]$/, '');
|
|
567
|
+
if (!internal_locales[key]) {
|
|
568
|
+
internal_locales[key] = [];
|
|
569
|
+
}
|
|
570
|
+
internal_locales[key].push(comment.slice(1));
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
if (_child.hover)
|
|
574
|
+
child.hover = _child.hover;
|
|
575
|
+
if (_child.color)
|
|
576
|
+
child.color = _child.color;
|
|
577
|
+
if (child.type !== 'error')
|
|
578
|
+
return child;
|
|
579
|
+
else {
|
|
580
|
+
errors++;
|
|
581
|
+
const lc = lineColumn(doc_contents);
|
|
582
|
+
function range(range) {
|
|
583
|
+
const start = lc.fromIndex(range.start);
|
|
584
|
+
const end = lc.fromIndex(range.end);
|
|
585
|
+
return `L${start?.line}${start?.col ? `:C${start?.col}` : ''} -> L${end?.line}${end?.col ? `:C${end?.col}` : ''}`;
|
|
586
|
+
}
|
|
587
|
+
if (!known_error) {
|
|
588
|
+
console.warn(`mcdoc error(s):`);
|
|
589
|
+
/* @ts-ignore */
|
|
590
|
+
if (_child.parent?.parserErrors.length !== 0) {
|
|
591
|
+
console.warn(' parser:');
|
|
592
|
+
/* @ts-ignore */
|
|
593
|
+
_child.parent?.parserErrors.forEach(error => {
|
|
594
|
+
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
/* @ts-ignore */
|
|
598
|
+
if (_child.parent?.binderErrors.length !== 0) {
|
|
599
|
+
console.warn(' binder:');
|
|
600
|
+
/* @ts-ignore */
|
|
601
|
+
_child.parent?.binderErrors.forEach(error => {
|
|
602
|
+
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
console.warn(`error @ ${doc_file.path}\n\n`);
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
children.forEach((child, i) => {
|
|
611
|
+
/* @ts-ignore */
|
|
612
|
+
children[i] = flattenChild(resource, `${resource}.[${i}]`, undefined, child);
|
|
613
|
+
});
|
|
614
|
+
const symbol = {
|
|
615
|
+
resource,
|
|
616
|
+
children,
|
|
617
|
+
};
|
|
618
|
+
symbols.push(symbol);
|
|
619
|
+
if (!args.dry && args.module) {
|
|
620
|
+
const dir = parse(join(generated_path, 'module', resource)).dir;
|
|
621
|
+
if (dir !== '')
|
|
622
|
+
await fs.ensureDir(dir);
|
|
623
|
+
await fs.writeFile(join(generated_path, 'module', `${resource}.mcdoc.json`), JSON.stringify(symbol));
|
|
624
|
+
if (args.pretty) {
|
|
625
|
+
await fs.writeFile(join(generated_path, 'module', `${resource}.pretty.mcdoc.json`), JSON.stringify(symbol, undefined, 3));
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return [symbols, locales, errors];
|
|
631
|
+
}
|
|
632
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env -S node
|
|
2
|
+
export type Logger = {
|
|
3
|
+
log: (...log_args: any[]) => void;
|
|
4
|
+
warn: (...log_args: any[]) => void;
|
|
5
|
+
error: (...log_args: any[]) => void;
|
|
6
|
+
info: (...log_args: any[]) => void;
|
|
7
|
+
trace: (message?: any, ...params: any) => void;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env -S node
|
|
2
|
-
import { dirname, join,
|
|
2
|
+
import { dirname, join, resolve } from 'path';
|
|
3
3
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
5
|
import walk from 'klaw';
|
|
6
|
-
import lineColumn from 'line-column';
|
|
7
6
|
import yargs from 'yargs';
|
|
8
7
|
import { hideBin } from 'yargs/helpers';
|
|
9
8
|
import { ConfigService, fileUtil, Service, VanillaConfig, } from '@spyglassmc/core';
|
|
10
9
|
import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js';
|
|
11
10
|
import * as mcdoc from '@spyglassmc/mcdoc';
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
import { generate } from './generate/index.js';
|
|
12
|
+
import { update_locales } from './update_locales/index.js';
|
|
13
|
+
const cache_root = join(dirname(fileURLToPath(import.meta.url)), 'cache');
|
|
14
14
|
const CLI = yargs(hideBin(process.argv));
|
|
15
|
-
function removeWindowsCruft(str) {
|
|
16
|
-
if (str.includes('\r'))
|
|
17
|
-
return str.replaceAll('\r', '');
|
|
18
|
-
return str;
|
|
19
|
-
}
|
|
20
15
|
await CLI.scriptName('mcdoc')
|
|
21
16
|
.command('generate [source]', 'Generate JSON files', () => CLI.positional('source', {
|
|
22
17
|
describe: 'path to directory containing mcdoc source.',
|
|
@@ -63,181 +58,62 @@ await CLI.scriptName('mcdoc')
|
|
|
63
58
|
info: (...log_args) => args.verbose ? console.info(...log_args) : false,
|
|
64
59
|
trace: (message, ...params) => console.trace(message, ...params),
|
|
65
60
|
};
|
|
66
|
-
const
|
|
67
|
-
await fileUtil.ensureDir(NodeJsExternals,
|
|
61
|
+
const project_path = resolve(process.cwd(), args.source);
|
|
62
|
+
await fileUtil.ensureDir(NodeJsExternals, project_path);
|
|
68
63
|
const service = new Service({
|
|
69
64
|
logger,
|
|
70
65
|
project: {
|
|
71
|
-
cacheRoot: fileUtil.ensureEndingSlash(pathToFileURL(
|
|
66
|
+
cacheRoot: fileUtil.ensureEndingSlash(pathToFileURL(cache_root).toString()),
|
|
72
67
|
defaultConfig: ConfigService.merge(VanillaConfig, {
|
|
73
68
|
env: { dependencies: [] },
|
|
74
69
|
}),
|
|
75
70
|
externals: NodeJsExternals,
|
|
76
71
|
initializers: [mcdoc.initialize],
|
|
77
|
-
projectRoot: fileUtil.ensureEndingSlash(pathToFileURL(
|
|
72
|
+
projectRoot: fileUtil.ensureEndingSlash(pathToFileURL(project_path).toString()),
|
|
78
73
|
},
|
|
79
74
|
});
|
|
80
75
|
await service.project.ready();
|
|
81
76
|
await service.project.cacheService.save();
|
|
82
|
-
const
|
|
77
|
+
const generated_path = join('out', 'generated');
|
|
83
78
|
if (args.dry !== true) {
|
|
84
|
-
await fs.ensureDir(
|
|
79
|
+
await fs.ensureDir(generated_path);
|
|
85
80
|
if (args.module) {
|
|
86
|
-
await fs.ensureDir(join(
|
|
81
|
+
await fs.ensureDir(join(generated_path, 'module'));
|
|
87
82
|
}
|
|
88
83
|
if (args.locale) {
|
|
89
84
|
await fs.ensureDir(join('out', 'locale'));
|
|
90
85
|
}
|
|
91
86
|
}
|
|
92
87
|
const symbols = [];
|
|
93
|
-
|
|
94
|
-
const locales = {};
|
|
88
|
+
let locales = {};
|
|
95
89
|
let errors = 0;
|
|
96
|
-
for await (const doc_file of walk(
|
|
90
|
+
for await (const doc_file of walk(project_path)) {
|
|
97
91
|
if (doc_file.path.endsWith('.mcdoc')) {
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (check && check.doc && check.node) {
|
|
103
|
-
const { doc, node } = check;
|
|
104
|
-
const path = parse(fileURLToPath(doc.uri));
|
|
105
|
-
const resource = join(path.dir.replace(`${projectPath}`, ''), path.name).replace(/^\//, '');
|
|
106
|
-
logger.info(`parsing ${resource}\n`);
|
|
107
|
-
if (node.children[0]) {
|
|
108
|
-
const children = node.children;
|
|
109
|
-
function flattenChild(parent, self, _parent, _child) {
|
|
110
|
-
const child = {};
|
|
111
|
-
const known_error = false;
|
|
112
|
-
/* @ts-ignore */
|
|
113
|
-
child.self = self;
|
|
114
|
-
/* @ts-ignore */
|
|
115
|
-
if (_child.parent)
|
|
116
|
-
child.parent = parent;
|
|
117
|
-
/* @ts-ignore */
|
|
118
|
-
if (_child.parentMap)
|
|
119
|
-
child.parentMap = parent;
|
|
120
|
-
if (_child.children) {
|
|
121
|
-
child.children = [];
|
|
122
|
-
for (const [i, __child] of Object.entries(_child.children)) {
|
|
123
|
-
/* @ts-ignore */
|
|
124
|
-
child.children[Number(i)] = flattenChild(self, `${self}[${i}]`, _child, __child);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
child.type = _child.type;
|
|
128
|
-
if (child.type === 'resource_location') {
|
|
129
|
-
/* @ts-ignore */
|
|
130
|
-
child.namespace = _child.namespace;
|
|
131
|
-
/* @ts-ignore */
|
|
132
|
-
child.path = _child.path;
|
|
133
|
-
}
|
|
134
|
-
if (Object.hasOwn(_child, 'isOptional')) {
|
|
135
|
-
/* @ts-ignore */
|
|
136
|
-
child.isOptional = _child.isOptional;
|
|
137
|
-
}
|
|
138
|
-
if (Object.hasOwn(_child, 'colorTokenType')) {
|
|
139
|
-
/* @ts-ignore */
|
|
140
|
-
child.colorTokenType = _child.colorTokenType;
|
|
141
|
-
}
|
|
142
|
-
/* @ts-ignore */
|
|
143
|
-
if (Object.hasOwn(_child, 'value')) {
|
|
144
|
-
/* @ts-ignore */
|
|
145
|
-
child.value = _child.value;
|
|
146
|
-
if (internal_locales[parent]) {
|
|
147
|
-
locales[`mcdoc.${resource.replace(/[\/\\]/g, '.')}.${child.value}`] = removeWindowsCruft(internal_locales[parent].join('').trimEnd());
|
|
148
|
-
delete internal_locales[parent];
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (child.type === 'mcdoc:struct/map_key' &&
|
|
152
|
-
internal_locales[parent]) {
|
|
153
|
-
locales[`mcdoc.${resource.replace(/[\/\\]/g, '.')}.map_key`] = removeWindowsCruft(internal_locales[parent].join('').trimEnd());
|
|
154
|
-
delete internal_locales[parent];
|
|
155
|
-
}
|
|
156
|
-
if (Object.hasOwn(_child, 'comment')) {
|
|
157
|
-
/* @ts-ignore */
|
|
158
|
-
const comment = _child.comment;
|
|
159
|
-
child.comment = comment;
|
|
160
|
-
if (!args.dry && args.locale &&
|
|
161
|
-
_parent?.type === 'mcdoc:doc_comments') {
|
|
162
|
-
const key = parent.replace(/\[\d+\]$/, '');
|
|
163
|
-
if (!internal_locales[key]) {
|
|
164
|
-
internal_locales[key] = [];
|
|
165
|
-
}
|
|
166
|
-
internal_locales[key].push(comment.slice(1));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (_child.hover)
|
|
170
|
-
child.hover = _child.hover;
|
|
171
|
-
if (_child.color)
|
|
172
|
-
child.color = _child.color;
|
|
173
|
-
if (child.type !== 'error')
|
|
174
|
-
return child;
|
|
175
|
-
else {
|
|
176
|
-
errors++;
|
|
177
|
-
const lc = lineColumn(doc_contents);
|
|
178
|
-
function range(range) {
|
|
179
|
-
const start = lc.fromIndex(range.start);
|
|
180
|
-
const end = lc.fromIndex(range.end);
|
|
181
|
-
return `L${start?.line}${start?.col ? `:C${start?.col}` : ''} -> L${end?.line}${end?.col ? `:C${end?.col}` : ''}`;
|
|
182
|
-
}
|
|
183
|
-
if (!known_error) {
|
|
184
|
-
console.warn(`mcdoc error(s):`);
|
|
185
|
-
/* @ts-ignore */
|
|
186
|
-
if (_child.parent?.parserErrors.length !== 0) {
|
|
187
|
-
console.warn(' parser:');
|
|
188
|
-
/* @ts-ignore */
|
|
189
|
-
_child.parent?.parserErrors.forEach(error => {
|
|
190
|
-
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
/* @ts-ignore */
|
|
194
|
-
if (_child.parent?.binderErrors.length !== 0) {
|
|
195
|
-
console.warn(' binder:');
|
|
196
|
-
/* @ts-ignore */
|
|
197
|
-
_child.parent?.binderErrors.forEach(error => {
|
|
198
|
-
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
console.warn(`error @ ${doc_file.path}\n\n`);
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
children.forEach((child, i) => {
|
|
207
|
-
/* @ts-ignore */
|
|
208
|
-
children[i] = flattenChild(resource, `${resource}.[${i}]`, undefined, child);
|
|
209
|
-
});
|
|
210
|
-
const symbol = {
|
|
211
|
-
resource,
|
|
212
|
-
children,
|
|
213
|
-
};
|
|
214
|
-
symbols.push(symbol);
|
|
215
|
-
if (!args.dry && args.module) {
|
|
216
|
-
const dir = parse(join(generated, 'module', resource)).dir;
|
|
217
|
-
if (dir !== '')
|
|
218
|
-
await fs.ensureDir(dir);
|
|
219
|
-
await fs.writeFile(join(generated, 'module', `${resource}.mcdoc.json`), JSON.stringify(symbol));
|
|
220
|
-
if (args.pretty) {
|
|
221
|
-
await fs.writeFile(join(generated, 'module', `${resource}.pretty.mcdoc.json`), JSON.stringify(symbol, undefined, 3));
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
92
|
+
const response = await generate(project_path, generated_path, args, doc_file, service, logger);
|
|
93
|
+
symbols.push(...response[0]);
|
|
94
|
+
locales = { ...locales, ...response[1] };
|
|
95
|
+
errors += response[2];
|
|
226
96
|
}
|
|
227
97
|
}
|
|
228
98
|
if (!args.dry) {
|
|
229
|
-
await fs.writeFile(join(
|
|
99
|
+
await fs.writeFile(join(generated_path, 'generated.mcdoc.json'), JSON.stringify(symbols));
|
|
230
100
|
if (args.pretty) {
|
|
231
|
-
await fs.writeFile(join(
|
|
101
|
+
await fs.writeFile(join(generated_path, 'generated.pretty.mcdoc.json'), JSON.stringify(symbols, undefined, 3));
|
|
232
102
|
}
|
|
233
103
|
if (args.module) {
|
|
234
|
-
await fs.writeFile(join(
|
|
104
|
+
await fs.writeFile(join(generated_path, 'module', 'index.json'), JSON.stringify(symbols.map(symbol => symbol.resource)));
|
|
235
105
|
}
|
|
236
106
|
if (args.locale) {
|
|
237
|
-
const
|
|
238
|
-
if (
|
|
239
|
-
|
|
240
|
-
|
|
107
|
+
const locale_path = join('out', 'locale', 'en-us.json');
|
|
108
|
+
if (await fs.exists(locale_path)) {
|
|
109
|
+
const old_locales = JSON.parse(await fs.readFile(locale_path, 'utf-8'));
|
|
110
|
+
await fs.ensureDir(join('out', 'meta'));
|
|
111
|
+
await fs.writeFile(join('out', 'meta', 'locale.json'), JSON.stringify({
|
|
112
|
+
old_keys: Object.keys(old_locales),
|
|
113
|
+
old_values: Object.values(old_locales),
|
|
114
|
+
new_keys: Object.keys(locales),
|
|
115
|
+
new_values: Object.values(locales),
|
|
116
|
+
}, undefined, 3));
|
|
241
117
|
}
|
|
242
118
|
await fs.writeFile(join('out', 'locale', 'en-us.json'), JSON.stringify(locales, undefined, 3));
|
|
243
119
|
}
|
|
@@ -245,6 +121,7 @@ await CLI.scriptName('mcdoc')
|
|
|
245
121
|
console.log(`Generated JSON files with ${errors} errors.`);
|
|
246
122
|
await service.project.close();
|
|
247
123
|
})
|
|
124
|
+
.command('update_locales', 'Attempt automatic upgrade of locales.', update_locales)
|
|
248
125
|
.strict()
|
|
249
126
|
.demandCommand(1)
|
|
250
127
|
.parse();
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import walk from 'klaw';
|
|
4
|
+
import { ofetch } from 'ofetch';
|
|
5
|
+
export async function update_locales() {
|
|
6
|
+
console.log('Reading locale diff');
|
|
7
|
+
const locales = JSON.parse(await fs.readFile(join('out', 'meta', 'locale.json'), 'utf-8'));
|
|
8
|
+
const removed_keys = [];
|
|
9
|
+
const added_keys = [];
|
|
10
|
+
let moved_keys_count = 0;
|
|
11
|
+
const moved_keys = {};
|
|
12
|
+
let changed_values_count = 0;
|
|
13
|
+
const changed_values = [];
|
|
14
|
+
for (let i = 0; i < locales.old_keys.length; i++) {
|
|
15
|
+
let keyRemoved = false;
|
|
16
|
+
const keyIndex = locales.new_keys.indexOf(locales.old_keys[i]);
|
|
17
|
+
if (keyIndex === -1) {
|
|
18
|
+
keyRemoved = true;
|
|
19
|
+
}
|
|
20
|
+
let valueRemoved = false;
|
|
21
|
+
const valueIndex = locales.new_values.indexOf(locales.old_values[i]);
|
|
22
|
+
if (valueIndex === -1) {
|
|
23
|
+
valueRemoved = true;
|
|
24
|
+
}
|
|
25
|
+
if (keyRemoved && valueRemoved) {
|
|
26
|
+
removed_keys.push([locales.old_keys[i], locales.old_values[i]]);
|
|
27
|
+
}
|
|
28
|
+
else if (keyRemoved && !valueRemoved) {
|
|
29
|
+
if (locales.new_values.filter(value => value === locales.old_values[i])
|
|
30
|
+
.length > 1) {
|
|
31
|
+
removed_keys.push([locales.old_keys[i], locales.old_values[i]]);
|
|
32
|
+
console.log('Duplicate key removed:', locales.old_keys[i]);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
// the key was moved
|
|
36
|
+
moved_keys[locales.old_keys[i]] = locales.new_keys[valueIndex];
|
|
37
|
+
moved_keys_count++;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// value changed
|
|
42
|
+
changed_values.push([locales.old_keys[i], [
|
|
43
|
+
locales.old_values[i],
|
|
44
|
+
locales.new_values[keyIndex],
|
|
45
|
+
]]);
|
|
46
|
+
changed_values_count++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
for (let i = 0; i < locales.new_keys.length; i++) {
|
|
50
|
+
if (!moved_keys[locales.new_keys[i]] &&
|
|
51
|
+
!locales.old_keys.includes(locales.new_keys[i])) {
|
|
52
|
+
added_keys.push([locales.new_keys[i], locales.new_values[i]]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console.log(`Removed ${removed_keys.length} keys:`, removed_keys);
|
|
56
|
+
console.log(`Added ${added_keys.length} keys:`, added_keys);
|
|
57
|
+
console.log(`Moved ${moved_keys_count} keys:`, moved_keys);
|
|
58
|
+
console.log(`Changed ${changed_values_count} values:`, changed_values);
|
|
59
|
+
if (moved_keys_count > 0) {
|
|
60
|
+
console.log('Moving keys');
|
|
61
|
+
for await (const locale_file of walk(join('out', 'locale'))) {
|
|
62
|
+
if (!locale_file.path.endsWith('en-us.json')) {
|
|
63
|
+
const locale = JSON.parse(await fs.readFile(locale_file.path, 'utf-8'));
|
|
64
|
+
let have_moved = 0;
|
|
65
|
+
for (const key of Object.keys(moved_keys)) {
|
|
66
|
+
if (locale[key]) {
|
|
67
|
+
locale[moved_keys[key]] = locale[key];
|
|
68
|
+
delete locale[key];
|
|
69
|
+
have_moved++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await fs.writeFile(locale_file.path, JSON.stringify(locale, undefined, 3));
|
|
73
|
+
if (have_moved > 0) {
|
|
74
|
+
console.log(`Moved ${have_moved} keys in ${locale_file.path}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const webhook = process.env.TRANSLATION_UPDATE_WEBHOOK;
|
|
80
|
+
if (changed_values_count > 0 && webhook) {
|
|
81
|
+
console.log('Notifying translators about changed values');
|
|
82
|
+
let description = '';
|
|
83
|
+
for (const [key, [from, to]] of changed_values) {
|
|
84
|
+
const isPeriod = to.slice(-1);
|
|
85
|
+
if (isPeriod !== '.' && from.slice(0, -1) !== to) {
|
|
86
|
+
description +=
|
|
87
|
+
`- \`${key}\` changed, before/after:\n - \`${from}\`\n - \`${to}\`\n`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
await ofetch(webhook, {
|
|
91
|
+
method: 'POST',
|
|
92
|
+
body: {
|
|
93
|
+
content: '',
|
|
94
|
+
embeds: [
|
|
95
|
+
{
|
|
96
|
+
color: 1863349,
|
|
97
|
+
title: 'mcdoc translation key values changed in en-us.json',
|
|
98
|
+
description,
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spyglassmc/mcdoc-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -24,15 +24,17 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"release": "npm publish",
|
|
26
26
|
"release:dry": "npm publish --dry-run",
|
|
27
|
-
"
|
|
27
|
+
"setup": "git clone https://github.com/SpyglassMC/vanilla-mcdoc",
|
|
28
|
+
"test": "rm -rf out/generated/module/* && cd vanilla-mcdoc && git pull && cd .. && tsc -b && node lib/index.js generate vanilla-mcdoc/ -l -m -p"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
31
|
"fs-extra": "^11.1.1",
|
|
31
32
|
"klaw": "^4.1.0",
|
|
32
33
|
"line-column": "^1.0.2",
|
|
34
|
+
"ofetch": "^1.3.4",
|
|
33
35
|
"yargs": "17.6.2",
|
|
34
36
|
"@spyglassmc/core": "0.4.5",
|
|
35
|
-
"@spyglassmc/mcdoc": "0.3.
|
|
37
|
+
"@spyglassmc/mcdoc": "0.3.8"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {
|
|
38
40
|
"@types/fs-extra": "^11.0.2",
|