-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorrect-import-extensions.js
65 lines (56 loc) · 1.72 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
63
64
65
import FileHound from "filehound";
import fs from "fs";
import path from "path";
import { promisify } from "util";
const packages = await FileHound.create()
.paths(`packages`)
.directory()
.depth(1)
.find();
for (const packageName of packages) {
const filePaths = await FileHound.create()
.paths(`${packageName}/lib`)
.discard("node_modules")
.ext("js")
.find()
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");
}
)
);
}