@rexeus/typeweaver-gen 0.5.0 → 0.6.0

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.
@@ -0,0 +1,284 @@
1
+ import { IHttpOperationDefinition, IHttpResponseDefinition } from "@rexeus/typeweaver-core";
2
+
3
+ //#region src/Resource.d.ts
4
+ type GetResourcesResult = {
5
+ entityResources: EntityResources;
6
+ sharedResponseResources: SharedResponseResource[];
7
+ };
8
+ type ExtendedResponseDefinition = IHttpResponseDefinition & {
9
+ statusCodeName: string;
10
+ isReference: boolean;
11
+ };
12
+ type EntityName = string;
13
+ type OperationResource = {
14
+ sourceDir: string;
15
+ sourceFile: string;
16
+ sourceFileName: string;
17
+ definition: Omit<IHttpOperationDefinition, "responses"> & {
18
+ responses: ExtendedResponseDefinition[];
19
+ };
20
+ outputDir: string;
21
+ entityName: EntityName;
22
+ outputRequestFile: string;
23
+ outputRequestFileName: string;
24
+ outputResponseFile: string;
25
+ outputResponseFileName: string;
26
+ outputRequestValidationFile: string;
27
+ outputRequestValidationFileName: string;
28
+ outputResponseValidationFile: string;
29
+ outputResponseValidationFileName: string;
30
+ outputClientFile: string;
31
+ outputClientFileName: string;
32
+ };
33
+ type EntityResources = Record<EntityName, {
34
+ operations: OperationResource[];
35
+ responses: EntityResponseResource[];
36
+ }>;
37
+ type SharedResponseResource = IHttpResponseDefinition & {
38
+ sourceDir: string;
39
+ sourceFile: string;
40
+ sourceFileName: string;
41
+ outputFile: string;
42
+ outputFileName: string;
43
+ outputDir: string;
44
+ };
45
+ type EntityResponseResource = IHttpResponseDefinition & {
46
+ sourceDir: string;
47
+ sourceFile: string;
48
+ sourceFileName: string;
49
+ outputFile: string;
50
+ outputFileName: string;
51
+ outputDir: string;
52
+ entityName: EntityName;
53
+ };
54
+ //#endregion
55
+ //#region src/plugins/types.d.ts
56
+ /**
57
+ * Configuration for a typeweaver plugin
58
+ */
59
+ type PluginConfig = Record<string, unknown>;
60
+ /**
61
+ * Context provided to plugins during initialization and finalization
62
+ */
63
+ type PluginContext = {
64
+ outputDir: string;
65
+ inputDir: string;
66
+ config: PluginConfig;
67
+ };
68
+ /**
69
+ * Context provided to plugins during generation
70
+ */
71
+ type GeneratorContext = PluginContext & {
72
+ resources: GetResourcesResult;
73
+ templateDir: string;
74
+ coreDir: string;
75
+ writeFile: (relativePath: string, content: string) => void;
76
+ renderTemplate: (templatePath: string, data: unknown) => string;
77
+ addGeneratedFile: (relativePath: string) => void;
78
+ getGeneratedFiles: () => string[];
79
+ };
80
+ /**
81
+ * Plugin metadata
82
+ */
83
+ type PluginMetadata = {
84
+ name: string;
85
+ };
86
+ /**
87
+ * typeweaver plugin interface
88
+ */
89
+ type TypeweaverPlugin = PluginMetadata & {
90
+ /**
91
+ * Initialize the plugin
92
+ * Called before any generation happens
93
+ */
94
+ initialize?(context: PluginContext): Promise<void> | void;
95
+ /**
96
+ * Collect and transform resources
97
+ * Allows plugins to modify the resource collection
98
+ */
99
+ collectResources?(resources: GetResourcesResult): Promise<GetResourcesResult> | GetResourcesResult;
100
+ /**
101
+ * Main generation logic
102
+ * Called with all resources and utilities
103
+ */
104
+ generate?(context: GeneratorContext): Promise<void> | void;
105
+ /**
106
+ * Finalize the plugin
107
+ * Called after all generation is complete
108
+ */
109
+ finalize?(context: PluginContext): Promise<void> | void;
110
+ };
111
+ /**
112
+ * Plugin constructor type
113
+ */
114
+ type PluginConstructor = new (config?: PluginConfig) => TypeweaverPlugin;
115
+ /**
116
+ * Plugin module export
117
+ */
118
+ type PluginModule = {
119
+ default: PluginConstructor;
120
+ };
121
+ /**
122
+ * Plugin registration entry
123
+ */
124
+ type PluginRegistration = {
125
+ name: string;
126
+ plugin: TypeweaverPlugin;
127
+ config?: PluginConfig;
128
+ };
129
+ /**
130
+ * typeweaver configuration
131
+ */
132
+ type TypeweaverConfig = {
133
+ input: string;
134
+ output: string;
135
+ shared?: string;
136
+ plugins?: (string | [string, PluginConfig])[];
137
+ format?: boolean;
138
+ clean?: boolean;
139
+ };
140
+ /**
141
+ * Plugin loading error
142
+ */
143
+ declare class PluginLoadError extends Error {
144
+ pluginName: string;
145
+ constructor(pluginName: string, message: string);
146
+ }
147
+ /**
148
+ * Plugin dependency error
149
+ */
150
+ declare class PluginDependencyError extends Error {
151
+ pluginName: string;
152
+ missingDependency: string;
153
+ constructor(pluginName: string, missingDependency: string);
154
+ }
155
+ //#endregion
156
+ //#region src/plugins/BasePlugin.d.ts
157
+ /**
158
+ * Base class for typeweaver plugins
159
+ * Provides default implementations and common utilities
160
+ */
161
+ declare abstract class BasePlugin implements TypeweaverPlugin {
162
+ abstract name: string;
163
+ description?: string;
164
+ author?: string;
165
+ depends?: string[];
166
+ protected config: PluginConfig;
167
+ constructor(config?: PluginConfig);
168
+ /**
169
+ * Default implementation - override in subclasses if needed
170
+ */
171
+ initialize(_context: PluginContext): Promise<void>;
172
+ /**
173
+ * Default implementation - override in subclasses if needed
174
+ */
175
+ collectResources(resources: GetResourcesResult): GetResourcesResult;
176
+ /**
177
+ * Main generation logic - must be implemented by subclasses
178
+ */
179
+ abstract generate(context: GeneratorContext): Promise<void> | void;
180
+ /**
181
+ * Default implementation - override in subclasses if needed
182
+ */
183
+ finalize(_context: PluginContext): Promise<void>;
184
+ /**
185
+ * Copy lib files from plugin package to generated lib folder
186
+ */
187
+ protected copyLibFiles(context: GeneratorContext, libSourceDir: string, libNamespace: string): void;
188
+ }
189
+ //#endregion
190
+ //#region src/plugins/BaseTemplatePlugin.d.ts
191
+ /**
192
+ * Base class for template-based generator plugins
193
+ * Provides utilities for working with EJS templates
194
+ */
195
+ declare abstract class BaseTemplatePlugin extends BasePlugin {
196
+ /**
197
+ * Render an EJS template with the given data
198
+ */
199
+ protected renderTemplate(templatePath: string, data: unknown): string;
200
+ /**
201
+ * Write a file relative to the output directory
202
+ */
203
+ protected writeFile(context: GeneratorContext, relativePath: string, content: string): void;
204
+ /**
205
+ * Ensure a directory exists
206
+ */
207
+ protected ensureDir(context: GeneratorContext, relativePath: string): void;
208
+ /**
209
+ * Get the template path for this plugin
210
+ */
211
+ protected getTemplatePath(context: GeneratorContext, templateName: string): string;
212
+ }
213
+ //#endregion
214
+ //#region src/plugins/PluginRegistry.d.ts
215
+ /**
216
+ * Registry for managing typeweaver plugins
217
+ */
218
+ declare class PluginRegistry {
219
+ private plugins;
220
+ constructor();
221
+ /**
222
+ * Register a plugin
223
+ */
224
+ register(plugin: TypeweaverPlugin, config?: unknown): void;
225
+ /**
226
+ * Get a registered plugin
227
+ */
228
+ get(name: string): PluginRegistration | undefined;
229
+ /**
230
+ * Get all registered plugins
231
+ */
232
+ getAll(): PluginRegistration[];
233
+ /**
234
+ * Check if a plugin is registered
235
+ */
236
+ has(name: string): boolean;
237
+ /**
238
+ * Clear all registered plugins (except required ones)
239
+ */
240
+ clear(): void;
241
+ }
242
+ //#endregion
243
+ //#region src/plugins/PluginContext.d.ts
244
+ /**
245
+ * Builder for plugin contexts
246
+ */
247
+ declare class PluginContextBuilder {
248
+ private generatedFiles;
249
+ /**
250
+ * Create a basic plugin context
251
+ */
252
+ createPluginContext(params: {
253
+ outputDir: string;
254
+ inputDir: string;
255
+ config: PluginConfig;
256
+ }): PluginContext;
257
+ /**
258
+ * Create a generator context with utilities
259
+ */
260
+ createGeneratorContext(params: {
261
+ outputDir: string;
262
+ inputDir: string;
263
+ config: PluginConfig;
264
+ resources: GetResourcesResult;
265
+ templateDir: string;
266
+ coreDir: string;
267
+ }): GeneratorContext;
268
+ /**
269
+ * Get all generated files
270
+ */
271
+ getGeneratedFiles(): string[];
272
+ /**
273
+ * Clear generated files tracking
274
+ */
275
+ clearGeneratedFiles(): void;
276
+ }
277
+ //#endregion
278
+ //#region src/helpers/Path.d.ts
279
+ declare class Path {
280
+ static relative(from: string, to: string): string;
281
+ }
282
+ //#endregion
283
+ export { BasePlugin, BaseTemplatePlugin, EntityName, EntityResources, EntityResponseResource, ExtendedResponseDefinition, GeneratorContext, GetResourcesResult, OperationResource, Path, PluginConfig, PluginConstructor, PluginContext, PluginContextBuilder, PluginDependencyError, PluginLoadError, PluginMetadata, PluginModule, PluginRegistration, PluginRegistry, SharedResponseResource, TypeweaverConfig, TypeweaverPlugin };
284
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/Resource.ts","../src/plugins/types.ts","../src/plugins/BasePlugin.ts","../src/plugins/BaseTemplatePlugin.ts","../src/plugins/PluginRegistry.ts","../src/plugins/PluginContext.ts","../src/helpers/Path.ts"],"mappings":";;;KAKY,kBAAA;EACV,eAAA,EAAiB,eAAA;EACjB,uBAAA,EAAyB,sBAAA;AAAA;AAAA,KAGf,0BAAA,GAA6B,uBAAA;EACvC,cAAA;EACA,WAAA;AAAA;AAAA,KAGU,UAAA;AAAA,KACA,iBAAA;EACV,SAAA;EACA,UAAA;EACA,cAAA;EACA,UAAA,EAAY,IAAA,CAAK,wBAAA;IACf,SAAA,EAAW,0BAAA;EAAA;EAEb,SAAA;EACA,UAAA,EAAY,UAAA;EACZ,iBAAA;EACA,qBAAA;EACA,kBAAA;EACA,sBAAA;EACA,2BAAA;EACA,+BAAA;EACA,4BAAA;EACA,gCAAA;EACA,gBAAA;EACA,oBAAA;AAAA;AAAA,KAGU,eAAA,GAAkB,MAAA,CAC5B,UAAA;EAEE,UAAA,EAAY,iBAAA;EACZ,SAAA,EAAW,sBAAA;AAAA;AAAA,KAIH,sBAAA,GAAyB,uBAAA;EACnC,SAAA;EACA,UAAA;EACA,cAAA;EACA,UAAA;EACA,cAAA;EACA,SAAA;AAAA;AAAA,KAGU,sBAAA,GAAyB,uBAAA;EACnC,SAAA;EACA,UAAA;EACA,cAAA;EACA,UAAA;EACA,cAAA;EACA,SAAA;EACA,UAAA,EAAY,UAAA;AAAA;;;;;AAxDd;KCAY,YAAA,GAAe,MAAA;;;;KAKf,aAAA;EACV,SAAA;EACA,QAAA;EACA,MAAA,EAAQ,YAAA;AAAA;ADHV;;;AAA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AAc,EAAA;AAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,239 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { render } from "ejs";
4
+
5
+ //#region src/plugins/types.ts
6
+ /**
7
+ * Plugin loading error
8
+ */
9
+ var PluginLoadError = class extends Error {
10
+ constructor(pluginName, message) {
11
+ super(`Failed to load plugin '${pluginName}': ${message}`);
12
+ this.pluginName = pluginName;
13
+ this.name = "PluginLoadError";
14
+ }
15
+ };
16
+ /**
17
+ * Plugin dependency error
18
+ */
19
+ var PluginDependencyError = class extends Error {
20
+ constructor(pluginName, missingDependency) {
21
+ super(`Plugin '${pluginName}' depends on '${missingDependency}' which is not loaded`);
22
+ this.pluginName = pluginName;
23
+ this.missingDependency = missingDependency;
24
+ this.name = "PluginDependencyError";
25
+ }
26
+ };
27
+
28
+ //#endregion
29
+ //#region src/plugins/BasePlugin.ts
30
+ /**
31
+ * Base class for typeweaver plugins
32
+ * Provides default implementations and common utilities
33
+ */
34
+ var BasePlugin = class {
35
+ description;
36
+ author;
37
+ depends;
38
+ config;
39
+ constructor(config = {}) {
40
+ this.config = config;
41
+ }
42
+ /**
43
+ * Default implementation - override in subclasses if needed
44
+ */
45
+ async initialize(_context) {}
46
+ /**
47
+ * Default implementation - override in subclasses if needed
48
+ */
49
+ collectResources(resources) {
50
+ return resources;
51
+ }
52
+ /**
53
+ * Default implementation - override in subclasses if needed
54
+ */
55
+ async finalize(_context) {}
56
+ /**
57
+ * Copy lib files from plugin package to generated lib folder
58
+ */
59
+ copyLibFiles(context, libSourceDir, libNamespace) {
60
+ const libDir = path.join(context.outputDir, "lib", libNamespace);
61
+ fs.mkdirSync(libDir, { recursive: true });
62
+ if (fs.existsSync(libSourceDir)) {
63
+ const files = fs.readdirSync(libSourceDir);
64
+ for (const file of files) {
65
+ const sourcePath = path.join(libSourceDir, file);
66
+ const targetPath = path.join(libDir, file);
67
+ if (fs.statSync(sourcePath).isFile()) fs.copyFileSync(sourcePath, targetPath);
68
+ }
69
+ const libIndexPath = path.join("lib", libNamespace, "index.ts");
70
+ if (fs.existsSync(path.join(libDir, "index.ts"))) context.addGeneratedFile(libIndexPath);
71
+ }
72
+ }
73
+ };
74
+
75
+ //#endregion
76
+ //#region src/plugins/BaseTemplatePlugin.ts
77
+ /**
78
+ * Base class for template-based generator plugins
79
+ * Provides utilities for working with EJS templates
80
+ */
81
+ var BaseTemplatePlugin = class extends BasePlugin {
82
+ /**
83
+ * Render an EJS template with the given data
84
+ */
85
+ renderTemplate(templatePath, data) {
86
+ return render(fs.readFileSync(templatePath, "utf8"), data);
87
+ }
88
+ /**
89
+ * Write a file relative to the output directory
90
+ */
91
+ writeFile(context, relativePath, content) {
92
+ context.writeFile(relativePath, content);
93
+ }
94
+ /**
95
+ * Ensure a directory exists
96
+ */
97
+ ensureDir(context, relativePath) {
98
+ const fullPath = path.join(context.outputDir, relativePath);
99
+ fs.mkdirSync(fullPath, { recursive: true });
100
+ }
101
+ /**
102
+ * Get the template path for this plugin
103
+ */
104
+ getTemplatePath(context, templateName) {
105
+ return path.join(context.templateDir, templateName);
106
+ }
107
+ };
108
+
109
+ //#endregion
110
+ //#region src/plugins/PluginRegistry.ts
111
+ /**
112
+ * Registry for managing typeweaver plugins
113
+ */
114
+ var PluginRegistry = class {
115
+ plugins;
116
+ constructor() {
117
+ this.plugins = /* @__PURE__ */ new Map();
118
+ }
119
+ /**
120
+ * Register a plugin
121
+ */
122
+ register(plugin, config) {
123
+ if (this.plugins.has(plugin.name)) {
124
+ console.info(`Skipping duplicate registration of required plugin: ${plugin.name}`);
125
+ return;
126
+ }
127
+ const registration = {
128
+ name: plugin.name,
129
+ plugin,
130
+ config
131
+ };
132
+ this.plugins.set(plugin.name, registration);
133
+ console.info(`Registered plugin: ${plugin.name}`);
134
+ }
135
+ /**
136
+ * Get a registered plugin
137
+ */
138
+ get(name) {
139
+ return this.plugins.get(name);
140
+ }
141
+ /**
142
+ * Get all registered plugins
143
+ */
144
+ getAll() {
145
+ return Array.from(this.plugins.values());
146
+ }
147
+ /**
148
+ * Check if a plugin is registered
149
+ */
150
+ has(name) {
151
+ return this.plugins.has(name);
152
+ }
153
+ /**
154
+ * Clear all registered plugins (except required ones)
155
+ */
156
+ clear() {
157
+ this.plugins.clear();
158
+ }
159
+ };
160
+
161
+ //#endregion
162
+ //#region src/plugins/PluginContext.ts
163
+ /**
164
+ * Builder for plugin contexts
165
+ */
166
+ var PluginContextBuilder = class {
167
+ generatedFiles = /* @__PURE__ */ new Set();
168
+ /**
169
+ * Create a basic plugin context
170
+ */
171
+ createPluginContext(params) {
172
+ return {
173
+ outputDir: params.outputDir,
174
+ inputDir: params.inputDir,
175
+ config: params.config
176
+ };
177
+ }
178
+ /**
179
+ * Create a generator context with utilities
180
+ */
181
+ createGeneratorContext(params) {
182
+ return {
183
+ ...this.createPluginContext(params),
184
+ resources: params.resources,
185
+ templateDir: params.templateDir,
186
+ coreDir: params.coreDir,
187
+ writeFile: (relativePath, content) => {
188
+ const fullPath = path.join(params.outputDir, relativePath);
189
+ const dir = path.dirname(fullPath);
190
+ fs.mkdirSync(dir, { recursive: true });
191
+ fs.writeFileSync(fullPath, content);
192
+ this.generatedFiles.add(relativePath);
193
+ console.info(`Generated: ${relativePath}`);
194
+ },
195
+ renderTemplate: (templatePath, data) => {
196
+ const fullTemplatePath = path.isAbsolute(templatePath) ? templatePath : path.join(params.templateDir, templatePath);
197
+ return render(fs.readFileSync(fullTemplatePath, "utf8"), data);
198
+ },
199
+ addGeneratedFile: (relativePath) => {
200
+ this.generatedFiles.add(relativePath);
201
+ },
202
+ getGeneratedFiles: () => {
203
+ return Array.from(this.generatedFiles);
204
+ }
205
+ };
206
+ }
207
+ /**
208
+ * Get all generated files
209
+ */
210
+ getGeneratedFiles() {
211
+ return Array.from(this.generatedFiles);
212
+ }
213
+ /**
214
+ * Clear generated files tracking
215
+ */
216
+ clearGeneratedFiles() {
217
+ this.generatedFiles.clear();
218
+ }
219
+ };
220
+
221
+ //#endregion
222
+ //#region src/helpers/Path.ts
223
+ var Path = class {
224
+ static relative(from, to) {
225
+ const relativePath = path.relative(from, to);
226
+ if (relativePath.includes("node_modules")) {
227
+ const parts = relativePath.split(path.sep);
228
+ const index = parts.indexOf("node_modules");
229
+ return parts.slice(index + 1).join("/");
230
+ }
231
+ const posixPath = relativePath.split(path.sep).join("/");
232
+ if (!posixPath.startsWith("./") && !posixPath.startsWith("../")) return `./${posixPath}`;
233
+ return posixPath;
234
+ }
235
+ };
236
+
237
+ //#endregion
238
+ export { BasePlugin, BaseTemplatePlugin, Path, PluginContextBuilder, PluginDependencyError, PluginLoadError, PluginRegistry };
239
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/plugins/types.ts","../src/plugins/BasePlugin.ts","../src/plugins/BaseTemplatePlugin.ts","../src/plugins/PluginRegistry.ts","../src/plugins/PluginContext.ts","../src/helpers/Path.ts"],"sourcesContent":["import type { GetResourcesResult } from \"../Resource\";\n\n/**\n * Configuration for a typeweaver plugin\n */\nexport type PluginConfig = Record<string, unknown>;\n\n/**\n * Context provided to plugins during initialization and finalization\n */\nexport type PluginContext = {\n outputDir: string;\n inputDir: string;\n config: PluginConfig;\n};\n\n/**\n * Context provided to plugins during generation\n */\nexport type GeneratorContext = PluginContext & {\n resources: GetResourcesResult;\n templateDir: string;\n coreDir: string;\n\n // Utility functions\n writeFile: (relativePath: string, content: string) => void;\n renderTemplate: (templatePath: string, data: unknown) => string;\n addGeneratedFile: (relativePath: string) => void;\n getGeneratedFiles: () => string[];\n};\n\n/**\n * Plugin metadata\n */\nexport type PluginMetadata = {\n name: string;\n};\n\n/**\n * typeweaver plugin interface\n */\nexport type TypeweaverPlugin = PluginMetadata & {\n /**\n * Initialize the plugin\n * Called before any generation happens\n */\n initialize?(context: PluginContext): Promise<void> | void;\n\n /**\n * Collect and transform resources\n * Allows plugins to modify the resource collection\n */\n collectResources?(\n resources: GetResourcesResult\n ): Promise<GetResourcesResult> | GetResourcesResult;\n\n /**\n * Main generation logic\n * Called with all resources and utilities\n */\n generate?(context: GeneratorContext): Promise<void> | void;\n\n /**\n * Finalize the plugin\n * Called after all generation is complete\n */\n finalize?(context: PluginContext): Promise<void> | void;\n};\n\n/**\n * Plugin constructor type\n */\nexport type PluginConstructor = new (config?: PluginConfig) => TypeweaverPlugin;\n\n/**\n * Plugin module export\n */\nexport type PluginModule = {\n default: PluginConstructor;\n};\n\n/**\n * Plugin registration entry\n */\nexport type PluginRegistration = {\n name: string;\n plugin: TypeweaverPlugin;\n config?: PluginConfig;\n};\n\n/**\n * typeweaver configuration\n */\nexport type TypeweaverConfig = {\n input: string;\n output: string;\n shared?: string;\n plugins?: (string | [string, PluginConfig])[];\n format?: boolean;\n clean?: boolean;\n};\n\n/**\n * Plugin loading error\n */\nexport class PluginLoadError extends Error {\n constructor(\n public pluginName: string,\n message: string\n ) {\n super(`Failed to load plugin '${pluginName}': ${message}`);\n this.name = \"PluginLoadError\";\n }\n}\n\n/**\n * Plugin dependency error\n */\nexport class PluginDependencyError extends Error {\n constructor(\n public pluginName: string,\n public missingDependency: string\n ) {\n super(\n `Plugin '${pluginName}' depends on '${missingDependency}' which is not loaded`\n );\n this.name = \"PluginDependencyError\";\n }\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport type { GetResourcesResult } from \"../Resource\";\nimport type {\n GeneratorContext,\n PluginConfig,\n PluginContext,\n TypeweaverPlugin,\n} from \"./types\";\n\n/**\n * Base class for typeweaver plugins\n * Provides default implementations and common utilities\n */\nexport abstract class BasePlugin implements TypeweaverPlugin {\n abstract name: string;\n description?: string;\n author?: string;\n depends?: string[];\n\n protected config: PluginConfig;\n\n constructor(config: PluginConfig = {}) {\n this.config = config;\n }\n\n /**\n * Default implementation - override in subclasses if needed\n */\n async initialize(_context: PluginContext): Promise<void> {\n // Default: no initialization needed\n }\n\n /**\n * Default implementation - override in subclasses if needed\n */\n collectResources(resources: GetResourcesResult): GetResourcesResult {\n // Default: return resources unchanged\n return resources;\n }\n\n /**\n * Main generation logic - must be implemented by subclasses\n */\n abstract generate(context: GeneratorContext): Promise<void> | void;\n\n /**\n * Default implementation - override in subclasses if needed\n */\n async finalize(_context: PluginContext): Promise<void> {\n // Default: no finalization needed\n }\n\n /**\n * Copy lib files from plugin package to generated lib folder\n */\n protected copyLibFiles(\n context: GeneratorContext,\n libSourceDir: string,\n libNamespace: string\n ): void {\n const libDir = path.join(context.outputDir, \"lib\", libNamespace);\n\n // Ensure lib directory exists\n fs.mkdirSync(libDir, { recursive: true });\n\n // Copy all files from lib source to lib directory\n if (fs.existsSync(libSourceDir)) {\n const files = fs.readdirSync(libSourceDir);\n\n for (const file of files) {\n const sourcePath = path.join(libSourceDir, file);\n const targetPath = path.join(libDir, file);\n\n if (fs.statSync(sourcePath).isFile()) {\n fs.copyFileSync(sourcePath, targetPath);\n }\n }\n\n const libIndexPath = path.join(\"lib\", libNamespace, \"index.ts\");\n if (fs.existsSync(path.join(libDir, \"index.ts\"))) {\n context.addGeneratedFile(libIndexPath);\n }\n }\n }\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { render } from \"ejs\";\nimport { BasePlugin } from \"./BasePlugin\";\nimport type { GeneratorContext } from \"./types\";\nimport type { Data } from \"ejs\";\n\n/**\n * Base class for template-based generator plugins\n * Provides utilities for working with EJS templates\n */\nexport abstract class BaseTemplatePlugin extends BasePlugin {\n /**\n * Render an EJS template with the given data\n */\n protected renderTemplate(templatePath: string, data: unknown): string {\n const template = fs.readFileSync(templatePath, \"utf8\");\n return render(template, data as Data);\n }\n\n /**\n * Write a file relative to the output directory\n */\n protected writeFile(\n context: GeneratorContext,\n relativePath: string,\n content: string\n ): void {\n context.writeFile(relativePath, content);\n }\n\n /**\n * Ensure a directory exists\n */\n protected ensureDir(context: GeneratorContext, relativePath: string): void {\n const fullPath = path.join(context.outputDir, relativePath);\n fs.mkdirSync(fullPath, { recursive: true });\n }\n\n /**\n * Get the template path for this plugin\n */\n protected getTemplatePath(\n context: GeneratorContext,\n templateName: string\n ): string {\n return path.join(context.templateDir, templateName);\n }\n}\n","import type { PluginRegistration, TypeweaverPlugin } from \"./types\";\n\n/**\n * Registry for managing typeweaver plugins\n */\nexport class PluginRegistry {\n private plugins: Map<string, PluginRegistration>;\n\n public constructor() {\n this.plugins = new Map();\n }\n\n /**\n * Register a plugin\n */\n public register(plugin: TypeweaverPlugin, config?: unknown): void {\n if (this.plugins.has(plugin.name)) {\n console.info(\n `Skipping duplicate registration of required plugin: ${plugin.name}`\n );\n return;\n }\n\n const registration: PluginRegistration = {\n name: plugin.name,\n plugin,\n config: config as Record<string, unknown>,\n };\n\n this.plugins.set(plugin.name, registration);\n console.info(`Registered plugin: ${plugin.name}`);\n }\n\n /**\n * Get a registered plugin\n */\n public get(name: string): PluginRegistration | undefined {\n return this.plugins.get(name);\n }\n\n /**\n * Get all registered plugins\n */\n public getAll(): PluginRegistration[] {\n return Array.from(this.plugins.values());\n }\n\n /**\n * Check if a plugin is registered\n */\n public has(name: string): boolean {\n return this.plugins.has(name);\n }\n\n /**\n * Clear all registered plugins (except required ones)\n */\n public clear(): void {\n this.plugins.clear();\n }\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { render } from \"ejs\";\nimport type { GetResourcesResult } from \"../Resource\";\nimport type { GeneratorContext, PluginConfig, PluginContext } from \"./types\";\nimport type { Data } from \"ejs\";\n\n/**\n * Builder for plugin contexts\n */\nexport class PluginContextBuilder {\n private generatedFiles = new Set<string>();\n\n /**\n * Create a basic plugin context\n */\n createPluginContext(params: {\n outputDir: string;\n inputDir: string;\n config: PluginConfig;\n }): PluginContext {\n return {\n outputDir: params.outputDir,\n inputDir: params.inputDir,\n config: params.config,\n };\n }\n\n /**\n * Create a generator context with utilities\n */\n createGeneratorContext(params: {\n outputDir: string;\n inputDir: string;\n config: PluginConfig;\n resources: GetResourcesResult;\n templateDir: string;\n coreDir: string;\n }): GeneratorContext {\n const pluginContext = this.createPluginContext(params);\n\n return {\n ...pluginContext,\n resources: params.resources,\n templateDir: params.templateDir,\n coreDir: params.coreDir,\n\n // Utility functions\n writeFile: (relativePath: string, content: string) => {\n const fullPath = path.join(params.outputDir, relativePath);\n const dir = path.dirname(fullPath);\n\n // Ensure directory exists\n fs.mkdirSync(dir, { recursive: true });\n\n // Write file\n fs.writeFileSync(fullPath, content);\n\n // Track generated file\n this.generatedFiles.add(relativePath);\n\n console.info(`Generated: ${relativePath}`);\n },\n\n renderTemplate: (templatePath: string, data: unknown) => {\n const fullTemplatePath = path.isAbsolute(templatePath)\n ? templatePath\n : path.join(params.templateDir, templatePath);\n\n const template = fs.readFileSync(fullTemplatePath, \"utf8\");\n return render(template, data as Data);\n },\n\n addGeneratedFile: (relativePath: string) => {\n this.generatedFiles.add(relativePath);\n },\n\n getGeneratedFiles: () => {\n return Array.from(this.generatedFiles);\n },\n };\n }\n\n /**\n * Get all generated files\n */\n getGeneratedFiles(): string[] {\n return Array.from(this.generatedFiles);\n }\n\n /**\n * Clear generated files tracking\n */\n clearGeneratedFiles(): void {\n this.generatedFiles.clear();\n }\n}\n","import path from \"node:path\";\n\nexport class Path {\n public static relative(from: string, to: string): string {\n const relativePath = path.relative(from, to);\n\n if (relativePath.includes(\"node_modules\")) {\n const parts = relativePath.split(path.sep);\n const index = parts.indexOf(\"node_modules\");\n return parts.slice(index + 1).join(\"/\");\n }\n\n const posixPath = relativePath.split(path.sep).join(\"/\");\n\n if (!posixPath.startsWith(\"./\") && !posixPath.startsWith(\"../\")) {\n return `./${posixPath}`;\n }\n\n return posixPath;\n }\n}\n"],"mappings":";;;;;;;;AAyGA,IAAa,kBAAb,cAAqC,MAAM;CACzC,YACE,AAAO,YACP,SACA;AACA,QAAM,0BAA0B,WAAW,KAAK,UAAU;EAHnD;AAIP,OAAK,OAAO;;;;;;AAOhB,IAAa,wBAAb,cAA2C,MAAM;CAC/C,YACE,AAAO,YACP,AAAO,mBACP;AACA,QACE,WAAW,WAAW,g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AM,mBAAmB,KAAK,WAAW,aAAa,GAClD,eACA,KAAK,KAAK,OAAO,aAAa,aAAa;AAG/C,WAAO,OADU,GAAG,aAAa,kBAAkB,OAAO,EAClC,KAAa;;GAGvC,mBAAmB,iBAAyB;AAC1C,SAAK,eAAe,IAAI,aAAa;;GAGvC,yBAAyB;AACvB,WAAO,MAAM,KAAK,KAAK,eAAe;;GAEzC;;;;;CAMH,oBAA8B;AAC5B,SAAO,MAAM,KAAK,KAAK,eAAe;;;;;CAMxC,sBAA4B;AAC1B,OAAK,eAAe,OAAO;;;;;;AC5F/B,IAAa,OAAb,MAAkB;CAChB,OAAc,SAAS,MAAc,IAAoB;EACvD,MAAM,eAAe,KAAK,SAAS,MAAM,GAAG;AAE5C,MAAI,aAAa,SAAS,eAAe,EAAE;GACzC,MAAM,QAAQ,aAAa,MAAM,KAAK,IAAI;GAC1C,MAAM,QAAQ,MAAM,QAAQ,eAAe;AAC3C,UAAO,MAAM,MAAM,QAAQ,EAAE,CAAC,KAAK,IAAI;;EAGzC,MAAM,YAAY,aAAa,MAAM,KAAK,IAAI,CAAC,KAAK,IAAI;AAExD,MAAI,CAAC,UAAU,WAAW,KAAK,IAAI,CAAC,UAAU,WAAW,MAAM,CAC7D,QAAO,KAAK;AAGd,SAAO"}
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@rexeus/typeweaver-gen",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Template-driven engine that turns structured API definitions into production-ready artifacts. Powered by Typeweaver 🧵✨",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "dist/index.cjs",
8
- "module": "dist/index.js",
9
- "types": "dist/index.d.ts",
8
+ "module": "dist/index.mjs",
9
+ "types": "dist/index.d.mts",
10
10
  "exports": {
11
11
  ".": {
12
12
  "import": {
13
- "types": "./dist/index.d.ts",
14
- "default": "./dist/index.js"
13
+ "types": "./dist/index.d.mts",
14
+ "default": "./dist/index.mjs"
15
15
  },
16
16
  "require": {
17
17
  "types": "./dist/index.d.cts",
@@ -47,18 +47,19 @@
47
47
  },
48
48
  "homepage": "https://github.com/rexeus/typeweaver#readme",
49
49
  "peerDependencies": {
50
- "@rexeus/typeweaver-core": "^0.5.0"
50
+ "@rexeus/typeweaver-core": "^0.6.0"
51
51
  },
52
52
  "devDependencies": {
53
- "@rexeus/typeweaver-core": "^0.5.0"
53
+ "@rexeus/typeweaver-core": "^0.6.0"
54
54
  },
55
55
  "dependencies": {
56
56
  "ejs": "^3.1.10"
57
57
  },
58
58
  "scripts": {
59
59
  "typecheck": "tsc --noEmit",
60
- "format": "prettier --write .",
61
- "build": "tsup && cp ../../LICENSE ../../NOTICE ./dist/",
60
+ "format": "oxfmt",
61
+ "test": "vitest run",
62
+ "build": "tsdown && cp ../../LICENSE ../../NOTICE ./dist/",
62
63
  "preversion": "npm run build"
63
64
  }
64
65
  }