@spyglassmc/mcdoc-cli 0.1.7 → 0.1.9

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