Skip to content

Commit

Permalink
[refactor] Changed file structure and added factories
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Nov 18, 2023
1 parent eebb752 commit 8567948
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 429 deletions.
40 changes: 19 additions & 21 deletions bin/index.ts
Expand Up @@ -7,18 +7,16 @@ import packages from '../package.json'
import {
CommandInvoker,

Check failure on line 8 in bin/index.ts

View workflow job for this annotation

GitHub Actions / build

Module '"../lib/command"' has no exported member 'CommandInvoker'.
type Command,

Check failure on line 9 in bin/index.ts

View workflow job for this annotation

GitHub Actions / build

Module '"../lib/command"' has no exported member 'Command'.
LsCommand,
SyncCommand,
UpdateAllCommand,
CompareCommand,
UpdateCommand,
RevertCommand,
RestoreCommand
CommandFactory
} from '../lib/command'

import { CommandTypes } from '../lib/const'

const program = new CommanderCommand()
inquirer.registerPrompt('autocomplete', inquirerPrompt)

const factory: CommandFactory = new CommandFactory()

program
.version(packages.version)
.description(packages.description)
Expand All @@ -27,60 +25,60 @@ program
.command('ls')
.description(`${chalk.yellow('LIST')} environment variables in an .env file for a specific service. Select a service and view its environment variables.`)
.action(async () => {
const command: Command = new LsCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.LS)
command !== null && CommandInvoker.executeCommands(command)
})

program
.command('sync')
.description(`${chalk.yellow('SYNC')} backs up your current project's .env file, restores the variables from a global .env file, and creates a symbolic link to the latest environment settings.`)
.action(async () => {
const command: Command = new SyncCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.SYNC)
command !== null && CommandInvoker.executeCommands(command)
})

program
.command('update-all')
.description(`${chalk.yellow('UPDATE-ALL')} occurrences of a specific environment variable across multiple service-specific .env files.`)
.alias('ua')
.action(async () => {
const command: Command = new UpdateAllCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.UPDATE_ALL)
command !== null && CommandInvoker.executeCommands(command)
})

program
.command('compare')
.description(`${chalk.yellow('COMPARE')} command is a handy utility for differences in two different files with the same variable.`)
.alias('comp')
.action(async () => {
const command: Command = new CompareCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.COMPARE)
command !== null && CommandInvoker.executeCommands(command)
})

program
.command('update')
.description(`${chalk.yellow('UPDATE')} a single field in .env file and create a version.`)
.alias('u')
.action(async () => {
const command: Command = new UpdateCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.UPDATE)
command !== null && CommandInvoker.executeCommands(command)
})

program
.command('revert')
.description(`${chalk.yellow('REVERT')} a field in .env file to a specific version`)
.alias('r')
.action(async () => {
const command: Command = new RevertCommand()
CommandInvoker.executeCommands(command)
const command: Command | null = factory.createCommand(CommandTypes.REVERT)
command !== null && CommandInvoker.executeCommands(command)
})

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

program.parse(process.argv)
2 changes: 1 addition & 1 deletion lib/command/Command.ts
@@ -1,4 +1,4 @@
import { getBaseFolder } from '../fileHandler'
import { getBaseFolder } from '../handler/fileHandler'
import inquirer from 'inquirer'

