@spyglassmc/mcdoc-cli 0.1.19 → 0.1.20

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,9 @@
1
+ interface Args {
2
+ source: string;
3
+ output: string;
4
+ gzip: boolean;
5
+ verbose: boolean;
6
+ }
7
+ export declare function exportCommand(args: Args): Promise<void>;
8
+ export {};
9
+ //# sourceMappingURL=export.d.ts.map
@@ -0,0 +1,36 @@
1
+ import * as mcdoc from '@spyglassmc/mcdoc';
2
+ import { resolve } from 'path';
3
+ import { pathToFileURL } from 'url';
4
+ import { createLogger, createProject as createProject, sortMaps, writeJson } from '../common.js';
5
+ export async function exportCommand(args) {
6
+ const logger = createLogger(args.verbose);
7
+ const project = await createProject(logger, args.source);
8
+ const data = {
9
+ mcdoc: new Map(),
10
+ 'mcdoc/dispatcher': new Map(),
11
+ };
12
+ const symbols = project.symbols.getVisibleSymbols('mcdoc');
13
+ for (const [name, symbol] of Object.entries(symbols)) {
14
+ if (mcdoc.binder.TypeDefSymbolData.is(symbol.data)) {
15
+ data.mcdoc.set(name, symbol.data.typeDef);
16
+ }
17
+ }
18
+ const dispatchers = project.symbols.getVisibleSymbols('mcdoc/dispatcher');
19
+ for (const [name, symbol] of Object.entries(dispatchers)) {
20
+ const dispatcherMap = new Map();
21
+ data['mcdoc/dispatcher'].set(name, dispatcherMap);
22
+ for (const [id, member] of Object.entries(symbol.members ?? {})) {
23
+ if (mcdoc.binder.TypeDefSymbolData.is(member.data)) {
24
+ dispatcherMap.set(id, member.data.typeDef);
25
+ }
26
+ }
27
+ }
28
+ const output = {
29
+ mcdoc: sortMaps(data.mcdoc),
30
+ 'mcdoc/dispatcher': sortMaps(data['mcdoc/dispatcher']),
31
+ };
32
+ const outputFile = pathToFileURL(resolve(process.cwd(), args.output)).toString();
33
+ await writeJson(outputFile, output, args.gzip);
34
+ await project.close();
35
+ }
36
+ //# sourceMappingURL=export.js.map
@@ -0,0 +1,10 @@
1
+ interface Args {
2
+ source: string;
3
+ output: string;
4
+ upgrade?: boolean;
5
+ gzip: boolean;
6
+ verbose: boolean;
7
+ }
8
+ export declare function localeCommand(args: Args): Promise<void>;
9
+ export {};
10
+ //# sourceMappingURL=locale.d.ts.map
@@ -0,0 +1,144 @@
1
+ import { fileUtil } from '@spyglassmc/core';
2
+ import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js';
3
+ import * as mcdoc from '@spyglassmc/mcdoc';
4
+ import { resolve } from 'path';
5
+ import { pathToFileURL } from 'url';
6
+ import { createLogger, createProject, sortMaps, writeJson } from '../common.js';
7
+ export async function localeCommand(args) {
8
+ const logger = createLogger(args.verbose);
9
+ const project = await createProject(logger, args.source);
10
+ const locale = new Map();
11
+ function add(name, desc) {
12
+ if (locale.has(name)) {
13
+ logger.warn(`Duplicate key ${name}`);
14
+ }
15
+ locale.set(name, desc.replaceAll('\r', '').trim()
16
+ .split('\n\n').map(line => line.trimStart()).join('\n'));
17
+ }
18
+ function collect(name, type) {
19
+ if (type.kind === 'struct') {
20
+ for (const field of type.fields) {
21
+ if (field.kind === 'spread') {
22
+ collect(name, field.type);
23
+ }
24
+ else if (field.desc) {
25
+ const fieldName = typeof field.key === 'string'
26
+ ? `${name}.${field.key}`
27
+ : `${name}.[${field.key.kind}]`;
28
+ add(fieldName, field.desc);
29
+ collect(fieldName, field.type);
30
+ }
31
+ }
32
+ }
33
+ else if (type.kind === 'union') {
34
+ type.members.forEach((member, i) => {
35
+ collect(name, member);
36
+ });
37
+ }
38
+ else if (type.kind === 'enum') {
39
+ // for (const field of type.values) {
40
+ // if (field.desc) {
41
+ // add(`${name}.${field.identifier}`, field.desc)
42
+ // }
43
+ // }
44
+ }
45
+ else if (type.kind === 'list') {
46
+ collect(name, type.item);
47
+ }
48
+ else if (type.kind === 'tuple') {
49
+ for (const item of type.items) {
50
+ collect(name, item);
51
+ }
52
+ }
53
+ else if (type.kind === 'template') {
54
+ collect(name, type.child);
55
+ }
56
+ else if (type.kind === 'concrete') {
57
+ collect(name, type.child);
58
+ for (const arg of type.typeArgs) {
59
+ collect(name, arg);
60
+ }
61
+ }
62
+ else if (type.kind === 'indexed') {
63
+ collect(name, type.child);
64
+ }
65
+ }
66
+ const symbols = project.symbols.getVisibleSymbols('mcdoc');
67
+ for (const [name, symbol] of Object.entries(symbols)) {
68
+ if (name.match(/<anonymous \d+>$/)) {
69
+ continue;
70
+ }
71
+ if (symbol.desc) {
72
+ add(name, symbol.desc);
73
+ }
74
+ if (mcdoc.binder.TypeDefSymbolData.is(symbol.data)) {
75
+ collect(name, symbol.data.typeDef);
76
+ }
77
+ }
78
+ const outputFile = pathToFileURL(resolve(process.cwd(), args.output)).toString();
79
+ if (args.upgrade) {
80
+ const oldLocale = await readLocale(outputFile);
81
+ const outputDir = fileUtil.getParentOfFile(NodeJsExternals, outputFile).toString();
82
+ const entries = await NodeJsExternals.fs.readdir(outputDir);
83
+ const others = entries.filter(e => e.isFile() && e.name.endsWith('.json'))
84
+ .map(e => e.name.slice(0, e.name.length - '.json'.length));
85
+ for (const key of others) {
86
+ const otherFile = `${outputDir}${key}.json`;
87
+ if (otherFile === outputFile) {
88
+ continue;
89
+ }
90
+ logger.info(`Upgrading ${otherFile}`);
91
+ const oldOther = await readLocale(otherFile);
92
+ const other = upgradeLocale(oldOther, oldLocale, locale, logger);
93
+ await writeJson(otherFile, sortMaps(other), args.gzip);
94
+ }
95
+ }
96
+ await writeJson(outputFile, sortMaps(locale), args.gzip);
97
+ await project.close();
98
+ }
99
+ async function readLocale(path) {
100
+ const data = await fileUtil.readJson(NodeJsExternals, path);
101
+ const locale = new Map();
102
+ for (const [key, value] of Object.entries(data ?? {})) {
103
+ if (typeof value === 'string') {
104
+ locale.set(key, value);
105
+ }
106
+ }
107
+ return locale;
108
+ }
109
+ function upgradeLocale(other, oldBase, newBase, logger) {
110
+ const invertedNew = new Map();
111
+ for (const [key, value] of newBase.entries()) {
112
+ const otherKeys = invertedNew.get(value);
113
+ if (otherKeys) {
114
+ invertedNew.set(value, [...otherKeys, key]);
115
+ }
116
+ else {
117
+ invertedNew.set(value, [key]);
118
+ }
119
+ }
120
+ const upgraded = new Map();
121
+ for (const [key, value] of other.entries()) {
122
+ if (newBase.has(key)) {
123
+ // Key exists, base value may have been altered.
124
+ upgraded.set(key, value);
125
+ continue;
126
+ }
127
+ const baseValue = oldBase.get(key);
128
+ if (!baseValue) {
129
+ // Key was already removed previously. Prune.
130
+ continue;
131
+ }
132
+ const possibleKeys = invertedNew.get(baseValue) ?? [];
133
+ if (possibleKeys.length === 1) {
134
+ logger.info('Moved key', key, '->', possibleKeys[0]);
135
+ upgraded.set(possibleKeys[0], value);
136
+ }
137
+ else {
138
+ // Unknown move or removal. Keeping unused key for one cycle.
139
+ upgraded.set(key, value);
140
+ }
141
+ }
142
+ return upgraded;
143
+ }
144
+ //# sourceMappingURL=locale.js.map
@@ -0,0 +1,6 @@
1
+ import * as core from '@spyglassmc/core';
2
+ export declare function createLogger(verbose?: boolean): core.Logger;
3
+ export declare function createProject(logger: core.Logger, root: string): Promise<core.Project>;
4
+ export declare function sortMaps(data: unknown): unknown;
5
+ export declare function writeJson(path: string, data: unknown, gzip: boolean): Promise<void>;
6
+ //# sourceMappingURL=common.d.ts.map
package/lib/common.js ADDED
@@ -0,0 +1,51 @@
1
+ import * as core from '@spyglassmc/core';
2
+ import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js';
3
+ import * as mcdoc from '@spyglassmc/mcdoc';
4
+ import { resolve } from 'path';
5
+ import { pathToFileURL } from 'url';
6
+ export function createLogger(verbose) {
7
+ return {
8
+ log: (...args) => verbose ? console.log(...args) : {},
9
+ info: (...args) => verbose ? console.info(...args) : {},
10
+ warn: (...args) => console.warn(...args),
11
+ error: (...args) => console.error(...args),
12
+ };
13
+ }
14
+ export async function createProject(logger, root) {
15
+ const cacheRoot = resolve(process.cwd(), '.cache');
16
+ const projectRoot = resolve(process.cwd(), root);
17
+ const project = new core.Project({
18
+ logger,
19
+ profilers: new core.ProfilerFactory(logger, [
20
+ 'project#init',
21
+ 'project#ready',
22
+ 'project#ready#bind',
23
+ ]),
24
+ cacheRoot: core.fileUtil.ensureEndingSlash(pathToFileURL(cacheRoot).toString()),
25
+ defaultConfig: core.ConfigService.merge(core.VanillaConfig, {
26
+ env: { dependencies: [] },
27
+ }),
28
+ externals: NodeJsExternals,
29
+ initializers: [mcdoc.initialize],
30
+ projectRoots: [core.fileUtil.ensureEndingSlash(pathToFileURL(projectRoot).toString())],
31
+ });
32
+ await project.ready();
33
+ await project.cacheService.save();
34
+ return project;
35
+ }
36
+ export function sortMaps(data) {
37
+ if (data instanceof Map) {
38
+ return Object.fromEntries([...data.entries()]
39
+ .sort((a, b) => a[0].localeCompare(b[0]))
40
+ .map(([k, v]) => [k, sortMaps(v)]));
41
+ }
42
+ return data;
43
+ }
44
+ export async function writeJson(path, data, gzip) {
45
+ const contents = JSON.stringify(data, undefined, '\t') + '\n';
46
+ await core.fileUtil.writeFile(NodeJsExternals, path, contents);
47
+ if (gzip) {
48
+ await core.fileUtil.writeGzippedJson(NodeJsExternals, `${path}.gz`, data);
49
+ }
50
+ }
51
+ //# sourceMappingURL=common.js.map
package/lib/index.d.ts CHANGED
@@ -1,9 +1,3 @@
1
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
- };
2
+ export {};
9
3
  //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -1,111 +1,38 @@
