Skip to content

Commit

Permalink
feat(cli): added --version and --help options (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
aarondill and fisker committed Jan 22, 2023
1 parent b9ebb93 commit dc1ad5a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
#!/usr/bin/env node
import fs from 'node:fs'
import { globbySync } from 'globby'
import fs from 'node:fs'
import sortPackageJson from './index.js'

const isCheckFlag = (argument) => argument === '--check' || argument === '-c'
const isHelpFlag = (argument) => argument === '--help' || argument === '-h'
const isVersionFlag = (argument) =>
argument === '--version' || argument === '-v'

const cliArguments = process.argv.slice(2)
const isCheck = cliArguments.some(isCheckFlag)
const isHelp = cliArguments.some(isHelpFlag)
const isVersion = cliArguments.some(isVersionFlag)

if (isHelp) {
console.log(
`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
-h, --help display this help and exit
-v, --version display the version and exit
`,
)
process.exit(0)
}
if (isVersion) {
const packageJsonUrl = new URL('package.json', import.meta.url)
const packageJsonBuffer = fs.readFileSync(packageJsonUrl)
const { version } = JSON.parse(packageJsonBuffer)

console.log(`sort-package-json ${version}`)
process.exit(0)
}

const patterns = cliArguments.filter((argument) => !isCheckFlag(argument))

Expand Down

0 comments on commit dc1ad5a

Please sign in to comment.