From 18667edaa2a5b47a1d2f32e909ada1e59220b978 Mon Sep 17 00:00:00 2001 From: Damian Glowala Date: Wed, 8 Jan 2025 19:56:15 +0100 Subject: [PATCH 1/2] feat(init): warn if dir already exists and no `--force` flag is provided --- src/commands/init.ts | 46 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 1baf9ef13..18cb8901e 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,14 +1,15 @@ import type { DownloadTemplateResult } from 'giget' import type { PackageManagerName } from 'nypm' -import process from 'node:process' +import { existsSync } from 'node:fs' +import process from 'node:process' import { defineCommand } from 'citty' import { downloadTemplate, startShell } from 'giget' import { installDependencies } from 'nypm' -import { relative, resolve } from 'pathe' -import { x } from 'tinyexec' +import { join, relative, resolve } from 'pathe' +import { x } from 'tinyexec' import { logger } from '../utils/logger' import { cwdArgs } from './_shared' @@ -75,6 +76,45 @@ export default defineCommand({ process.exit(1) } + // Prompt the user if the template download directory already exists + // when no `--force` flag is provided + if (!ctx.args.force) { + const templateDownloadPath = join(cwd, ctx.args.dir) + + const templateDownloadDirExists = existsSync(templateDownloadPath) + + if (templateDownloadDirExists) { + const selectedAction = await logger.prompt( + `The directory \`${templateDownloadPath}\` already exists. What would you like to do?`, + { + type: 'select', + options: ['Override its contents', 'Select different directory', 'Abort'], + }, + ) + + switch (selectedAction) { + case 'Override its contents': + (ctx.args.force as boolean) = true + + break + + case 'Select different directory': + (ctx.args.dir as string) = await logger.prompt( + 'Please specify a different directory:', + { + type: 'text', + }, + ) + + break + + case 'Abort': + logger.info('Initialization aborted.') + process.exit(1) + } + } + } + // Download template let template: DownloadTemplateResult From 7cf83456828598b2b54de83066a79e2bb0e54173 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 9 Jan 2025 15:28:48 +0100 Subject: [PATCH 2/2] chore: small refactors --- src/commands/init.ts | 75 +++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 18cb8901e..e4ea76904 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -2,14 +2,14 @@ import type { DownloadTemplateResult } from 'giget' import type { PackageManagerName } from 'nypm' import { existsSync } from 'node:fs' - import process from 'node:process' + import { defineCommand } from 'citty' import { downloadTemplate, startShell } from 'giget' import { installDependencies } from 'nypm' -import { join, relative, resolve } from 'pathe' - +import { relative, resolve } from 'pathe' import { x } from 'tinyexec' + import { logger } from '../utils/logger' import { cwdArgs } from './_shared' @@ -76,42 +76,40 @@ export default defineCommand({ process.exit(1) } + let templateDownloadPath = resolve(cwd, ctx.args.dir) + let shouldForce = Boolean(ctx.args.force) + // Prompt the user if the template download directory already exists // when no `--force` flag is provided - if (!ctx.args.force) { - const templateDownloadPath = join(cwd, ctx.args.dir) - - const templateDownloadDirExists = existsSync(templateDownloadPath) - - if (templateDownloadDirExists) { - const selectedAction = await logger.prompt( - `The directory \`${templateDownloadPath}\` already exists. What would you like to do?`, - { - type: 'select', - options: ['Override its contents', 'Select different directory', 'Abort'], - }, - ) - - switch (selectedAction) { - case 'Override its contents': - (ctx.args.force as boolean) = true - - break - - case 'Select different directory': - (ctx.args.dir as string) = await logger.prompt( - 'Please specify a different directory:', - { - type: 'text', - }, - ) - - break - - case 'Abort': - logger.info('Initialization aborted.') - process.exit(1) + const shouldVerify = !shouldForce && existsSync(templateDownloadPath) + if (shouldVerify) { + const selectedAction = await logger.prompt( + `The directory \`${templateDownloadPath}\` already exists. What would you like to do?`, + { + type: 'select', + options: ['Override its contents', 'Select different directory', 'Abort'], + }, + ) + + switch (selectedAction) { + case 'Override its contents': + shouldForce = true + break + + case 'Select different directory': { + const dir = await logger.prompt('Please specify a different directory:', { + type: 'text', + }) + if (dir && typeof dir === 'string') { + templateDownloadPath = resolve(cwd, dir) + } + break } + + // 'Abort' or Ctrl+C + default: + logger.info('Initialization aborted.') + process.exit(1) } } @@ -120,9 +118,8 @@ export default defineCommand({ try { template = await downloadTemplate(templateName, { - dir: ctx.args.dir, - cwd, - force: Boolean(ctx.args.force), + dir: templateDownloadPath, + force: shouldForce, offline: Boolean(ctx.args.offline), preferOffline: Boolean(ctx.args.preferOffline), registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY,