Skip to content

Commit b466172

Browse files
committed
promt ui
1 parent a4b47e3 commit b466172

2 files changed

Lines changed: 63 additions & 35 deletions

File tree

src/services/ensureFilesOrPrompt.ts

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import chalk from 'chalk';
43
import { confirmYesNo } from '../ui/prompts.js';
54
import { warnIfEnvNotIgnored } from './git.js';
5+
import { printPrompt } from '../ui/compare/printPrompt.js';
66

77
/**
88
* Ensures that the necessary .env files exist or prompts the user to create them.
@@ -26,6 +26,7 @@ export async function ensureFilesOrPrompt(args: {
2626
isYesMode,
2727
isCiMode,
2828
} = args;
29+
2930
const envPath = path.resolve(cwd, primaryEnv);
3031
const examplePath = path.resolve(cwd, primaryExample);
3132
const envExists = fs.existsSync(envPath);
@@ -35,60 +36,53 @@ export async function ensureFilesOrPrompt(args: {
3536
if (!envExists && !exampleExists) {
3637
const hasAnyEnv = fs.readdirSync(cwd).some((f) => f.startsWith('.env'));
3738
if (!hasAnyEnv) {
38-
console.log(
39-
chalk.yellow(
40-
'⚠️ No .env* or .env.example file found. Skipping comparison.',
41-
),
42-
);
39+
printPrompt.noEnvFound();
4340
return { didCreate: false, shouldExit: true, exitCode: 0 };
4441
}
4542
}
4643

4744
// Case 2: missing .env but has .env.example
4845
if (!envExists && exampleExists) {
4946
if (!alreadyWarnedMissingEnv) {
50-
console.log();
51-
console.log(chalk.yellow(`📄 ${path.basename(envPath)} file not found.`));
47+
printPrompt.missingEnv(envPath);
5248
}
53-
let createEnv = isYesMode
49+
50+
const createEnv = isYesMode
5451
? true
5552
: isCiMode
56-
? false
57-
: await confirmYesNo(
58-
`❓ Do you want to create a new ${path.basename(envPath)} file from ${path.basename(examplePath)}?`,
59-
{ isCiMode, isYesMode },
60-
);
53+
? false
54+
: await confirmYesNo(
55+
`❓ Do you want to create a new ${path.basename(envPath)} file from ${path.basename(examplePath)}?`,
56+
{ isCiMode, isYesMode },
57+
);
6158

6259
if (!createEnv) {
63-
console.log(chalk.gray('🚫 Skipping .env creation.'));
60+
printPrompt.skipCreation('.env');
6461
return { didCreate: false, shouldExit: true, exitCode: 0 };
6562
}
63+
6664
const exampleContent = fs.readFileSync(examplePath, 'utf-8');
6765
fs.writeFileSync(envPath, exampleContent);
68-
console.log(
69-
chalk.green(
70-
`✅ ${path.basename(envPath)} file created successfully from ${path.basename(examplePath)}.`,
71-
),
72-
);
66+
printPrompt.envCreated(envPath, examplePath);
67+
7368
warnIfEnvNotIgnored({ envFile: path.basename(envPath) });
7469
}
7570

7671
// Case 3: has .env but is missing .env.example
7772
if (envExists && !exampleExists) {
78-
console.log(
79-
chalk.yellow(`📄 ${path.basename(examplePath)} file not found.`),
80-
);
81-
let createExample = isYesMode
73+
printPrompt.missingEnv(examplePath);
74+
75+
const createExample = isYesMode
8276
? true
8377
: isCiMode
84-
? false
85-
: await confirmYesNo(
86-
`❓ Do you want to create a new ${path.basename(examplePath)} file from ${path.basename(envPath)}?`,
87-
{ isCiMode, isYesMode },
88-
);
78+
? false
79+
: await confirmYesNo(
80+
`❓ Do you want to create a new ${path.basename(examplePath)} file from ${path.basename(envPath)}?`,
81+
{ isCiMode, isYesMode },
82+
);
8983

9084
if (!createExample) {
91-
console.log(chalk.gray('🚫 Skipping .env.example creation.'));
85+
printPrompt.skipCreation('.env.example');
9286
return { didCreate: false, shouldExit: true, exitCode: 0 };
9387
}
9488

@@ -104,11 +98,7 @@ export async function ensureFilesOrPrompt(args: {
10498
.join('\n');
10599

106100
fs.writeFileSync(examplePath, envContent);
107-
console.log(
108-
chalk.green(
109-
`✅ ${path.basename(examplePath)} file created successfully from ${path.basename(envPath)}.`,
110-
),
111-
);
101+
printPrompt.exampleCreated(examplePath, envPath);
112102
}
113103

114104
return { didCreate: true, shouldExit: false, exitCode: 0 };

src/ui/compare/printPrompt.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import chalk from 'chalk';
2+
import path from 'path';
3+
4+
/**
5+
* Prompt messages for user interactions.
6+
*/
7+
export const printPrompt = {
8+
noEnvFound() {
9+
console.log(
10+
chalk.yellow('⚠️ No .env* or .env.example file found. Skipping comparison.'),
11+
);
12+
},
13+
14+
missingEnv(envPath: string) {
15+
console.log();
16+
console.log(chalk.yellow(`📄 ${path.basename(envPath)} file not found.`));
17+
},
18+
19+
skipCreation(fileType: string) {
20+
console.log(chalk.gray(`🚫 Skipping ${fileType} creation.`));
21+
},
22+
23+
envCreated(envPath: string, examplePath: string) {
24+
console.log(
25+
chalk.green(
26+
`✅ ${path.basename(envPath)} file created successfully from ${path.basename(examplePath)}.`,
27+
),
28+
);
29+
},
30+
31+
exampleCreated(examplePath: string, envPath: string) {
32+
console.log(
33+
chalk.green(
34+
`✅ ${path.basename(examplePath)} file created successfully from ${path.basename(envPath)}.`,
35+
),
36+
);
37+
},
38+
};

0 commit comments

Comments
 (0)