Skip to content

Commit

Permalink
feat: confirm all questions before init repo (#70)
Browse files Browse the repository at this point in the history
* fix: confirm all questions before init repo

* fix: used EventEmitter

* chore: changed any to unknown
  • Loading branch information
MathurAditya724 authored Sep 3, 2024
1 parent 1afc94a commit 44555b4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
44 changes: 26 additions & 18 deletions src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { exec } from 'child_process'
import type { EventEmitter } from 'events'
import { chdir, exit } from 'process'
import confirm from '@inquirer/confirm'
import select from '@inquirer/select'
Expand All @@ -22,10 +23,13 @@ const currentPackageManager = getCurrentPackageManager()
// Deno and Netlify need no dependency installation step
const excludeTemplate = ['deno', 'netlify']

export type EventMap = { dependencies: unknown[]; completed: unknown[] }

const registerInstallationHook = (
template: string,
installArg: boolean | undefined,
pmArg: string,
emitter: EventEmitter<EventMap>,
) => {
if (excludeTemplate.includes(template)) return

Expand Down Expand Up @@ -67,28 +71,32 @@ const registerInstallationHook = (
})
}

chdir(directoryPath)

if (!knownPackageManagers[packageManager]) {
exit(1)
}
emitter.on('dependencies', async () => {
chdir(directoryPath)

const spinner = createSpinner('Installing project dependencies').start()
const proc = exec(knownPackageManagers[packageManager])
if (!knownPackageManagers[packageManager]) {
exit(1)
}

const procExit: number = await new Promise((res) => {
proc.on('exit', (code) => res(code == null ? 0xff : code))
})
const spinner = createSpinner('Installing project dependencies').start()
const proc = exec(knownPackageManagers[packageManager])

if (procExit == 0) {
spinner.success()
} else {
spinner.stop({
mark: chalk.red('×'),
text: 'Failed to install project dependencies',
const procExit: number = await new Promise((res) => {
proc.on('exit', (code) => res(code == null ? 0xff : code))
})
exit(procExit)
}

if (procExit == 0) {
spinner.success()
} else {
spinner.stop({
mark: chalk.red('×'),
text: 'Failed to install project dependencies',
})
exit(procExit)
}

emitter.emit('completed')
})

return
})
Expand Down
44 changes: 27 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EventEmitter from 'events'
import fs from 'fs'
import path from 'path'
import confirm from '@inquirer/confirm'
Expand All @@ -11,6 +12,7 @@ import { version } from '../package.json'
import { projectDependenciesHook } from './hook'
import { afterCreateHook } from './hooks/after-create'
import {
type EventMap,
knownPackageManagerNames,
registerInstallationHook,
} from './hooks/dependencies'
Expand Down Expand Up @@ -145,30 +147,36 @@ async function main(
}

const targetDirectoryPath = path.join(process.cwd(), target)
const spinner = createSpinner('Cloning the template').start()

await downloadTemplate(
`gh:${config.user}/${config.repository}/${config.directory}/${templateName}#${config.ref}`,
{
dir: targetDirectoryPath,
offline,
force: true,
},
).then(() => spinner.success())
const emitter = new EventEmitter<EventMap>()

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

try {
afterCreateHook.applyHook(templateName, {
projectName,
directoryPath: targetDirectoryPath,
})

await Promise.all(
projectDependenciesHook.applyHook(templateName, {
directoryPath: targetDirectoryPath,
}),
)

const spinner = createSpinner('Cloning the template').start()

await downloadTemplate(
`gh:${config.user}/${config.repository}/${config.directory}/${templateName}#${config.ref}`,
{
dir: targetDirectoryPath,
offline,
force: true,
},
).then(() => {
spinner.success()
emitter.emit('dependencies')
})

afterCreateHook.applyHook(templateName, {
projectName,
directoryPath: targetDirectoryPath,
})
} catch (e) {
throw new Error(
`Error running hook for ${templateName}: ${
Expand All @@ -191,8 +199,10 @@ async function main(
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2))
}

console.log(chalk.green(`🎉 ${chalk.bold('Copied project files')}`))
console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`))
emitter.on('completed', () => {
console.log(chalk.green(`🎉 ${chalk.bold('Copied project files')}`))
console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`))
})
}

program.parse()

0 comments on commit 44555b4

Please sign in to comment.