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

chore: leverage yargs arg-parser lib #704

Merged
merged 5 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/create-redwood-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"decompress": "^4.2.0",
"execa": "^4.0.0",
"listr": "^0.14.3",
"tmp": "^0.1.0"
"tmp": "^0.1.0",
"yargs": "^15.3.1"
},
"scripts": {
"build": "yarn cross-env NODE_ENV=production babel src -d dist --delete-dir-on-start",
Expand Down
21 changes: 7 additions & 14 deletions packages/create-redwood-app/src/create-redwood-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import execa from 'execa'
import tmp from 'tmp'
import checkNodeVersion from 'check-node-version'
import chalk from 'chalk'
import yargs from 'yargs'

import { name, version } from '../package'

Expand All @@ -40,20 +41,12 @@ const downloadFile = async (sourceUrl, targetFile) => {
})
}

const [_arg1, _arg2, ...args] = process.argv

const helpInfo = `Usage: ${name} <target-dir> [options]\n\n Available options\n\n --help, -h\n --version, -v`

if (args[0].startsWith('-')) {
if (['-h', '--help'].includes(args[0])) {
console.log(helpInfo)
} else if (['-v', '--version'].includes(args[0])) {
console.log(version)
} else {
console.error(`Invalid option, use ${name} --help to know more`)
}
process.exit(0)
}
const { _: args } = yargs
.scriptName(name)
.usage('Usage: $0 <project directory>')
.example('$0 newapp')
.version(version)
.strict().argv

const targetDir = String(args).replace(/,/g, '-')
if (!targetDir) {
Expand Down