Skip to content

Commit

Permalink
feat: improve error handling during project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Feb 13, 2025
1 parent 0a7ef09 commit a039763
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/utils/create-app-task-clone-template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { log } from '@clack/prompts'
import { downloadTemplate } from 'giget'
import { existsSync } from 'node:fs'
import { readdir } from 'node:fs/promises'
import { GetArgsResult } from './get-args-result'
import { Task, taskFail } from './vendor/clack-tasks'

Expand All @@ -19,10 +20,21 @@ export function createAppTaskCloneTemplate(args: GetArgsResult): Task {
if (args.verbose) {
log.warn(`Cloning template ${args.template.repository} to ${args.targetDirectory}`)
}
const { dir } = await downloadTemplate(args.template.repository, {
dir: args.targetDirectory,
})
return result({ message: `Cloned template to ${dir}` })
try {
const { dir } = await downloadTemplate(args.template.repository, {
dir: args.targetDirectory,
})
// make sure the dir is not empty
const files = await readdir(dir)
if (files.length === 0) {
taskFail(`The template directory is empty. Please check the repository: ${args.template.repository}`)
return
}

return result({ message: `Cloned template to ${dir}` })
} catch (error) {
taskFail(`init: Error cloning the template: ${error}`)
}
},
}
}
3 changes: 2 additions & 1 deletion src/utils/create-app-task-initialize-git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from '@clack/prompts'
import { GetArgsResult } from './get-args-result'
import { Task } from './vendor/clack-tasks'
import { Task, taskFail } from './vendor/clack-tasks'
import { initializeGitRepo } from './vendor/git'

export function createAppTaskInitializeGit(args: GetArgsResult): Task {
Expand All @@ -22,6 +22,7 @@ export function createAppTaskInitializeGit(args: GetArgsResult): Task {
console.error(error)
}
log.error(`${error}`)
taskFail(`init: Error initializing git: ${error}`)
}
},
}
Expand Down
12 changes: 7 additions & 5 deletions src/utils/create-app-task-install-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { GetArgsResult } from './get-args-result'
import { execAndWait } from './vendor/child-process-utils'
import { Task } from './vendor/clack-tasks'
import { Task, taskFail } from './vendor/clack-tasks'
import { getPackageManagerCommand } from './vendor/package-manager'

export function createAppTaskInstallDependencies(args: GetArgsResult): Task {
Expand All @@ -28,13 +28,15 @@ export function createAppTaskInstallDependencies(args: GetArgsResult): Task {
}
await execAndWait(`rm ${lockFile}`, args.targetDirectory)
}

if (args.verbose) {
log.warn(`Running ${install}`)
}
await execAndWait(install, args.targetDirectory)

return result({ message: `Installed via ${pm}` })
try {
await execAndWait(install, args.targetDirectory)
return result({ message: `Installed via ${pm}` })
} catch (error) {
taskFail(`init: Error installing dependencies: ${error}`)
}
},
}
}
2 changes: 1 addition & 1 deletion src/utils/create-app-task-run-init-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function createAppTaskRunInitScript(args: GetArgsResult): Task {
}
return result({ message: 'Executed init script!', instructions })
} catch (error) {
taskFail(`${error}`)
taskFail(`init: Error running init script: ${error}`)
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions src/utils/vendor/child-process-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export function execAndWait(command: string, cwd: string) {
return new Promise<{ code: number; stdout: string }>((res, rej) => {
exec(command, { cwd, env: { ...process.env } }, (error, stdout, stderr) => {
if (error) {
const errorStr = stderr && stderr.trim().length > 0 ? stderr : `${error}`
const logFile = join(cwd, 'error.log')
writeFileSync(logFile, `${stdout}\n${stderr}`)
const message = stderr && stderr.trim().length > 0 ? stderr : stdout
writeFileSync(logFile, `${stdout}\n${errorStr}`)
const message = errorStr.trim().length > 0 ? errorStr : `An error occurred while running ${command}`
rej(new CreateAppError(message, error.code, logFile))
} else {
res({ code: 0, stdout })
Expand Down

0 comments on commit a039763

Please sign in to comment.