Skip to content

Instantly share code, notes, and snippets.

@JosXa
Created May 31, 2024 16:13
Show Gist options
  • Save JosXa/41e8883e8d7ae7cbb195cbfff19a4dd1 to your computer and use it in GitHub Desktop.
Save JosXa/41e8883e8d7ae7cbb195cbfff19a4dd1 to your computer and use it in GitHub Desktop.
// Name: Generate scripts.d.ts
// Description: Enables autocompletion for the `run` command
import "@johnlindquist/kit"
import { writeFile } from "node:fs/promises"
// Waaaatch: ~/.kit/db/scripts.json
if (args.length < 2) {
exit()
}
const [_, event] = args
if (event !== "change") {
exit()
}
const scripts = await getScripts()
const availableScripts = scripts
.map((x) => {
const parts = [' | "']
if (!["", "main"].includes(x.kenv)) {
parts.push(`${x.kenv}/`)
}
parts.push(x.command)
parts.push('"')
return parts.join("")
})
.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",
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment