Skip to content

Commit

Permalink
feat: Add --install and --pm flags which skip interactive questions w… (
Browse files Browse the repository at this point in the history
#34)

* feat: Add --install and --pm flags which skip interactive questions when specified

* Update README with new flags
  • Loading branch information
jculvey authored Apr 21, 2024
1 parent 96d0d57 commit 4c7e586
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ You can specify the desired template from the command line. This is useful for a
npm create hono@latest ./my-app -- --template cloudflare-pages
```

### `--install`

Install dependencies after cloning template.

```
npm create hono@latest ./my-app -- --install
```

### `--pm <pnpm|bun|npm|yarn>`

Allows you to specify which package manager to use.

```
npm create hono@latest ./my-app -- --pm pnpm
```

## Author

Yusuke Wada <https://github.com/yusukebe>
Expand Down
43 changes: 30 additions & 13 deletions src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,41 @@ const knownPackageManagers: { [key: string]: string } = {
const knownPackageManagerNames = Object.keys(knownPackageManagers)
const currentPackageManager = getCurrentPackageManager()

const registerInstallationHook = (template: string) => {
const registerInstallationHook = (
template: string,
installArg: boolean,
pmArg: string,
) => {
if (template == 'deno') return // Deno needs no dependency installation step

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

if (installArg) {
installDeps = true
} else {
installDeps = await confirm({
message: 'Do you want to install project dependencies?',
default: true,
})
}

if (!installDeps) return
const packageManager = await select({
message: 'Which package manager do you want to use?',
choices: knownPackageManagerNames.map((template: string) => ({
title: template,
value: template,
})),
default: currentPackageManager,
})

let packageManager

if (pmArg && knownPackageManagerNames.includes(pmArg)) {
packageManager = pmArg
} else {
packageManager = await select({
message: 'Which package manager do you want to use?',
choices: knownPackageManagerNames.map((template: string) => ({
title: template,
value: template,
})),
default: currentPackageManager,
})
}

chdir(directoryPath)

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function main() {

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

const templateArg = args.template
const { install, pm, template: templateArg } = args

const templates: Record<string, { name: string }> = {}

Expand Down Expand Up @@ -139,7 +139,7 @@ async function main() {
})
})

registerInstallationHook(templateName)
registerInstallationHook(templateName, install, pm)

try {
afterCreateHook.applyHook(templateName, {
Expand Down

0 comments on commit 4c7e586

Please sign in to comment.