-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_imports.ts
59 lines (56 loc) · 1.38 KB
/
sort_imports.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
import { licenseHeader } from "./_shared.ts";
const directories = [
"client",
"connection",
"storage",
"tl",
"transport",
"types",
"utilities",
];
for (const dir of directories) {
for (const entry of Deno.readDirSync(dir)) {
if (!entry.isFile) {
continue;
}
if (!entry.name.endsWith(".ts")) {
continue;
}
let x = false;
let c = Deno.readTextFileSync(dir + "/" + entry.name);
if (c.startsWith(licenseHeader)) {
x = true;
c = c.slice(licenseHeader.length).trim();
}
let lines = c.split("\n");
if (!lines[0].startsWith("import ")) {
continue;
}
let imports = new Array<string>();
for (const line of lines) {
if (line.startsWith("import ")) {
imports.push(line);
}
}
lines = lines.slice(imports.length);
imports = imports
.sort((a, b) =>
a.replace(/^import.+from/, "").localeCompare(
b.replace(/^import.+from/, ""),
)
)
.sort((a, b) => {
const bMatch = b.match(/\./g);
const aMatch = a.match(/\./g);
if (!bMatch || !aMatch) {
return -1;
}
return b.match(/\./g)!.length - a.match(/\./g)!.length;
});
Deno.writeTextFileSync(
dir + "/" + entry.name,
(x ? licenseHeader + "\n\n" : "") +
(imports.join("\n") + "\n" + lines.join("\n")).trim() + "\n",
);
}
}