-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve type inference on subcommands
- Loading branch information
Showing
16 changed files
with
348 additions
and
297 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
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
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 |
---|---|---|
@@ -1,3 +1,35 @@ | ||
export { createMessage } from "./message"; | ||
import { | ||
Context, | ||
CreateInstruction, | ||
InstructionParameters, | ||
ObjectLikeConstraint, | ||
} from "../types"; | ||
import { format, print } from "./helpers"; | ||
|
||
export type { MessageParameters } from "./message"; | ||
export const createMessage: CreateInstruction< | ||
MessageParameters<ObjectLikeConstraint> | ||
> = (parameters) => { | ||
const { handler } = parameters; | ||
|
||
return async function execute(context) { | ||
handler(context, HELPERS); | ||
|
||
return null; | ||
}; | ||
}; | ||
|
||
const HELPERS = { | ||
format, | ||
print, | ||
}; | ||
|
||
export type MessageParameters<Values extends ObjectLikeConstraint> = | ||
InstructionParameters< | ||
Values, | ||
{ | ||
handler: ( | ||
context: Context<Values>, | ||
helpers: typeof HELPERS | ||
) => void; | ||
} | ||
>; |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,65 @@ | ||
export { createTask } from "./task"; | ||
import { Listr } from "listr2"; | ||
import { | ||
Context, | ||
CreateInstruction, | ||
InstructionKey, | ||
InstructionParameters, | ||
Label, | ||
ObjectLikeConstraint, | ||
} from "../types"; | ||
import { exec } from "../../core/process"; | ||
|
||
export type { TaskParameters } from "./task"; | ||
export const createTask: CreateInstruction< | ||
TaskParameters<ObjectLikeConstraint, keyof ObjectLikeConstraint> | ||
> = (parameters) => { | ||
const { key, label, handler } = parameters; | ||
const receiver = new Listr([], { | ||
rendererOptions: { | ||
collapseErrors: false, | ||
formatOutput: "wrap", | ||
showErrorMessage: true, | ||
showTimer: true, | ||
}, | ||
}); | ||
|
||
return async function execute(context) { | ||
let value: unknown; | ||
|
||
receiver.add({ | ||
title: typeof label === "function" ? label(context) : label, | ||
task: async () => (value = await handler(context, HELPERS)), | ||
}); | ||
|
||
await receiver.run(); | ||
|
||
return { key, value }; | ||
}; | ||
}; | ||
|
||
const HELPERS = { | ||
exec, | ||
}; | ||
|
||
export type TaskParameters< | ||
Values extends ObjectLikeConstraint, | ||
Key | ||
> = InstructionParameters<Values, Parameters<Values, Key>>; | ||
|
||
type Parameters< | ||
Values extends ObjectLikeConstraint, | ||
Key | ||
> = Key extends keyof Values | ||
? Partial<InstructionKey<Key>> & { | ||
label: Label<Values>; | ||
handler: ( | ||
context: Context<Values>, | ||
helpers: typeof HELPERS | ||
) => Promise<Values[Key]>; | ||
} | ||
: { | ||
label: Label<Values>; | ||
handler: ( | ||
context: Context<Values>, | ||
helpers: typeof HELPERS | ||
) => void; | ||
}; |
Oops, something went wrong.