Skip to content

Commit

Permalink
feat: github plugin specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bcheidemann committed Aug 20, 2023
1 parent a3dcabb commit 341281b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ entype \
fixtures/datapack/blockstates/*.json
```

Alternatively, the `github:` qualifier can be used:

```sh
entype \
--allow-unstable \
--lang rust \
--plugin "github:bcheidemann/entype/lib/plugins/derive-debug.ts" \
fixtures/datapack/blockstates/*.json
```

The format is `github:<branch>@<owner>/<repo>/<path>`, where `branch` defaults
to `main` and `path` defaults to `mod.ts`. The plugin speicifier
`github:bcheidemann/entype-plugin-example` would be resolved to
`https://raw.githubusercontent.com/bcheidemann/entype-plugin-example/main/mod.ts`.

## How it works

Entype tries to generate the simplest possible type that accurately describes
Expand Down
32 changes: 31 additions & 1 deletion lib/plugins/mod.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import { Plugin, PluginName } from "./types.ts";

export const GITHUB_PLUGIN_PREFIX = "github:";

export async function loadPlugin(pluginName: PluginName): Promise<Plugin> {
switch (pluginName) {
case "derive-debug":
return (await import("./derive-debug.ts")).plugin;
case "serde-derive":
return (await import("./serde-derive.ts")).plugin;
default:
return (await import(pluginName)).plugin;
return await loadThirdPartyPlugin(pluginName);
}
}

export async function loadThirdPartyPlugin(
pluginName: string,
): Promise<Plugin> {
if (pluginName.startsWith(GITHUB_PLUGIN_PREFIX)) {
return await loadGithubPlugin(pluginName);
}

return (await import(pluginName)).plugin;
}

export async function loadGithubPlugin(
pluginName: string,
): Promise<Plugin> {
const moduleSpecifier = pluginName.slice(GITHUB_PLUGIN_PREFIX.length);
const [branch, rest] = moduleSpecifier.includes("@")
? moduleSpecifier.split("@")
: [null, moduleSpecifier];
const [owner, repo, ...path] = rest.split("/");

const { plugin } = await import(
`https://raw.githubusercontent.com/${owner}/${repo}/${branch || "main"}/${
path ? path.join("/") : "mod.ts"
}`
);

return plugin;
}
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function main(args: string[]): Promise<0 | 1> {
}

if ("version" in config) {
console.log("1.1.0");
console.log("1.2.0");
return 0;
}

Expand Down

0 comments on commit 341281b

Please sign in to comment.