-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixPaths.cjs
183 lines (150 loc) · 5.14 KB
/
fixPaths.cjs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* A very basic search and replace function to find import and export statements
* in .js files where the path of the file being specified does not have a .js
* extension and replace those paths with one that has a .js extension.
*
* This would be much better written using abstract source trees to do the
* re-writing which would be "safer", however this is a fully working function
* and doesn't seem to mess anything up at present.
*/
const chokidar = require("chokidar");
const fs = require("fs");
const { readFile, writeFile, readdir } = require("fs").promises;
const { resolve } = require("path");
const { minimatch } = require("minimatch");
const matchPattern = ["**/*.js", "**/*.jsx"];
const excludePattern = ["node_modules", "react", ".git"];
const expressions = [{
exp: /import(.*)?["']([.]+)(((?!\.js).)*?)["'];/g,
update: (match, isIndex) => {
if (isIndex) {
return `import${match[1]}"${match[2]}${match[3]}/index.js";`;
}
return `import${match[1]}"${match[2]}${match[3]}.js";`;
}
}, {
exp: /export\s(.*)?\sfrom\s["']([.]+)(((?!\.js).)*?)["'];/g,
update: (match, isIndex) => {
if (isIndex) {
return `export ${match[1]} from "${match[2]}${match[3]}/index.js";`;
}
return `export ${match[1]} from "${match[2]}${match[3]}.js";`;
}
}];
let tsPaths = {};
let tsResolvedPaths = {};
const readTsConfig = () => {
const config = require("./tsconfig.json");
tsPaths = config.compilerOptions.paths;
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
tsResolvedPaths[key] = replacementArr.map((dir) => {
return resolve(__dirname, dir);
});
});
};
async function getFiles (dir, gitIgnoreArr) {
const dirArr = await readdir(dir, { withFileTypes: true });
const finalList = dirArr.filter((filePath) => {
return excludePattern.every((glob) => !minimatch(filePath.name, glob));
});
const files = await Promise.all(finalList.map((tmpDir) => {
const res = resolve(dir, tmpDir.name);
return tmpDir.isDirectory() ? getFiles(res) : [res];
})).then((results) => {
return results.reduce((arr, item) => {
arr.push(...item);
return arr;
}, []);
});
const finalFiles = files.filter((file) => {
if (!file || !file.trim()) return false;
return matchPattern.some((glob) => minimatch(file, glob));
});
return Array.prototype.concat(...finalFiles);
}
const processMatches = (regExp, fileFolder, content, updateFunc) => {
let match;
while (match = regExp.exec(content)) {
const pathToItemFile = resolve(fileFolder, match[2] + match[3] + ".js");
const pathToItemFolderIndexFile = resolve(fileFolder, match[2] + match[3] + "/index.js");
let pathStatFile;
let pathStatFolderIndexFile;
try {
pathStatFile = fs.lstatSync(pathToItemFile);
} catch (err) {
pathStatFolderIndexFile = undefined;
}
try {
pathStatFolderIndexFile = fs.lstatSync(pathToItemFolderIndexFile);
} catch (err) {
pathStatFolderIndexFile = undefined;
}
if (pathStatFile) {
// Just add .js to the end of the path
content = content.replace(match[0], updateFunc(match, false));
} else if (pathStatFolderIndexFile) {
content = content.replace(match[0], updateFunc(match, true));
}
}
return content;
};
const processFile = (file) => {
if (!file) return;
//console.log(`Processing ${file}...`);
readFile(file).then((fileContentsBuffer) => {
const fileFolderArr = file.split("/");
fileFolderArr.pop();
const fileFolder = fileFolderArr.join("/");
const fileContent = fileContentsBuffer.toString();
let updatedContent = fileContent;
Object.entries(tsPaths).forEach(([key, replacementArr]) => {
const find = key.replace("*", "");
const replace = replacementArr[0].replace("*", "");
const relativeSteps = new Array(file.replace(__dirname, "").split("/").length - 2).fill("../").join("");
const replacementPath = relativeSteps + replace;
updatedContent = updatedContent.replaceAll(find, replacementPath);
});
expressions.forEach((expression) => {
updatedContent = processMatches(expression.exp, fileFolder, updatedContent, expression.update);
expression.exp.lastIndex = 0;
})
if (fileContent === updatedContent) {
//console.log(`No change ${file}`);
return;
}
writeFile(file, updatedContent).then(() => {
console.log(`Updated ${file}`);
});
});
};
const runSearchReplace = () => {
// Scan all folders
readFile(resolve(__dirname, ".gitignore")).then((result) => {
return result.toString().split("\n").filter((line) => {
return !line.startsWith("#");
}).filter((line) => {
return Boolean(line.trim());
});
}).then((gitIgnoreArr) => {
return getFiles(__dirname, gitIgnoreArr);
}).then((files) => {
// Now scan the files for matching regular expressions
// of imports without extensions
files.forEach((file) => {
processFile(file);
});
console.log(`Processed ${files.length} files`);
});
};
readTsConfig();
runSearchReplace();
const watcher = chokidar.watch(__dirname, { persistent: true });
watcher.on("change", (filename) => {
if (filename.indexOf("tsconfig.json") > -1) {
console.log("Updating tsconfig.json data");
readTsConfig();
}
if (!matchPattern.some((glob) => minimatch(filename, glob))) return;
//console.log("Changed", filename);
processFile(filename);
});