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: Add --install and --pm flags which skip interactive questions w… #34

Merged
merged 2 commits into from
Apr 21, 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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think -- is not necessary. We can write it shorter.

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

Copy link
Contributor Author

@jculvey jculvey Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it is necessary 😓 . A weird quirk of npm create is that it requires -- to be specified before any additional flags that are intended to be passed to an underlying cli. This can be demonstrated with the existing --template flag:

image

compared to:

image

It's worth noting that npm is the only package manager with this weird quirk however. See pnpm:

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jculvey Thank you very very much for explaining the details. I could understand well. -- should be needed!

```

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

Allows you to specify which package manager to use.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above, -- is not necessary.

```

## 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
Loading