export abstract class Command {
Expand Down
2 changes: 1 addition & 1 deletion lib/command/CommandInvoker.ts
@@ -1,4 +1,4 @@
import { type Command } from './Command'
import { type Command } from './command'

Check failure on line 1 in lib/command/CommandInvoker.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module './command' or its corresponding type declarations.

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class CommandInvoker {
Expand Down
35 changes: 35 additions & 0 deletions lib/command/commandFactory.ts
@@ -0,0 +1,35 @@
import { CommandTypes } from '../const'
import {
type Command,

Check failure on line 3 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'Command'.
LsCommand,

Check failure on line 4 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'LsCommand'.
SyncCommand,

Check failure on line 5 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'SyncCommand'.
UpdateAllCommand,

Check failure on line 6 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'UpdateAllCommand'.
CompareCommand,

Check failure on line 7 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'CompareCommand'.
UpdateCommand,

Check failure on line 8 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'UpdateCommand'.
RevertCommand,

Check failure on line 9 in lib/command/commandFactory.ts

View workflow job for this annotation

GitHub Actions / build

Module '"."' has no exported member 'RevertCommand'.
RestoreCommand
} from '.'

export class CommandFactory {
createCommand (commandType: number): Command | null {
switch (commandType) {
case CommandTypes.LS:
return new LsCommand()
case CommandTypes.SYNC:
return new SyncCommand()
case CommandTypes.COMPARE:
return new CompareCommand()
case CommandTypes.UPDATE:
return new UpdateCommand()
case CommandTypes.UPDATE_ALL:
return new UpdateAllCommand()
case CommandTypes.REVERT:
return new RevertCommand()
case CommandTypes.RESTORE_ENV:
return new RestoreCommand()

default:
return null
}
}
}
48 changes: 25 additions & 23 deletions lib/command/commandTypes/CompareCommand.ts
@@ -1,6 +1,6 @@
import { Command } from '../Command'
import { compareEnvFiles } from '../../envHandler'
import { getEnvFilesRecursively } from '../../fileHandler'
import { Command } from '../command'
import { compareEnvFiles } from '../../handler/envHandler'
import { getEnvFilesRecursively } from '../../handler/fileHandler'
import Table from 'cli-table3'
import chalk from 'chalk'
import inquirer from 'inquirer'
Expand All @@ -10,9 +10,7 @@ export class CompareCommand extends Command {
const files: string [] = await getEnvFilesRecursively({ directory: this.baseFolder })

if (files.length < 2) {
console.log(`You must have a minimum of ${chalk.blue('2')} services registered to compare.`)

return
throw new Error(`You must have a minimum of ${chalk.blue('2')} services registered to compare.`)
}

const answers = await inquirer.prompt([
Expand All @@ -39,27 +37,31 @@ export class CompareCommand extends Command {
}

async execute (): Promise<void> {
const { source, destination } = await this.beforeExecute()
try {
const { source, destination } = await this.beforeExecute()

const {
differentVariables,
sourceServiceName,
destinationServiceName
} = await compareEnvFiles({ source, destination })
const {
differentVariables,
sourceServiceName,
destinationServiceName
} = await compareEnvFiles({ source, destination })

const table = new Table({
head: ['VALUES', sourceServiceName, destinationServiceName],
wordWrap: true,
colWidths: [20, 30, 30],
wrapOnWordBoundary: false
})
const table = new Table({
head: ['VALUES', sourceServiceName, destinationServiceName],
wordWrap: true,
colWidths: [20, 30, 30],
wrapOnWordBoundary: false
})

differentVariables.forEach(row => {
table.push(row)
})
differentVariables.forEach(row => {
table.push(row)
})

if (differentVariables.length > 0) {
console.log(table.toString())
if (differentVariables.length > 0) {
console.log(table.toString())
}
} catch (error) {
console.log(error.message)
}
}
}
38 changes: 20 additions & 18 deletions lib/command/commandTypes/LsCommand.ts
@@ -1,6 +1,6 @@
import { Command } from '../Command'
import { getValuesInEnv } from '../../envHandler'
import { getEnvFilesRecursively } from '../../fileHandler'
import { Command } from '../command'
import { getValuesInEnv } from '../../handler/envHandler'
import { getEnvFilesRecursively } from '../../handler/fileHandler'
import Table from 'cli-table3'
import chalk from 'chalk'
import inquirer from 'inquirer'
Expand All @@ -10,9 +10,7 @@ export class LsCommand extends Command {
const files = await getEnvFilesRecursively({ directory: this.baseFolder })

if (files.length === 0) {
console.log(`You have not registered any service yet. Go to the file path of the request with your ${chalk.blue('.env')} file in it and run the ${chalk.blue('sync')} command.`)

return
throw new Error(`You have not registered any service yet. Go to the file path of the request with your ${chalk.blue('.env')} file in it and run the ${chalk.blue('sync')} command.`)
}

const { targetPath } = await inquirer.prompt({
Expand All @@ -26,21 +24,25 @@ export class LsCommand extends Command {
}

async execute (): Promise<void> {
const targetPath: string = await this.beforeExecute()
try {
const targetPath: string = await this.beforeExecute()

const { data } = await getValuesInEnv({ targetPath })
const { data } = await getValuesInEnv({ targetPath })

const table = new Table({
head: ['ENV', 'VALUE'],
colWidths: [20, 30],
wrapOnWordBoundary: false,
wordWrap: true
})
const table = new Table({
head: ['ENV', 'VALUE'],
colWidths: [20, 30],
wrapOnWordBoundary: false,
wordWrap: true
})

data.forEach(row => {
table.push(row)
})
data.forEach(row => {
table.push(row)
})

console.log(table.toString())
console.log(table.toString())
} catch (error) {
console.log(error.message)
}
}
}
4 changes: 2 additions & 2 deletions lib/command/commandTypes/RestoreCommand.ts
@@ -1,5 +1,5 @@
import { Command } from '../Command'
import { restoreEnvFile } from '../../envHandler'
import { Command } from '../command'
import { restoreEnvFile } from '../../handler/envHandler'
import chalk from 'chalk'

export class RestoreCommand extends Command {
Expand Down
8 changes: 4 additions & 4 deletions lib/command/commandTypes/RevertCommand.ts
@@ -1,7 +1,7 @@
import { Command } from '../Command'
import { updateEnvFile, getUniqueEnvNames } from '../../envHandler'
import { getEnvFilesRecursively } from '../../fileHandler'
import { getEnvVersions } from '../../historyHandler'
import { Command } from '../command'
import { updateEnvFile, getUniqueEnvNames } from '../../handler/envHandler'
import { getEnvFilesRecursively } from '../../handler/fileHandler'
import { getEnvVersions } from '../../handler/historyHandler'
import chalk from 'chalk'
import inquirer from 'inquirer'
import { format } from 'date-fns'
Expand Down
4 changes: 2 additions & 2 deletions lib/command/commandTypes/SyncCommand.ts
@@ -1,5 +1,5 @@
import { Command } from '../Command'
import { syncEnvFile } from '../../envHandler'
import { Command } from '../command'
import { syncEnvFile } from '../../handler/envHandler'
import chalk from 'chalk'

export class SyncCommand extends Command {
Expand Down
4 changes: 2 additions & 2 deletions lib/command/commandTypes/UpdateAllCommand.ts
@@ -1,5 +1,5 @@
import { Command } from '../Command'
import { updateAllEnvFile, promptForEnvVariable } from '../../envHandler'
import { Command } from '../command'
import { updateAllEnvFile, promptForEnvVariable } from '../../handler/envHandler'
import chalk from 'chalk'
import inquirer from 'inquirer'

Expand Down
6 changes: 3 additions & 3 deletions lib/command/commandTypes/UpdateCommand.ts
@@ -1,6 +1,6 @@
import { Command } from '../Command'
import { getUniqueEnvNames, updateEnvFile } from '../../envHandler'
import { getEnvFilesRecursively } from '../../fileHandler'
import { Command } from '../command'
import { getUniqueEnvNames, updateEnvFile } from '../../handler/envHandler'
import { getEnvFilesRecursively } from '../../handler/fileHandler'
import chalk from 'chalk'
import inquirer from 'inquirer'

Expand Down
19 changes: 10 additions & 9 deletions lib/command/index.ts
@@ -1,9 +1,10 @@
export * from './commandTypes/LsCommand'
export * from './commandTypes/SyncCommand'
export * from './commandTypes/UpdateAllCommand'
export * from './commandTypes/UpdateCommand'
export * from './commandTypes/CompareCommand'
export * from './commandTypes/RevertCommand'
export * from './commandTypes/RestoreCommand'
export * from './CommandInvoker'
export * from './Command'
export * from './commandTypes/lsCommand'
export * from './commandTypes/syncCommand'
export * from './commandTypes/updateAllCommand'
export * from './commandTypes/updateCommand'
export * from './commandTypes/compareCommand'
export * from './commandTypes/revertCommand'
export * from './commandTypes/restoreCommand'
export * from './commandInvoker'
export * from './command'
export * from './commandFactory'
9 changes: 9 additions & 0 deletions lib/const.ts
@@ -0,0 +1,9 @@
export enum CommandTypes {
LS = 1,
SYNC = 2,
UPDATE_ALL = 3,
COMPARE = 4,
UPDATE = 5,
REVERT = 6,
RESTORE_ENV = 7
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/historyHandler.ts → lib/handler/historyHandler.ts
@@ -1,6 +1,6 @@
import * as path from 'path'

import { type IEnvVersion } from './interfaces/env-version'
import { type IEnvVersion } from '../interfaces/env-version'

import {
createFileIfNotExists,
Expand Down

0 comments on commit 8567948

Please sign in to comment.