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: switched from yargs-parser to commander #64

Merged
merged 2 commits into from
Aug 8, 2024
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/node": "^18.11.18",
"@types/yargs-parser": "^21.0.0",
"chalk": "^5.3.0",
"commander": "^12.1.0",
"esbuild": "^0.16.17",
"eslint": "^8.55.0",
"execa": "^8.0.1",
Expand All @@ -43,7 +44,6 @@
"prettier": "^3.3.3",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"vitest": "^0.34.6",
"yargs-parser": "^21.1.1"
"vitest": "^0.34.6"
}
}
2 changes: 1 addition & 1 deletion src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const knownPackageManagers: { [key: string]: string } = {
yarn: 'yarn',
}

const knownPackageManagerNames = Object.keys(knownPackageManagers)
export const knownPackageManagerNames = Object.keys(knownPackageManagers)
const currentPackageManager = getCurrentPackageManager()

// Deno and Netlify need no dependency installation step
Expand Down
56 changes: 46 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import confirm from '@inquirer/confirm'
import input from '@inquirer/input'
import select from '@inquirer/select'
import chalk from 'chalk'
import { Option, program, type Command } from 'commander'
import { downloadTemplate } from 'giget'
import { createSpinner } from 'nanospinner'
import yargsParser from 'yargs-parser'
import { version } from '../package.json'
import { projectDependenciesHook } from './hook'
import { afterCreateHook } from './hooks/after-create'
import { registerInstallationHook } from './hooks/dependencies'
import {
knownPackageManagerNames,
registerInstallationHook,
} from './hooks/dependencies'

const directoryName = 'templates'
const config = {
Expand Down Expand Up @@ -46,16 +49,49 @@ function mkdirp(dir: string) {
}
}

async function main() {
console.log(chalk.gray(`create-hono version ${version}`))
program
.name('create-hono')
.version(version)
.arguments('[target]')
.addOption(
new Option('-i, --install', 'Install dependencies').argParser(Boolean),
)
.addOption(
new Option('-p, --pm <pm>', 'Package manager to use').choices(
knownPackageManagerNames,
),
)
.addOption(
new Option('-t, --template <template>', 'Template to use').choices(
templates,
),
)
.addOption(
new Option('-o, --offline', 'Use offline mode')
.argParser(Boolean)
.default(false),
)
.action(main)

type ArgOptions = {
install: boolean
offline: boolean
pm: string
template: string
}

const args = yargsParser(process.argv.slice(2))
async function main(
targetDir: string | undefined,
options: ArgOptions,
command: Command,
) {
console.log(chalk.gray(`${command.name()} version ${command.version()}`))

const { install, pm, template: templateArg } = args
const { install, pm, offline, template: templateArg } = options

let target = ''
if (args._[0]) {
target = args._[0].toString()
if (targetDir) {
target = targetDir
console.log(
`${chalk.bold(`${chalk.green('✔')} Using target directory`)} … ${target}`,
)
Expand Down Expand Up @@ -115,7 +151,7 @@ async function main() {
`gh:${config.user}/${config.repository}/${config.directory}/${templateName}#${config.ref}`,
{
dir: targetDirectoryPath,
offline: false,
offline,
force: true,
},
).then(() => spinner.success())
Expand Down Expand Up @@ -159,4 +195,4 @@ async function main() {
console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`))
}

main()
program.parse()
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,11 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

commander@^12.1.0:
version "12.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-12.1.0.tgz#01423b36f501259fdaac4d0e4d60c96c991585d3"
integrity sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -4902,11 +4907,6 @@ yargs-parser@^20.2.3:
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==

yargs-parser@^21.1.1:
version "21.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
Expand Down