-
Notifications
You must be signed in to change notification settings - Fork 0
/
correct-import-extensions.js
63 lines (55 loc) · 1.83 KB
/
correct-import-extensions.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
const FileHound = require('filehound');
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
FileHound.create()
.paths("./dist")
.discard("node_modules")
.ext("js")
.find()
.then(
async filePaths => {
await Promise.all(
filePaths.map(
async filePath => {
let contents = await promisify(fs.readFile)(
filePath,
"utf-8"
);
const statements = contents.match(/(import|export) .+ from ".+";/g);
if (!statements) {
return;
}
await Promise.all(
statements.map(
async statement => {
const url = statement.match(/"(.+)";/)[1];
if (url.indexOf(".") !== 0) {
return;
}
const [stat, indexStat] = await Promise.all([
promisify(fs.stat)(path.resolve(path.dirname(filePath), url + ".js")).catch(() => {}),
promisify(fs.stat)(path.resolve(path.dirname(filePath), url + "/index.js")).catch(() => {})
]);
if (stat && stat.isFile()) {
contents = contents.replace(
statement,
statement.replace(url, url + ".js")
);
} else if (indexStat && indexStat.isFile()) {
contents = contents.replace(
statement,
statement.replace(url, url + "/index.js")
);
}
}
)
);
await promisify(fs.writeFile)(filePath, contents, "utf-8");
}
)
)
}
)
.then(() => console.log("Complete"))
.catch(error => console.error(error));