/
util.ts
73 lines (69 loc) · 1.94 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import type { ParseResult } from '@babel/parser'
import { parse } from '@babel/parser'
import type { File } from '@babel/types'
import colors from 'picocolors'
import MagicString from 'magic-string'
export function rewriteImports(
fileOrDir: string,
rewrite: (importPath: string, currentFile: string) => string | void,
): void {
walkDir(fileOrDir, (file) => {
rewriteFileImports(file, (importPath) => {
return rewrite(importPath, file)
})
})
}
const windowsSlashRE = /\\/g
export function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}
export function walkDir(dir: string, handleFile: (file: string) => void): void {
if (statSync(dir).isDirectory()) {
const files = readdirSync(dir)
for (const file of files) {
const resolved = resolve(dir, file)
walkDir(resolved, handleFile)
}
} else {
handleFile(dir)
}
}
function rewriteFileImports(
file: string,
rewrite: (importPath: string) => string | void,
): void {
const content = readFileSync(file, 'utf-8')
const str = new MagicString(content)
let ast: ParseResult<File>
try {
ast = parse(content, {
sourceType: 'module',
plugins: ['typescript', 'classProperties'],
})
} catch (e) {
console.log(colors.red(`failed to parse ${file}`))
throw e
}
for (const statement of ast.program.body) {
if (
statement.type === 'ImportDeclaration' ||
statement.type === 'ExportNamedDeclaration' ||
statement.type === 'ExportAllDeclaration'
) {
const source = statement.source
if (source?.value) {
const newImportPath = rewrite(source.value)
if (newImportPath) {
str.overwrite(
source.start!,
source.end!,
JSON.stringify(newImportPath),
)
}
}
}
}
writeFileSync(file, str.toString())
}