Skip to content

Commit

Permalink
Added auto generated env-example file with generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Jan 23, 2024
1 parent 80a50c8 commit 3404f78
Show file tree
Hide file tree
Showing 8 changed files with 1,387 additions and 1,436 deletions.
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST=
TEST1=
11 changes: 10 additions & 1 deletion bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ program

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

program
.command('generate')
.description(`${chalk.yellow('GENERATE')} the .env-example file based on ypur env file.`)
.alias('g')
.action(async () => {
const command: Command | null = factory.createCommand(CommandTypes.GENERATE_EXAMPLE_ENV)
command !== null && CommandInvoker.executeCommands(command)
})

program.parse(process.argv)
3 changes: 3 additions & 0 deletions lib/command/commandFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RevertCommand from './commandTypes/revertCommand'
import SyncCommand from './commandTypes/syncCommand'
import UpdateAllCommand from './commandTypes/updateAllCommand'
import UpdateCommand from './commandTypes/updateCommand'
import GenerateCommand from './commandTypes/generateCommand'

export default class CommandFactory {
createCommand (commandType: number, ...params: any []): Command | null {
Expand All @@ -25,6 +26,8 @@ export default class CommandFactory {
return new RevertCommand(params)
case CommandTypes.RESTORE_ENV:
return new RestoreCommand(params)
case CommandTypes.GENERATE_EXAMPLE_ENV:
return new GenerateCommand(params)

default:
return null
Expand Down
22 changes: 22 additions & 0 deletions lib/command/commandTypes/generateCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Command from '../command'
import { generateEnvExampleFile } from '../../handler/envHandler'
import chalk from 'chalk'
import { consola } from 'consola'

export default class RestoreCommand extends Command {
protected async beforeExecute (): Promise<any> {
const isConfirmed = await this.askForConfirmation()

if (!isConfirmed) {
console.log(`Operation is ${chalk.red('cancelled!')}`)
}
}

protected async onExecute (beforeExecuteReturnValue: any): Promise<void> {
const isSuccess = await generateEnvExampleFile()

isSuccess
? consola.success('Env example was creating successfully. You are ready to go!')
: consola.error('There was a problem generate .env-example file.')
}
}
3 changes: 2 additions & 1 deletion lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export enum CommandTypes {
COMPARE = 4,
UPDATE = 5,
REVERT = 6,
RESTORE_ENV = 7
RESTORE_ENV = 7,
GENERATE_EXAMPLE_ENV = 8,
}
18 changes: 18 additions & 0 deletions lib/handler/envHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,21 @@ export async function restoreEnvFile (): Promise<boolean> {

return true
}

export async function generateEnvExampleFile (): Promise<boolean> {
const currentDirectory = process.cwd()

const currentPathDoesContainEnvFile = await doesFileExist(path.join(currentDirectory, '.env'))

if (!currentPathDoesContainEnvFile) {
return false
}

const envValues = await getValuesInEnv({ targetPath: path.join(currentDirectory, '.env') })

const result = envValues.data.map(innerArr => innerArr[0] + '=').join('\n')

await writeFile(path.join(currentDirectory, '.env-example'), result)

return true
}
Loading

0 comments on commit 3404f78

Please sign in to comment.