@spyglassmc/mcdoc-cli 0.1.3 → 0.1.4
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/index.d.mts +3 -0
- package/lib/index.mjs +252 -0
- package/package.json +6 -6
package/lib/index.d.mts
ADDED
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
#!/usr/bin/env -S tsx
|
|
2
|
+
import { dirname, join, parse, resolve } from 'path';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import walk from 'klaw';
|
|
6
|
+
import lineColumn from 'line-column';
|
|
7
|
+
import yargs from 'yargs';
|
|
8
|
+
import { hideBin } from 'yargs/helpers';
|
|
9
|
+
import { ConfigService, fileUtil, Service, VanillaConfig, } from '@spyglassmc/core';
|
|
10
|
+
import { NodeJsExternals } from '@spyglassmc/core/lib/nodejs.js';
|
|
11
|
+
import * as je from '@spyglassmc/java-edition';
|
|
12
|
+
import * as mcdoc from '@spyglassmc/mcdoc';
|
|
13
|
+
const parentPath = dirname(fileURLToPath(import.meta.url));
|
|
14
|
+
const cacheRoot = join(parentPath, 'cache');
|
|
15
|
+
const CLI = yargs(hideBin(process.argv));
|
|
16
|
+
function removeWindowsCruft(str) {
|
|
17
|
+
if (str.includes('\r'))
|
|
18
|
+
return str.replaceAll('\r', '');
|
|
19
|
+
return str;
|
|
20
|
+
}
|
|
21
|
+
await CLI.scriptName('mcdoc')
|
|
22
|
+
.command('generate [source]', 'Generate JSON files', () => CLI.positional('source', {
|
|
23
|
+
describe: 'path to directory containing mcdoc source.',
|
|
24
|
+
type: 'string',
|
|
25
|
+
default: '.',
|
|
26
|
+
}).options({
|
|
27
|
+
'locale': {
|
|
28
|
+
alias: 'l',
|
|
29
|
+
description: 'en-us language key-value store of all doc comments.',
|
|
30
|
+
default: false,
|
|
31
|
+
},
|
|
32
|
+
'module': {
|
|
33
|
+
alias: 'm',
|
|
34
|
+
description: 'file tree mirroring definitions; to optimize for web.',
|
|
35
|
+
default: false,
|
|
36
|
+
},
|
|
37
|
+
'pretty': {
|
|
38
|
+
alias: 'p',
|
|
39
|
+
description: 'pretty printed variants.',
|
|
40
|
+
default: false,
|
|
41
|
+
},
|
|
42
|
+
'verbose': {
|
|
43
|
+
alias: 'v',
|
|
44
|
+
default: false,
|
|
45
|
+
},
|
|
46
|
+
'dry': {
|
|
47
|
+
alias: 'd',
|
|
48
|
+
description: 'will not write to disk.',
|
|
49
|
+
default: false,
|
|
50
|
+
},
|
|
51
|
+
}).boolean('locale').boolean('module').boolean('pretty').boolean('verbose').boolean('dry'), async (args) => {
|
|
52
|
+
const include = [];
|
|
53
|
+
if (args.locale)
|
|
54
|
+
include.push('locales');
|
|
55
|
+
if (args.module)
|
|
56
|
+
include.push('modules');
|
|
57
|
+
console.info(`Generating JSON files${args.locale || args.module
|
|
58
|
+
? `, including ${include.join(', ')}`
|
|
59
|
+
: ''}`);
|
|
60
|
+
const logger = {
|
|
61
|
+
log: (...log_args) => args.verbose ? console.log(...log_args) : false,
|
|
62
|
+
warn: (...log_args) => console.warn(...log_args),
|
|
63
|
+
error: (...log_args) => console.error(...log_args),
|
|
64
|
+
info: (...log_args) => args.verbose ? console.info(...log_args) : false,
|
|
65
|
+
trace: (message, ...params) => console.trace(message, ...params),
|
|
66
|
+
};
|
|
67
|
+
const projectPath = resolve(parentPath, args.source);
|
|
68
|
+
await fileUtil.ensureDir(NodeJsExternals, projectPath);
|
|
69
|
+
const service = new Service({
|
|
70
|
+
logger,
|
|
71
|
+
project: {
|
|
72
|
+
cacheRoot: fileUtil.ensureEndingSlash(pathToFileURL(cacheRoot).toString()),
|
|
73
|
+
defaultConfig: ConfigService.merge(VanillaConfig, {
|
|
74
|
+
env: { dependencies: [] },
|
|
75
|
+
}),
|
|
76
|
+
externals: NodeJsExternals,
|
|
77
|
+
initializers: [mcdoc.initialize, je.initialize],
|
|
78
|
+
projectRoot: fileUtil.ensureEndingSlash(pathToFileURL(projectPath).toString()),
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
await service.project.ready();
|
|
82
|
+
await service.project.cacheService.save();
|
|
83
|
+
const generated = join('out', 'generated');
|
|
84
|
+
if (args.dry !== true) {
|
|
85
|
+
await fs.ensureDir(generated);
|
|
86
|
+
if (args.module) {
|
|
87
|
+
await fs.ensureDir(join(generated, 'module'));
|
|
88
|
+
}
|
|
89
|
+
if (args.locale) {
|
|
90
|
+
await fs.ensureDir(join('out', 'locale'));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const symbols = [];
|
|
94
|
+
const internal_locales = {};
|
|
95
|
+
const locales = {};
|
|
96
|
+
let errors = 0;
|
|
97
|
+
for await (const doc_file of walk(projectPath)) {
|
|
98
|
+
if (doc_file.path.endsWith('.mcdoc')) {
|
|
99
|
+
const DocumentUri = pathToFileURL(doc_file.path).toString();
|
|
100
|
+
const doc_contents = await fs.readFile(doc_file.path, 'utf-8');
|
|
101
|
+
await service.project.onDidOpen(DocumentUri, 'mcdoc', 0, doc_contents);
|
|
102
|
+
const check = await service.project.ensureClientManagedChecked(DocumentUri);
|
|
103
|
+
if (check && check.doc && check.node) {
|
|
104
|
+
const { doc, node } = check;
|
|
105
|
+
const path = parse(fileURLToPath(doc.uri));
|
|
106
|
+
const resource = join(path.dir.replace(`${projectPath}`, ''), path.name).replace(/^\//, '');
|
|
107
|
+
logger.info(`parsing ${resource}\n`);
|
|
108
|
+
if (node.children[0]) {
|
|
109
|
+
const children = node.children;
|
|
110
|
+
function flattenChild(parent, self, _parent, _child) {
|
|
111
|
+
const child = {};
|
|
112
|
+
const known_error = false;
|
|
113
|
+
/* @ts-ignore */
|
|
114
|
+
child.self = self;
|
|
115
|
+
/* @ts-ignore */
|
|
116
|
+
if (_child.parent)
|
|
117
|
+
child.parent = parent;
|
|
118
|
+
/* @ts-ignore */
|
|
119
|
+
if (_child.parentMap)
|
|
120
|
+
child.parentMap = parent;
|
|
121
|
+
if (_child.children) {
|
|
122
|
+
child.children = [];
|
|
123
|
+
for (const [i, __child] of Object.entries(_child.children)) {
|
|
124
|
+
/* @ts-ignore */
|
|
125
|
+
child.children[Number(i)] = flattenChild(self, `${self}[${i}]`, _child, __child);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
child.type = _child.type;
|
|
129
|
+
if (child.type === 'resource_location') {
|
|
130
|
+
/* @ts-ignore */
|
|
131
|
+
child.namespace = _child.namespace;
|
|
132
|
+
/* @ts-ignore */
|
|
133
|
+
child.path = _child.path;
|
|
134
|
+
}
|
|
135
|
+
if (Object.hasOwn(_child, 'isOptional')) {
|
|
136
|
+
/* @ts-ignore */
|
|
137
|
+
child.isOptional = _child.isOptional;
|
|
138
|
+
}
|
|
139
|
+
if (Object.hasOwn(_child, 'colorTokenType')) {
|
|
140
|
+
/* @ts-ignore */
|
|
141
|
+
child.colorTokenType = _child.colorTokenType;
|
|
142
|
+
}
|
|
143
|
+
/* @ts-ignore */
|
|
144
|
+
if (Object.hasOwn(_child, 'value')) {
|
|
145
|
+
/* @ts-ignore */
|
|
146
|
+
child.value = _child.value;
|
|
147
|
+
if (internal_locales[parent]) {
|
|
148
|
+
locales[`mcdoc.${resource.replace(/[\/\\]/g, '.')}.${child.value}`] = removeWindowsCruft(internal_locales[parent].join('').trimEnd());
|
|
149
|
+
delete internal_locales[parent];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (child.type === 'mcdoc:struct/map_key' &&
|
|
153
|
+
internal_locales[parent]) {
|
|
154
|
+
locales[`mcdoc.${resource.replace(/[\/\\]/g, '.')}.map_key`] = removeWindowsCruft(internal_locales[parent].join('').trimEnd());
|
|
155
|
+
delete internal_locales[parent];
|
|
156
|
+
}
|
|
157
|
+
if (Object.hasOwn(_child, 'comment')) {
|
|
158
|
+
/* @ts-ignore */
|
|
159
|
+
const comment = _child.comment;
|
|
160
|
+
child.comment = comment;
|
|
161
|
+
if (!args.dry && args.locale &&
|
|
162
|
+
_parent?.type === 'mcdoc:doc_comments') {
|
|
163
|
+
const key = parent.replace(/\[\d+\]$/, '');
|
|
164
|
+
if (!internal_locales[key]) {
|
|
165
|
+
internal_locales[key] = [];
|
|
166
|
+
}
|
|
167
|
+
internal_locales[key].push(comment.slice(1));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (_child.hover)
|
|
171
|
+
child.hover = _child.hover;
|
|
172
|
+
if (_child.color)
|
|
173
|
+
child.color = _child.color;
|
|
174
|
+
if (child.type !== 'error')
|
|
175
|
+
return child;
|
|
176
|
+
else {
|
|
177
|
+
errors++;
|
|
178
|
+
const lc = lineColumn(doc_contents);
|
|
179
|
+
function range(range) {
|
|
180
|
+
const start = lc.fromIndex(range.start);
|
|
181
|
+
const end = lc.fromIndex(range.end);
|
|
182
|
+
return `L${start?.line}${start?.col ? `:C${start?.col}` : ''} -> L${end?.line}${end?.col ? `:C${end?.col}` : ''}`;
|
|
183
|
+
}
|
|
184
|
+
if (!known_error) {
|
|
185
|
+
console.warn(`mcdoc error(s):`);
|
|
186
|
+
/* @ts-ignore */
|
|
187
|
+
if (_child.parent?.parserErrors.length !== 0) {
|
|
188
|
+
console.warn(' parser:');
|
|
189
|
+
/* @ts-ignore */
|
|
190
|
+
_child.parent?.parserErrors.forEach(error => {
|
|
191
|
+
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/* @ts-ignore */
|
|
195
|
+
if (_child.parent?.binderErrors.length !== 0) {
|
|
196
|
+
console.warn(' binder:');
|
|
197
|
+
/* @ts-ignore */
|
|
198
|
+
_child.parent?.binderErrors.forEach(error => {
|
|
199
|
+
console.warn(` ${error.message}\n Location: ${range(error.range)}. Severity ${error.severity}.`);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
console.warn(`error @ ${doc_file.path}\n\n`);
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
children.forEach((child, i) => {
|
|
208
|
+
/* @ts-ignore */
|
|
209
|
+
children[i] = flattenChild(resource, `${resource}.[${i}]`, undefined, child);
|
|
210
|
+
});
|
|
211
|
+
const symbol = {
|
|
212
|
+
resource,
|
|
213
|
+
children,
|
|
214
|
+
};
|
|
215
|
+
symbols.push(symbol);
|
|
216
|
+
if (!args.dry && args.module) {
|
|
217
|
+
const dir = parse(join(generated, 'module', resource)).dir;
|
|
218
|
+
if (dir !== '')
|
|
219
|
+
await fs.ensureDir(dir);
|
|
220
|
+
await fs.writeFile(join(generated, 'module', `${resource}.mcdoc.json`), JSON.stringify(symbol));
|
|
221
|
+
if (args.pretty) {
|
|
222
|
+
await fs.writeFile(join(generated, 'module', `${resource}.pretty.mcdoc.json`), JSON.stringify(symbol, undefined, 3));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!args.dry) {
|
|
230
|
+
await fs.writeFile(join(generated, 'generated.mcdoc.json'), JSON.stringify(symbols));
|
|
231
|
+
if (args.pretty) {
|
|
232
|
+
await fs.writeFile(join(generated, 'generated.pretty.mcdoc.json'), JSON.stringify(symbols, undefined, 3));
|
|
233
|
+
}
|
|
234
|
+
if (args.module) {
|
|
235
|
+
await fs.writeFile(join(generated, 'module', 'index.json'), JSON.stringify(symbols.map(symbol => symbol.resource)));
|
|
236
|
+
}
|
|
237
|
+
if (args.locale) {
|
|
238
|
+
const orphaned_doc = Object.keys(internal_locales);
|
|
239
|
+
if (orphaned_doc.length !== 0) {
|
|
240
|
+
console.warn(`parsing error, ${orphaned_doc.length} orphaned doc comments or incorrectly parsed markup comments`);
|
|
241
|
+
console.warn(internal_locales);
|
|
242
|
+
}
|
|
243
|
+
await fs.writeFile(join('out', 'locale', 'locale.en-us.json'), JSON.stringify(locales, undefined, 3));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
console.log(`Generated JSON files with ${errors} errors.`);
|
|
247
|
+
await service.project.close();
|
|
248
|
+
})
|
|
249
|
+
.strict()
|
|
250
|
+
.demandCommand(1)
|
|
251
|
+
.parse();
|
|
252
|
+
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spyglassmc/mcdoc-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "lib/index.
|
|
6
|
-
"types": "lib/index.d.
|
|
5
|
+
"main": "lib/index.mjs",
|
|
6
|
+
"types": "lib/index.d.mts",
|
|
7
7
|
"contributors": [
|
|
8
8
|
{
|
|
9
9
|
"name": "Misode",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"test": "test/"
|
|
20
20
|
},
|
|
21
21
|
"bin": {
|
|
22
|
-
"mcdoc": "lib/index.
|
|
22
|
+
"mcdoc": "lib/index.mjs"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"release": "npm publish",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"klaw": "^4.1.0",
|
|
32
32
|
"line-column": "^1.0.2",
|
|
33
33
|
"yargs": "17.6.2",
|
|
34
|
-
"@spyglassmc/core": "0.4.
|
|
35
|
-
"@spyglassmc/mcdoc": "0.3.
|
|
34
|
+
"@spyglassmc/core": "0.4.4",
|
|
35
|
+
"@spyglassmc/mcdoc": "0.3.5"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/fs-extra": "^11.0.2",
|