From 9669e51ceb0b967ffdb81eef5aab187cdcf3937f Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Tue, 3 Sep 2024 01:51:06 +0530 Subject: [PATCH 1/3] fix: confirm all questions before init repo --- src/hooks/dependencies.ts | 41 +++++++++++++++++++---------------- src/index.ts | 45 ++++++++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/hooks/dependencies.ts b/src/hooks/dependencies.ts index be411be..b800c74 100644 --- a/src/hooks/dependencies.ts +++ b/src/hooks/dependencies.ts @@ -26,6 +26,7 @@ const registerInstallationHook = ( template: string, installArg: boolean | undefined, pmArg: string, + controllers: { dependencies: AbortController; completed: AbortController }, ) => { if (excludeTemplate.includes(template)) return @@ -67,28 +68,32 @@ const registerInstallationHook = ( }) } - chdir(directoryPath) + controllers.dependencies.signal.addEventListener('abort', async () => { + chdir(directoryPath) - if (!knownPackageManagers[packageManager]) { - exit(1) - } - - 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) + } + + controllers.completed.abort() + }) return }) diff --git a/src/index.ts b/src/index.ts index e7a6a31..28c485a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -145,30 +145,39 @@ 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 controllers = { + dependencies: new AbortController(), + completed: new AbortController(), + } - registerInstallationHook(templateName, install, pm) + registerInstallationHook(templateName, install, pm, controllers) 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() + controllers.dependencies.abort() + }) + + afterCreateHook.applyHook(templateName, { + projectName, + directoryPath: targetDirectoryPath, + }) } catch (e) { throw new Error( `Error running hook for ${templateName}: ${ @@ -191,8 +200,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}`)) + controllers.completed.signal.addEventListener('abort', () => { + console.log(chalk.green(`🎉 ${chalk.bold('Copied project files')}`)) + console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`)) + }) } program.parse() From 0a9193285664d30c9c6732445ba04c4e630c5ebc Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Tue, 3 Sep 2024 08:18:00 +0530 Subject: [PATCH 2/3] fix: used EventEmitter --- src/hooks/dependencies.ts | 9 ++++++--- src/index.ts | 13 ++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/hooks/dependencies.ts b/src/hooks/dependencies.ts index b800c74..f3fa399 100644 --- a/src/hooks/dependencies.ts +++ b/src/hooks/dependencies.ts @@ -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' @@ -22,11 +23,13 @@ const currentPackageManager = getCurrentPackageManager() // Deno and Netlify need no dependency installation step const excludeTemplate = ['deno', 'netlify'] +export type EventMap = { dependencies: any[]; completed: any[] } + const registerInstallationHook = ( template: string, installArg: boolean | undefined, pmArg: string, - controllers: { dependencies: AbortController; completed: AbortController }, + emitter: EventEmitter, ) => { if (excludeTemplate.includes(template)) return @@ -68,7 +71,7 @@ const registerInstallationHook = ( }) } - controllers.dependencies.signal.addEventListener('abort', async () => { + emitter.on('dependencies', async () => { chdir(directoryPath) if (!knownPackageManagers[packageManager]) { @@ -92,7 +95,7 @@ const registerInstallationHook = ( exit(procExit) } - controllers.completed.abort() + emitter.emit('completed') }) return diff --git a/src/index.ts b/src/index.ts index 28c485a..97fa2a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import EventEmitter from 'events' import fs from 'fs' import path from 'path' import confirm from '@inquirer/confirm' @@ -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' @@ -146,12 +148,9 @@ async function main( const targetDirectoryPath = path.join(process.cwd(), target) - const controllers = { - dependencies: new AbortController(), - completed: new AbortController(), - } + const emitter = new EventEmitter() - registerInstallationHook(templateName, install, pm, controllers) + registerInstallationHook(templateName, install, pm, emitter) try { await Promise.all( @@ -171,7 +170,7 @@ async function main( }, ).then(() => { spinner.success() - controllers.dependencies.abort() + emitter.emit('dependencies') }) afterCreateHook.applyHook(templateName, { @@ -200,7 +199,7 @@ async function main( fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2)) } - controllers.completed.signal.addEventListener('abort', () => { + emitter.on('completed', () => { console.log(chalk.green(`🎉 ${chalk.bold('Copied project files')}`)) console.log(chalk.gray('Get started with:'), chalk.bold(`cd ${target}`)) }) From fa38236303667469d2be4606b10830ee3edee323 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Tue, 3 Sep 2024 08:20:37 +0530 Subject: [PATCH 3/3] chore: changed any to unknown --- src/hooks/dependencies.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/dependencies.ts b/src/hooks/dependencies.ts index f3fa399..040e1cc 100644 --- a/src/hooks/dependencies.ts +++ b/src/hooks/dependencies.ts @@ -23,7 +23,7 @@ const currentPackageManager = getCurrentPackageManager() // Deno and Netlify need no dependency installation step const excludeTemplate = ['deno', 'netlify'] -export type EventMap = { dependencies: any[]; completed: any[] } +export type EventMap = { dependencies: unknown[]; completed: unknown[] } const registerInstallationHook = ( template: string,