Skip to content

Commit a8a36b6

Browse files
authored
feat: add generate command (#244)
1 parent 6f208f1 commit a8a36b6

File tree

20 files changed

+661
-34
lines changed

20 files changed

+661
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ yarn.lock
1515
## Genrated files
1616
test/**/*.mjs
1717
test/**/*.js
18+
test/generate/**/*.ts
1819
test/init/game
1920
/src/commands/init/generated.ts

docs/API.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ Build typescript files
1616
| `--dry` | | Do a dry run with this command - prints/returns output | `` | |
1717
| `--minifyClasses` | | Minifies GodotJS classes | `` | `true` |
1818

19+
## generate
20+
21+
Generate typescript utility files
22+
23+
> You can use `generate.json` as a config file.
24+
By default it tries to search for the configuration otherwise use a correct path by passing `--config=./generate.json`.
25+
26+
| long | short | description | required | defaultValue |
27+
| :------ | :---: | :---------------------------------------------- | :------: | :----------- |
28+
| `--src` | | Relative path where script files located | `` | `"."` |
29+
| `--out` | | Relative path where generated files are written | `` | `"."` |
30+
1931
## init
2032

2133
Creates a new GodotJS project with TypeScript support

package-lock.json

Lines changed: 205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"globals": "^16.0.0",
4848
"husky": "^9.0.11",
4949
"markdown-table": "^3.0.3",
50+
"nodemon": "3.1.10",
5051
"npm-run-all2": "8.0.1",
5152
"prettier": "^3.0.3",
5253
"tsx": "^4.19.1",

src/commands/generate/data.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ProgramOptionsType } from "../../data";
2+
import { ConfigType, OutConfigType } from "../../utils/shared";
3+
4+
export type GenerateConfigType = {
5+
src?: string;
6+
} & OutConfigType &
7+
ConfigType;
8+
9+
export const generateOptions: ProgramOptionsType[] = [
10+
{
11+
name: "src",
12+
defaultValue: ".",
13+
description: "Relative path where script files located",
14+
},
15+
{
16+
name: "out",
17+
defaultValue: ".",
18+
description: "Relative path where generated files are written",
19+
},
20+
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { writeFileSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
import { GDScript } from "./parse";
4+
5+
const getResourcePath = (filePath: string) => {
6+
return `"res://${filePath.replace(/\\/g, "/")}"`;
7+
};
8+
9+
export const generateGDScriptPathsFile = (
10+
gdScripts: GDScript[],
11+
outputPath: string,
12+
) => {
13+
const imports = gdScripts.map(({ class_name }) => class_name).join(",");
14+
const pathTypes = gdScripts
15+
.map(({ filePath }) => getResourcePath(filePath))
16+
.join("\n| ");
17+
const classTypes = gdScripts.map(({ class_name }) => class_name).join("\n| ");
18+
19+
const fileContent = `import { ResourceLoader } from "godot";
20+
import { ${imports} } from "gdscript";
21+
22+
type GDScriptPaths = ${pathTypes} | string;
23+
24+
type GDScriptClasses = ${classTypes} | unknown;
25+
26+
/**
27+
* Instantiate a GDScript class with \`.new()\`.
28+
* @param path Local path to the GDScript file.
29+
*/
30+
export function instantiate_gdscript<T extends GDScriptClasses>(
31+
path: GDScriptPaths ,
32+
): T {
33+
return ResourceLoader.load(path).call("new") as T;
34+
}
35+
`;
36+
37+
const resolvedPath = resolve(outputPath);
38+
writeFileSync(resolvedPath, fileContent, "utf-8");
39+
};

0 commit comments

Comments
 (0)