-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-js.ts
56 lines (45 loc) · 1.43 KB
/
fix-js.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
import * as fs from "fs"
import * as path from "path"
const getAllFiles = function (dirPath: string, input?: string[]) {
const files = fs.readdirSync(dirPath)
let arrayOfFiles = input || []
files.forEach(function (file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))
}
})
return arrayOfFiles
}
const files = getAllFiles("./src")
const fixup = (file: string) => {
const content = fs.readFileSync(file).toString("utf-8")
const lines = content.split("\n")
if (content) {
const fixed = lines.map((line) => {
const res = line.match(/(import|export|from|declare module).*"(.*)"/)
if (res) {
const [, , relative] = res
if (relative?.startsWith(".")) {
const indexPath = path.join(file, "..", relative) + "/index.ts"
const filePath = path.join(file, "..", relative) + ".ts"
if (fs.existsSync(filePath)) {
return line.replace(relative, relative + ".js")
}
if (fs.existsSync(indexPath)) {
return line.replace(relative, relative + "/index.js")
}
}
}
return line
})
const fixedContent = fixed.join("\n")
if (fixedContent !== content) {
fs.writeFileSync(file, fixedContent)
}
}
}
files.forEach((file) => {
fixup(file)
})