@tuyau/core 0.1.1 → 0.1.2

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.
@@ -3,6 +3,7 @@ import {
3
3
  } from "../chunk-3KNDPG6Y.js";
4
4
 
5
5
  // commands/generate.ts
6
+ import { fileURLToPath as fileURLToPath2 } from "node:url";
6
7
  import { Project, QuoteKind } from "ts-morph";
7
8
  import { BaseCommand, flags } from "@adonisjs/core/ace";
8
9
 
@@ -30,13 +31,13 @@ var ApiTypesGenerator = class {
30
31
  this.#prepareDestination();
31
32
  }
32
33
  #getDestinationDirectory() {
33
- return dirname(this.#destination.pathname);
34
+ return dirname(this.#destination);
34
35
  }
35
36
  /**
36
37
  * Create the destination directory if it does not exists
37
38
  */
38
39
  #prepareDestination() {
39
- this.#destination = new URL("./.adonisjs/api.ts", this.#appRoot);
40
+ this.#destination = fileURLToPath(new URL("./.adonisjs/api.ts", this.#appRoot));
40
41
  const directory = this.#getDestinationDirectory();
41
42
  if (!existsSync(directory)) {
42
43
  mkdirSync(directory, { recursive: true });
@@ -109,20 +110,34 @@ var ApiTypesGenerator = class {
109
110
  return interfaceContent;
110
111
  }
111
112
  /**
112
- * Filter routes to generate based on the ignoreRoutes config
113
+ * Filter routes to generate based on the config
113
114
  */
114
- #filterRoutesToGenerate(routes) {
115
+ #filterRoutes(routes, mode) {
116
+ const config = this.#config.codegen?.[mode];
117
+ if (!config || !config.only && !config.except)
118
+ return routes;
115
119
  return routes.filter((route) => {
116
- if (!this.#config.codegen?.ignoreRoutes)
117
- return true;
118
- if (typeof this.#config.codegen?.ignoreRoutes === "function") {
119
- return !this.#config.codegen.ignoreRoutes(route);
120
+ if (typeof config.only === "function")
121
+ return config.only(route);
122
+ if (typeof config.except === "function")
123
+ return !config.except(route);
124
+ if (config.only) {
125
+ for (const pattern of config.only) {
126
+ if (pattern instanceof RegExp && pattern.test(route.pattern))
127
+ return true;
128
+ if (route.pattern === pattern)
129
+ return true;
130
+ }
131
+ return false;
120
132
  }
121
- for (const ignore of this.#config.codegen.ignoreRoutes) {
122
- if (typeof ignore === "string" && route.pattern === ignore)
123
- return false;
124
- if (ignore instanceof RegExp && ignore.test(route.pattern))
125
- return false;
133
+ if (config.except) {
134
+ for (const pattern of config.except) {
135
+ if (pattern instanceof RegExp && pattern.test(route.pattern))
136
+ return false;
137
+ if (route.pattern === pattern)
138
+ return false;
139
+ }
140
+ return true;
126
141
  }
127
142
  return true;
128
143
  });
@@ -147,8 +162,7 @@ var ApiTypesGenerator = class {
147
162
  }).filter((route) => !!route.name);
148
163
  }
149
164
  async #writeApiFile(options) {
150
- const path = fileURLToPath(this.#destination);
151
- const file = this.#project.createSourceFile(path, "", { overwrite: true });
165
+ const file = this.#project.createSourceFile(this.#destination, "", { overwrite: true });
152
166
  if (!file)
153
167
  throw new Error("Unable to create the api.ts file");
154
168
  file.removeText().insertText(0, (writer) => {
@@ -159,7 +173,7 @@ var ApiTypesGenerator = class {
159
173
  writer.writeLine(` response: ${value.response}`);
160
174
  writer.writeLine(`}`);
161
175
  });
162
- writer.writeLine(`interface AdonisApi {`).write(this.#generateDefinitionInterface(options.definition, " ")).writeLine(`}`);
176
+ writer.writeLine(`export interface ApiDefinition {`).write(this.#generateDefinitionInterface(options.definition, " ")).writeLine(`}`);
163
177
  writer.writeLine(`const routes = [`);
164
178
  for (const route of options.routesNameArray) {
165
179
  writer.writeLine(` {`);
@@ -171,7 +185,11 @@ var ApiTypesGenerator = class {
171
185
  writer.writeLine(` },`);
172
186
  }
173
187
  writer.writeLine(`] as const;`);
174
- writer.writeLine(`export const api = {`).writeLine(` routes,`).writeLine(` definition: {} as AdonisApi`).writeLine(`}`);
188
+ writer.writeLine(`export const api = {`).writeLine(` routes,`).writeLine(` definition: {} as ApiDefinition`).writeLine(`}`);
189
+ writer.writeLine(`declare module '@tuyau/inertia/types' {`);
190
+ writer.writeLine(` type ApiDefinition = typeof api`);
191
+ writer.writeLine(` export interface Api extends ApiDefinition {}`);
192
+ writer.writeLine(`}`);
175
193
  });
176
194
  await file.save();
177
195
  }
@@ -179,7 +197,7 @@ var ApiTypesGenerator = class {
179
197
  const definition = {};
180
198
  const typesByPattern = {};
181
199
  const sourcesFiles = this.#project.getSourceFiles();
182
- const routes = this.#filterRoutesToGenerate(this.#routes);
200
+ const routes = this.#filterRoutes(this.#routes, "definitions");
183
201
  for (const route of routes) {
184
202
  if (typeof route.handler === "function")
185
203
  continue;
@@ -218,11 +236,11 @@ var ApiTypesGenerator = class {
218
236
  currentLevel[method] = typeName;
219
237
  });
220
238
  }
221
- await this.#writeApiFile({
222
- definition,
223
- typesByPattern,
224
- routesNameArray: this.#generateRoutesNameArray(routes, typesByPattern)
225
- });
239
+ const routesNameArray = this.#generateRoutesNameArray(
240
+ this.#filterRoutes(routes, "routes"),
241
+ typesByPattern
242
+ );
243
+ await this.#writeApiFile({ definition, typesByPattern, routesNameArray });
226
244
  }
227
245
  };
