This script generates a scripts.d.ts file that annotates the run command with the available script names, using declaration merging.

https://github.com/johnlindquist/kit/assets/7313176/48344f2b-2eee-4383-9de8-39c023d0470e

Open generate-scripts-declarations in Script Kit

// Name: Generate scripts.d.ts
// Description: Enables autocompletion for the `run` command
import "@johnlindquist/kit"
import { writeFile } from "node:fs/promises"
const scripts = await getScripts()
const availableScripts = scripts.map((x) => ` | "${x.command}"`).join("\n")
const body = `// Do not edit. Autogenerated by generate-scripts-declarations.ts
import type { Run } from "../../.kit/types/kit"
type AvailableScript =
${availableScripts};
declare module "@johnlindquist/kit/types/kit" {
export interface Run {
// biome-ignore lint/style/useShorthandFunctionType: <explanation>
(command?: AvailableScript, ...args: string[]): Promise<any>
}
}
declare global {
var run: Run
}`
await ensureDir(kenvPath("@types"))
await writeFile(kenvPath("@types", "scripts.d.ts"), body, { encoding: "utf-8" })