-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-import-path.js
54 lines (40 loc) · 1.37 KB
/
fix-import-path.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
const fs = require("fs");
const path = require("path");
// Grabbing launch arguments
if(process.argv.length !== 5) {
console.error('!> Invalid syntax !');
console.error('Use: node fix-import-path.js <input_file> <import_name> <replacement_file>');
process.exit(1);
}
const inputFile = process.argv[2];
const inputImportName = process.argv[3];
const inputReplacement = process.argv[4];
let filesToProcess = inputFile.split(";");
// Fixing the files
for(const fileToProcess of filesToProcess) {
console.log("> Replacing '"+inputImportName+"' with '"+inputReplacement+"' in '"+fileToProcess+"' ...");
const inputFileLines = fs.readFileSync(fileToProcess).toString().split("\n");
if(inputFileLines == null) {
console.error('!> Failed to read lines !');
process.exit(2);
}
const outputFileLines = [];
for(let inputLine of inputFileLines) {
if(inputLine.startsWith("import") && inputLine.includes("from")) {
inputLine = inputLine.split(/['"]+/);
// inputLine is now an array !
//console.log(inputLine);
inputLine[inputLine.length - 2] = inputLine[inputLine.length - 2].replace(
inputImportName, inputReplacement
);
inputLine = inputLine.join("\"");
}
outputFileLines.push(inputLine);
}
try {
fs.unlinkSync(fileToProcess);
fs.writeFileSync(fileToProcess, outputFileLines.join("\n"), "utf8");
} catch(err) {
console.error(err);
}
}