Skip to content

Commit

Permalink
refactor: replace inquirer with prompts (#25)
Browse files Browse the repository at this point in the history
* replace inquirer with prompts

* separate packages

* use `chalk`

---------

Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
  • Loading branch information
ryuapp and yusukebe authored Mar 28, 2024
1 parent d68a9a7 commit 5f5f76a
Show file tree
Hide file tree
Showing 4 changed files with 1,006 additions and 811 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@
},
"devDependencies": {
"@hono/eslint-config": "^0.0.2",
"@inquirer/confirm": "^3.1.0",
"@inquirer/input": "^2.1.0",
"@inquirer/select": "^2.2.0",
"@types/degit": "^2.8.3",
"@types/node": "^18.11.18",
"@types/prompts": "^2.4.2",
"@types/yargs-parser": "^21.0.0",
"chalk": "^5.3.0",
"esbuild": "^0.16.17",
"eslint": "^8.55.0",
"execa": "^8.0.1",
"kleur": "^4.1.5",
"node-fetch": "^3.3.0",
"np": "^7.6.3",
"ora": "^8.0.1",
"prettier": "^3.2.5",
"prompts": "^2.4.2",
"tiged": "^2.12.7",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"vitest": "^0.34.6",
"yargs-parser": "^21.1.1"
}
}
}
22 changes: 9 additions & 13 deletions src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { exec } from 'child_process'
import { chdir, exit } from 'process'
import { bold, green, red } from 'kleur/colors'
import confirm from '@inquirer/confirm'
import select from '@inquirer/select'
import chalk from 'chalk'
import ora from 'ora'
import prompts from 'prompts'
import { projectDependenciesHook } from '../hook'

type PackageManager = 'npm' | 'bun' | 'pnpm' | 'yarn'
Expand All @@ -21,24 +22,19 @@ const registerInstallationHook = (template: string) => {
if (template == 'deno') return // Deno needs no dependency installation step

projectDependenciesHook.addHook(template, async ({ directoryPath }) => {
const { installDeps } = await prompts({
type: 'confirm',
name: 'installDeps',
const installDeps = await confirm({
message: 'Do you want to install project dependencies?',
initial: true,
default: true,
})

if (!installDeps) return

const { packageManager } = await prompts({
type: 'select',
name: 'packageManager',
const packageManager = await select({
message: 'Which package manager do you want to use?',
choices: knownPackageManagerNames.map((template: string) => ({
title: template,
value: template,
})),
initial: knownPackageManagerNames.indexOf(currentPackageManager),
default: knownPackageManagerNames.indexOf(currentPackageManager),
})

chdir(directoryPath)
Expand All @@ -57,9 +53,9 @@ const registerInstallationHook = (template: string) => {
spinner.stop().clear()

if (procExit == 0) {
console.log(bold(`${green('✔')} Installed project dependencies`))
console.log(`${chalk.green('✔')} Installed project dependencies`)
} else {
console.log(bold(`${red('×')} Failed to install project dependencies`))
console.log(`${chalk.red('×')} Failed to install project dependencies`)
exit(procExit)
}

Expand Down
60 changes: 28 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs'
import path from 'path'
import { bold, gray, green } from 'kleur/colors'
import confirm from '@inquirer/confirm'
import input from '@inquirer/input'
import select from '@inquirer/select'
import chalk from 'chalk'
import ora from 'ora'
import prompts from 'prompts'
// @ts-expect-error tiged does not have types
import tiged from 'tiged'
import yargsParser from 'yargs-parser'
Expand Down Expand Up @@ -46,7 +48,7 @@ function mkdirp(dir: string) {
}

async function main() {
console.log(gray(`\ncreate-hono version ${version}`))
console.log(chalk.gray(`\ncreate-hono version ${version}`))

const args = yargsParser(process.argv.slice(2))

Expand All @@ -68,39 +70,34 @@ async function main() {
let projectName = ''
if (args._[0]) {
target = args._[0].toString()
console.log(`${bold(`${green('✔')} Using target directory`)}${target}`)
console.log(
`${chalk.bold(`${chalk.green('✔')} Using target directory`)}${target}`,
)
projectName = path.basename(target)
} else {
const answer = await prompts({
type: 'text',
name: 'target',
const answer = await input({
message: 'Target directory',
initial: 'my-app',
default: 'my-app',
})
target = answer.target
if (answer.target === '.') {
target = answer
if (answer === '.') {
projectName = path.basename(process.cwd())
} else {
projectName = path.basename(answer.target)
projectName = path.basename(answer)
}
}

const templateName =
templateArg ||
(
await prompts({
type: 'select',
name: 'template',
message: 'Which template do you want to use?',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
choices: templateNames.map((template: any) => ({
title: template.name,
value: template.name,
})),
initial: 0,
})
).template

(await select({
loop: true,
message: 'Which template do you want to use?',
choices: templateNames.map((template: { name: string }) => ({
title: template.name,
value: template.name,
})),
default: 0,
}))
if (!templateName) {
throw new Error('No template selected')
}
Expand All @@ -111,13 +108,11 @@ async function main() {

if (fs.existsSync(target)) {
if (fs.readdirSync(target).length > 0) {
const response = await prompts({
type: 'confirm',
name: 'value',
const response = await confirm({
message: 'Directory not empty. Continue?',
initial: false,
default: false,
})
if (!response.value) {
if (!response) {
process.exit(1)
}
}
Expand All @@ -138,6 +133,7 @@ async function main() {
)
emitter.clone(targetDirectoryPath).then(() => {
spinner.stop().clear()
console.log(`${chalk.green('✔')} Cloned the template`)
res({})
})
})
Expand All @@ -163,8 +159,8 @@ async function main() {
)
}

console.log(bold(green('✔ Copied project files')))
console.log(gray('Get started with:'), bold(`cd ${target}`))
console.log(chalk.green('✔ ' + chalk.bold('Copied project files')))
console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`))
}

main()
Loading

0 comments on commit 5f5f76a

Please sign in to comment.