keithamus / sort-package-json

Sort an Object or package.json based on the well-known package.json keys
MIT License
790 stars 83 forks source link

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

Closed aarondill closed 1 year ago

aarondill commented 1 year ago

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.

aarondill commented 1 year ago

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!

aarondill commented 1 year ago

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 commented 1 year ago

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

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.2.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: