Skip to content

Commit 341281b

Browse files
committed
feat: github plugin specifier
1 parent a3dcabb commit 341281b

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,21 @@ entype \
349349
fixtures/datapack/blockstates/*.json
350350
```
351351

352+
Alternatively, the `github:` qualifier can be used:
353+
354+
```sh
355+
entype \
356+
--allow-unstable \
357+
--lang rust \
358+
--plugin "github:bcheidemann/entype/lib/plugins/derive-debug.ts" \
359+
fixtures/datapack/blockstates/*.json
360+
```
361+
362+
The format is `github:<branch>@<owner>/<repo>/<path>`, where `branch` defaults
363+
to `main` and `path` defaults to `mod.ts`. The plugin speicifier
364+
`github:bcheidemann/entype-plugin-example` would be resolved to
365+
`https://raw.githubusercontent.com/bcheidemann/entype-plugin-example/main/mod.ts`.
366+
352367
## How it works
353368

354369
Entype tries to generate the simplest possible type that accurately describes

lib/plugins/mod.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
import { Plugin, PluginName } from "./types.ts";
22

3+
export const GITHUB_PLUGIN_PREFIX = "github:";
4+
35
export async function loadPlugin(pluginName: PluginName): Promise<Plugin> {
46
switch (pluginName) {
57
case "derive-debug":
68
return (await import("./derive-debug.ts")).plugin;
79
case "serde-derive":
810
return (await import("./serde-derive.ts")).plugin;
911
default:
10-
return (await import(pluginName)).plugin;
12+
return await loadThirdPartyPlugin(pluginName);
1113
}
1214
}
15+
16+
export async function loadThirdPartyPlugin(
17+
pluginName: string,
18+
): Promise<Plugin> {
19+
if (pluginName.startsWith(GITHUB_PLUGIN_PREFIX)) {
20+
return await loadGithubPlugin(pluginName);
21+
}
22+
23+
return (await import(pluginName)).plugin;
24+
}
25+
26+
export async function loadGithubPlugin(
27+
pluginName: string,
28+
): Promise<Plugin> {
29+
const moduleSpecifier = pluginName.slice(GITHUB_PLUGIN_PREFIX.length);
30+
const [branch, rest] = moduleSpecifier.includes("@")
31+
? moduleSpecifier.split("@")
32+
: [null, moduleSpecifier];
33+
const [owner, repo, ...path] = rest.split("/");
34+
35+
const { plugin } = await import(
36+
`https://raw.githubusercontent.com/${owner}/${repo}/${branch || "main"}/${
37+
path ? path.join("/") : "mod.ts"
38+
}`
39+
);
40+
41+
return plugin;
42+
}

main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function main(args: string[]): Promise<0 | 1> {
2727
}
2828

2929
if ("version" in config) {
30-
console.log("1.1.0");
30+
console.log("1.2.0");
3131
return 0;
3232
}
3333

0 commit comments

Comments
 (0)