@izumisy-tailor/omakase-modules 0.3.0 → 0.4.1
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/README.md +0 -16
- package/docs/generated/README.md +35 -0
- package/docs/{tutorials → generated/_media}/creating-modules.md +192 -17
- package/docs/{tutorials → generated/_media}/using-modules.md +17 -18
- package/docs/generated/builder/README.md +25 -0
- package/docs/generated/builder/functions/defineModule.md +60 -0
- package/docs/generated/builder/functions/withModuleConfiguration.md +68 -0
- package/docs/generated/builder/type-aliases/AnyDefinedModule.md +57 -0
- package/docs/generated/builder/type-aliases/ConfiguredDependencies.md +44 -0
- package/docs/generated/builder/type-aliases/ConfiguredModule.md +60 -0
- package/docs/generated/builder/type-aliases/DefinedModule.md +93 -0
- package/docs/generated/builder/type-aliases/DependencyModules.md +29 -0
- package/docs/generated/builder/type-aliases/EmptyDependencies.md +34 -0
- package/docs/generated/builder/type-aliases/ModuleBuilder.md +124 -0
- package/docs/generated/builder/type-aliases/ModuleBuilderProps.md +42 -0
- package/docs/generated/builder/type-aliases/ModuleFactoryContext.md +40 -0
- package/docs/generated/builder/type-aliases/TablesFromNames.md +28 -0
- package/docs/generated/config/README.md +19 -0
- package/docs/generated/config/classes/ModuleLoader.md +128 -0
- package/docs/generated/config/functions/loadModules.md +79 -0
- package/docs/generated/config/sdk/README.md +16 -0
- package/docs/generated/config/sdk/functions/getModulesReference.md +81 -0
- package/docs/generated/config/sdk/functions/loadModuleForDev.md +53 -0
- package/docs/generated/config/sdk/type-aliases/GetModulesReferenceOptions.md +60 -0
- package/docs/generated/config/type-aliases/LoadedModules.md +162 -0
- package/docs/generated/modules.md +11 -0
- package/package.json +11 -7
- package/src/builder/helpers.ts +347 -27
- package/src/builder/index.ts +6 -1
- package/src/builder/register.ts +3 -6
- package/src/config/module-loader.ts +234 -12
- package/src/config/sdk/dev-context.ts +82 -0
- package/src/config/sdk/index.ts +2 -1
- package/src/config/sdk/paths.ts +85 -3
- package/src/config/sdk/wrapper/base.ts +68 -15
- package/src/config/sdk/wrapper/generator.ts +40 -3
- package/src/config/sdk/wrapper/strategies.ts +32 -13
|
@@ -1,16 +1,93 @@
|
|
|
1
|
-
import { ConfiguredModule } from "../builder/helpers";
|
|
1
|
+
import type { ConfiguredModule } from "../builder/helpers";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Module loader
|
|
5
|
-
*
|
|
4
|
+
* Module loader for registering and configuring modules.
|
|
5
|
+
*
|
|
6
|
+
* The `ModuleLoader` is used within the {@link loadModules} function to register
|
|
7
|
+
* configured modules. Modules can depend on each other, and the loader ensures
|
|
8
|
+
* proper dependency management.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { loadModules } from "@izumisy-tailor/omakase-modules";
|
|
13
|
+
* import commerceModule from "omakase-module-commerce-core";
|
|
14
|
+
* import orderModule from "omakase-module-order";
|
|
15
|
+
* import inventoryModule from "omakase-module-inventory";
|
|
16
|
+
*
|
|
17
|
+
* export default loadModules((loader) => {
|
|
18
|
+
* // Add a module without dependencies
|
|
19
|
+
* const $commerce = loader.add(
|
|
20
|
+
* commerceModule.configure({
|
|
21
|
+
* config: {
|
|
22
|
+
* dataModel: {
|
|
23
|
+
* product: { docNumberPrefix: "PROD" },
|
|
24
|
+
* },
|
|
25
|
+
* },
|
|
26
|
+
* })
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* // Add a module that depends on commerce
|
|
30
|
+
* const $order = loader.add(
|
|
31
|
+
* orderModule.configure({
|
|
32
|
+
* config: { dataModel: {} },
|
|
33
|
+
* dependencies: { commerce: $commerce },
|
|
34
|
+
* })
|
|
35
|
+
* );
|
|
36
|
+
*
|
|
37
|
+
* // Add a module that depends on both commerce and order
|
|
38
|
+
* loader.add(
|
|
39
|
+
* inventoryModule.configure({
|
|
40
|
+
* config: { dbNamespace: "main-db" },
|
|
41
|
+
* dependencies: { commerce: $commerce, order: $order },
|
|
42
|
+
* })
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* return loader;
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
6
48
|
*/
|
|
7
49
|
export class ModuleLoader {
|
|
8
50
|
private modules: Array<ConfiguredModule<any>> = [];
|
|
9
51
|
|
|
10
52
|
/**
|
|
11
|
-
* Add a module to the loader
|
|
12
|
-
*
|
|
13
|
-
*
|
|
53
|
+
* Add a configured module to the loader.
|
|
54
|
+
*
|
|
55
|
+
* Returns the added module so it can be used as a dependency for other modules.
|
|
56
|
+
* This enables type-safe dependency wiring between modules.
|
|
57
|
+
*
|
|
58
|
+
* @param module - A configured module created by calling `moduleDefinition.configure()`
|
|
59
|
+
* @returns The same configured module, to be used as a dependency for other modules
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* loadModules((loader) => {
|
|
64
|
+
* // Capture the returned value to use as a dependency
|
|
65
|
+
* const $commerce = loader.add(
|
|
66
|
+
* commerceModule.configure({
|
|
67
|
+
* config: {
|
|
68
|
+
* dataModel: {
|
|
69
|
+
* product: {
|
|
70
|
+
* docNumberPrefix: "PP-PROD",
|
|
71
|
+
* customAttributes: {
|
|
72
|
+
* customStatus: db.enum(["new", "used", "refurbished"]),
|
|
73
|
+
* },
|
|
74
|
+
* },
|
|
75
|
+
* },
|
|
76
|
+
* },
|
|
77
|
+
* })
|
|
78
|
+
* );
|
|
79
|
+
*
|
|
80
|
+
* // Use the captured module as a dependency
|
|
81
|
+
* loader.add(
|
|
82
|
+
* orderModule.configure({
|
|
83
|
+
* config: { dataModel: {} },
|
|
84
|
+
* dependencies: { commerce: $commerce },
|
|
85
|
+
* })
|
|
86
|
+
* );
|
|
87
|
+
*
|
|
88
|
+
* return loader;
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
14
91
|
*/
|
|
15
92
|
add<C extends Record<string, unknown>>(
|
|
16
93
|
module: ConfiguredModule<C>
|
|
@@ -28,8 +105,59 @@ export class ModuleLoader {
|
|
|
28
105
|
}
|
|
29
106
|
}
|
|
30
107
|
|
|
108
|
+
/**
|
|
109
|
+
* The result of loading modules, providing access to module configurations and tables.
|
|
110
|
+
*
|
|
111
|
+
* This type is returned by {@link loadModules} and passed to module factory functions.
|
|
112
|
+
* It provides utilities for:
|
|
113
|
+
* - Accessing configured module instances
|
|
114
|
+
* - Loading module configurations
|
|
115
|
+
* - Retrieving tables from dependency modules
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // In tailor.config.ts - using getModulesReference with LoadedModules
|
|
120
|
+
* import { getModulesReference } from "@izumisy-tailor/omakase-modules/config/sdk";
|
|
121
|
+
* import modules from "./modules";
|
|
122
|
+
*
|
|
123
|
+
* const moduleReference = await getModulesReference(modules);
|
|
124
|
+
*
|
|
125
|
+
* export default defineConfig({
|
|
126
|
+
* db: {
|
|
127
|
+
* "main-db": {
|
|
128
|
+
* files: ["./src/tailordb/*.ts", ...moduleReference.tailordb],
|
|
129
|
+
* },
|
|
130
|
+
* },
|
|
131
|
+
* // ...
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
31
135
|
export type LoadedModules = {
|
|
32
136
|
loadedModules: Record<string, ConfiguredModule<any>>;
|
|
137
|
+
/**
|
|
138
|
+
* Load the configuration for a specific module.
|
|
139
|
+
*
|
|
140
|
+
* Use this in module factory functions to access the user-provided configuration.
|
|
141
|
+
* Throws an error if the module has not been registered via `loadModules`.
|
|
142
|
+
*
|
|
143
|
+
* @param module - An object with `packageName` (typically the module definition)
|
|
144
|
+
* @returns An object containing the module's configuration
|
|
145
|
+
* @throws Error if the module has not been configured
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* // In a module's tailordb/index.ts
|
|
150
|
+
* import moduleDef from "../module";
|
|
151
|
+
*
|
|
152
|
+
* export default withModuleConfiguration(moduleDef, (context, loadedModules) => {
|
|
153
|
+
* // Access this module's configuration
|
|
154
|
+
* const { config } = loadedModules.loadConfig<ModuleConfig>(moduleDef);
|
|
155
|
+
* const prefix = config.dataModel?.product?.docNumberPrefix ?? "PROD";
|
|
156
|
+
*
|
|
157
|
+
* return { product: buildProductTable(context) };
|
|
158
|
+
* });
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
33
161
|
loadConfig: <C extends Record<string, unknown>>(module: {
|
|
34
162
|
packageName: string;
|
|
35
163
|
}) => {
|
|
@@ -37,20 +165,114 @@ export type LoadedModules = {
|
|
|
37
165
|
};
|
|
38
166
|
/**
|
|
39
167
|
* Get the tables from a dependency module by calling its factory function.
|
|
40
|
-
* This is used when an executor or resolver needs to reference tables from another module.
|
|
41
168
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
169
|
+
* Use this when a module needs to reference tables from another module,
|
|
170
|
+
* such as creating foreign key relationships or using shared types.
|
|
171
|
+
*
|
|
172
|
+
* @param factory - The factory function exported by the dependency module's `tailordb/index.ts`
|
|
173
|
+
* @returns A promise resolving to the tables created by the factory
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* // In inventory-module's tailordb/index.ts
|
|
178
|
+
* import commerceModuleTables from "omakase-module-commerce-core/backend/tailordb";
|
|
179
|
+
* import orderModuleTables from "omakase-module-order/backend/tailordb";
|
|
180
|
+
*
|
|
181
|
+
* export default withModuleConfiguration(
|
|
182
|
+
* moduleDef,
|
|
183
|
+
* async (context, loadedModules) => {
|
|
184
|
+
* // Get tables from commerce module to reference product/productVariant
|
|
185
|
+
* const { product, productVariant } = await loadedModules.getTables(
|
|
186
|
+
* commerceModuleTables
|
|
187
|
+
* );
|
|
188
|
+
*
|
|
189
|
+
* // Get tables from order module to reference order
|
|
190
|
+
* const { order } = await loadedModules.getTables(orderModuleTables);
|
|
191
|
+
*
|
|
192
|
+
* // Use dependency tables when building this module's tables
|
|
193
|
+
* const inventory = buildInventoryTable(context, { product, productVariant });
|
|
194
|
+
* const inventoryTransaction = buildInventoryTransactionTable(context, {
|
|
195
|
+
* inventory,
|
|
196
|
+
* order,
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* return { inventory, inventoryTransaction };
|
|
200
|
+
* }
|
|
201
|
+
* );
|
|
202
|
+
* ```
|
|
44
203
|
*/
|
|
45
204
|
getTables: <T>(
|
|
46
205
|
factory: (loadedModules: LoadedModules) => Promise<T>
|
|
47
206
|
) => Promise<T>;
|
|
207
|
+
/**
|
|
208
|
+
* Indicates this is a dev context created by loadModuleForDev.
|
|
209
|
+
* When true, wrapper generation uses local src paths instead of node_modules.
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
__devContext?: {
|
|
213
|
+
/** The package name of the module being developed */
|
|
214
|
+
moduleName: string;
|
|
215
|
+
};
|
|
48
216
|
};
|
|
49
217
|
|
|
50
218
|
/**
|
|
51
|
-
* Load modules
|
|
52
|
-
*
|
|
53
|
-
*
|
|
219
|
+
* Load and configure modules for your Tailor application.
|
|
220
|
+
*
|
|
221
|
+
* This is the main entry point for setting up omakase modules. Use this function
|
|
222
|
+
* to register all modules your application needs, configure them with your settings,
|
|
223
|
+
* and wire up dependencies between modules.
|
|
224
|
+
*
|
|
225
|
+
* @param configurator - A function that receives a {@link ModuleLoader} and returns it after adding modules
|
|
226
|
+
* @returns A {@link LoadedModules} object containing all registered modules and utilities
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* // modules.ts - Define your module configuration
|
|
231
|
+
* import { loadModules } from "@izumisy-tailor/omakase-modules";
|
|
232
|
+
* import { db } from "@tailor-platform/sdk";
|
|
233
|
+
* import commerceModule from "omakase-module-commerce-core";
|
|
234
|
+
* import orderModule from "omakase-module-order";
|
|
235
|
+
* import inventoryModule from "omakase-module-inventory";
|
|
236
|
+
*
|
|
237
|
+
* export default loadModules((loader) => {
|
|
238
|
+
* // Configure and add the commerce module
|
|
239
|
+
* const $commerce = loader.add(
|
|
240
|
+
* commerceModule.configure({
|
|
241
|
+
* config: {
|
|
242
|
+
* dataModel: {
|
|
243
|
+
* product: {
|
|
244
|
+
* docNumberPrefix: "PP-PROD",
|
|
245
|
+
* customAttributes: {
|
|
246
|
+
* customStatus: db.enum(["new", "used", "refurbished"]),
|
|
247
|
+
* },
|
|
248
|
+
* },
|
|
249
|
+
* },
|
|
250
|
+
* },
|
|
251
|
+
* })
|
|
252
|
+
* );
|
|
253
|
+
*
|
|
254
|
+
* // Configure order module with commerce as a dependency
|
|
255
|
+
* const $order = loader.add(
|
|
256
|
+
* orderModule.configure({
|
|
257
|
+
* config: { dataModel: {} },
|
|
258
|
+
* dependencies: { commerce: $commerce },
|
|
259
|
+
* })
|
|
260
|
+
* );
|
|
261
|
+
*
|
|
262
|
+
* // Configure inventory module with both dependencies
|
|
263
|
+
* loader.add(
|
|
264
|
+
* inventoryModule.configure({
|
|
265
|
+
* config: {
|
|
266
|
+
* dbNamespace: "main-db",
|
|
267
|
+
* invantoryBootstrapBaseValue: 300,
|
|
268
|
+
* },
|
|
269
|
+
* dependencies: { commerce: $commerce, order: $order },
|
|
270
|
+
* })
|
|
271
|
+
* );
|
|
272
|
+
*
|
|
273
|
+
* return loader;
|
|
274
|
+
* });
|
|
275
|
+
* ```
|
|
54
276
|
*/
|
|
55
277
|
export const loadModules = (
|
|
56
278
|
configurator: (loader: ModuleLoader) => ModuleLoader
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { loadModules, type LoadedModules } from "../module-loader";
|
|
2
|
+
import type { DefinedModule, DependencyModules } from "../../builder/helpers";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a LoadedModules context for individual module development.
|
|
6
|
+
*
|
|
7
|
+
* This allows using `getModulesReference` in a module's own `tailor.config.ts`,
|
|
8
|
+
* enabling the kysely-type generator to work during module development.
|
|
9
|
+
*
|
|
10
|
+
* Dependencies declared in the module's `defineModule` are automatically
|
|
11
|
+
* registered with default (empty) configurations.
|
|
12
|
+
*
|
|
13
|
+
* @param module The module being developed
|
|
14
|
+
* @param config Optional development configuration. If omitted, uses module.devConfig.
|
|
15
|
+
* @returns A LoadedModules object that can be passed to getModulesReference
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // tailor.config.ts
|
|
20
|
+
* import { loadModuleForDev, getModulesReference } from "@izumisy-tailor/omakase-modules/config/sdk";
|
|
21
|
+
* import inventoryModule from "./src/module";
|
|
22
|
+
*
|
|
23
|
+
* const modules = loadModuleForDev(inventoryModule);
|
|
24
|
+
* const moduleReference = await getModulesReference(modules);
|
|
25
|
+
*
|
|
26
|
+
* export default defineConfig({ ... });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export const loadModuleForDev = <
|
|
30
|
+
C extends Record<string, unknown>,
|
|
31
|
+
Tables extends Record<string, unknown>,
|
|
32
|
+
Deps extends DependencyModules
|
|
33
|
+
>(
|
|
34
|
+
module: DefinedModule<C, Tables, Deps>,
|
|
35
|
+
config?: C
|
|
36
|
+
): LoadedModules => {
|
|
37
|
+
// Use provided config, fall back to module's devConfig, or use empty object as default
|
|
38
|
+
const resolvedConfig = config ?? module.devConfig ?? ({} as C);
|
|
39
|
+
|
|
40
|
+
const modules = loadModules((loader) => {
|
|
41
|
+
// Recursively register all dependency modules with default config
|
|
42
|
+
const registerDependencies = (deps: DependencyModules) => {
|
|
43
|
+
for (const dep of Object.values(deps)) {
|
|
44
|
+
if (!dep) continue; // Skip empty dependency marker
|
|
45
|
+
// First register transitive dependencies
|
|
46
|
+
if (dep.dependencies && Object.keys(dep.dependencies).length > 0) {
|
|
47
|
+
registerDependencies(dep.dependencies);
|
|
48
|
+
}
|
|
49
|
+
// Then register this dependency with empty config
|
|
50
|
+
// For dev context, we use type assertion since dependencies are auto-resolved
|
|
51
|
+
loader.add(
|
|
52
|
+
(
|
|
53
|
+
dep.configure as (props: { config: Record<string, unknown> }) => any
|
|
54
|
+
)({ config: {} })
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Register all dependencies first
|
|
60
|
+
if (module.dependencies && Object.keys(module.dependencies).length > 0) {
|
|
61
|
+
registerDependencies(module.dependencies);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Register the main module being developed
|
|
65
|
+
// For dev context, we don't need to pass dependencies
|
|
66
|
+
loader.add(
|
|
67
|
+
(module.configure as (props: { config: C }) => any)({
|
|
68
|
+
config: resolvedConfig,
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return loader;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Mark this as a dev context with the module name being developed
|
|
76
|
+
return {
|
|
77
|
+
...modules,
|
|
78
|
+
__devContext: {
|
|
79
|
+
moduleName: module.packageName,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
};
|
package/src/config/sdk/index.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { getModulesReference } from "./paths";
|
|
1
|
+
export { getModulesReference, type GetModulesReferenceOptions } from "./paths";
|
|
2
|
+
export { loadModuleForDev } from "./dev-context";
|
package/src/config/sdk/paths.ts
CHANGED
|
@@ -1,18 +1,100 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
-
import { LoadedModules } from "../module-loader";
|
|
2
|
+
import type { LoadedModules } from "../module-loader";
|
|
3
3
|
import { generateWrapperFiles, OMAKASE_WRAPPER_DIR } from "./wrapper/generator";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Options for configuring {@link getModulesReference} behavior.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const moduleReference = await getModulesReference(modules, {
|
|
11
|
+
* basePath: "/custom/path",
|
|
12
|
+
* silent: true, // Suppress console output
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
5
16
|
export type GetModulesReferenceOptions = {
|
|
6
17
|
/**
|
|
7
|
-
* Base path for the application
|
|
18
|
+
* Base path for the application.
|
|
19
|
+
*
|
|
20
|
+
* This determines where wrapper files are generated and where module
|
|
21
|
+
* paths are resolved from. Defaults to `process.cwd()`.
|
|
22
|
+
*
|
|
23
|
+
* @default process.cwd()
|
|
8
24
|
*/
|
|
9
25
|
basePath?: string;
|
|
10
26
|
/**
|
|
11
|
-
* Whether to suppress log output
|
|
27
|
+
* Whether to suppress log output.
|
|
28
|
+
*
|
|
29
|
+
* When `true`, no console output will be printed during module loading.
|
|
30
|
+
* Useful for testing or when you want cleaner build output.
|
|
31
|
+
*
|
|
32
|
+
* @default false
|
|
12
33
|
*/
|
|
13
34
|
silent?: boolean;
|
|
14
35
|
};
|
|
15
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Get file path patterns for modules to use in Tailor configuration.
|
|
39
|
+
*
|
|
40
|
+
* This function processes your loaded modules and returns glob patterns
|
|
41
|
+
* that can be spread into your `defineConfig`. It handles all the necessary
|
|
42
|
+
* setup to make module TailorDB types, resolvers, and executors available
|
|
43
|
+
* to Tailor's configuration system.
|
|
44
|
+
*
|
|
45
|
+
* @param loadedModules - The result of calling `loadModules()`
|
|
46
|
+
* @param options - Optional configuration for path resolution and logging
|
|
47
|
+
* @returns An object containing glob patterns for `tailordb`, `resolver`, and `executor` files
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // tailor.config.ts
|
|
52
|
+
* import { defineConfig, defineGenerators } from "@tailor-platform/sdk";
|
|
53
|
+
* import { getModulesReference } from "@izumisy-tailor/omakase-modules/config/sdk";
|
|
54
|
+
* import modules from "./modules";
|
|
55
|
+
*
|
|
56
|
+
* // Get module path patterns
|
|
57
|
+
* const moduleReference = await getModulesReference(modules);
|
|
58
|
+
*
|
|
59
|
+
* export default defineConfig({
|
|
60
|
+
* name: "my-app",
|
|
61
|
+
*
|
|
62
|
+
* // Spread module paths alongside your local files
|
|
63
|
+
* db: {
|
|
64
|
+
* "main-db": {
|
|
65
|
+
* files: ["./src/tailordb/*.ts", ...moduleReference.tailordb],
|
|
66
|
+
* },
|
|
67
|
+
* },
|
|
68
|
+
*
|
|
69
|
+
* resolver: {
|
|
70
|
+
* "main-pipeline": {
|
|
71
|
+
* files: ["./src/resolvers/*.ts", ...moduleReference.resolver],
|
|
72
|
+
* },
|
|
73
|
+
* },
|
|
74
|
+
*
|
|
75
|
+
* executor: {
|
|
76
|
+
* files: ["./src/executors/*.ts", ...moduleReference.executor],
|
|
77
|
+
* },
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // With custom options
|
|
84
|
+
* const moduleReference = await getModulesReference(modules, {
|
|
85
|
+
* basePath: import.meta.dirname, // Use ESM module directory
|
|
86
|
+
* silent: true, // No console output
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @remarks
|
|
91
|
+
* The returned object contains three arrays:
|
|
92
|
+
* - `tailordb` - Paths to TailorDB type definitions from modules
|
|
93
|
+
* - `resolver` - Paths to GraphQL resolver definitions from modules
|
|
94
|
+
* - `executor` - Paths to event executor definitions from modules
|
|
95
|
+
*
|
|
96
|
+
* If a module has no files of a certain type, that array will be empty.
|
|
97
|
+
*/
|
|
16
98
|
export const getModulesReference = async (
|
|
17
99
|
loadedModules: LoadedModules,
|
|
18
100
|
options: GetModulesReferenceOptions = {}
|
|
@@ -2,12 +2,29 @@ import dedent from "dedent";
|
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Sanitize a package name for use in file paths.
|
|
7
|
+
* Removes leading @ and replaces / with __ to create a flat directory structure.
|
|
8
|
+
* e.g., "@izumisy-tailor/hoge-modules" -> "izumisy-tailor__hoge-modules"
|
|
9
|
+
*/
|
|
10
|
+
export const sanitizePackageName = (packageName: string): string => {
|
|
11
|
+
return packageName.replace(/^@/, "").replace(/\//g, "__");
|
|
12
|
+
};
|
|
13
|
+
|
|
5
14
|
/**
|
|
6
15
|
* Path alias for importing modules config.
|
|
7
16
|
* App's tsconfig.json should have: "@omakase-modules/config": ["./modules"]
|
|
17
|
+
* Module's tsconfig.json should have: "@omakase-modules/config": ["./src/module"]
|
|
8
18
|
*/
|
|
9
19
|
export const MODULES_IMPORT_PATH = "@omakase-modules/config";
|
|
10
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Relative path from wrapper files to dev-modules.ts for dev context.
|
|
23
|
+
* Wrapper files are at: .tailor-sdk/.omakase/{packageName}/{category}/file.ts
|
|
24
|
+
* dev-modules.ts is at: .tailor-sdk/.omakase/dev-modules.ts
|
|
25
|
+
*/
|
|
26
|
+
export const DEV_MODULES_RELATIVE_PATH = "../../dev-modules";
|
|
27
|
+
|
|
11
28
|
/**
|
|
12
29
|
* Category types for wrapper generation
|
|
13
30
|
*/
|
|
@@ -17,10 +34,33 @@ export type Category = "tailordb" | "resolvers" | "executors";
|
|
|
17
34
|
* Abstract base class for category-specific wrapper generation
|
|
18
35
|
*/
|
|
19
36
|
export abstract class WrapperStrategy {
|
|
37
|
+
/**
|
|
38
|
+
* Whether this module is being developed locally (not from node_modules)
|
|
39
|
+
*/
|
|
40
|
+
protected readonly isLocalModule: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The import path for modules (either alias or relative path for dev context)
|
|
44
|
+
*/
|
|
45
|
+
protected readonly modulesImportPath: string;
|
|
46
|
+
|
|
20
47
|
constructor(
|
|
21
48
|
protected readonly packageName: string,
|
|
22
|
-
protected readonly basePath: string
|
|
23
|
-
|
|
49
|
+
protected readonly basePath: string,
|
|
50
|
+
/**
|
|
51
|
+
* The package name of the module being developed locally.
|
|
52
|
+
* When set, the strategy will use local src paths for this module
|
|
53
|
+
* and relative import path for dev-modules.ts.
|
|
54
|
+
*/
|
|
55
|
+
protected readonly devModuleName?: string
|
|
56
|
+
) {
|
|
57
|
+
this.isLocalModule = devModuleName === packageName;
|
|
58
|
+
// In dev context, use relative path to auto-generated dev-modules.ts
|
|
59
|
+
// In app context, use the @omakase-modules/config alias
|
|
60
|
+
this.modulesImportPath = devModuleName
|
|
61
|
+
? DEV_MODULES_RELATIVE_PATH
|
|
62
|
+
: MODULES_IMPORT_PATH;
|
|
63
|
+
}
|
|
24
64
|
|
|
25
65
|
/**
|
|
26
66
|
* The category this strategy handles
|
|
@@ -33,10 +73,18 @@ export abstract class WrapperStrategy {
|
|
|
33
73
|
abstract filterFiles(files: string[]): string[];
|
|
34
74
|
|
|
35
75
|
/**
|
|
36
|
-
* Get the module export path
|
|
76
|
+
* Get the module export path for npm packages
|
|
37
77
|
*/
|
|
38
78
|
abstract getExportPath(fileName: string): string;
|
|
39
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Get the local export path for the module being developed.
|
|
82
|
+
* Uses @/ alias which maps to src/ in module's tsconfig.
|
|
83
|
+
*/
|
|
84
|
+
getLocalExportPath(fileName: string): string {
|
|
85
|
+
return `@/${this.category}/${fileName.replace(/\.ts$/, "")}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
40
88
|
/**
|
|
41
89
|
* Generate the export statements using the factory result.
|
|
42
90
|
* Default implementation exports the result as default export.
|
|
@@ -62,8 +110,8 @@ export abstract class WrapperStrategy {
|
|
|
62
110
|
* ensuring proper configuration injection and avoiding tree-shaking issues.
|
|
63
111
|
*/
|
|
64
112
|
|
|
65
|
-
// Import the loadModules result from the app's modules.ts
|
|
66
|
-
import modules from "${
|
|
113
|
+
// Import the loadModules result from the app's modules.ts (or auto-generated dev-modules.ts)
|
|
114
|
+
import modules from "${this.modulesImportPath}";
|
|
67
115
|
|
|
68
116
|
// Import the factory function from the module
|
|
69
117
|
import createFactory from "${moduleExportPath}";
|
|
@@ -79,15 +127,17 @@ export abstract class WrapperStrategy {
|
|
|
79
127
|
* Generate wrapper files for this category
|
|
80
128
|
*/
|
|
81
129
|
async generateFiles(wrapperDir: string): Promise<string[]> {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
130
|
+
// Determine source path based on whether this is a local module or a dependency
|
|
131
|
+
const sourcePath = this.isLocalModule
|
|
132
|
+
? path.join(this.basePath, "src", this.category)
|
|
133
|
+
: path.join(
|
|
134
|
+
this.basePath,
|
|
135
|
+
"node_modules",
|
|
136
|
+
this.packageName,
|
|
137
|
+
"src",
|
|
138
|
+
this.category
|
|
139
|
+
);
|
|
89
140
|
|
|
90
|
-
// Check if the source directory exists
|
|
91
141
|
if (!(await this.exists(sourcePath))) {
|
|
92
142
|
return [];
|
|
93
143
|
}
|
|
@@ -113,13 +163,16 @@ export abstract class WrapperStrategy {
|
|
|
113
163
|
): Promise<string> {
|
|
114
164
|
const categoryWrapperDir = path.join(
|
|
115
165
|
wrapperDir,
|
|
116
|
-
this.packageName,
|
|
166
|
+
sanitizePackageName(this.packageName),
|
|
117
167
|
this.category
|
|
118
168
|
);
|
|
119
169
|
await fs.mkdir(categoryWrapperDir, { recursive: true });
|
|
120
170
|
|
|
121
171
|
const wrapperFilePath = path.join(categoryWrapperDir, fileName);
|
|
122
|
-
|
|
172
|
+
// Use local path for the module being developed, npm path for dependencies
|
|
173
|
+
const moduleExportPath = this.isLocalModule
|
|
174
|
+
? this.getLocalExportPath(fileName)
|
|
175
|
+
: this.getExportPath(fileName);
|
|
123
176
|
const content = await this.generateContent(moduleExportPath);
|
|
124
177
|
|
|
125
178
|
await fs.writeFile(wrapperFilePath, content, "utf-8");
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import dedent from "dedent";
|
|
1
2
|
import fs from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
|
-
import { LoadedModules } from "../../module-loader";
|
|
4
|
+
import type { LoadedModules } from "../../module-loader";
|
|
4
5
|
import { createStrategy } from "./strategies";
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -8,10 +9,35 @@ import { createStrategy } from "./strategies";
|
|
|
8
9
|
*/
|
|
9
10
|
export const OMAKASE_WRAPPER_DIR = ".tailor-sdk/.omakase";
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Generates the dev-modules.ts file for module development.
|
|
14
|
+
* This file is auto-generated and imports the module via @omakase-modules/config alias,
|
|
15
|
+
* then calls loadModuleForDev to create the LoadedModules context.
|
|
16
|
+
*/
|
|
17
|
+
const generateDevModulesFile = async (wrapperDir: string) => {
|
|
18
|
+
const devModulesPath = path.join(wrapperDir, "dev-modules.ts");
|
|
19
|
+
|
|
20
|
+
const content = dedent`
|
|
21
|
+
/**
|
|
22
|
+
* Auto-generated dev-modules file by @izumisy-tailor/omakase-modules
|
|
23
|
+
* DO NOT EDIT THIS FILE MANUALLY
|
|
24
|
+
*
|
|
25
|
+
* This file imports the module definition and creates a LoadedModules context
|
|
26
|
+
* for wrapper files to use during module development.
|
|
27
|
+
*/
|
|
28
|
+
import { loadModuleForDev } from "@izumisy-tailor/omakase-modules/config/sdk";
|
|
29
|
+
import module from "@omakase-modules/config";
|
|
30
|
+
|
|
31
|
+
export default loadModuleForDev(module);
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
await fs.writeFile(devModulesPath, content, "utf-8");
|
|
35
|
+
};
|
|
36
|
+
|
|
11
37
|
/**
|
|
12
38
|
* Generates wrapper files for each module's tailordb, executor, and resolver files.
|
|
13
39
|
* These wrappers import the factory functions from modules and call them with
|
|
14
|
-
* the loadModules result from the app's modules.ts.
|
|
40
|
+
* the loadModules result from the app's modules.ts (or dev-modules.ts for module development).
|
|
15
41
|
*/
|
|
16
42
|
export const generateWrapperFiles = async (
|
|
17
43
|
loadedModules: LoadedModules,
|
|
@@ -28,14 +54,25 @@ export const generateWrapperFiles = async (
|
|
|
28
54
|
// Directory doesn't exist, no need to clean up
|
|
29
55
|
}
|
|
30
56
|
|
|
57
|
+
// Ensure wrapper directory exists
|
|
58
|
+
await fs.mkdir(wrapperDir, { recursive: true });
|
|
59
|
+
|
|
31
60
|
const result = {
|
|
32
61
|
tailordb: [] as string[],
|
|
33
62
|
resolver: [] as string[],
|
|
34
63
|
executor: [] as string[],
|
|
35
64
|
};
|
|
36
65
|
|
|
66
|
+
// Get dev module name if this is a dev context
|
|
67
|
+
const devModuleName = loadedModules.__devContext?.moduleName;
|
|
68
|
+
|
|
69
|
+
// Generate dev-modules.ts for dev context
|
|
70
|
+
if (devModuleName) {
|
|
71
|
+
await generateDevModulesFile(wrapperDir);
|
|
72
|
+
}
|
|
73
|
+
|
|
37
74
|
for (const packageName of modulePackageNames) {
|
|
38
|
-
const strategyFor = createStrategy(packageName, basePath);
|
|
75
|
+
const strategyFor = createStrategy(packageName, basePath, devModuleName);
|
|
39
76
|
|
|
40
77
|
result.tailordb.push(
|
|
41
78
|
...(await strategyFor("tailordb").generateFiles(wrapperDir))
|