Skip to content

Commit 4d6f20b

Browse files
committed
fix making schema-codegen accessible via CLI
1 parent b49d82c commit 4d6f20b

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

bin/schema-codegen

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env node
22

3-
require('../lib/codegen/cli');
3+
import('../build/codegen/cli.cjs');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@colyseus/schema",
3-
"version": "4.0.6",
3+
"version": "4.0.7",
44
"description": "Binary state serializer with delta encoding for games",
55
"type": "module",
66
"bin": {

rollup.config.mjs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import typescript from '@rollup/plugin-typescript';
22

33
export default [
44
// https://github.com/microsoft/TypeScript/issues/18442#issuecomment-749896695
5-
65
{
76
input: ['src/index.ts'],
87
output: [{ dir: 'build', format: 'esm', entryFileNames: '[name].mjs', sourcemap: true, preserveModules: false }],
@@ -20,4 +19,18 @@ export default [
2019
output: [{ dir: 'build', name: "schema", format: 'umd', entryFileNames: '[name].js', preserveModules: false }],
2120
plugins: [typescript({ tsconfig: './tsconfig/tsconfig.cjs.json' })],
2221
},
22+
23+
// Codegen CLI (CJS for Node.js CLI compatibility)
24+
{
25+
input: ['src/codegen/cli.ts'],
26+
output: [{
27+
dir: 'build/codegen',
28+
format: 'cjs',
29+
entryFileNames: '[name].cjs',
30+
sourcemap: true,
31+
banner: '#!/usr/bin/env node'
32+
}],
33+
plugins: [typescript({ tsconfig: './tsconfig/tsconfig.cjs.json' })],
34+
external: ['fs', 'path', 'module', 'typescript']
35+
},
2336
];

src/codegen/api.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import * as fs from "fs";
22
import * as path from "path";
3-
import { createRequire } from "module";
4-
// import { fileURLToPath } from "url";
53

64
import { File } from "./types.js";
75
import { parseFiles } from "./parser.js";
86

9-
// Create require relative to this file's location (for ESM compatibility)
10-
// const __filename = fileURLToPath(import.meta.url);
11-
// const __dirname = path.dirname(__filename);
12-
const require = createRequire(import.meta.url);
7+
// Statically import all language generators (for bundling)
8+
import { generate as csharp } from "./languages/csharp.js";
9+
import { generate as cpp } from "./languages/cpp.js";
10+
import { generate as haxe } from "./languages/haxe.js";
11+
import { generate as ts } from "./languages/ts.js";
12+
import { generate as js } from "./languages/js.js";
13+
import { generate as java } from "./languages/java.js";
14+
import { generate as lua } from "./languages/lua.js";
15+
16+
const generators: Record<string, Function> = { csharp, cpp, haxe, ts, js, java, lua, };
1317

1418
export interface GenerateOptions {
1519
files: string[],
@@ -19,12 +23,9 @@ export interface GenerateOptions {
1923
}
2024

2125
export function generate(targetId: string, options: GenerateOptions) {
22-
let generator: Function;
23-
24-
try {
25-
generator = require('./languages/' + targetId + '.js').generate;
26+
const generator = generators[targetId];
2627

27-
} catch (e) {
28+
if (!generator) {
2829
throw new Error("You must provide a valid generator as argument, such as: --csharp, --haxe or --cpp");
2930
}
3031

0 commit comments

Comments
 (0)