-
Notifications
You must be signed in to change notification settings - Fork 30
CLI: refactor cli from commander to yargs #1232
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
11 commits
Select commit
Hold shift + click to select a range
de4e879
refactor cli from commander to yargs
bcotrim 5b2613f
fix unit tests
bcotrim cb1c419
add locale to yargs
bcotrim d769c02
add comment in to cli webpack build
bcotrim c98407a
address pr comments
bcotrim 92845a2
fix command execution
bcotrim 4fda597
update command init and create name
bcotrim f65b077
fix uni test
bcotrim 6cac39c
add translation to preview command
bcotrim 6611443
rename go to preview create and fix argv consistency
bcotrim d07cd9b
add helper for preview
bcotrim 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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -5,9 +5,12 @@ import { getSnapshotCliTable } from 'cli/lib/output'; | |||||
import { getSnapshotsFromAppdata } from 'cli/lib/snapshots'; | ||||||
import { validateSiteFolder } from 'cli/lib/validation'; | ||||||
import { Logger, LoggerError } from 'cli/logger'; | ||||||
import { RegisterCommand, OutputFormat } from 'cli/types'; | ||||||
import { OutputFormat, StudioArgv } from 'cli/types'; | ||||||
|
||||||
async function runCommand( siteFolder: string, outputFormat?: OutputFormat ): Promise< void > { | ||||||
export async function runCommand( | ||||||
siteFolder: string, | ||||||
outputFormat?: OutputFormat | ||||||
): Promise< void > { | ||||||
const logger = new Logger< LoggerAction >( outputFormat ); | ||||||
|
||||||
try { | ||||||
|
@@ -40,14 +43,19 @@ async function runCommand( siteFolder: string, outputFormat?: OutputFormat ): Pr | |||||
} | ||||||
} | ||||||
|
||||||
export const registerCommand: RegisterCommand = ( parentCommand, rootCommand = parentCommand ) => { | ||||||
parentCommand | ||||||
.command( 'list [folder]' ) | ||||||
.description( | ||||||
__( 'List preview sites for the specified folder (defaults to current directory)' ) | ||||||
) | ||||||
.action( async ( siteFolder: string = process.cwd() ) => { | ||||||
const outputFormat = rootCommand.opts().outputFormat; | ||||||
await runCommand( siteFolder, outputFormat ); | ||||||
} ); | ||||||
export const registerCommand = ( yargs: StudioArgv ) => { | ||||||
return yargs.command( { | ||||||
command: 'list [folder]', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
describe: __( 'List preview sites for the specified folder (defaults to current directory)' ), | ||||||
builder: ( yargs ) => { | ||||||
return yargs.positional( 'folder', { | ||||||
type: 'string', | ||||||
default: process.cwd(), | ||||||
description: __( 'The folder to list previews for' ), | ||||||
} ); | ||||||
}, | ||||||
handler: async ( argv ) => { | ||||||
await runCommand( argv.folder, argv.outputFormat ); | ||||||
}, | ||||||
} ); | ||||||
}; |
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
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.
I think it would be better if all commands were printed on the top-level help page. Having to explicitly explore the preview command to understand which commands are available will be cumbersome, especially for the first release (when there are so few commands in total).
This change would also require some changes in
src/index.ts
.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.
I couldn't make it work with "2 names commands". From my testing all the commands would register as "preview", so only the last one to be registered would be available.
I still moved to the logic to the main index, grouped under the
preview
command.I explored options for displaying sub-commands in the main help output but couldn't find a clean way to do this without manually writing the help text.
I think that's worse than having the different helps for the sub-commands.
What do you think?
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.
Ugh, pulling my hair over this issue 😭
I couldn't figure out a solution either, so let's stick with this approach for now.