Skip to content
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

feat(cli): added --version and --help options #282

Merged
merged 12 commits into from
Jan 22, 2023
Merged

Conversation

aarondill
Copy link
Contributor

added --version (-V) and --help (-h) options to the CLI for user interface.

--help output:

Usage: sort-package-json [OPTION...] [FILE...]
Sort npm package.json files. Default: ./package.json
Strings passed as files are parsed as globs.

  -c, --check                 check if FILES are sorted
  -q, --quiet                  don't output success messages
  -h, --help                   display this help and exit
  -V, --version              display the version and exit
  

This help menu can be easily modified in cli.js. It includes the option from #281, but that could be easily removed if so desired.

--version output:

sort-package-json 0.0.0-development

Or other such versions. The version is pulled from the project.json file in the cli script's directory.
This is found by reading process.env[1], using fs.realpathSync, to read symlinks when it's installed, and path.dirname to get the parent directory. From there it is simple to read the file and call JSON.parse.

added --version (-V) and --help (-h) options to the CLI for user interface.
Fixed a major bug with the version option in order to work from any directory
Uses dynamic import to import the node:path module when checking the
version so it is not loaded when it is not needed.
cli.js Outdated Show resolved Hide resolved
cli.js Outdated Show resolved Hide resolved
@aarondill
Copy link
Contributor Author

Having just tested this revision on a windows OS, I can confirm that the help and version options work at the very least. It was not an extensive test, but I ran it with both options, ran it from a few different CWDs, and created a symlink and moved that around, and everything worked!

keithamus
keithamus previously approved these changes Jan 21, 2023
Copy link
Owner

@keithamus keithamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

cli.js Outdated Show resolved Hide resolved
removes not needed call to fileURLToPath, replacing with the URL object itself
Removed try-catch block in --version which provided a default response
if the file system requests failed.
@aarondill
Copy link
Contributor Author

This should now be ready for review. The try-catch block has been removed, and I removed the extra call/import to fileURLToPath. I have tested this on both Ubuntu Bash and Windows 11 command line(through node cli.js) from many different CWDs and even through a symlink.

@aarondill
Copy link
Contributor Author

Reopening this PR according to @fisker's advice to merge this PR seperate to #283

@aarondill aarondill reopened this Jan 22, 2023
@aarondill aarondill requested a review from fisker January 22, 2023 14:23
cli.js Outdated Show resolved Hide resolved
cli.js Outdated Show resolved Hide resolved
cli.js Outdated Show resolved Hide resolved
Co-authored-by: fisker Cheung <[email protected]>
cli.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@fisker fisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

@fisker fisker merged commit dc1ad5a into keithamus:master Jan 22, 2023
@github-actions
Copy link

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@aarondill aarondill deleted the help branch February 1, 2023 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants