-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformRelativeToAbsoluteImports.ts
More file actions
39 lines (31 loc) · 1.07 KB
/
transformRelativeToAbsoluteImports.ts
File metadata and controls
39 lines (31 loc) · 1.07 KB
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
#!npx ts-node
import {Project} from 'ts-morph';
const project = new Project({
tsConfigFilePath: 'tsconfig.json',
});
project.getSourceFiles().forEach(sourceFile => {
const imports = sourceFile.getImportDeclarations();
imports.forEach((import_) => {
const filePath = import_.getModuleSpecifierSourceFile()?.getFilePath();
const filePathSpecified = import_.getModuleSpecifierValue()
if (filePath == null) {
return
}
if (filePathSpecified.startsWith("./") || filePathSpecified.startsWith("../")) {
console.log("---")
console.log(filePathSpecified)
console.log(filePath)
// poor man's regex
const startIndexOfPath = filePath.indexOf("src")
let absolutePath = filePath.slice(startIndexOfPath)
if (absolutePath.endsWith(".ts")) {
absolutePath = absolutePath.slice(0, absolutePath.length - 3)
}
if (absolutePath.endsWith("/index")) {
absolutePath = absolutePath.slice(0, absolutePath.length - 6)
}
import_.setModuleSpecifier(absolutePath)
}
})
sourceFile.save().then()
})