@nanoforge-dev/schematics 2.1.2 → 2.1.4-beta.af6c1e0

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.
Files changed (43) hide show
  1. package/dist/index.cjs +15 -46
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +3 -1
  4. package/dist/index.d.cts.map +1 -0
  5. package/dist/index.d.ts +3 -1
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +10 -15
  8. package/dist/index.js.map +1 -1
  9. package/dist/libs/application/application.factory.d.ts +6 -13
  10. package/dist/libs/application/application.factory.d.ts.map +1 -0
  11. package/dist/libs/application/application.factory.js +49 -68
  12. package/dist/libs/application/application.factory.js.map +1 -1
  13. package/dist/libs/component/component.factory.d.ts +6 -6
  14. package/dist/libs/component/component.factory.d.ts.map +1 -0
  15. package/dist/libs/component/component.factory.js +32 -47
  16. package/dist/libs/component/component.factory.js.map +1 -1
  17. package/dist/libs/configuration/configuration.factory.d.ts +6 -7
  18. package/dist/libs/configuration/configuration.factory.d.ts.map +1 -0
  19. package/dist/libs/configuration/configuration.factory.js +91 -133
  20. package/dist/libs/configuration/configuration.factory.js.map +1 -1
  21. package/dist/libs/docker/docker.factory.d.ts +6 -4
  22. package/dist/libs/docker/docker.factory.d.ts.map +1 -0
  23. package/dist/libs/docker/docker.factory.js +17 -36
  24. package/dist/libs/docker/docker.factory.js.map +1 -1
  25. package/dist/libs/part-base/files/js/__nanoforgeFolder__/client.save.json +2 -2
  26. package/dist/libs/part-base/files/js/__nanoforgeFolder__/server.save.json +2 -2
  27. package/dist/libs/part-base/files/js/__part__/components/example.component.js +21 -27
  28. package/dist/libs/part-base/files/ts/__nanoforgeFolder__/client.save.json +2 -2
  29. package/dist/libs/part-base/files/ts/__nanoforgeFolder__/server.save.json +2 -2
  30. package/dist/libs/part-base/files/ts/__part__/components/example.component.ts +21 -27
  31. package/dist/libs/part-base/part-base.factory.d.ts +6 -7
  32. package/dist/libs/part-base/part-base.factory.d.ts.map +1 -0
  33. package/dist/libs/part-base/part-base.factory.js +30 -41
  34. package/dist/libs/part-base/part-base.factory.js.map +1 -1
  35. package/dist/libs/part-main/part-main.factory.d.ts +6 -9
  36. package/dist/libs/part-main/part-main.factory.d.ts.map +1 -0
  37. package/dist/libs/part-main/part-main.factory.js +225 -260
  38. package/dist/libs/part-main/part-main.factory.js.map +1 -1
  39. package/dist/libs/system/system.factory.d.ts +6 -6
  40. package/dist/libs/system/system.factory.d.ts.map +1 -0
  41. package/dist/libs/system/system.factory.js +32 -50
  42. package/dist/libs/system/system.factory.js.map +1 -1
  43. package/package.json +18 -18
@@ -1,44 +1,33 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/libs/part-base/part-base.factory.ts
5
1
  import { join, normalize, strings } from "@angular-devkit/core";
6
- import {
7
- apply,
8
- filter,
9
- mergeWith,
10
- move,
11
- template,
12
- url
13
- } from "@angular-devkit/schematics";
14
- var transform = /* @__PURE__ */ __name((schema) => {
15
- return {
16
- part: schema.part,
17
- server: schema.server,
18
- appClass: schema.part === "client" ? "NanoforgeClient" : "NanoforgeServer",
19
- nanoforgeFolder: ".nanoforge"
20
- };
21
- }, "transform");
22
- var generate = /* @__PURE__ */ __name((options, path, initFunctions, language) => {
23
- const rules = [
24
- template({
25
- ...strings,
26
- ...options
27
- }),
28
- move(normalize(path)),
29
- filter((path2) => {
30
- const splited = path2.split("/");
31
- return splited.at(-2) !== ".nanoforge" || splited.at(-1) === `${options.part}.save.json`;
32
- })
33
- ];
34
- if (!initFunctions) rules.push(filter((path2) => path2.split("/").at(-2) !== "init"));
35
- return apply(url(join("./files", language)), rules);
36
- }, "generate");
37
- var main = /* @__PURE__ */ __name((schema) => {
38
- const options = transform(schema);
39
- return mergeWith(generate(options, schema.directory, schema.initFunctions, schema.language));
40
- }, "main");
41
- export {
42
- main
2
+ import { apply, filter, mergeWith, move, template, url } from "@angular-devkit/schematics";
3
+ //#region src/libs/part-base/part-base.factory.ts
4
+ const transform = (schema) => {
5
+ return {
6
+ part: schema.part,
7
+ server: schema.server,
8
+ appClass: schema.part === "client" ? "NanoforgeClient" : "NanoforgeServer",
9
+ nanoforgeFolder: ".nanoforge"
10
+ };
11
+ };
12
+ const generate = (options, path, initFunctions, language) => {
13
+ const rules = [
14
+ template({
15
+ ...strings,
16
+ ...options
17
+ }),
18
+ move(normalize(path)),
19
+ filter((path) => {
20
+ const splited = path.split("/");
21
+ return splited.at(-2) !== ".nanoforge" || splited.at(-1) === `${options.part}.save.json`;
22
+ })
23
+ ];
24
+ if (!initFunctions) rules.push(filter((path) => path.split("/").at(-2) !== "init"));
25
+ return apply(url(join("./files", language)), rules);
43
26
  };
27
+ const main = (schema) => {
28
+ return mergeWith(generate(transform(schema), schema.directory, schema.initFunctions, schema.language));
29
+ };
30
+ //#endregion
31
+ export { main };
32
+
44
33
  //# sourceMappingURL=part-base.factory.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/libs/part-base/part-base.factory.ts"],"sourcesContent":["import { type Path, join, normalize, strings } from \"@angular-devkit/core\";\nimport {\n type Rule,\n type Source,\n apply,\n filter,\n mergeWith,\n move,\n template,\n url,\n} from \"@angular-devkit/schematics\";\n\nimport { type PartBaseOptions } from \"./part-base.options\";\nimport { type PartBaseSchema } from \"./part-base.schema\";\n\nconst transform = (schema: PartBaseSchema): PartBaseOptions => {\n return {\n part: schema.part,\n server: schema.server,\n appClass: schema.part === \"client\" ? \"NanoforgeClient\" : \"NanoforgeServer\",\n nanoforgeFolder: \".nanoforge\",\n };\n};\n\nconst generate = (\n options: PartBaseOptions,\n path: string,\n initFunctions: boolean,\n language: \"ts\" | \"js\",\n): Source => {\n const rules = [\n template({\n ...strings,\n ...options,\n }),\n move(normalize(path)),\n filter((path) => {\n const splited = path.split(\"/\");\n return splited.at(-2) !== \".nanoforge\" || splited.at(-1) === `${options.part}.save.json`;\n }),\n ];\n if (!initFunctions) rules.push(filter((path) => path.split(\"/\").at(-2) !== \"init\"));\n\n return apply(url(join(\"./files\" as Path, language)), rules);\n};\n\nexport const main = (schema: PartBaseSchema): Rule => {\n const options = transform(schema);\n\n return mergeWith(generate(options, schema.directory, schema.initFunctions, schema.language));\n};\n"],"mappings":";;;;AAAA,SAAoB,MAAM,WAAW,eAAe;AACpD;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAKP,IAAM,YAAY,wBAAC,WAA4C;AAC7D,SAAO;AAAA,IACL,MAAM,OAAO;AAAA,IACb,QAAQ,OAAO;AAAA,IACf,UAAU,OAAO,SAAS,WAAW,oBAAoB;AAAA,IACzD,iBAAiB;AAAA,EACnB;AACF,GAPkB;AASlB,IAAM,WAAW,wBACf,SACA,MACA,eACA,aACW;AACX,QAAM,QAAQ;AAAA,IACZ,SAAS;AAAA,MACP,GAAG;AAAA,MACH,GAAG;AAAA,IACL,CAAC;AAAA,IACD,KAAK,UAAU,IAAI,CAAC;AAAA,IACpB,OAAO,CAACA,UAAS;AACf,YAAM,UAAUA,MAAK,MAAM,GAAG;AAC9B,aAAO,QAAQ,GAAG,EAAE,MAAM,gBAAgB,QAAQ,GAAG,EAAE,MAAM,GAAG,QAAQ,IAAI;AAAA,IAC9E,CAAC;AAAA,EACH;AACA,MAAI,CAAC,cAAe,OAAM,KAAK,OAAO,CAACA,UAASA,MAAK,MAAM,GAAG,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAElF,SAAO,MAAM,IAAI,KAAK,WAAmB,QAAQ,CAAC,GAAG,KAAK;AAC5D,GApBiB;AAsBV,IAAM,OAAO,wBAAC,WAAiC;AACpD,QAAM,UAAU,UAAU,MAAM;AAEhC,SAAO,UAAU,SAAS,SAAS,OAAO,WAAW,OAAO,eAAe,OAAO,QAAQ,CAAC;AAC7F,GAJoB;","names":["path"]}
1
+ {"version":3,"file":"part-base.factory.js","names":[],"sources":["../../../src/libs/part-base/part-base.factory.ts"],"sourcesContent":["import { type Path, join, normalize, strings } from \"@angular-devkit/core\";\nimport {\n type Rule,\n type Source,\n apply,\n filter,\n mergeWith,\n move,\n template,\n url,\n} from \"@angular-devkit/schematics\";\n\nimport { type PartBaseOptions } from \"./part-base.options\";\nimport { type PartBaseSchema } from \"./part-base.schema\";\n\nconst transform = (schema: PartBaseSchema): PartBaseOptions => {\n return {\n part: schema.part,\n server: schema.server,\n appClass: schema.part === \"client\" ? \"NanoforgeClient\" : \"NanoforgeServer\",\n nanoforgeFolder: \".nanoforge\",\n };\n};\n\nconst generate = (\n options: PartBaseOptions,\n path: string,\n initFunctions: boolean,\n language: \"ts\" | \"js\",\n): Source => {\n const rules = [\n template({\n ...strings,\n ...options,\n }),\n move(normalize(path)),\n filter((path) => {\n const splited = path.split(\"/\");\n return splited.at(-2) !== \".nanoforge\" || splited.at(-1) === `${options.part}.save.json`;\n }),\n ];\n if (!initFunctions) rules.push(filter((path) => path.split(\"/\").at(-2) !== \"init\"));\n\n return apply(url(join(\"./files\" as Path, language)), rules);\n};\n\nexport const main = (schema: PartBaseSchema): Rule => {\n const options = transform(schema);\n\n return mergeWith(generate(options, schema.directory, schema.initFunctions, schema.language));\n};\n"],"mappings":";;;AAeA,MAAM,aAAa,WAA4C;CAC7D,OAAO;EACL,MAAM,OAAO;EACb,QAAQ,OAAO;EACf,UAAU,OAAO,SAAS,WAAW,oBAAoB;EACzD,iBAAiB;CACnB;AACF;AAEA,MAAM,YACJ,SACA,MACA,eACA,aACW;CACX,MAAM,QAAQ;EACZ,SAAS;GACP,GAAG;GACH,GAAG;EACL,CAAC;EACD,KAAK,UAAU,IAAI,CAAC;EACpB,QAAQ,SAAS;GACf,MAAM,UAAU,KAAK,MAAM,GAAG;GAC9B,OAAO,QAAQ,GAAG,EAAE,MAAM,gBAAgB,QAAQ,GAAG,EAAE,MAAM,GAAG,QAAQ,KAAK;EAC/E,CAAC;CACH;CACA,IAAI,CAAC,eAAe,MAAM,KAAK,QAAQ,SAAS,KAAK,MAAM,GAAG,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;CAElF,OAAO,MAAM,IAAI,KAAK,WAAmB,QAAQ,CAAC,GAAG,KAAK;AAC5D;AAEA,MAAa,QAAQ,WAAiC;CAGpD,OAAO,UAAU,SAFD,UAAU,MAEA,GAAS,OAAO,WAAW,OAAO,eAAe,OAAO,QAAQ,CAAC;AAC7F"}
@@ -1,42 +1,39 @@
1
- import { Rule } from '@angular-devkit/schematics';
1
+ import { Rule } from "@angular-devkit/schematics";
2
2
 
3
+ //#region src/libs/part-main/part-main.schema.d.ts
3
4
  interface PartMainSchema {
4
5
  /**
5
6
  * NanoForge application destination directory
6
7
  */
7
8
  directory: string;
8
-
9
9
  /**
10
10
  * The part of the application to generate
11
11
  */
12
12
  part: "client" | "server";
13
-
14
13
  /**
15
14
  * NanoForge Application language
16
15
  */
17
16
  language: "js" | "ts";
18
-
19
17
  /**
20
18
  * Add init functions to the application
21
19
  */
22
20
  initFunctions: boolean;
23
-
24
21
  /**
25
22
  * Save file path with components and systems in JSON format
26
23
  */
27
24
  saveFile?: string;
28
-
29
25
  /**
30
26
  * Path of the out file
31
27
  */
32
28
  outFile?: string;
33
-
34
29
  /**
35
30
  * Build main editor
36
31
  */
37
32
  editor: boolean;
38
33
  }
39
-
34
+ //#endregion
35
+ //#region src/libs/part-main/part-main.factory.d.ts
40
36
  declare const main: (schema: PartMainSchema) => Rule;
41
-
37
+ //#endregion
42
38
  export { main };
39
+ //# sourceMappingURL=part-main.factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"part-main.factory.d.ts","names":["directory","part","language","initFunctions","saveFile","outFile","editor"],"sources":["../../../src/libs/part-main/part-main.schema.d.ts","../../../src/libs/part-main/part-main.factory.ts"],"mappings":";;;UAAiB,cAAA;;;AAAjB;EAIEA,SAAAA;EAJ6B;;;EAS7BC,IAAAA;EAeAG;;;EAVAF,QAAAA;;;;EAKAC,aAAAA;;;;EAKAC,QAAAA;EC2CD;AAAA;;EDtCCC,OAAAA;;;;EAKAC,MAAAA;AAAAA;;;cC6BW,IAAA,GAAQ,MAAA,EAAQ,cAAA,KAAiB,IAI7C"}
@@ -1,266 +1,231 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/libs/part-main/part-main.factory.ts
5
- import { join as join3, normalize, strings } from "@angular-devkit/core";
6
- import {
7
- apply,
8
- asSource,
9
- mergeWith,
10
- move,
11
- template
12
- } from "@angular-devkit/schematics";
1
+ import { join, normalize, strings } from "@angular-devkit/core";
2
+ import { apply, asSource, mergeWith, move, template } from "@angular-devkit/schematics";
13
3
  import * as fs from "fs";
14
-
15
- // src/utils/main/main-functions.ts
16
- import { join as join2 } from "path";
17
-
18
- // src/utils/path.ts
19
- import { join } from "path";
20
- var joinRelative = /* @__PURE__ */ __name((...paths) => {
21
- const path = join(...paths);
22
- return path.startsWith("./") || path.startsWith("../") ? path : `./${path}`;
23
- }, "joinRelative");
24
-
25
- // src/utils/main/const.ts
26
- var LIBS_FUNCTIONS_NAME = {
27
- ["asset-manager" /* ASSET_MANAGER */]: "useAssetManager",
28
- ["component-system" /* COMPONENT_SYSTEM */]: "useComponentSystem",
29
- ["graphics" /* GRAPHICS */]: "useGraphics",
30
- ["input" /* INPUT */]: "useInput",
31
- ["network" /* NETWORK */]: "useNetwork",
32
- ["sound" /* SOUND */]: "useSound"
4
+ import { join as join$1 } from "path";
5
+ //#endregion
6
+ //#region src/utils/path.ts
7
+ const joinRelative = (...paths) => {
8
+ const path = join$1(...paths);
9
+ return path.startsWith("./") || path.startsWith("../") ? path : `./${path}`;
33
10
  };
34
-
35
- // src/utils/main/main-utils.ts
36
- var getStringParam = /* @__PURE__ */ __name((value) => {
37
- if (typeof value === "string") return `"${value}"`;
38
- if (typeof value === "number") return value.toString();
39
- if (typeof value === "boolean") return value ? "true" : "false";
40
- if (typeof value === "object") return JSON.stringify(value);
41
- return "undefined";
42
- }, "getStringParam");
43
-
44
- // src/utils/main/main.generator.ts
11
+ //#endregion
12
+ //#region src/utils/main/const.ts
13
+ const LIBS_FUNCTIONS_NAME = {
14
+ ["asset-manager"]: "useAssetManager",
15
+ ["component-system"]: "useComponentSystem",
16
+ ["graphics"]: "useGraphics",
17
+ ["input"]: "useInput",
18
+ ["network"]: "useNetwork",
19
+ ["sound"]: "useSound"
20
+ };
21
+ //#endregion
22
+ //#region src/utils/main/main-utils.ts
23
+ const getStringParam = (value) => {
24
+ if (typeof value === "string") return `"${value}"`;
25
+ if (typeof value === "number") return value.toString();
26
+ if (typeof value === "boolean") return value ? "true" : "false";
27
+ if (typeof value === "object") return JSON.stringify(value);
28
+ return "undefined";
29
+ };
30
+ //#endregion
31
+ //#region src/utils/main/main.generator.ts
45
32
  var MainGenerator = class {
46
- static {
47
- __name(this, "MainGenerator");
48
- }
49
- buffer = "";
50
- indentation = 0;
51
- editor;
52
- pathToRoot;
53
- constructor(editor = false, pathToRoot = "") {
54
- this.editor = editor;
55
- this.pathToRoot = pathToRoot;
56
- }
57
- toString() {
58
- return this.buffer;
59
- }
60
- generateBaseImports(hasTypes) {
61
- if (this.editor) {
62
- if (hasTypes)
63
- this.writeLine(
64
- `import { type IEditorRunOptions, NanoforgeFactory } from "@nanoforge-dev/core-editor";`
65
- );
66
- else this.writeLine(`import { NanoforgeFactory } from "@nanoforge-dev/core-editor";`);
67
- } else {
68
- if (hasTypes) this.writeLine(`import { type IRunOptions } from "@nanoforge-dev/common";`);
69
- this.writeLine(`import { NanoforgeFactory } from "@nanoforge-dev/core";`);
70
- }
71
- this.endSection();
72
- return this;
73
- }
74
- generateLibsImports(libs) {
75
- return this.generateImports(libs, false);
76
- }
77
- generateComponentsImports(libs) {
78
- return this.generateImports(libs, true);
79
- }
80
- generateSystemsImports(libs) {
81
- return this.generateImports(libs, true);
82
- }
83
- generateMainFunction(hasTypes, cb) {
84
- if (this.editor)
85
- this.writeLine(
86
- `export async function main(options${hasTypes ? ": IEditorRunOptions" : ""}) {`
87
- );
88
- else this.writeLine(`export async function main(options${hasTypes ? ": IRunOptions" : ""}) {`);
89
- this.indentation += 1;
90
- cb(this);
91
- this.indentation -= 1;
92
- this.writeLine("}");
93
- return this;
94
- }
95
- generateApp(isServer) {
96
- this.writeLine(`const app = NanoforgeFactory.create${isServer ? "Server" : "Client"}();`);
97
- this.endSection();
98
- return this;
99
- }
100
- generateAppInit() {
101
- this.writeLine(`await app.init(options);`);
102
- this.endSection();
103
- return this;
104
- }
105
- generateAppRun(hasInitFunctions) {
106
- this.writeLine(`await app.run();`);
107
- if (hasInitFunctions) this.endSection();
108
- return this;
109
- }
110
- generateLibsInstances(libs) {
111
- libs.forEach(({ id, name }) => this.writeLine(`const ${id} = new ${name}();`));
112
- this.endSection();
113
- return this;
114
- }
115
- generateLibsInit(libs) {
116
- libs.forEach(({ id, type }) => this.writeLine(this.getInitFunction(id, type)));
117
- this.endSection();
118
- return this;
119
- }
120
- generateRegistry(libs) {
121
- const libName = libs.find((lib) => lib.type === "component-system" /* COMPONENT_SYSTEM */)?.id ?? "ecsLibrary";
122
- this.writeLine(`const registry = ${libName}.registry;`);
123
- this.endSection();
124
- return this;
125
- }
126
- generateEntities(components, entities) {
127
- entities.forEach((entity, index) => this.generateEntity(components, entity, index));
128
- return this;
129
- }
130
- generateSystems(systems) {
131
- systems.forEach(({ name }) => this.writeLine(`registry.addSystem(${name});`));
132
- this.endSection();
133
- return this;
134
- }
135
- generateInitFunctionIfNeeded(needed, func) {
136
- if (needed) return this.generateInitFunction(func);
137
- return this;
138
- }
139
- generateInitFunctionsImportsIfNeeded(needed) {
140
- if (!needed) return this;
141
- return this.generateImports(
142
- [
143
- { name: "afterInit", path: "./init/after-init" },
144
- { name: "afterRegistryInit", path: "./init/after-registry-init" },
145
- { name: "afterRun", path: "./init/after-run" },
146
- { name: "beforeInit", path: "./init/before-init" },
147
- { name: "beforeRegistryInit", path: "./init/before-registry-init" },
148
- { name: "beforeRun", path: "./init/before-run" }
149
- ],
150
- true
151
- );
152
- }
153
- generateInitFunction(func) {
154
- const params = ["app"];
155
- if (["beforeRegistryInit" /* BEFORE_REGISTRY_INIT */, "afterRegistryInit" /* AFTER_REGISTRY_INIT */].includes(func))
156
- params.push("registry");
157
- this.writeLine(`await ${func}(${params.join(", ")});`);
158
- if (func !== "afterRun" /* AFTER_RUN */) this.endSection();
159
- return this;
160
- }
161
- generateImports(els, relative) {
162
- els.sort((a, b) => a.path.localeCompare(b.path)).forEach(
163
- ({ name, path }) => this.writeLine(
164
- `import { ${name} } from "${relative ? joinRelative(this.pathToRoot, path) : path}";`
165
- )
166
- );
167
- this.endSection();
168
- return this;
169
- }
170
- generateEntity(components, entity, entityIndex) {
171
- this.writeLine(`const ${entity.id} = registry.spawnEntity();`);
172
- Object.entries(entity.components).forEach(([componentName, rawParams]) => {
173
- const originalsParams = components.find(({ name }) => name === componentName)?.paramsNames;
174
- if (!originalsParams)
175
- throw new Error("Missing component in entity or inexistent saved component");
176
- const params = !this.editor ? Object.values(originalsParams).map((ogParam) => {
177
- return getStringParam(rawParams[ogParam]);
178
- }) : Object.values(originalsParams).map((ogParam) => {
179
- return `options.editor.save.entities[${entityIndex}].components["${componentName}"]["${ogParam}"]`;
180
- });
181
- this.writeLine(
182
- `registry.addComponent(${entity.id}, new ${componentName}(${params.join(", ")}));`
183
- );
184
- });
185
- if (this.editor) {
186
- this.writeLine(
187
- `registry.addComponent(${entity.id}, { name: "__RESERVED_entityId", entityId: "${entity.id}" });`
188
- );
189
- }
190
- this.endSection();
191
- }
192
- getInitFunction(name, type) {
193
- const baseName = LIBS_FUNCTIONS_NAME[type];
194
- if (baseName) return `app.${baseName}(${name});`;
195
- return `app.use(Symbol("${type}"), ${name});`;
196
- }
197
- writeLine(text, indentation = true) {
198
- const line = `${" ".repeat(indentation ? this.indentation * 2 : 0)}${text}
199
- `;
200
- this.buffer += line;
201
- }
202
- endSection() {
203
- this.writeLine("");
204
- }
33
+ buffer = "";
34
+ indentation = 0;
35
+ editor;
36
+ pathToRoot;
37
+ constructor(editor = false, pathToRoot = "") {
38
+ this.editor = editor;
39
+ this.pathToRoot = pathToRoot;
40
+ }
41
+ toString() {
42
+ return this.buffer;
43
+ }
44
+ generateBaseImports(hasTypes) {
45
+ if (this.editor) if (hasTypes) this.writeLine(`import { type IEditorRunOptions, NanoforgeFactory } from "@nanoforge-dev/core-editor";`);
46
+ else this.writeLine(`import { NanoforgeFactory } from "@nanoforge-dev/core-editor";`);
47
+ else {
48
+ if (hasTypes) this.writeLine(`import { type IRunOptions } from "@nanoforge-dev/common";`);
49
+ this.writeLine(`import { NanoforgeFactory } from "@nanoforge-dev/core";`);
50
+ }
51
+ this.endSection();
52
+ return this;
53
+ }
54
+ generateLibsImports(libs) {
55
+ return this.generateImports(libs, false);
56
+ }
57
+ generateComponentsImports(libs) {
58
+ return this.generateImports(libs, true);
59
+ }
60
+ generateSystemsImports(libs) {
61
+ return this.generateImports(libs, true);
62
+ }
63
+ generateMainFunction(hasTypes, cb) {
64
+ if (this.editor) this.writeLine(`export async function main(options${hasTypes ? ": IEditorRunOptions" : ""}) {`);
65
+ else this.writeLine(`export async function main(options${hasTypes ? ": IRunOptions" : ""}) {`);
66
+ this.indentation += 1;
67
+ cb(this);
68
+ this.indentation -= 1;
69
+ this.writeLine("}");
70
+ return this;
71
+ }
72
+ generateApp(isServer) {
73
+ this.writeLine(`const app = NanoforgeFactory.create${isServer ? "Server" : "Client"}();`);
74
+ this.endSection();
75
+ return this;
76
+ }
77
+ generateAppInit() {
78
+ this.writeLine(`await app.init(options);`);
79
+ this.endSection();
80
+ return this;
81
+ }
82
+ generateAppRun(hasInitFunctions) {
83
+ this.writeLine(`await app.run();`);
84
+ if (hasInitFunctions) this.endSection();
85
+ return this;
86
+ }
87
+ generateLibsInstances(libs) {
88
+ libs.forEach(({ id, name }) => this.writeLine(`const ${id} = new ${name}();`));
89
+ this.endSection();
90
+ return this;
91
+ }
92
+ generateLibsInit(libs) {
93
+ libs.forEach(({ id, type }) => this.writeLine(this.getInitFunction(id, type)));
94
+ this.endSection();
95
+ return this;
96
+ }
97
+ generateRegistry(libs) {
98
+ const libName = libs.find((lib) => lib.type === "component-system")?.id ?? "ecsLibrary";
99
+ this.writeLine(`const registry = ${libName}.registry;`);
100
+ this.endSection();
101
+ return this;
102
+ }
103
+ generateEntities(components, entities) {
104
+ entities.forEach((entity, index) => this.generateEntity(components, entity, index));
105
+ return this;
106
+ }
107
+ generateSystems(systems) {
108
+ systems.forEach(({ name }) => this.writeLine(`registry.addSystem(${name});`));
109
+ this.endSection();
110
+ return this;
111
+ }
112
+ generateInitFunctionIfNeeded(needed, func) {
113
+ if (needed) return this.generateInitFunction(func);
114
+ return this;
115
+ }
116
+ generateInitFunctionsImportsIfNeeded(needed) {
117
+ if (!needed) return this;
118
+ return this.generateImports([
119
+ {
120
+ name: "afterInit",
121
+ path: "./init/after-init"
122
+ },
123
+ {
124
+ name: "afterRegistryInit",
125
+ path: "./init/after-registry-init"
126
+ },
127
+ {
128
+ name: "afterRun",
129
+ path: "./init/after-run"
130
+ },
131
+ {
132
+ name: "beforeInit",
133
+ path: "./init/before-init"
134
+ },
135
+ {
136
+ name: "beforeRegistryInit",
137
+ path: "./init/before-registry-init"
138
+ },
139
+ {
140
+ name: "beforeRun",
141
+ path: "./init/before-run"
142
+ }
143
+ ], true);
144
+ }
145
+ generateInitFunction(func) {
146
+ const params = ["app"];
147
+ if (["beforeRegistryInit", "afterRegistryInit"].includes(func)) params.push("registry");
148
+ this.writeLine(`await ${func}(${params.join(", ")});`);
149
+ if (func !== "afterRun") this.endSection();
150
+ return this;
151
+ }
152
+ generateImports(els, relative) {
153
+ els.sort((a, b) => a.path.localeCompare(b.path)).forEach(({ name, path }) => this.writeLine(`import { ${name} } from "${relative ? joinRelative(this.pathToRoot, path) : path}";`));
154
+ this.endSection();
155
+ return this;
156
+ }
157
+ generateEntity(components, entity, entityIndex) {
158
+ this.writeLine(`const ${entity.id} = registry.spawnEntity();`);
159
+ Object.entries(entity.components).forEach(([componentName, rawParams]) => {
160
+ const originalsParams = components.find(({ name }) => name === componentName)?.paramsNames;
161
+ if (!originalsParams) throw new Error("Missing component in entity or inexistent saved component");
162
+ const params = !this.editor ? Object.values(originalsParams).map((ogParam) => {
163
+ return getStringParam(rawParams[ogParam]);
164
+ }) : Object.values(originalsParams).map((ogParam) => {
165
+ return `options.editor.save.entities[${entityIndex}].components["${componentName}"]["${ogParam}"]`;
166
+ });
167
+ this.writeLine(`registry.addComponent(${entity.id}, new ${componentName}(${params.join(", ")}));`);
168
+ });
169
+ if (this.editor) this.writeLine(`registry.addComponent(${entity.id}, { name: "__RESERVED_entityId", entityId: "${entity.id}" });`);
170
+ this.endSection();
171
+ }
172
+ getInitFunction(name, type) {
173
+ const baseName = LIBS_FUNCTIONS_NAME[type];
174
+ if (baseName) return `app.${baseName}(${name});`;
175
+ return `app.use(Symbol("${type}"), ${name});`;
176
+ }
177
+ writeLine(text, indentation = true) {
178
+ const line = `${" ".repeat(indentation ? this.indentation * 2 : 0)}${text}\n`;
179
+ this.buffer += line;
180
+ }
181
+ endSection() {
182
+ this.writeLine("");
183
+ }
205
184
  };
206
-
207
- // src/utils/main/main-functions.ts
208
- var generateMain = /* @__PURE__ */ __name((options, save) => {
209
- const isServer = options.part === "server";
210
- const hasTypes = options.language === "ts";
211
- const initFunctions = options.initFunctions;
212
- return (!options.editor ? new MainGenerator() : new MainGenerator(
213
- true,
214
- join2(
215
- "../".repeat(options.outFile ? options.outFile.split("/").length - 1 : 3),
216
- options.part
217
- )
218
- )).generateBaseImports(hasTypes).generateLibsImports(save.libraries).generateInitFunctionsImportsIfNeeded(initFunctions).generateComponentsImports(save.components).generateSystemsImports(save.systems).generateMainFunction(hasTypes, (generator) => {
219
- generator.generateApp(isServer).generateLibsInstances(save.libraries).generateLibsInit(save.libraries).generateInitFunctionIfNeeded(initFunctions, "beforeInit" /* BEFORE_INIT */).generateAppInit().generateInitFunctionIfNeeded(initFunctions, "afterInit" /* AFTER_INIT */).generateRegistry(save.libraries).generateInitFunctionIfNeeded(initFunctions, "beforeRegistryInit" /* BEFORE_REGISTRY_INIT */).generateEntities(save.components, save.entities).generateSystems(save.systems).generateInitFunctionIfNeeded(initFunctions, "afterRegistryInit" /* AFTER_REGISTRY_INIT */).generateInitFunctionIfNeeded(initFunctions, "beforeRun" /* BEFORE_RUN */).generateAppRun(initFunctions).generateInitFunctionIfNeeded(initFunctions, "afterRun" /* AFTER_RUN */);
220
- }).toString();
221
- }, "generateMain");
222
-
223
- // src/libs/part-main/part-main.factory.ts
224
- var transform = /* @__PURE__ */ __name((schema) => {
225
- void schema;
226
- return {};
227
- }, "transform");
228
- var generate = /* @__PURE__ */ __name((options, path, schema) => {
229
- const rules = [
230
- template({
231
- ...strings,
232
- ...options
233
- }),
234
- move(normalize(path))
235
- ];
236
- return apply(asSource(writeMain(schema, path)), rules);
237
- }, "generate");
238
- var getMainPath = /* @__PURE__ */ __name((schema) => {
239
- if (schema.outFile) return schema.outFile;
240
- if (schema.editor)
241
- return join3(".nanoforge/editor", schema.part, `main.${schema.language}`);
242
- return join3(schema.part, `main.${schema.language}`);
243
- }, "getMainPath");
244
- var writeMain = /* @__PURE__ */ __name((schema, path) => {
245
- return (tree) => {
246
- const save = getSave(path, schema.part, schema.saveFile);
247
- const resPath = getMainPath(schema);
248
- const content = generateMain(schema, save);
249
- fs.rmSync(join3(path, resPath), { force: true });
250
- tree.create(resPath, content);
251
- return tree;
252
- };
253
- }, "writeMain");
254
- var getSave = /* @__PURE__ */ __name((path, part, saveFile) => {
255
- return JSON.parse(
256
- fs.readFileSync(saveFile ?? join3(path, `.nanoforge/${part}.save.json`), "utf-8")
257
- );
258
- }, "getSave");
259
- var main = /* @__PURE__ */ __name((schema) => {
260
- const options = transform(schema);
261
- return mergeWith(generate(options, schema.directory, schema));
262
- }, "main");
263
- export {
264
- main
185
+ //#endregion
186
+ //#region src/utils/main/main-functions.ts
187
+ const generateMain = (options, save) => {
188
+ const isServer = options.part === "server";
189
+ const hasTypes = options.language === "ts";
190
+ const initFunctions = options.initFunctions;
191
+ return (!options.editor ? new MainGenerator() : new MainGenerator(true, join$1("../".repeat(options.outFile ? options.outFile.split("/").length - 1 : 3), options.part))).generateBaseImports(hasTypes).generateLibsImports(save.libraries).generateInitFunctionsImportsIfNeeded(initFunctions).generateComponentsImports(save.components).generateSystemsImports(save.systems).generateMainFunction(hasTypes, (generator) => {
192
+ generator.generateApp(isServer).generateLibsInstances(save.libraries).generateLibsInit(save.libraries).generateInitFunctionIfNeeded(initFunctions, "beforeInit").generateAppInit().generateInitFunctionIfNeeded(initFunctions, "afterInit").generateRegistry(save.libraries).generateInitFunctionIfNeeded(initFunctions, "beforeRegistryInit").generateEntities(save.components, save.entities).generateSystems(save.systems).generateInitFunctionIfNeeded(initFunctions, "afterRegistryInit").generateInitFunctionIfNeeded(initFunctions, "beforeRun").generateAppRun(initFunctions).generateInitFunctionIfNeeded(initFunctions, "afterRun");
193
+ }).toString();
194
+ };
195
+ //#endregion
196
+ //#region src/libs/part-main/part-main.factory.ts
197
+ const transform = (schema) => {
198
+ return {};
199
+ };
200
+ const generate = (options, path, schema) => {
201
+ const rules = [template({
202
+ ...strings,
203
+ ...options
204
+ }), move(normalize(path))];
205
+ return apply(asSource(writeMain(schema, path)), rules);
206
+ };
207
+ const getMainPath = (schema) => {
208
+ if (schema.outFile) return schema.outFile;
209
+ if (schema.editor) return join(".nanoforge/editor", schema.part, `main.${schema.language}`);
210
+ return join(schema.part, `main.${schema.language}`);
265
211
  };
212
+ const writeMain = (schema, path) => {
213
+ return (tree) => {
214
+ const save = getSave(path, schema.part, schema.saveFile);
215
+ const resPath = getMainPath(schema);
216
+ const content = generateMain(schema, save);
217
+ fs.rmSync(join(path, resPath), { force: true });
218
+ tree.create(resPath, content);
219
+ return tree;
220
+ };
221
+ };
222
+ const getSave = (path, part, saveFile) => {
223
+ return JSON.parse(fs.readFileSync(saveFile ?? join(path, `.nanoforge/${part}.save.json`), "utf-8"));
224
+ };
225
+ const main = (schema) => {
226
+ return mergeWith(generate(transform(schema), schema.directory, schema));
227
+ };
228
+ //#endregion
229
+ export { main };
230
+
266
231
  //# sourceMappingURL=part-main.factory.js.map