|
| 1 | +API Reference |
| 2 | +============= |
| 3 | + |
| 4 | +This section documents the internal utility modules and classes available in the |
| 5 | +``src/utils/`` directory. |
| 6 | + |
| 7 | +MainGenerator |
| 8 | +------------- |
| 9 | + |
| 10 | +**Module**: ``src/utils/main/main.generator.ts`` |
| 11 | + |
| 12 | +A builder-pattern class that programmatically generates the contents of a |
| 13 | +NanoForge ``main.ts`` entry point file. Used by the |
| 14 | +:ref:`part-main <schematic-part-main>` schematic. |
| 15 | + |
| 16 | +Methods |
| 17 | +^^^^^^^ |
| 18 | + |
| 19 | +.. method:: generateBaseImports(hasTypes: boolean): MainGenerator |
| 20 | + |
| 21 | + Adds the base NanoForge imports (``IRunOptions`` from ``@nanoforge-dev/common`` |
| 22 | + and ``NanoforgeFactory`` from ``@nanoforge-dev/core``). Type imports are |
| 23 | + conditional on the ``hasTypes`` flag (true for TypeScript, false for |
| 24 | + JavaScript). |
| 25 | + |
| 26 | +.. method:: generateLibsImports(libs: SaveLibrary[]): MainGenerator |
| 27 | + |
| 28 | + Generates import statements for all libraries listed in the save file, |
| 29 | + sorted alphabetically by import path. |
| 30 | + |
| 31 | +.. method:: generateComponentsImports(components: SaveComponent[]): MainGenerator |
| 32 | + |
| 33 | + Generates import statements for all components, sorted by path. |
| 34 | + |
| 35 | +.. method:: generateSystemsImports(systems: SaveSystem[]): MainGenerator |
| 36 | + |
| 37 | + Generates import statements for all systems, sorted by path. |
| 38 | + |
| 39 | +.. method:: generateMainFunction(hasTypes: boolean, cb: (generator: MainGenerator) => void): MainGenerator |
| 40 | + |
| 41 | + Wraps a callback's output inside an ``export async function main(options) { ... }`` |
| 42 | + block. The ``hasTypes`` flag controls whether the ``options`` parameter |
| 43 | + receives a type annotation. |
| 44 | + |
| 45 | +.. method:: generateApp(isServer: boolean): MainGenerator |
| 46 | + |
| 47 | + Emits ``const app = NanoforgeFactory.createClient()`` or |
| 48 | + ``NanoforgeFactory.createServer()`` depending on the ``isServer`` flag. |
| 49 | + |
| 50 | +.. method:: generateAppInit(): MainGenerator |
| 51 | + |
| 52 | + Emits ``await app.init(options);``. |
| 53 | + |
| 54 | +.. method:: generateAppRun(hasInitFunctions: boolean): MainGenerator |
| 55 | + |
| 56 | + Emits ``await app.run();``. |
| 57 | + |
| 58 | +.. method:: generateLibsInstances(libs: SaveLibrary[]): MainGenerator |
| 59 | + |
| 60 | + Emits ``const <id> = new <Name>();`` for each library. |
| 61 | + |
| 62 | +.. method:: generateLibsInit(libs: SaveLibrary[]): MainGenerator |
| 63 | + |
| 64 | + Emits the appropriate ``app.use*(<id>);`` call for each library based on |
| 65 | + its type (see :ref:`Library Type Mapping <schematic-part-main>`). |
| 66 | + |
| 67 | +.. method:: generateRegistry(libs: SaveLibrary[]): MainGenerator |
| 68 | + |
| 69 | + Emits ``const registry = <ecsLib>.registry;`` where ``<ecsLib>`` is the |
| 70 | + library with type ``component-system``. |
| 71 | + |
| 72 | +.. method:: generateEntities(entities: SaveEntity[]): MainGenerator |
| 73 | + |
| 74 | + For each entity, emits ``registry.spawnEntity()`` followed by |
| 75 | + ``registry.addComponent()`` calls for each component with its parameters. |
| 76 | + |
| 77 | +.. method:: generateSystems(systems: SaveSystem[]): MainGenerator |
| 78 | + |
| 79 | + Emits ``registry.addSystem(<name>);`` for each system. |
| 80 | + |
| 81 | +.. method:: generateInitFunctionIfNeeded(needed: boolean, func: InitFunctionEnum): MainGenerator |
| 82 | + |
| 83 | + Conditionally emits an ``await <func>(app);`` or |
| 84 | + ``await <func>(app, registry);`` call based on the init function type. |
| 85 | + |
| 86 | +.. method:: generateInitFunctionsImportsIfNeeded(needed: boolean): MainGenerator |
| 87 | + |
| 88 | + Conditionally emits import statements for all six lifecycle init functions. |
| 89 | + |
| 90 | +.. method:: toString(): string |
| 91 | + |
| 92 | + Returns the accumulated generated code as a string. |
| 93 | + |
| 94 | +ConfigFinder |
| 95 | +------------ |
| 96 | + |
| 97 | +**Module**: ``src/utils/config/config.finder.ts`` |
| 98 | + |
| 99 | +Searches the virtual file tree for an existing ``nanoforge.config.json``. |
| 100 | + |
| 101 | +.. method:: find(tree: Tree, path: string): Config | null |
| 102 | + |
| 103 | + Recursively searches from the given path upward through parent directories |
| 104 | + for a ``nanoforge.config.json`` file. Returns the parsed ``Config`` object |
| 105 | + if found, or ``null`` if no config exists in the tree. |
| 106 | + |
| 107 | +ConfigDeclarator |
| 108 | +---------------- |
| 109 | + |
| 110 | +**Module**: ``src/utils/config/config.declarator.ts`` |
| 111 | + |
| 112 | +Modifies a configuration tree by merging server options. |
| 113 | + |
| 114 | +.. method:: declare(tree: Tree, path: string, server: boolean): void |
| 115 | + |
| 116 | + Reads the ``nanoforge.config.json`` at the given path, deep-merges the |
| 117 | + server configuration if ``server`` is ``true``, and writes the result |
| 118 | + back to the tree. |
| 119 | + |
| 120 | +Formatting Utilities |
| 121 | +-------------------- |
| 122 | + |
| 123 | +**Module**: ``src/utils/formatting.ts`` |
| 124 | + |
| 125 | +.. function:: toKebabCase(str: string): string |
| 126 | + |
| 127 | + Converts a string to kebab-case. Used to normalize project names for |
| 128 | + directory and package naming. |
| 129 | + |
| 130 | + Examples:: |
| 131 | + |
| 132 | + toKebabCase("MyProject") // "my-project" |
| 133 | + toKebabCase("some string") // "some-string" |
| 134 | + |
| 135 | +Name Utilities |
| 136 | +-------------- |
| 137 | + |
| 138 | +**Module**: ``src/utils/name.ts`` |
| 139 | + |
| 140 | +.. function:: resolvePackageName(path: string): string |
| 141 | + |
| 142 | + Extracts the package name from a path string. Handles scoped packages |
| 143 | + (``@scope/name``) by returning only the name portion. |
| 144 | + |
| 145 | +Object Utilities |
| 146 | +---------------- |
| 147 | + |
| 148 | +**Module**: ``src/utils/object.ts`` |
| 149 | + |
| 150 | +.. function:: deepMerge(...objects: object[]): object |
| 151 | + |
| 152 | + Recursively merges multiple objects together. Later objects take precedence |
| 153 | + over earlier ones for primitive values. Nested objects are merged recursively |
| 154 | + rather than replaced. |
| 155 | + |
| 156 | +.. function:: isObject(item: unknown): boolean |
| 157 | + |
| 158 | + Type guard that returns ``true`` if the item is a plain object (not an array |
| 159 | + or null). |
| 160 | + |
| 161 | +Type Definitions |
| 162 | +---------------- |
| 163 | + |
| 164 | +**Module**: ``src/utils/type.ts`` |
| 165 | + |
| 166 | +Shared TypeScript type definitions used across schematics. |
| 167 | + |
| 168 | +Enums |
| 169 | +----- |
| 170 | + |
| 171 | +InitFunctionEnum |
| 172 | +^^^^^^^^^^^^^^^^ |
| 173 | + |
| 174 | +**Module**: ``src/utils/main/enums.ts`` |
| 175 | + |
| 176 | +Enumerates the six lifecycle init function names: |
| 177 | + |
| 178 | +.. code-block:: typescript |
| 179 | +
|
| 180 | + enum InitFunctionEnum { |
| 181 | + BEFORE_INIT = "beforeInit", |
| 182 | + AFTER_INIT = "afterInit", |
| 183 | + BEFORE_REGISTRY_INIT = "beforeRegistryInit", |
| 184 | + AFTER_REGISTRY_INIT = "afterRegistryInit", |
| 185 | + BEFORE_RUN = "beforeRun", |
| 186 | + AFTER_RUN = "afterRun", |
| 187 | + } |
| 188 | +
|
| 189 | +SaveLibraryTypeEnum |
| 190 | +^^^^^^^^^^^^^^^^^^^ |
| 191 | + |
| 192 | +**Module**: ``src/utils/main/save.type.ts`` |
| 193 | + |
| 194 | +Enumerates the known library types: |
| 195 | + |
| 196 | +.. code-block:: typescript |
| 197 | +
|
| 198 | + enum SaveLibraryTypeEnum { |
| 199 | + COMPONENT_SYSTEM = "component-system", |
| 200 | + GRAPHICS = "graphics", |
| 201 | + ASSET_MANAGER = "asset-manager", |
| 202 | + NETWORK = "network", |
| 203 | + INPUT = "input", |
| 204 | + SOUND = "sound", |
| 205 | + } |
| 206 | +
|
| 207 | +Constants |
| 208 | +--------- |
| 209 | + |
| 210 | +LIBS_FUNCTIONS_NAME |
| 211 | +^^^^^^^^^^^^^^^^^^^ |
| 212 | + |
| 213 | +**Module**: ``src/utils/main/conts.ts`` |
| 214 | + |
| 215 | +Maps library types to the corresponding ``app.use*()`` method name: |
| 216 | + |
| 217 | +.. code-block:: typescript |
| 218 | +
|
| 219 | + const LIBS_FUNCTIONS_NAME = { |
| 220 | + "component-system": "useComponentSystem", |
| 221 | + "graphics": "useGraphics", |
| 222 | + "asset-manager": "useAssetManager", |
| 223 | + "network": "useNetwork", |
| 224 | + "input": "useInput", |
| 225 | + "sound": "useSound", |
| 226 | + }; |
0 commit comments