-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implemented initial functionality #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
128e2dc
Merge Path/File
roll ba0772d
Renamed file to module
roll 9c8609b
Improved parseFunction
roll 7576684
Fixed arguments
roll 408a708
Use terminal folder
roll f9fea65
Smart object handling
roll 45f65fd
Optional args handling
roll f558c6c
Handle variadic arguments
roll a153da3
Handle repeatable options
roll 576228f
Limit recursion depth
roll cf49a05
Support json input
roll 769c177
Support nested jsdoc
roll 1721b9f
Moved fixtures to top level
roll 782a63a
Enable index
roll 1127ebf
Bootstrap config
roll ff1f019
Added config support
roll 0eb4a29
Added plugin support
roll a92157e
Move program to actions
roll 12e17e4
Export createProgram
roll 7b4aac6
Improved readability
roll 8b3e84c
Added more tests
roll 7317e11
Fixed tests
roll f099010
Fixed windows tests
roll 326206e
Pin tempy version
roll eee050d
Fixed windows test
roll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,4 +1,4 @@ | ||
| dangerouslyAllowAllBuilds: true | ||
| packages: | ||
| - uptask | ||
| - terminal | ||
| - website |
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import { Command } from "commander" | ||
| import { helpConfiguration } from "../../helpers/program.ts" | ||
| import type { Function } from "../../models/function.ts" | ||
| import type { Parameter } from "../../models/parameter.ts" | ||
|
|
||
| /** | ||
| * Create a Commander command from a Function, wiring options to dynamic import and execution. | ||
| */ | ||
| export function createCommand(func: Function): Command { | ||
| const cmd = new Command(func.name).configureHelp(helpConfiguration) | ||
| if (func.description) cmd.description(func.description) | ||
|
|
||
| const argumentParams: typeof func.parameters = [] | ||
| let hasVariadic = false | ||
|
|
||
| for (const [i, param] of func.parameters.entries()) { | ||
| if (i === 0 && (param.type === "string[]" || param.type === "number[]")) { | ||
| hasVariadic = true | ||
| argumentParams.push(param) | ||
| const bracket = param.required | ||
| ? `<${param.name}...>` | ||
| : `[${param.name}...]` | ||
| cmd.argument(bracket, param.description || "") | ||
| } else if ( | ||
| !hasVariadic && | ||
| (param.type === "string" || param.type === "number") && | ||
| param.required | ||
| ) { | ||
| argumentParams.push(param) | ||
| cmd.argument(`<${param.name}>`, param.description || "") | ||
| } else if (param.type === "object" && param.properties?.length) { | ||
| registerObjectOptions(cmd, param.properties) | ||
| } else { | ||
| registerOption(cmd, param) | ||
| } | ||
| } | ||
|
|
||
| cmd.action(async (...actionArgs: unknown[]) => { | ||
| const options = actionArgs.at(-2) as Record<string, unknown> | ||
| const positionalValues = actionArgs.slice(0, argumentParams.length) | ||
| const args = func.parameters.map(param => { | ||
| const argIndex = argumentParams.indexOf(param) | ||
| if (argIndex !== -1) { | ||
| const val = positionalValues[argIndex] | ||
| if (param.type === "number") return Number(val) | ||
| if (param.type === "number[]") | ||
| return (val as unknown[]).map(v => Number(v)) | ||
| return val | ||
| } | ||
| if (param.type === "object" && param.properties?.length) { | ||
| return buildObject(param.properties, options) | ||
| } | ||
| return options[param.name] ?? param.default | ||
| }) | ||
| const mod = (await import(func.path)) as Record< | ||
| string, | ||
| (...args: unknown[]) => unknown | ||
| > | ||
| const fn = mod[func.name] | ||
| if (!fn) throw new Error(`Function ${func.name} not found in ${func.path}`) | ||
| await fn(...args) | ||
| }) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| function buildObject( | ||
| properties: Parameter[], | ||
| options: Record<string, unknown>, | ||
| ): Record<string, unknown> { | ||
| const obj: Record<string, unknown> = {} | ||
| for (const prop of properties) { | ||
| if (prop.type === "object" && prop.properties?.length) { | ||
| obj[prop.name] = buildObject(prop.properties, options) | ||
| } else { | ||
| obj[prop.name] = options[prop.name] ?? prop.default | ||
| } | ||
| } | ||
| return obj | ||
| } | ||
|
|
||
| function registerObjectOptions(cmd: Command, properties: Parameter[]) { | ||
| for (const prop of properties) { | ||
| if (prop.type === "object" && prop.properties?.length) { | ||
| registerObjectOptions(cmd, prop.properties) | ||
| } else { | ||
| registerOption(cmd, prop) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function registerOption(cmd: Command, param: Parameter) { | ||
| const flag = camelToKebab(param.name) | ||
| const description = param.description || "" | ||
|
|
||
| if (param.type === "string") { | ||
| if (param.required) { | ||
| cmd.requiredOption(`--${flag} <value>`, description) | ||
| } else { | ||
| cmd.option(`--${flag} <value>`, description, param.default as string) | ||
| } | ||
| } else if (param.type === "number") { | ||
| if (param.required) { | ||
| cmd.requiredOption(`--${flag} <value>`, description, Number) | ||
| } else { | ||
| cmd.option( | ||
| `--${flag} <value>`, | ||
| description, | ||
| Number, | ||
| param.default as number, | ||
| ) | ||
| } | ||
| } else if (param.type === "boolean") { | ||
| cmd.option(`--${flag}`, description, param.default as boolean | undefined) | ||
| } else if (param.type === "string[]") { | ||
| if (param.required) { | ||
| cmd.requiredOption(`--${flag} <value...>`, description) | ||
| } else { | ||
| const defaultVal = (param.default ?? []) as string[] | ||
| cmd.option(`--${flag} <value...>`, description, defaultVal) | ||
| } | ||
| } else if (param.type === "number[]") { | ||
| const coerce = (v: string, prev: number[] | undefined) => { | ||
| const list = prev ?? [] | ||
| list.push(Number(v)) | ||
| return list | ||
| } | ||
| if (param.required) { | ||
| cmd.requiredOption(`--${flag} <value...>`, description, coerce) | ||
| } else { | ||
| const defaultVal = (param.default ?? []) as number[] | ||
| cmd.option(`--${flag} <value...>`, description, coerce, defaultVal) | ||
| } | ||
| } else if (param.type === "object") { | ||
| const parse = (v: string) => JSON.parse(v) as unknown | ||
| if (param.required) { | ||
| cmd.requiredOption(`--${flag} <json>`, description, parse) | ||
| } else { | ||
| cmd.option(`--${flag} <json>`, description, parse, param.default) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function camelToKebab(str: string): string { | ||
| return str.replace(/[A-Z]/g, m => `-${m.toLowerCase()}`) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Required non-positional params can pass through as
undefined.Line 55 returns option/default directly without enforcing
param.required, so required option-backed params can silently becomeundefinedat invocation time.🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents