-
Notifications
You must be signed in to change notification settings - Fork 190
feat: basic command implementation #3
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9670cc7
chore: enable pure esm
branchseer d807ef1
chore: rename script name `dev` to `start` to avoid confusing `pnpm d…
branchseer e2ab7c0
feat: basic command implementation
branchseer d203e30
chore: require a Node.js version that supports `import.meta.dirname`
branchseer 69e228e
fix: exit with 255 for critical error
branchseer b780615
fix: use `process.execPath` as node path
branchseer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,67 @@ | ||
| import yargs from 'yargs/yargs' | ||
| import { hideBin } from 'yargs/helpers' | ||
| import { x } from 'tinyexec' | ||
|
|
||
| const args = hideBin(process.argv) | ||
| const commandArgs = args.slice(1) | ||
|
|
||
| const cli = yargs(args).scriptName('vite-plus') | ||
|
|
||
| cli.command(['$0', 'dev'], '', ({ argv }) => { | ||
| console.log('dev command', argv) | ||
| }) | ||
| cli.command('build', '') | ||
| cli.command('preview', '') | ||
| cli.command('lib', '') | ||
| cli.command('run', '') | ||
| cli.command('lint', '', () => { | ||
| return x('./node_modules/.bin/oxlint', commandArgs, { | ||
| nodeOptions: { stdio: 'inherit' }, | ||
| }) | ||
| }) | ||
|
|
||
| cli.command('fmt', '') | ||
| cli.command('test', '') | ||
| cli.command('bench', '') | ||
| cli.command('docs', '') | ||
| cli.command('publish', '') | ||
| cli.command('ui', '') | ||
|
|
||
| cli.help().parse() | ||
| import yargs from "yargs/yargs"; | ||
| import { hideBin } from "yargs/helpers"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import path from "node:path"; | ||
|
|
||
| function execPackageBin(binName: string, args: string[]) { | ||
| // TODO: exec from bundled packages | ||
| const program = path.join( | ||
| import.meta.dirname, | ||
| "../node_modules/.bin", | ||
| binName, | ||
| ); | ||
| exec(program, args); | ||
| } | ||
|
|
||
| function exec(program: string, args: string[]) { | ||
| const { status, error } = spawnSync(program, args, { stdio: "inherit" }); | ||
| if (error !== undefined) { | ||
| throw error; | ||
| } | ||
| process.exit(status ?? 255); | ||
| } | ||
|
|
||
| const args = hideBin(process.argv); | ||
| const commandArgs = args.slice(1); | ||
|
|
||
| const cli = yargs(args).scriptName("vite"); | ||
|
|
||
| for (const viteCommand of ["build", "optimize", "preview", "dev"]) { | ||
| // register vite command one by one instead of cli.command(['build', 'optimize', 'preview', 'dev'], ..) | ||
| // so that the help message won't list them as aliases (vite build [aliases: optimize, preview, dev]) | ||
| cli.command(viteCommand, "", () => { | ||
| execPackageBin("vite", args); | ||
| }); | ||
| } | ||
| cli.command("lib", "", () => { | ||
| execPackageBin("tsdown", commandArgs); | ||
| }); | ||
|
|
||
| cli.command("run", "", () => { | ||
| exec(process.execPath, [ | ||
| "--import", | ||
| import.meta.resolve("@oxc-node/core/register"), | ||
| ...commandArgs, | ||
| ]); | ||
| }); | ||
|
|
||
| cli.command("lint", "", () => { | ||
| execPackageBin("oxlint", commandArgs); | ||
| }); | ||
|
|
||
| cli.command("test", "", () => { | ||
| execPackageBin("vitest", commandArgs); | ||
| }); | ||
| cli.command("bench", "", () => { | ||
| execPackageBin("vitest", ["bench", ...commandArgs]); | ||
| }); | ||
|
|
||
| cli.command("docs", "", () => { | ||
| execPackageBin("vitepress", commandArgs); | ||
| }); | ||
|
|
||
| // cli.command('fmt', '') | ||
| // cli.command('publish', '') | ||
| // cli.command('ui', '') | ||
|
|
||
| cli.help().parse(); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.