Skip to content

Commit b6a4926

Browse files
committed
feat: add routes scanner
1 parent af7ce8d commit b6a4926

18 files changed

+2251
-294
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
"engines": {
66
"node": ">=24.0.0"
77
},
8+
"imports": {
9+
"#tests/*": "./tests/*.ts"
10+
},
811
"main": "build/index.js",
912
"type": "module",
1013
"files": [
1114
"build",
1215
"!build/bin",
1316
"!build/tests"
1417
],
15-
"imports": {
16-
"#src/*": "./src/*.ts"
17-
},
1818
"exports": {
1919
".": "./build/index.js",
2020
"./code_transformer": "./build/src/code_transformer/main.js",
21+
"./routes_scanner": "./build/src/code_scanners/routes_scanner/main.js",
2122
"./types": "./build/src/types/main.js"
2223
},
2324
"scripts": {
@@ -33,7 +34,7 @@
3334
"release": "release-it",
3435
"version": "npm run build",
3536
"prepublishOnly": "npm run build",
36-
"quick:test": "cross-env NODE_DEBUG=adonisjs:assembler node --enable-source-maps --import=@poppinss/ts-exec bin/test.ts"
37+
"quick:test": "cross-env NODE_DEBUG=adonisjs:assembler node --enable-source-maps --import=@poppinss/ts-exec --experimental-import-meta-resolve bin/test.ts"
3738
},
3839
"devDependencies": {
3940
"@adonisjs/eslint-config": "^3.0.0-next.0",

src/ast_file_system.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { readFile } from 'node:fs/promises'
11+
import { Lang, parse, type SgNode } from '@ast-grep/napi'
12+
13+
import debug from './debug.ts'
14+
15+
/**
16+
* An abstraction around converting files to an AST and caching
17+
* them forever. The cache could be cleared manually.
18+
*/
19+
export class AstFileSystem {
20+
#cache: Map<string, SgNode> = new Map()
21+
22+
/**
23+
* Returns the file contents as AST-grep node and caches it
24+
* forever.
25+
*/
26+
async get(filePath: string): Promise<SgNode> {
27+
const cached = this.#cache.get(filePath)
28+
if (cached) {
29+
debug('returning AST nodes from cache "%s"', filePath)
30+
return cached
31+
}
32+
33+
const fileContents = await readFile(filePath, 'utf-8')
34+
debug('parsing "%s" file to AST', filePath)
35+
this.#cache.set(filePath, parse(Lang.TypeScript, fileContents).root())
36+
return this.#cache.get(filePath)!
37+
}
38+
39+
/**
40+
* Clear AST cache for a single file or all the files
41+
*/
42+
clear(filePath?: string) {
43+
debug('clear AST cache "%s"', filePath)
44+
filePath ? this.#cache.delete(filePath) : this.#cache.clear()
45+
}
46+
}

0 commit comments

Comments
 (0)