Skip to content

Commit

Permalink
feat: switch to yargs
Browse files Browse the repository at this point in the history
  • Loading branch information
b-x-wu committed Apr 20, 2024
1 parent 9883c89 commit eb99fa5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:
run: mkdir temp

- name: Run Package Init Script
run: npx create-react-sandbox client ${{ matrix.flags }}
run: npx create-react-sandbox ${{ matrix.flags }}
working-directory: ./temp

- name: Run Package Build Script
run: npm run build
working-directory: ./temp/client
working-directory: ./temp/app

37 changes: 21 additions & 16 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#! /usr/bin/env node

// guided by https://blog.shahednasser.com/how-to-create-a-npx-tool/

const fs = require('fs').promises;
const path = require('node:path')
const loadingSpinner = require('loading-spinner')
const { spawn } = require('node:child_process')
const commandLineArgs = require('command-line-args')
const yargs = require('yargs/yargs')

const SPINNER_OPTIONS = { clearChar: true, hideCursor: true }
const SPINNER_SPEED = 400
Expand All @@ -16,18 +14,25 @@ const SPINNER_SPEED = 400
* @returns {{ sandboxDirectory: string, typescript: boolean }} the command line options
*/
function getCommandLineArgs() {
const optionDefinitions = [
{ name: 'sandboxDirectory', type: String, defaultOption: true },
{ name: 'typescript', type: Boolean, alias: 't' }
]
try {
return commandLineArgs(optionDefinitions, { stopAtFirstUnknown: true })
} catch (e) {
if (e.name === 'ALREADY_SET') {
throw new Error('Typescript flag was set more than once. The flag is only permitted to be set 0 or 1 times.')
}
throw new Error('Unknown error while parsing command line.\nCalls should follow the format: `npx create-react-sandbox <sandbox-name> [-t]`')
}
return yargs(process.argv.slice(2))
.option('name', {
alias: 'n',
describe: 'name of the app',
default: 'app',
type: 'string',
})
.option('typescript', {
alias: 't',
type: 'boolean',
default: false,
})
.option('eslint', {
alias: 'l',
type: 'boolean',
default: false,
})
.help()
.parse()
}

/**
Expand Down Expand Up @@ -66,7 +71,7 @@ function installDependencies(sandboxDirectory, onData, onErr, onExit) {
* Run the main thread of the program
*/
async function main() {
const { sandboxDirectory, typescript: isTypescript } = getCommandLineArgs()
const { name: sandboxDirectory, typescript: isTypescript, eslint: isEslint } = getCommandLineArgs()

process.stdout.write('Initializing the sandbox...')
loadingSpinner.start(SPINNER_SPEED, SPINNER_OPTIONS)
Expand Down
175 changes: 145 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"homepage": "https://github.com/b-x-wu/create-react-sandbox",
"dependencies": {
"command-line-args": "^5.2.1",
"loading-spinner": "^1.2.1"
"loading-spinner": "^1.2.1",
"yargs": "^17.7.2"
}
}

0 comments on commit eb99fa5

Please sign in to comment.