From 0ed7e0e75ae8cc3d5e76256b08cc519fa283eb4d Mon Sep 17 00:00:00 2001 From: bgenia Date: Wed, 25 Oct 2023 23:59:25 +0300 Subject: [PATCH] feat: implement user script feature demo --- src/cli/commands/execute/index.ts | 48 +++++++++++++++++++++++++++++++ src/cli/index.ts | 2 ++ src/configuration/index.ts | 12 +++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/cli/commands/execute/index.ts diff --git a/src/cli/commands/execute/index.ts b/src/cli/commands/execute/index.ts new file mode 100644 index 0000000..de88f57 --- /dev/null +++ b/src/cli/commands/execute/index.ts @@ -0,0 +1,48 @@ +import { resolve } from "path" +import { command, positional } from "cmd-ts" +import { Configuration } from "@/configuration" +import { getDefaultClient } from "@/tools/getDefaultClient" + +export const executeCommand = command({ + name: "execute", + description: "Executes user script", + aliases: ["exec", "x"], + args: { + name: positional({ + displayName: "name", + }), + }, + async handler(argv) { + const scripts = Configuration.optional.scripts ?? [] + + const script = scripts.filter((script) => script.name === argv.name)[0] + + if (!script) { + throw new Error(`Unable to find script "${argv.name}"`) + } + + const scriptModule = (await import( + resolve(process.cwd(), script.path) + )) as unknown + + if ( + !scriptModule || + typeof scriptModule !== "object" || + !("main" in scriptModule) + ) { + return + } + + if (typeof scriptModule.main !== "function") { + throw new TypeError( + `Invariant violation: ["${script.name}"].main must be a function`, + ) + } + + await scriptModule.main({ + meta: script, + configuration: Configuration, + client: getDefaultClient(), + }) + }, +}) diff --git a/src/cli/index.ts b/src/cli/index.ts index eee6625..686040b 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1,6 +1,7 @@ import { subcommands } from "cmd-ts" import { apiCommand } from "./commands/api" import { authCommand } from "./commands/auth/index" +import { executeCommand } from "./commands/execute" import { gqlCommand } from "./commands/gql" import { peerReviewCommand } from "./commands/peer-review" import { testCommand } from "./commands/test" @@ -14,5 +15,6 @@ export const cli = subcommands({ api: apiCommand, gql: gqlCommand, auth: authCommand, + execute: executeCommand, }, }) diff --git a/src/configuration/index.ts b/src/configuration/index.ts index 7bf111f..03f5260 100644 --- a/src/configuration/index.ts +++ b/src/configuration/index.ts @@ -1,5 +1,6 @@ -import { type } from "arktype" +import { arrayOf, type } from "arktype" import { ConfigurationManager } from "./ConfigurationManager" +import { ConfigurationSource } from "./ConfigurationSchema" export const Configuration = new ConfigurationManager({ // Authentication @@ -17,6 +18,15 @@ export const Configuration = new ConfigurationManager({ // pr/clone prDirectory: type("string"), + + // Scripts + scripts: [ + arrayOf({ + name: "string", + path: "string", + }), + ConfigurationSource.File, + ], }) await Configuration.load()