@sandstone-mc/mcdoc-ts-generator 0.1.4 → 0.1.6
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/dist/cli.js +39 -16
- package/dist/index.d.ts +1 -0
- package/dist/index.js +40 -16
- package/dist/typegen/export.d.ts +2 -2
- package/dist/typegen/index.d.ts +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -28,6 +28,8 @@ function pascal_case(name) {
|
|
|
28
28
|
return words.map((word) => word[0].toUpperCase() + word.slice(1)).join("");
|
|
29
29
|
}
|
|
30
30
|
function pluralize(name) {
|
|
31
|
+
if (name.endsWith("ey"))
|
|
32
|
+
return name + "s";
|
|
31
33
|
if (name.endsWith("y"))
|
|
32
34
|
return name.slice(0, -1) + "ies";
|
|
33
35
|
if (name.endsWith("s") || name.endsWith("ch") || name.endsWith("sh") || name.endsWith("x") || name.endsWith("z"))
|
|
@@ -1803,15 +1805,12 @@ function mcdoc_string(type) {
|
|
|
1803
1805
|
}).with({ name: "time_pattern" }, () => {
|
|
1804
1806
|
return (args) => static_value3.time;
|
|
1805
1807
|
}).with({ name: "translation_key" }, () => {
|
|
1806
|
-
const
|
|
1807
|
-
const LiteralUnion = "LiteralUnion";
|
|
1808
|
+
const Registry = "::java::registry::Registry";
|
|
1808
1809
|
return (args) => ({
|
|
1809
|
-
type: factory17.createTypeReferenceNode(
|
|
1810
|
-
factory17.createTypeReferenceNode(TRANSLATION_KEYS)
|
|
1811
|
-
]),
|
|
1810
|
+
type: factory17.createIndexedAccessTypeNode(factory17.createTypeReferenceNode("Registry"), Bind.StringLiteral("minecraft:translation_key")),
|
|
1812
1811
|
imports: {
|
|
1813
|
-
ordered: [
|
|
1814
|
-
check: new Map([[
|
|
1812
|
+
ordered: [Registry],
|
|
1813
|
+
check: new Map([[Registry, 0]])
|
|
1815
1814
|
}
|
|
1816
1815
|
});
|
|
1817
1816
|
}).with({ name: "translation_value" }, () => {
|
|
@@ -2543,10 +2542,13 @@ function dispatcher_symbol(id, name, members, dispatcher_properties, module_map,
|
|
|
2543
2542
|
var DispatcherSymbol = dispatcher_symbol;
|
|
2544
2543
|
|
|
2545
2544
|
// src/typegen/export.ts
|
|
2545
|
+
import { TaggableResourceLocationCategories } from "@spyglassmc/core";
|
|
2546
2546
|
import ts25 from "typescript";
|
|
2547
2547
|
var { factory: factory24 } = ts25;
|
|
2548
2548
|
function export_registry(resolved_registries) {
|
|
2549
2549
|
let imports;
|
|
2550
|
+
imports = add_import(imports, "sandstone::Set");
|
|
2551
|
+
imports = add_import(imports, "sandstone::SetType");
|
|
2550
2552
|
const properties = [];
|
|
2551
2553
|
for (const [registry_name, { import_path, registry }] of resolved_registries) {
|
|
2552
2554
|
imports = add_import(imports, import_path);
|
|
@@ -2554,8 +2556,10 @@ function export_registry(resolved_registries) {
|
|
|
2554
2556
|
properties.push(factory24.createPropertySignature(undefined, factory24.createStringLiteral(registry_id), undefined, factory24.createTypeReferenceNode(registry)));
|
|
2555
2557
|
}
|
|
2556
2558
|
const registry_type = factory24.createTypeAliasDeclaration([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], "Registry", undefined, factory24.createTypeLiteralNode(properties));
|
|
2559
|
+
const registries_set = factory24.createVariableStatement([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], factory24.createVariableDeclarationList([factory24.createVariableDeclaration("REGISTRIES_SET", undefined, undefined, factory24.createNewExpression(factory24.createIdentifier("Set"), undefined, [factory24.createAsExpression(factory24.createArrayLiteralExpression(TaggableResourceLocationCategories.map((category) => factory24.createStringLiteral(category, true)), true), factory24.createTypeReferenceNode("const"))]))], ts25.NodeFlags.Const));
|
|
2560
|
+
const registries_type = factory24.createTypeAliasDeclaration([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], "REGISTRIES", undefined, factory24.createTypeReferenceNode("SetType", [factory24.createTypeQueryNode(factory24.createIdentifier("REGISTRIES_SET"))]));
|
|
2557
2561
|
return {
|
|
2558
|
-
exports: [registry_type],
|
|
2562
|
+
exports: [registry_type, registries_set, registries_type],
|
|
2559
2563
|
paths: new Set,
|
|
2560
2564
|
...add({ imports })
|
|
2561
2565
|
};
|
|
@@ -2632,9 +2636,9 @@ class TypesGenerator {
|
|
|
2632
2636
|
resolved_symbols = new Map;
|
|
2633
2637
|
resolved_dispatchers = new Map;
|
|
2634
2638
|
constructor() {}
|
|
2635
|
-
resolve_types(symbols) {
|
|
2639
|
+
resolve_types(symbols, translation_keys) {
|
|
2636
2640
|
console.log("registries");
|
|
2637
|
-
this.resolve_registry_symbols(symbols);
|
|
2641
|
+
this.resolve_registry_symbols(symbols, translation_keys);
|
|
2638
2642
|
const registry_exports = export_registry(this.resolved_registries);
|
|
2639
2643
|
this.resolved_symbols.set("::java::registry", registry_exports);
|
|
2640
2644
|
const dispatchers = symbols.getVisibleSymbols("mcdoc/dispatcher");
|
|
@@ -2651,12 +2655,12 @@ class TypesGenerator {
|
|
|
2651
2655
|
const dispatcher_exports = export_dispatcher(this.resolved_dispatchers);
|
|
2652
2656
|
this.resolved_symbols.set("mcdoc::dispatcher", dispatcher_exports);
|
|
2653
2657
|
}
|
|
2654
|
-
resolve_registry_symbols(registries) {
|
|
2655
|
-
for (const registry_name of AllCategories) {
|
|
2658
|
+
resolve_registry_symbols(registries, translation_keys) {
|
|
2659
|
+
for (const registry_name of [...AllCategories]) {
|
|
2656
2660
|
if (registry_name === "mcdoc" || registry_name === "mcdoc/dispatcher") {
|
|
2657
2661
|
continue;
|
|
2658
2662
|
}
|
|
2659
|
-
const registry = Object.keys(registries.getVisibleSymbols(registry_name));
|
|
2663
|
+
const registry = registry_name === "translation_key" ? translation_keys : Object.keys(registries.getVisibleSymbols(registry_name));
|
|
2660
2664
|
if (registry.length === 0)
|
|
2661
2665
|
continue;
|
|
2662
2666
|
const type_name = pluralize(registry_name.split("/").join("_")).toUpperCase();
|
|
@@ -2881,7 +2885,13 @@ function handle_imports(imports) {
|
|
|
2881
2885
|
} else if (path[1] === "java") {
|
|
2882
2886
|
file = `sandstone/arguments/generated/${path.slice(2).join("/")}.js`;
|
|
2883
2887
|
} else if (path[0] === "sandstone") {
|
|
2884
|
-
|
|
2888
|
+
if (path.length === 1) {
|
|
2889
|
+
file = "sandstone";
|
|
2890
|
+
} else if (path[1] === "arguments" && path.length === 2) {
|
|
2891
|
+
file = "sandstone/arguments";
|
|
2892
|
+
} else {
|
|
2893
|
+
file = `${path.join("/")}.js`;
|
|
2894
|
+
}
|
|
2885
2895
|
} else {
|
|
2886
2896
|
throw new Error(`[mcdoc_import] Unsupported import location "${path[0]}" in "${import_path}"`);
|
|
2887
2897
|
}
|
|
@@ -2979,6 +2989,18 @@ async function fetchBlockStates(versionId) {
|
|
|
2979
2989
|
}
|
|
2980
2990
|
return [result, etag];
|
|
2981
2991
|
}
|
|
2992
|
+
async function fetchTranslationKeys() {
|
|
2993
|
+
console.debug(`[fetchTranslationKeys] latest from github`);
|
|
2994
|
+
const result = new Map;
|
|
2995
|
+
try {
|
|
2996
|
+
const req = await fetchWithCache(`https://raw.githubusercontent.com/misode/mcmeta/refs/heads/assets-tiny/assets/minecraft/lang/en_us.json`);
|
|
2997
|
+
const data = await req.json();
|
|
2998
|
+
return Object.keys(data).map((key) => `minecraft:${key}`);
|
|
2999
|
+
} catch (e) {
|
|
3000
|
+
console.warn("Error occurred while fetching translation keys:", errorMessage(e));
|
|
3001
|
+
}
|
|
3002
|
+
return [];
|
|
3003
|
+
}
|
|
2982
3004
|
var VanillaMcdocUri = "mcdoc://vanilla-mcdoc/symbols.json";
|
|
2983
3005
|
function vanillaMcdocRegistrar(vanillaMcdoc) {
|
|
2984
3006
|
return (symbols) => {
|
|
@@ -3053,7 +3075,7 @@ async function generate(options = {}) {
|
|
|
3053
3075
|
await service.project.ready();
|
|
3054
3076
|
await service.project.cacheService.save();
|
|
3055
3077
|
const type_gen = new TypesGenerator;
|
|
3056
|
-
type_gen.resolve_types(service.project.symbols);
|
|
3078
|
+
type_gen.resolve_types(service.project.symbols, await fetchTranslationKeys());
|
|
3057
3079
|
for await (const [symbol_path, { exports, imports }] of type_gen.resolved_symbols.entries()) {
|
|
3058
3080
|
const parts = symbol_path.split("::");
|
|
3059
3081
|
if (parts[0] === "") {
|
|
@@ -3079,7 +3101,8 @@ async function generate(options = {}) {
|
|
|
3079
3101
|
baseUrl: "./",
|
|
3080
3102
|
paths: {
|
|
3081
3103
|
sandstone: ["../sandstone-types/index.ts"],
|
|
3082
|
-
"sandstone/arguments
|
|
3104
|
+
"sandstone/arguments": ["../sandstone-types/arguments/index.ts"],
|
|
3105
|
+
"sandstone/arguments/generated/*": ["./*"]
|
|
3083
3106
|
}
|
|
3084
3107
|
}
|
|
3085
3108
|
}, null, 2));
|
package/dist/index.d.ts
CHANGED
|
@@ -14,4 +14,5 @@ export declare function fetchVanillaMcdoc(): Promise<VanillaMcdocSymbols>;
|
|
|
14
14
|
export declare function fetchRegistries(versionId: string): Promise<readonly [Map<string, string[]>, string]>;
|
|
15
15
|
export type BlockStateData = [Record<string, string[]>, Record<string, string>];
|
|
16
16
|
export declare function fetchBlockStates(versionId: string): Promise<readonly [Map<string, BlockStateData>, string]>;
|
|
17
|
+
export declare function fetchTranslationKeys(): Promise<string[]>;
|
|
17
18
|
export declare function generate(options?: GeneratorOptions): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -26,6 +26,8 @@ function pascal_case(name) {
|
|
|
26
26
|
return words.map((word) => word[0].toUpperCase() + word.slice(1)).join("");
|
|
27
27
|
}
|
|
28
28
|
function pluralize(name) {
|
|
29
|
+
if (name.endsWith("ey"))
|
|
30
|
+
return name + "s";
|
|
29
31
|
if (name.endsWith("y"))
|
|
30
32
|
return name.slice(0, -1) + "ies";
|
|
31
33
|
if (name.endsWith("s") || name.endsWith("ch") || name.endsWith("sh") || name.endsWith("x") || name.endsWith("z"))
|
|
@@ -1801,15 +1803,12 @@ function mcdoc_string(type) {
|
|
|
1801
1803
|
}).with({ name: "time_pattern" }, () => {
|
|
1802
1804
|
return (args) => static_value3.time;
|
|
1803
1805
|
}).with({ name: "translation_key" }, () => {
|
|
1804
|
-
const
|
|
1805
|
-
const LiteralUnion = "LiteralUnion";
|
|
1806
|
+
const Registry = "::java::registry::Registry";
|
|
1806
1807
|
return (args) => ({
|
|
1807
|
-
type: factory17.createTypeReferenceNode(
|
|
1808
|
-
factory17.createTypeReferenceNode(TRANSLATION_KEYS)
|
|
1809
|
-
]),
|
|
1808
|
+
type: factory17.createIndexedAccessTypeNode(factory17.createTypeReferenceNode("Registry"), Bind.StringLiteral("minecraft:translation_key")),
|
|
1810
1809
|
imports: {
|
|
1811
|
-
ordered: [
|
|
1812
|
-
check: new Map([[
|
|
1810
|
+
ordered: [Registry],
|
|
1811
|
+
check: new Map([[Registry, 0]])
|
|
1813
1812
|
}
|
|
1814
1813
|
});
|
|
1815
1814
|
}).with({ name: "translation_value" }, () => {
|
|
@@ -2541,10 +2540,13 @@ function dispatcher_symbol(id, name, members, dispatcher_properties, module_map,
|
|
|
2541
2540
|
var DispatcherSymbol = dispatcher_symbol;
|
|
2542
2541
|
|
|
2543
2542
|
// src/typegen/export.ts
|
|
2543
|
+
import { TaggableResourceLocationCategories } from "@spyglassmc/core";
|
|
2544
2544
|
import ts25 from "typescript";
|
|
2545
2545
|
var { factory: factory24 } = ts25;
|
|
2546
2546
|
function export_registry(resolved_registries) {
|
|
2547
2547
|
let imports;
|
|
2548
|
+
imports = add_import(imports, "sandstone::Set");
|
|
2549
|
+
imports = add_import(imports, "sandstone::SetType");
|
|
2548
2550
|
const properties = [];
|
|
2549
2551
|
for (const [registry_name, { import_path, registry }] of resolved_registries) {
|
|
2550
2552
|
imports = add_import(imports, import_path);
|
|
@@ -2552,8 +2554,10 @@ function export_registry(resolved_registries) {
|
|
|
2552
2554
|
properties.push(factory24.createPropertySignature(undefined, factory24.createStringLiteral(registry_id), undefined, factory24.createTypeReferenceNode(registry)));
|
|
2553
2555
|
}
|
|
2554
2556
|
const registry_type = factory24.createTypeAliasDeclaration([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], "Registry", undefined, factory24.createTypeLiteralNode(properties));
|
|
2557
|
+
const registries_set = factory24.createVariableStatement([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], factory24.createVariableDeclarationList([factory24.createVariableDeclaration("REGISTRIES_SET", undefined, undefined, factory24.createNewExpression(factory24.createIdentifier("Set"), undefined, [factory24.createAsExpression(factory24.createArrayLiteralExpression(TaggableResourceLocationCategories.map((category) => factory24.createStringLiteral(category, true)), true), factory24.createTypeReferenceNode("const"))]))], ts25.NodeFlags.Const));
|
|
2558
|
+
const registries_type = factory24.createTypeAliasDeclaration([factory24.createToken(ts25.SyntaxKind.ExportKeyword)], "REGISTRIES", undefined, factory24.createTypeReferenceNode("SetType", [factory24.createTypeQueryNode(factory24.createIdentifier("REGISTRIES_SET"))]));
|
|
2555
2559
|
return {
|
|
2556
|
-
exports: [registry_type],
|
|
2560
|
+
exports: [registry_type, registries_set, registries_type],
|
|
2557
2561
|
paths: new Set,
|
|
2558
2562
|
...add({ imports })
|
|
2559
2563
|
};
|
|
@@ -2630,9 +2634,9 @@ class TypesGenerator {
|
|
|
2630
2634
|
resolved_symbols = new Map;
|
|
2631
2635
|
resolved_dispatchers = new Map;
|
|
2632
2636
|
constructor() {}
|
|
2633
|
-
resolve_types(symbols) {
|
|
2637
|
+
resolve_types(symbols, translation_keys) {
|
|
2634
2638
|
console.log("registries");
|
|
2635
|
-
this.resolve_registry_symbols(symbols);
|
|
2639
|
+
this.resolve_registry_symbols(symbols, translation_keys);
|
|
2636
2640
|
const registry_exports = export_registry(this.resolved_registries);
|
|
2637
2641
|
this.resolved_symbols.set("::java::registry", registry_exports);
|
|
2638
2642
|
const dispatchers = symbols.getVisibleSymbols("mcdoc/dispatcher");
|
|
@@ -2649,12 +2653,12 @@ class TypesGenerator {
|
|
|
2649
2653
|
const dispatcher_exports = export_dispatcher(this.resolved_dispatchers);
|
|
2650
2654
|
this.resolved_symbols.set("mcdoc::dispatcher", dispatcher_exports);
|
|
2651
2655
|
}
|
|
2652
|
-
resolve_registry_symbols(registries) {
|
|
2653
|
-
for (const registry_name of AllCategories) {
|
|
2656
|
+
resolve_registry_symbols(registries, translation_keys) {
|
|
2657
|
+
for (const registry_name of [...AllCategories]) {
|
|
2654
2658
|
if (registry_name === "mcdoc" || registry_name === "mcdoc/dispatcher") {
|
|
2655
2659
|
continue;
|
|
2656
2660
|
}
|
|
2657
|
-
const registry = Object.keys(registries.getVisibleSymbols(registry_name));
|
|
2661
|
+
const registry = registry_name === "translation_key" ? translation_keys : Object.keys(registries.getVisibleSymbols(registry_name));
|
|
2658
2662
|
if (registry.length === 0)
|
|
2659
2663
|
continue;
|
|
2660
2664
|
const type_name = pluralize(registry_name.split("/").join("_")).toUpperCase();
|
|
@@ -2879,7 +2883,13 @@ function handle_imports(imports) {
|
|
|
2879
2883
|
} else if (path[1] === "java") {
|
|
2880
2884
|
file = `sandstone/arguments/generated/${path.slice(2).join("/")}.js`;
|
|
2881
2885
|
} else if (path[0] === "sandstone") {
|
|
2882
|
-
|
|
2886
|
+
if (path.length === 1) {
|
|
2887
|
+
file = "sandstone";
|
|
2888
|
+
} else if (path[1] === "arguments" && path.length === 2) {
|
|
2889
|
+
file = "sandstone/arguments";
|
|
2890
|
+
} else {
|
|
2891
|
+
file = `${path.join("/")}.js`;
|
|
2892
|
+
}
|
|
2883
2893
|
} else {
|
|
2884
2894
|
throw new Error(`[mcdoc_import] Unsupported import location "${path[0]}" in "${import_path}"`);
|
|
2885
2895
|
}
|
|
@@ -2977,6 +2987,18 @@ async function fetchBlockStates(versionId) {
|
|
|
2977
2987
|
}
|
|
2978
2988
|
return [result, etag];
|
|
2979
2989
|
}
|
|
2990
|
+
async function fetchTranslationKeys() {
|
|
2991
|
+
console.debug(`[fetchTranslationKeys] latest from github`);
|
|
2992
|
+
const result = new Map;
|
|
2993
|
+
try {
|
|
2994
|
+
const req = await fetchWithCache(`https://raw.githubusercontent.com/misode/mcmeta/refs/heads/assets-tiny/assets/minecraft/lang/en_us.json`);
|
|
2995
|
+
const data = await req.json();
|
|
2996
|
+
return Object.keys(data).map((key) => `minecraft:${key}`);
|
|
2997
|
+
} catch (e) {
|
|
2998
|
+
console.warn("Error occurred while fetching translation keys:", errorMessage(e));
|
|
2999
|
+
}
|
|
3000
|
+
return [];
|
|
3001
|
+
}
|
|
2980
3002
|
var VanillaMcdocUri = "mcdoc://vanilla-mcdoc/symbols.json";
|
|
2981
3003
|
function vanillaMcdocRegistrar(vanillaMcdoc) {
|
|
2982
3004
|
return (symbols) => {
|
|
@@ -3051,7 +3073,7 @@ async function generate(options = {}) {
|
|
|
3051
3073
|
await service.project.ready();
|
|
3052
3074
|
await service.project.cacheService.save();
|
|
3053
3075
|
const type_gen = new TypesGenerator;
|
|
3054
|
-
type_gen.resolve_types(service.project.symbols);
|
|
3076
|
+
type_gen.resolve_types(service.project.symbols, await fetchTranslationKeys());
|
|
3055
3077
|
for await (const [symbol_path, { exports, imports }] of type_gen.resolved_symbols.entries()) {
|
|
3056
3078
|
const parts = symbol_path.split("::");
|
|
3057
3079
|
if (parts[0] === "") {
|
|
@@ -3077,7 +3099,8 @@ async function generate(options = {}) {
|
|
|
3077
3099
|
baseUrl: "./",
|
|
3078
3100
|
paths: {
|
|
3079
3101
|
sandstone: ["../sandstone-types/index.ts"],
|
|
3080
|
-
"sandstone/arguments
|
|
3102
|
+
"sandstone/arguments": ["../sandstone-types/arguments/index.ts"],
|
|
3103
|
+
"sandstone/arguments/generated/*": ["./*"]
|
|
3081
3104
|
}
|
|
3082
3105
|
}
|
|
3083
3106
|
}, null, 2));
|
|
@@ -3088,6 +3111,7 @@ export {
|
|
|
3088
3111
|
mcdoc_raw,
|
|
3089
3112
|
generate,
|
|
3090
3113
|
fetchVanillaMcdoc,
|
|
3114
|
+
fetchTranslationKeys,
|
|
3091
3115
|
fetchRegistries,
|
|
3092
3116
|
fetchBlockStates
|
|
3093
3117
|
};
|
package/dist/typegen/export.d.ts
CHANGED
|
@@ -2,14 +2,14 @@ import ts from 'typescript';
|
|
|
2
2
|
import { type NonEmptyList } from './mcdoc/utils';
|
|
3
3
|
import type { ResolvedDispatcher, ResolvedRegistry } from '.';
|
|
4
4
|
export declare function export_registry(resolved_registries: Map<string, ResolvedRegistry>): {
|
|
5
|
-
readonly exports: ts.TypeAliasDeclaration[];
|
|
5
|
+
readonly exports: (ts.TypeAliasDeclaration | ts.VariableStatement)[];
|
|
6
6
|
readonly paths: Set<string>;
|
|
7
7
|
} | {
|
|
8
8
|
readonly imports: {
|
|
9
9
|
readonly ordered: NonEmptyList<string>;
|
|
10
10
|
readonly check: Map<string, number>;
|
|
11
11
|
};
|
|
12
|
-
readonly exports: ts.TypeAliasDeclaration[];
|
|
12
|
+
readonly exports: (ts.TypeAliasDeclaration | ts.VariableStatement)[];
|
|
13
13
|
readonly paths: Set<string>;
|
|
14
14
|
};
|
|
15
15
|
/**
|
package/dist/typegen/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export declare class TypesGenerator {
|
|
|
31
31
|
readonly resolved_symbols: Map<string, ResolvedSymbol>;
|
|
32
32
|
readonly resolved_dispatchers: Map<string, ResolvedDispatcher>;
|
|
33
33
|
constructor();
|
|
34
|
-
resolve_types(symbols: SymbolUtil): void;
|
|
34
|
+
resolve_types(symbols: SymbolUtil, translation_keys: string[]): void;
|
|
35
35
|
private resolve_registry_symbols;
|
|
36
36
|
private resolve_module_symbols;
|
|
37
37
|
private resolve_dispatcher_symbols;
|