1
1
  #!/usr/bin/env -S node
2
- import { dirname, join, resolve } from 'path';
3
- import { fileURLToPath, pathToFileURL } from 'url';
4
- import fs from 'fs-extra';
5
- import walk from 'klaw';
6
2
  import yargs from 'yargs';
7
3
  import { hideBin } from 'yargs/helpers';
8
- import { ConfigService, fileUtil, Service, VanillaConfig } from '@spyglassmc/core';
9
- import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js';
10
- import * as mcdoc from '@spyglassmc/mcdoc';
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');
4
+ import { exportCommand } from './commands/export.js';
5
+ import { localeCommand } from './commands/locale.js';
14
6
  const CLI = yargs(hideBin(process.argv));
15
- await CLI.scriptName('mcdoc').command('generate [source]', 'Generate JSON files', () => CLI.positional('source', {
16
- describe: 'path to directory containing mcdoc source.',
17
- type: 'string',
18
- default: '.',
19
- }).options({
20
- 'locale': {
21
- alias: 'l',
22
- description: 'en-us language key-value store of all doc comments.',
7
+ await CLI.scriptName('mcdoc')
8
+ .command('export <output>', 'Generate export dump of mcdoc types.', () => {
9
+ return CLI
10
+ .positional('output', {
11
+ describe: 'file to write the export to',
12
+ type: 'string',
13
+ demandOption: true,
14
+ })
15
+ .option('source', { describe: 'directory containing mcdoc sources', default: '.' })
16
+ .option('gzip', { type: 'boolean', default: false })
17
+ .option('verbose', { alias: 'v', type: 'boolean', default: false });
18
+ }, exportCommand)
19
+ .command('locale <output>', 'Generate and upgrade locales.', () => {
20
+ return CLI
21
+ .positional('output', {
22
+ describe: 'file to write the default locale to',
23
+ type: 'string',
24
+ demandOption: true,
25
+ })
26
+ .option('source', { describe: 'directory containing mcdoc sources', default: '.' })
27
+ .option('upgrade', {
28
+ descript: 'whether to upgrade the other locales in the same directory',
29
+ type: 'boolean',
23
30
  default: false,
24
- },
25
- 'module': {
26
- alias: 'm',
27
- description: 'file tree mirroring definitions; to optimize for web.',
28
- default: false,
29
- },
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 },
33
- }).boolean('locale').boolean('module').boolean('pretty').boolean('verbose').boolean('dry'), async (args) => {
34
- const include = [];
35
- if (args.locale) {
36
- include.push('locales');
37
- }
38
- if (args.module) {
39
- include.push('modules');
40
- }
41
- console.info(`Generating JSON files${args.locale || args.module ? `, including ${include.join(', ')}` : ''}`);
42
- const logger = {
43
- log: (...log_args) => args.verbose ? console.log(...log_args) : false,
44
- warn: (...log_args) => console.warn(...log_args),
45
- error: (...log_args) => console.error(...log_args),
46
- info: (...log_args) => args.verbose ? console.info(...log_args) : false,
47
- trace: (message, ...params) => console.trace(message, ...params),
48
- };
49
- const project_path = resolve(process.cwd(), args.source);
50
- await fileUtil.ensureDir(NodeJsExternals, project_path);
51
- const service = new Service({
52
- logger,
53
- project: {
54
- cacheRoot: fileUtil.ensureEndingSlash(pathToFileURL(cache_root).toString()),
55
- defaultConfig: ConfigService.merge(VanillaConfig, { env: { dependencies: [] } }),
56
- externals: NodeJsExternals,
57
- initializers: [mcdoc.initialize],
58
- projectRoots: [fileUtil.ensureEndingSlash(pathToFileURL(project_path).toString())],
59
- },
60
- });
61
- await service.project.ready();
62
- await service.project.cacheService.save();
63
- const generated_path = join('out', 'generated');
64
- if (args.dry !== true) {
65
- await fs.ensureDir(generated_path);
66
- if (args.module) {
67
- await fs.ensureDir(join(generated_path, 'module'));
68
- }
69
- if (args.locale) {
70
- await fs.ensureDir(join('out', 'locale'));
71
- }
72
- }
73
- const symbols = [];
74
- let locales = {};
75
- let errors = 0;
76
- for await (const doc_file of walk(project_path)) {
77
- if (doc_file.path.endsWith('.mcdoc')) {
78
- const response = await generate(project_path, generated_path, args, doc_file, service, logger);
79
- symbols.push(...response[0]);
80
- locales = { ...locales, ...response[1] };
81
- errors += response[2];
82
- }
83
- }
84
- if (!args.dry) {
85
- await fs.writeFile(join(generated_path, 'generated.mcdoc.json'), JSON.stringify(symbols));
86
- if (args.pretty) {
87
- await fs.writeFile(join(generated_path, 'generated.pretty.mcdoc.json'), JSON.stringify(symbols, undefined, 3));
88
- }
89
- if (args.module) {
90
- await fs.writeFile(join(generated_path, 'module', 'index.json'), JSON.stringify(symbols.map(symbol => symbol.resource)));
91
- }
92
- if (args.locale) {
93
- const locale_path = join('out', 'locale', 'en-us.json');
94
- if (await fs.exists(locale_path)) {
95
- const old_locales = JSON.parse(await fs.readFile(locale_path, 'utf-8'));
96
- await fs.ensureDir(join('out', 'meta'));
97
- await fs.writeFile(join('out', 'meta', 'locale.json'), JSON.stringify({
98
- old_keys: Object.keys(old_locales),
99
- old_values: Object.values(old_locales),
100
- new_keys: Object.keys(locales),
101
- new_values: Object.values(locales),
102
- }, undefined, 3));
103
- }
104
- await fs.writeFile(join('out', 'locale', 'en-us.json'), JSON.stringify(locales, undefined, 3));
105
- }
106
- }
107
- console.log(`Generated JSON files with ${errors} errors.`);
108
- await service.project.close();
109
- }).command('update_locales', 'Attempt automatic upgrade of locales.', update_locales).strict()
110
- .demandCommand(1).parse();
31
+ })
32
+ .option('gzip', { type: 'boolean', default: false })
33
+ .option('verbose', { alias: 'v', type: 'boolean', default: false });
34
+ }, localeCommand)
35
+ .strict()
36
+ .demandCommand(1)
37
+ .parse();
111
38
  //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spyglassmc/mcdoc-cli",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,16 +0,0 @@
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
@@ -1,507 +0,0 @@
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
@@ -1,2 +0,0 @@
1
- export declare function update_locales(): Promise<void>;
2
- //# sourceMappingURL=index.d.ts.map
@@ -1,100 +0,0 @@
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