-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
121 lines (93 loc) · 3.13 KB
/
script.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env node
import { readdirSync, statSync, readFile, writeFile, existsSync } from "fs";
import { join, extname } from "path";
import { parseFlags, parseExtensions } from "./utils.js";
const args = process.argv.slice(2);
const flags = parseFlags(args);
const allowedExtensions = parseExtensions(args) ?? [".ts", ".js"];
function traverseDirectory(directory) {
const files = readdirSync(directory);
files.forEach((file) => {
const filePath = join(directory, file);
const fileStat = statSync(filePath);
if (fileStat.isDirectory()) {
traverseDirectory(filePath); // Recursively traverse subdirectories
return;
}
const fileExtension = extname(file);
if (!allowedExtensions.includes(fileExtension.slice(1))) {
// console.log(`Ignoring file: ${filePath}`);
return;
}
let fileContent = "";
// Read and process the file
readFile(filePath, "utf8", (err, data) => {
fileContent = data;
if (err) {
console.error(`Error reading file: ${filePath}`.red);
return;
}
// Regex pattern to match the import statement
const importPattern = /from ['"].+['"]/gm;
// import foo from 'bar'
const oneLineImports = data.match(importPattern) ?? [];
// import 'some-file';
const allFileImports = data.match(/import\s* ['"].*['"]/g) ?? [];
const importMatches = [...oneLineImports, ...allFileImports];
if (importMatches.length === 0) {
return;
}
importMatches.forEach((importMatch) => {
if (!importMatch.includes("../") && !importMatch.includes("./")) {
return;
}
const numberOfBackJumps = Array.from(
importMatch.matchAll(/\.\.\//g)
).length;
const currentFilePathAfterSrcDirectory = filePath
.substring(filePath.indexOf("\\src") + "\\src".length)
.replaceAll("\\", "/")
.replace("/", "");
// from the path components/general/input.vue => ['components', 'general']
const directoriesOfPath = getDirectoriesOfPath(
currentFilePathAfterSrcDirectory
);
for (let i = 0; i < numberOfBackJumps; i++) {
directoriesOfPath.pop();
}
let newPath = `${flags.alias ?? "@"}/${directoriesOfPath.join("/")}`;
if (directoriesOfPath.length > 0) {
newPath += "/";
}
const newImportStatement = importMatch.replace(
/([\.\/]*[\.\.\/])+/,
newPath
);
// replace the old path with new one
fileContent = fileContent.replace(importMatch, newImportStatement);
});
writeFile(filePath, fileContent, (err) => {
if (err) {
console.error("An error occurred:", err);
return;
}
});
});
});
}
const rootDirectory = process.cwd() + "\\src";
const exists = existsSync(rootDirectory);
if (exists) {
traverseDirectory(rootDirectory);
} else {
console.log("root dir is not correct");
}
/**
* @param {stirng} path
* @returns {string[]}
*/
function getDirectoriesOfPath(path) {
const directories = path.split("/");
directories.pop();
return directories;
}