-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement command generators, a way for plugins to provide entrypoints
- Loading branch information
Showing
19 changed files
with
558 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
interface GeneratedCommand { // TODO Add this type to api | ||
id: string | ||
name: string | ||
fn: () => void | ||
} | ||
|
||
export default function CommandGenerator(): GeneratedCommand[] { | ||
return [ | ||
{ | ||
id: 'generated-test-1', | ||
name: 'Generated Item 1', | ||
fn: () => { | ||
console.log('generated-test-1') | ||
} | ||
}, | ||
{ | ||
id: 'generated-test-2', | ||
name: 'Generated Item 2', | ||
fn: () => { | ||
console.log('generated-test-2') | ||
} | ||
}, | ||
{ | ||
id: 'generated-test-3', | ||
name: 'Generated Item 3', | ||
fn: () => { | ||
console.log('generated-test-3') | ||
} | ||
}, | ||
{ | ||
id: 'generated-test-4', | ||
name: 'Generated Item 4', | ||
fn: () => { | ||
console.log('generated-test-4') | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-expect-error does typescript support such symbol declarations? | ||
const denoCore: DenoCore = Deno[Deno.internal].core; | ||
const InternalApi = denoCore.ops; | ||
|
||
interface GeneratedCommand { // TODO Add this type to api | ||
id: string | ||
name: string | ||
fn: () => void | ||
} | ||
|
||
let storedGeneratedCommands: (GeneratedCommand & { lookupId: string })[] = [] | ||
|
||
export async function runCommandGenerators(): Promise<void> { | ||
storedGeneratedCommands = [] | ||
|
||
const entrypointIds = await InternalApi.get_command_generator_entrypoint_ids(); | ||
for (const entrypointId of entrypointIds) { | ||
try { | ||
const generator: () => GeneratedCommand[] = (await import(`gauntlet:entrypoint?${entrypointId}`)).default; | ||
|
||
const generatedCommands = generator() | ||
.map(value => ({ | ||
lookupId: entrypointId + ":" + value.id, | ||
...value | ||
})); | ||
|
||
storedGeneratedCommands.push(...generatedCommands) | ||
} catch (e) { | ||
console.error("Error occurred when calling command generator for entrypoint: " + entrypointId, e) | ||
} | ||
} | ||
} | ||
|
||
export function generatedCommandSearchIndex(): AdditionalSearchItem[] { | ||
return storedGeneratedCommands.map(value => ({ | ||
entrypoint_id: value.lookupId, | ||
entrypoint_name: value.name, | ||
})) | ||
} | ||
|
||
export function runGeneratedCommand(entrypointId: string) { | ||
const generatedCommand = storedGeneratedCommands.find(value => value.lookupId === entrypointId); | ||
|
||
if (generatedCommand) { | ||
generatedCommand.fn() | ||
} else { | ||
throw new Error("Generated command with entrypoint id '" + entrypointId + "' not found") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { generatedCommandSearchIndex } from "./command-generator"; | ||
|
||
// @ts-expect-error does typescript support such symbol declarations? | ||
const denoCore: DenoCore = Deno[Deno.internal].core; | ||
const InternalApi = denoCore.ops; | ||
|
||
export async function loadSearchIndex() { | ||
await InternalApi.load_search_index(generatedCommandSearchIndex()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.