/
mod.ts
132 lines (109 loc) · 3.22 KB
/
mod.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
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
import { parse } from "https://deno.land/std@0.141.0/flags/mod.ts";
import { walk } from "https://deno.land/std@0.141.0/fs/mod.ts";
import * as recast from "https://jspm.dev/npm:recast";
import parser from "https://jspm.dev/npm:recast/parsers/typescript";
import ImportMap from "./ImportMap.ts";
if (import.meta.main) {
const addOrRemove = getAddOrRemove();
const pattern = getFilePattern();
const importMap = await tryFindImportMap();
console.log("Processing...");
let errors = 0;
let unmodified = 0;
let ok = 0;
for await (const { path } of walk(".")) {
if (!path.match(pattern)) continue;
try {
const code = await Deno.readTextFile(path);
const result = (await (addOrRemove === "add"))
? applyImportMap(path, code, importMap)
: removeImportMap(path, code, importMap);
if (result.modified) {
await Deno.writeTextFile(path, result.code);
ok++;
} else unmodified++;
} catch (e) {
console.log(e);
errors++;
}
}
console.log("Done.");
console.log(`Results: ${errors} errors ${unmodified} unmodified ${ok} ok`);
}
function getAddOrRemove(): "add" | "remove" {
const {
_: [addOrRemove],
} = parse(Deno.args);
if (!addOrRemove || (addOrRemove !== "add" && addOrRemove !== "remove")) {
throw "Select one of two options: 'add' or 'remove'";
}
return addOrRemove;
}
function getFilePattern(): RegExp {
const {
_: [, pattern],
} = parse(Deno.args);
return pattern ? new RegExp(String(pattern)) : /\.[tj]sx?$/;
}
async function tryFindImportMap(): Promise<ImportMap> {
// --import-map argument
const { "import-map": path } = parse(Deno.args);
if (path) {
return new ImportMap(await Deno.readTextFile(path));
}
// deno.jsonc
try {
const config = JSON.parse(await Deno.readTextFile("./deno.jsonc"));
return new ImportMap(await Deno.readTextFile(config.importMap));
} catch {
// not found
}
// deno.json
try {
const config = JSON.parse(await Deno.readTextFile("./deno.json"));
return new ImportMap(await Deno.readTextFile(config.importMap));
} catch {
// not found
}
throw new Error("Could not find import map");
}
export function removeImportMap(
referrer: string,
code: string,
importMap: ImportMap
) {
const ast = recast.parse(code, { parser });
let modified = false;
recast.visit(ast, {
visitImportDeclaration(path: any) {
const source = path.value.source.value;
const url = importMap.resolve(source, referrer);
if (url !== source) {
path.value.source.value = url;
modified = true;
}
this.traverse(path);
},
});
return { modified, code: modified ? recast.print(ast).code : code };
}
export function applyImportMap(
referrer: string,
code: string,
importMap: ImportMap
) {
const ast = recast.parse(code, { parser });
let modified = false;
recast.visit(ast, {
visitImportDeclaration(path: any) {
const source = path.value.source.value;
const url = importMap.apply(source, referrer);
if (url !== source) {
path.value.source.value = url;
modified = true;
}
this.traverse(path);
},
});
return { modified, code: modified ? recast.print(ast).code : code };
}