Skip to content

Commit 984c134

Browse files
committed
feat: setup typedoc
1 parent ee82912 commit 984c134

21 files changed

+816
-151
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
- uses: actions/setup-node@v4
1818
with:
19-
node-version: 20
19+
node-version: 24
2020

2121
- name: git config
2222
run: |

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
export { Bundler } from './src/bundler.ts'
1111
export { DevServer } from './src/dev_server.ts'
1212
export { TestRunner } from './src/test_runner.ts'
13+
export { SUPPORTED_PACKAGE_MANAGERS } from './src/bundler.ts'

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
".": "./build/index.js",
2020
"./code_transformer": "./build/src/code_transformer/main.js",
2121
"./routes_scanner": "./build/src/code_scanners/routes_scanner/main.js",
22+
"./helpers": "./build/src/helpers.js",
2223
"./types": "./build/src/types/main.js"
2324
},
2425
"scripts": {
@@ -61,7 +62,7 @@
6162
"typescript": "^5.9.2"
6263
},
6364
"dependencies": {
64-
"@adonisjs/env": "^6.2.0",
65+
"@adonisjs/env": "^7.0.0-next.1",
6566
"@antfu/install-pkg": "^1.1.0",
6667
"@ast-grep/napi": "^0.39.4",
6768
"@poppinss/cliui": "^6.4.4",
@@ -105,6 +106,8 @@
105106
"entry": [
106107
"./index.ts",
107108
"./src/types/main.ts",
109+
"./src/helpers.ts",
110+
"./src/code_scanners/routes_scanner/main.ts",
108111
"./src/code_transformer/main.ts"
109112
],
110113
"outDir": "./build",

src/ast_file_system.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ import debug from './debug.ts'
1515
/**
1616
* An abstraction around converting files to an AST and caching
1717
* them forever. The cache could be cleared manually.
18+
*
19+
* @example
20+
* const astfs = new AstFileSystem()
21+
* const node = await astfs.get('./app/controllers/users_controller.ts')
22+
* astfs.clear('./app/controllers/users_controller.ts')
1823
*/
1924
export class AstFileSystem {
25+
/**
26+
* Cache map storing parsed AST nodes by file path
27+
*/
2028
#cache: Map<string, SgNode> = new Map()
2129

2230
/**
2331
* Returns the file contents as AST-grep node and caches it
2432
* forever.
33+
*
34+
* @param filePath - The path to the file to parse
35+
* @returns Promise resolving to the AST-grep node
2536
*/
2637
async get(filePath: string): Promise<SgNode> {
2738
const cached = this.#cache.get(filePath)
@@ -38,6 +49,8 @@ export class AstFileSystem {
3849

3950
/**
4051
* Clear AST cache for a single file or all the files
52+
*
53+
* @param filePath - Optional file path to clear. If omitted, clears entire cache
4154
*/
4255
clear(filePath?: string) {
4356
debug('clear AST cache "%s"', filePath)

src/bundler.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { type SupportedPackageManager } from './types/code_transformer.ts'
2727
* List of package managers we support in order to
2828
* copy lockfiles
2929
*/
30-
const SUPPORTED_PACKAGE_MANAGERS: {
30+
export const SUPPORTED_PACKAGE_MANAGERS: {
3131
[K in SupportedPackageManager]: {
3232
packageManagerFiles: string[]
3333
installCommand: string
@@ -57,9 +57,20 @@ const SUPPORTED_PACKAGE_MANAGERS: {
5757

5858
/**
5959
* The bundler class exposes the API to build an AdonisJS project.
60+
*
61+
* @example
62+
* const bundler = new Bundler(new URL('./'), ts, { hooks: [] })
63+
* const success = await bundler.bundle()
6064
*/
6165
export class Bundler {
66+
/**
67+
* The current working project directory path as string
68+
*/
6269
#cwdPath: string
70+
71+
/**
72+
* Reference to the TypeScript module
73+
*/
6374
#ts: typeof tsStatic
6475

6576
/**
@@ -72,13 +83,23 @@ export class Bundler {
7283
]
7384
}>
7485

86+
/**
87+
* CLI UI instance for displaying colorful messages and progress information
88+
*/
7589
ui = cliui()
7690

7791
/**
78-
* Package manager detect from the project environment
92+
* Package manager detected from the project environment
7993
*/
8094
declare packageManager: SupportedPackageManager
8195

96+
/**
97+
* Create a new bundler instance
98+
*
99+
* @param cwd - The current working directory URL
100+
* @param ts - TypeScript module reference
101+
* @param options - Bundler configuration options
102+
*/
82103
constructor(
83104
public cwd: URL,
84105
ts: typeof tsStatic,
@@ -168,6 +189,13 @@ export class Bundler {
168189

169190
/**
170191
* Bundles the application to be run in production
192+
*
193+
* @param stopOnError - Whether to stop the build process on TypeScript errors
194+
* @param client - Override the detected package manager
195+
* @returns Promise that resolves to true if build succeeded, false otherwise
196+
*
197+
* @example
198+
* const success = await bundler.bundle(true, 'npm')
171199
*/
172200
async bundle(stopOnError: boolean = true, client?: SupportedPackageManager): Promise<boolean> {
173201
this.#hooks = await loadHooks(this.options.hooks, ['buildStarting', 'buildFinished'])

src/code_scanners/routes_scanner/main.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ import {
2525
type RoutesScannerRules,
2626
} from '../../types/code_scanners.ts'
2727

28+
/**
29+
* RoutesScanner is responsible for scanning application routes,
30+
* extracting their controllers, validators, request and response types.
31+
*
32+
* @example
33+
* const scanner = new RoutesScanner(appRoot, [rules])
34+
* scanner.defineResponse((route, controller) => {
35+
* return { type: 'CustomResponseType', imports: [] }
36+
* })
37+
* await scanner.scan(routes)
38+
* const scannedRoutes = scanner.getScannedRoutes()
39+
*/
2840
export class RoutesScanner {
2941
/**
3042
* The root of the application from where we will resolve
@@ -74,7 +86,7 @@ export class RoutesScanner {
7486
) => AsyncOrSync<ScannedRoute['validators']>
7587

7688
/**
77-
* CLI UI to log colorful messages
89+
* CLI UI instance to log colorful messages and progress information
7890
*/
7991
ui = cliui()
8092

@@ -99,6 +111,12 @@ export class RoutesScanner {
99111
response: {},
100112
}
101113

114+
/**
115+
* Create a new RoutesScanner instance
116+
*
117+
* @param appRoot - The root directory of the application
118+
* @param rulesCollection - Collection of rules to apply during scanning
119+
*/
102120
constructor(appRoot: string, rulesCollection: RoutesScannerRules[]) {
103121
this.#appRoot = appRoot
104122

@@ -340,6 +358,9 @@ export class RoutesScanner {
340358
* a given route. The callback will be executed for all
341359
* the routes and you must return undefined to fallback
342360
* to the default behavior of detecting types
361+
*
362+
* @param callback - Function to compute response types for routes
363+
* @returns This RoutesScanner instance for method chaining
343364
*/
344365
defineResponse(
345366
callback: (
@@ -357,6 +378,9 @@ export class RoutesScanner {
357378
* a given route. The callback will be executed for all
358379
* the routes and you must return undefined to fallback
359380
* to the default behavior of detecting types
381+
*
382+
* @param callback - Function to compute request types for routes
383+
* @returns This RoutesScanner instance for method chaining
360384
*/
361385
defineRequest(
362386
callback: (
@@ -370,8 +394,10 @@ export class RoutesScanner {
370394
}
371395

372396
/**
373-
* Register a callback to extract validators from the route
374-
* controller.
397+
* Register a callback to extract validators from the route controller
398+
*
399+
* @param callback - Function to extract validators from controllers
400+
* @returns This RoutesScanner instance for method chaining
375401
*/
376402
extractValidators(
377403
callback: (
@@ -386,6 +412,8 @@ export class RoutesScanner {
386412

387413
/**
388414
* Returns the scanned routes
415+
*
416+
* @returns Array of scanned routes with their metadata
389417
*/
390418
getScannedRoutes() {
391419
return this.#scannedRoutes
@@ -394,6 +422,8 @@ export class RoutesScanner {
394422
/**
395423
* Invalidating a controller will trigger computing the validators,
396424
* request types and the response types.
425+
*
426+
* @param controllerPath - Path to the controller file to invalidate
397427
*/
398428
async invalidate(controllerPath: string) {
399429
controllerPath = string.toUnixSlash(controllerPath)
@@ -414,6 +444,8 @@ export class RoutesScanner {
414444
/**
415445
* Scans an array of Route list items and fetches their validators,
416446
* controllers, and request/response types.
447+
*
448+
* @param routes - Array of route list items to scan
417449
*/
418450
async scan(routes: RoutesListItem[]) {
419451
for (let route of routes) {

src/code_transformer/main.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,26 @@ import type {
3535
} from '../types/code_transformer.ts'
3636

3737
/**
38-
* This class is responsible for updating
38+
* This class is responsible for transforming AdonisJS project code,
39+
* including updating middleware, environment validations, and other
40+
* code generation tasks.
41+
*
42+
* @example
43+
* const transformer = new CodeTransformer(cwd)
44+
* await transformer.addMiddlewareToStack('server', [{
45+
* path: '#middleware/cors_middleware',
46+
* position: 'before'
47+
* }])
3948
*/
4049
export class CodeTransformer {
4150
/**
42-
* Exporting utilities to install package and detect
43-
* the package manager
51+
* Utility function for installing packages
4452
*/
4553
installPackage = installPackage
54+
55+
/**
56+
* Utility function for detecting the package manager
57+
*/
4658
detectPackageManager = detectPackageManager
4759

4860
/**
@@ -52,7 +64,7 @@ export class CodeTransformer {
5264
#cwdPath: string
5365

5466
/**
55-
* The TsMorph project
67+
* The TsMorph project instance for AST manipulation
5668
*/
5769
project: Project
5870

@@ -69,6 +81,11 @@ export class CodeTransformer {
6981
semicolons: 'remove',
7082
}
7183

84+
/**
85+
* Create a new CodeTransformer instance
86+
*
87+
* @param cwd - The current working directory URL
88+
*/
7289
constructor(cwd: URL) {
7390
this.#cwd = cwd
7491
this.#cwdPath = fileURLToPath(this.#cwd)
@@ -237,8 +254,9 @@ export class CodeTransformer {
237254
}
238255

239256
/**
240-
* Add new env variable validation in the
241-
* `env.ts` file
257+
* Add new env variable validation in the `env.ts` file
258+
*
259+
* @param definition - Environment validation definition containing variables and comment
242260
*/
243261
async defineEnvValidations(definition: EnvValidationNode) {
244262
/**
@@ -306,12 +324,14 @@ export class CodeTransformer {
306324
}
307325

308326
/**
309-
* Define new middlewares inside the `start/kernel.ts`
310-
* file
327+
* Define new middlewares inside the `start/kernel.ts` file
311328
*
312329
* This function is highly based on some assumptions
313330
* and will not work if you significantly tweaked
314331
* your `start/kernel.ts` file.
332+
*
333+
* @param stack - The middleware stack to add to ('server', 'router', or 'named')
334+
* @param middleware - Array of middleware entries to add
315335
*/
316336
async addMiddlewareToStack(stack: 'server' | 'router' | 'named', middleware: MiddlewareNode[]) {
317337
/**
@@ -336,7 +356,9 @@ export class CodeTransformer {
336356
}
337357

338358
/**
339-
* Update the `adonisrc.ts` file
359+
* Update the `adonisrc.ts` file using the provided callback
360+
*
361+
* @param callback - Function that receives the RcFileTransformer for modifications
340362
*/
341363
async updateRcFile(callback: (transformer: RcFileTransformer) => void) {
342364
const rcFileTransformer = new RcFileTransformer(this.#cwd, this.project)
@@ -346,6 +368,9 @@ export class CodeTransformer {
346368

347369
/**
348370
* Add a new Japa plugin in the `tests/bootstrap.ts` file
371+
*
372+
* @param pluginCall - The plugin function call to add
373+
* @param importDeclarations - Import declarations needed for the plugin
349374
*/
350375
async addJapaPlugin(
351376
pluginCall: string,
@@ -383,7 +408,10 @@ export class CodeTransformer {
383408
}
384409

385410
/**
386-
* Add a new Vite plugin
411+
* Add a new Vite plugin to the `vite.config.ts` file
412+
*
413+
* @param pluginCall - The plugin function call to add
414+
* @param importDeclarations - Import declarations needed for the plugin
387415
*/
388416
async addVitePlugin(
389417
pluginCall: string,
@@ -442,6 +470,8 @@ export class CodeTransformer {
442470
/**
443471
* Adds a policy to the list of `policies` object configured
444472
* inside the `app/policies/main.ts` file.
473+
*
474+
* @param policies - Array of bouncer policy entries to add
445475
*/
446476
async addPolicies(policies: BouncerPolicyNode[]) {
447477
/**
@@ -475,9 +505,14 @@ export class CodeTransformer {
475505
* }
476506
* ```
477507
*
478-
* @param source
479-
* @param outputPath
480-
* @param importAlias
508+
* @param input - Source configuration for entity scanning
509+
* @param output - Output configuration for the generated index file
510+
*
511+
* @example
512+
* await transformer.makeEntityIndex(
513+
* { source: 'app/controllers', importAlias: '#controllers' },
514+
* { destination: 'start/controllers.ts', exportName: 'controllers' }
515+
* )
481516
*/
482517
async makeEntityIndex(
483518
input: OneOrMore<{ source: string; importAlias?: string; allowedExtensions?: string[] }>,

0 commit comments

Comments
 (0)