Skip to content

Commit 3404f78

Browse files
committed
Added auto generated env-example file with generate command
1 parent 80a50c8 commit 3404f78

File tree

8 files changed

+1387
-1436
lines changed

8 files changed

+1387
-1436
lines changed

Diff for: .env-example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TEST=
2+
TEST1=

Diff for: bin/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,19 @@ program
7777

7878
program
7979
.command('restore-env')
80-
.description(`${chalk.yellow('RESTORE')} the .env file based on the latest changes in the version.json file.`)
80+
.description(`${chalk.yellow('RESTORE')} env file if u remove your env`)
8181
.action(async () => {
8282
const command: Command | null = factory.createCommand(CommandTypes.RESTORE_ENV)
8383
command !== null && CommandInvoker.executeCommands(command)
8484
})
8585

86+
program
87+
.command('generate')
88+
.description(`${chalk.yellow('GENERATE')} the .env-example file based on ypur env file.`)
89+
.alias('g')
90+
.action(async () => {
91+
const command: Command | null = factory.createCommand(CommandTypes.GENERATE_EXAMPLE_ENV)
92+
command !== null && CommandInvoker.executeCommands(command)
93+
})
94+
8695
program.parse(process.argv)

Diff for: lib/command/commandFactory.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import RevertCommand from './commandTypes/revertCommand'
77
import SyncCommand from './commandTypes/syncCommand'
88
import UpdateAllCommand from './commandTypes/updateAllCommand'
99
import UpdateCommand from './commandTypes/updateCommand'
10+
import GenerateCommand from './commandTypes/generateCommand'
1011

1112
export default class CommandFactory {
1213
createCommand (commandType: number, ...params: any []): Command | null {
@@ -25,6 +26,8 @@ export default class CommandFactory {
2526
return new RevertCommand(params)
2627
case CommandTypes.RESTORE_ENV:
2728
return new RestoreCommand(params)
29+
case CommandTypes.GENERATE_EXAMPLE_ENV:
30+
return new GenerateCommand(params)
2831

2932
default:
3033
return null

Diff for: lib/command/commandTypes/generateCommand.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Command from '../command'
2+
import { generateEnvExampleFile } from '../../handler/envHandler'
3+
import chalk from 'chalk'
4+
import { consola } from 'consola'
5+
6+
export default class RestoreCommand extends Command {
7+
protected async beforeExecute (): Promise<any> {
8+
const isConfirmed = await this.askForConfirmation()
9+
10+
if (!isConfirmed) {
11+
console.log(`Operation is ${chalk.red('cancelled!')}`)
12+
}
13+
}
14+
15+
protected async onExecute (beforeExecuteReturnValue: any): Promise<void> {
16+
const isSuccess = await generateEnvExampleFile()
17+
18+
isSuccess
19+
? consola.success('Env example was creating successfully. You are ready to go!')
20+
: consola.error('There was a problem generate .env-example file.')
21+
}
22+
}

Diff for: lib/const.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export enum CommandTypes {
55
COMPARE = 4,
66
UPDATE = 5,
77
REVERT = 6,
8-
RESTORE_ENV = 7
8+
RESTORE_ENV = 7,
9+
GENERATE_EXAMPLE_ENV = 8,
910
}

Diff for: lib/handler/envHandler.ts

+18
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,21 @@ export async function restoreEnvFile (): Promise<boolean> {
336336

337337
return true
338338
}
339+
340+
export async function generateEnvExampleFile (): Promise<boolean> {
341+
const currentDirectory = process.cwd()
342+
343+
const currentPathDoesContainEnvFile = await doesFileExist(path.join(currentDirectory, '.env'))
344+
345+
if (!currentPathDoesContainEnvFile) {
346+
return false
347+
}
348+
349+
const envValues = await getValuesInEnv({ targetPath: path.join(currentDirectory, '.env') })
350+
351+
const result = envValues.data.map(innerArr => innerArr[0] + '=').join('\n')
352+
353+
await writeFile(path.join(currentDirectory, '.env-example'), result)
354+
355+
return true
356+
}

0 commit comments

Comments
 (0)