-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodemod.ts
57 lines (51 loc) · 1.54 KB
/
codemod.ts
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
// Fragile find and replace of imports
import {
walk,
readFileStr,
writeFileStr,
} from "https://deno.land/std/fs/mod.ts";
const importRegex = / from "([^"]*)"/;
// Async
const jsonSet = new Set<string>();
const fixFiles = async () => {
for await (const entry of walk(".")) {
if (!entry.path.startsWith(".")) {
if (entry.path.includes("/")) {
if (entry.path.endsWith(".ts")) {
let file = await readFileStr(entry.path);
while (importRegex.test(file)) {
const match = (file.match(importRegex) as RegExpMatchArray);
const index = match.index as number;
const path = match[1];
if (path.endsWith(".json")) {
jsonSet.add(
`${entry.path} ~> ${path}`,
);
file = file.replace(
file.slice(index, index + path.length + 8),
` from '${path}'`,
);
} else {
file = file.replace(
file.slice(index, index + path.length + 8),
` from '${
path.startsWith(".") ? `${path}.ts` : (
path.includes("/") ? `../${path}.ts` : `../${path}/index.ts`
)
}'`,
);
}
}
await writeFileStr(entry.path, file);
}
}
}
}
};
await fixFiles();
if (jsonSet.size > 0) {
console.log("There are some JSON files to manually fix to work with Deno");
console.log([...jsonSet].join("\n"));
} else {
console.log("Done.");
}