228
246
 
@@ -245,7 +263,7 @@ var CodegenTypes = class extends BaseCommand {
245
263
  async run() {
246
264
  const project = new Project({
247
265
  manipulationSettings: { quoteKind: QuoteKind.Single },
248
- tsConfigFilePath: new URL("./tsconfig.json", this.app.appRoot).pathname
266
+ tsConfigFilePath: fileURLToPath2(new URL("./tsconfig.json", this.app.appRoot))
249
267
  });
250
268
  const apiTypesGenerator = new ApiTypesGenerator({
251
269
  project,
@@ -6,9 +6,14 @@ import { defineConfig } from '@tuyau/core'
6
6
  const tuyauConfig = defineConfig({
7
7
  codegen: {
8
8
  /**
9
- * List of routes to ignore during code generation
9
+ * Filters the definitions and named routes to be generated
10
10
  */
11
- ignoreRoutes: []
11
+ // definitions: {
12
+ // only: [],
13
+ // }
14
+ // routes: {
15
+ // only: [],
16
+ // }
12
17
  }
13
18
  })
14
19
 
@@ -1,12 +1,25 @@
1
1
  import { RouteJSON } from '@adonisjs/core/types/http';
2
2
 
3
+ type Without<T, U> = {
4
+ [P in Exclude<keyof T, keyof U>]?: never;
5
+ };
6
+ type XOR<T, U> = T | U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
7
+ type FilterConfig<FilterType> = XOR<{
8
+ only: FilterType;
9
+ }, {
10
+ except: FilterType;
11
+ }>;
3
12
  interface TuyauConfig {
4
13
  codegen?: {
5
14
  /**
6
- * List of routes to ignore during code generation
15
+ * Filters the definitions to be generated
7
16
  */
8
- ignoreRoutes?: Array<string | RegExp> | ((route: RouteJSON) => boolean);
17
+ definitions?: FilterConfig<Array<string | RegExp> | ((route: RouteJSON) => boolean)>;
18
+ /**
19
+ * Filters the named routes to be generated
20
+ */
21
+ routes?: FilterConfig<Array<string | RegExp> | ((route: RouteJSON) => boolean)>;
9
22
  };
10
23
  }
11
24
 
12
- export type { TuyauConfig };
25
+ export type { FilterConfig, TuyauConfig };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tuyau/core",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "MIT",
@@ -31,8 +31,8 @@
31
31
  "@poppinss/cliui": "^6.4.1",
32
32
  "@poppinss/matchit": "^3.1.2",
33
33
  "@types/node": "^20.12.7",
34
- "@tuyau/utils": "0.0.4",
35
- "@tuyau/client": "0.1.0"
34
+ "@tuyau/client": "0.1.1",
35
+ "@tuyau/utils": "0.0.4"
36
36
  },
37
37
  "publishConfig": {
38
38
  "access": "public",