Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
@@ -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(),
})
},
})
2 changes: 2 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -14,5 +15,6 @@ export const cli = subcommands({
api: apiCommand,
gql: gqlCommand,
auth: authCommand,
execute: executeCommand,
},
})
12 changes: 11 additions & 1 deletion src/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()