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: confirm all questions before init repo #70

Merged
merged 3 commits into from
Sep 3, 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
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()