Skip to content

Commit 695cdfc

Browse files
committed
☕ Add gen task to generate mod.ts and exports
1 parent 0089f06 commit 695cdfc

File tree

17 files changed

+183
-13
lines changed

17 files changed

+183
-13
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
run: deno fmt --check
5353
- name: Type check
5454
run: deno task check
55+
- name: Gen check
56+
run: |
57+
deno task gen
58+
git diff --exit-code
5559
5660
test:
5761
strategy:

.script/gen-exports.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {
2+
dirname,
3+
fromFileUrl,
4+
globToRegExp,
5+
relative,
6+
} from "jsr:@std/path@^1.0.8";
7+
import { walk } from "jsr:@std/fs@^1.0.5";
8+
import { parse } from "jsr:@std/jsonc@^1.0.1";
9+
10+
const excludes = [
11+
"*_test.ts",
12+
"*_bench.ts",
13+
"_*.ts",
14+
];
15+
16+
async function generateExports(
17+
path: string,
18+
): Promise<Record<string, string>> {
19+
const patterns = excludes.map((p) => globToRegExp(p));
20+
const root = fromFileUrl(new URL("../", import.meta.url));
21+
const it = walk(path, {
22+
includeFiles: true,
23+
includeDirs: false,
24+
});
25+
const exports: Record<string, string> = {};
26+
for await (const entry of it) {
27+
if (!entry.name.endsWith(".ts")) continue;
28+
if (patterns.some((p) => p.test(entry.name))) continue;
29+
const exportName = [".", relative(path, normalizeExportName(entry.path))]
30+
.filter((v) => !!v)
31+
.join("/");
32+
const exportPath = [".", relative(root, entry.path)]
33+
.filter((v) => !!v)
34+
.join("/");
35+
exports[exportName] = exportPath;
36+
}
37+
return Object.fromEntries(Object.entries(exports).toSorted());
38+
}
39+
40+
function normalizeExportName(name: string): string {
41+
if (name.endsWith("/mod.ts")) {
42+
name = dirname(name);
43+
}
44+
name = name.replace(/\.ts$/, "");
45+
name = name.replace(/_/g, "-");
46+
return name;
47+
}
48+
49+
if (import.meta.main) {
50+
const exports = await generateExports(
51+
fromFileUrl(new URL("../denops/@fall", import.meta.url)),
52+
);
53+
const denoJsoncPath = new URL("../deno.jsonc", import.meta.url);
54+
const denoJsonc = parse(await Deno.readTextFile(denoJsoncPath)) as Record<
55+
string,
56+
unknown
57+
>;
58+
await Deno.writeTextFile(
59+
denoJsoncPath,
60+
JSON.stringify(
61+
{
62+
...denoJsonc,
63+
exports,
64+
},
65+
undefined,
66+
2,
67+
),
68+
);
69+
}

.script/gen-mod.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
dirname,
3+
fromFileUrl,
4+
globToRegExp,
5+
join,
6+
relative,
7+
} from "jsr:@std/path@^1.0.8";
8+
import { exists, walk } from "jsr:@std/fs@^1.0.5";
9+
10+
const excludes = [
11+
"mod.ts",
12+
"*_test.ts",
13+
"*_bench.ts",
14+
"_*.ts",
15+
];
16+
17+
async function* iterModules(path: string): AsyncIterable<string> {
18+
const patterns = excludes.map((p) => globToRegExp(p));
19+
for await (const entry of Deno.readDir(path)) {
20+
if (entry.isFile) {
21+
if (!entry.name.endsWith(".ts")) continue;
22+
if (patterns.some((p) => p.test(entry.name))) continue;
23+
yield entry.name;
24+
} else if (entry.isDirectory) {
25+
const modPath = join(path, entry.name, "mod.ts");
26+
if (await exists(modPath)) {
27+
yield relative(path, modPath);
28+
}
29+
}
30+
}
31+
}
32+
33+
async function generateModTs(
34+
path: string,
35+
): Promise<void> {
36+
const it = walk(path, {
37+
includeFiles: false,
38+
includeDirs: true,
39+
includeSymlinks: false,
40+
});
41+
for await (const entry of it) {
42+
const filenames = await Array.fromAsync(iterModules(entry.path));
43+
if (filenames.length === 0) continue;
44+
filenames.sort();
45+
const lines = [
46+
"// This file is generated by gen-mod.ts",
47+
...filenames.map((name) => {
48+
if (name.endsWith("/mod.ts")) {
49+
return `export * as ${dirname(name)} from "./${name}";`;
50+
} else {
51+
return `export * from "./${name}";`;
52+
}
53+
}),
54+
];
55+
await Deno.writeTextFile(
56+
join(entry.path, "mod.ts"),
57+
lines.join("\n"),
58+
);
59+
}
60+
}
61+
62+
if (import.meta.main) {
63+
generateModTs(
64+
fromFileUrl(new URL("../denops/@fall/builtin", import.meta.url)),
65+
);
66+
}

deno.jsonc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
"./action": "./denops/@fall/action.ts",
77
"./builtin": "./denops/@fall/builtin/mod.ts",
88
"./builtin/action": "./denops/@fall/builtin/action/mod.ts",
9+
"./builtin/action/buffer": "./denops/@fall/builtin/action/buffer.ts",
10+
"./builtin/action/cd": "./denops/@fall/builtin/action/cd.ts",
911
"./builtin/action/cmd": "./denops/@fall/builtin/action/cmd.ts",
1012
"./builtin/action/echo": "./denops/@fall/builtin/action/echo.ts",
13+
"./builtin/action/help": "./denops/@fall/builtin/action/help.ts",
14+
"./builtin/action/noop": "./denops/@fall/builtin/action/noop.ts",
1115
"./builtin/action/open": "./denops/@fall/builtin/action/open.ts",
1216
"./builtin/action/quickfix": "./denops/@fall/builtin/action/quickfix.ts",
1317
"./builtin/action/submatch": "./denops/@fall/builtin/action/submatch.ts",
1418
"./builtin/action/systemopen": "./denops/@fall/builtin/action/systemopen.ts",
1519
"./builtin/action/yank": "./denops/@fall/builtin/action/yank.ts",
1620
"./builtin/curator": "./denops/@fall/builtin/curator/mod.ts",
21+
"./builtin/curator/noop": "./denops/@fall/builtin/curator/noop.ts",
1722
"./builtin/curator/rg": "./denops/@fall/builtin/curator/rg.ts",
23+
"./builtin/filter": "./denops/@fall/builtin/filter/mod.ts",
24+
"./builtin/filter/cwd": "./denops/@fall/builtin/filter/cwd.ts",
25+
"./builtin/filter/exists": "./denops/@fall/builtin/filter/exists.ts",
26+
"./builtin/filter/noop": "./denops/@fall/builtin/filter/noop.ts",
27+
"./builtin/filter/regexp": "./denops/@fall/builtin/filter/regexp.ts",
1828
"./builtin/layout": "./denops/@fall/builtin/layout/mod.ts",
1929
"./builtin/layout/compact": "./denops/@fall/builtin/layout/compact.ts",
2030
"./builtin/layout/modern": "./denops/@fall/builtin/layout/modern.ts",
@@ -24,13 +34,16 @@
2434
"./builtin/matcher/noop": "./denops/@fall/builtin/matcher/noop.ts",
2535
"./builtin/matcher/regexp": "./denops/@fall/builtin/matcher/regexp.ts",
2636
"./builtin/matcher/substring": "./denops/@fall/builtin/matcher/substring.ts",
37+
"./builtin/modifier": "./denops/@fall/builtin/modifier/mod.ts",
38+
"./builtin/modifier/noop": "./denops/@fall/builtin/modifier/noop.ts",
39+
"./builtin/modifier/relative-path": "./denops/@fall/builtin/modifier/relative_path.ts",
2740
"./builtin/previewer": "./denops/@fall/builtin/previewer/mod.ts",
2841
"./builtin/previewer/buffer": "./denops/@fall/builtin/previewer/buffer.ts",
2942
"./builtin/previewer/file": "./denops/@fall/builtin/previewer/file.ts",
3043
"./builtin/previewer/helptag": "./denops/@fall/builtin/previewer/helptag.ts",
3144
"./builtin/previewer/noop": "./denops/@fall/builtin/previewer/noop.ts",
3245
"./builtin/renderer": "./denops/@fall/builtin/renderer/mod.ts",
33-
"./builtin/renderer/helptg": "./denops/@fall/builtin/renderer/helptag.ts",
46+
"./builtin/renderer/helptag": "./denops/@fall/builtin/renderer/helptag.ts",
3447
"./builtin/renderer/noop": "./denops/@fall/builtin/renderer/noop.ts",
3548
"./builtin/renderer/smart-path": "./denops/@fall/builtin/renderer/smart_path.ts",
3649
"./builtin/sorter": "./denops/@fall/builtin/sorter/mod.ts",
@@ -61,6 +74,7 @@
6174
"./sorter": "./denops/@fall/sorter.ts",
6275
"./source": "./denops/@fall/source.ts",
6376
"./theme": "./denops/@fall/theme.ts",
77+
"./util/derivable": "./denops/@fall/util/derivable.ts",
6478
"./util/testutil": "./denops/@fall/util/testutil.ts"
6579
},
6680
"publish": {
@@ -78,6 +92,9 @@
7892
".coverage/**"
7993
],
8094
"tasks": {
95+
"gen:mod": "deno run -A ./.script/gen-mod.ts && deno fmt",
96+
"gen:exports": "deno run -A ./.script/gen-exports.ts",
97+
"gen": "deno task gen:mod && deno task gen:exports",
8198
"check": "deno check ./**/*.ts",
8299
"test": "deno test -A --parallel --shuffle --doc",
83100
"test:coverage": "deno task test --coverage=.coverage",
@@ -86,4 +103,4 @@
86103
"update:write": "deno task -q update --write",
87104
"update:commit": "deno task -q update --commit --prefix :package: --pre-commit=fmt,lint"
88105
}
89-
}
106+
}

denops/@fall/builtin/action/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This file is generated by gen-mod.ts
12
export * from "./buffer.ts";
23
export * from "./cd.ts";
34
export * from "./cmd.ts";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
// This file is generated by gen-mod.ts
12
export * from "./noop.ts";
23
export * from "./rg.ts";

denops/@fall/builtin/filter/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This file is generated by gen-mod.ts
12
export * from "./cwd.ts";
23
export * from "./exists.ts";
34
export * from "./noop.ts";

denops/@fall/builtin/layout/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This file is generated by gen-mod.ts
12
export * from "./compact.ts";
23
export * from "./modern.ts";
34
export * from "./separate.ts";

denops/@fall/builtin/matcher/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This file is generated by gen-mod.ts
12
export * from "./fzf.ts";
23
export * from "./noop.ts";
34
export * from "./regexp.ts";

denops/@fall/builtin/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This file is generated by gen-mod.ts
12
export * as action from "./action/mod.ts";
23
export * as curator from "./curator/mod.ts";
34
export * as filter from "./filter/mod.ts";

0 commit comments

Comments
 (0)