diff --git a/lib/command/Command.ts b/lib/command/Command.ts index a0f8c14..b164cc4 100644 --- a/lib/command/Command.ts +++ b/lib/command/Command.ts @@ -8,9 +8,19 @@ export abstract class Command { this.baseFolder = getBaseFolder() } - abstract beforeExecute (): Promise - abstract execute (): Promise - async askForConfirmation (): Promise { + async execute (): Promise { + try { + const beforeExecuteReturnValue: any = await this.beforeExecute() + await this.onExecute(beforeExecuteReturnValue) + } catch (error) { + } + } + + protected abstract beforeExecute (): Promise + + protected abstract onExecute (value: any): Promise + + protected async askForConfirmation (): Promise { const answer = await inquirer.prompt([ { type: 'confirm', diff --git a/lib/command/commandTypes/CompareCommand.ts b/lib/command/commandTypes/CompareCommand.ts index d79f9db..37fcfd0 100644 --- a/lib/command/commandTypes/CompareCommand.ts +++ b/lib/command/commandTypes/CompareCommand.ts @@ -6,11 +6,12 @@ import chalk from 'chalk' import inquirer from 'inquirer' export class CompareCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const files: string [] = await getEnvFilesRecursively({ directory: this.baseFolder }) if (files.length < 2) { - throw new Error(`You must have a minimum of ${chalk.blue('2')} services registered to compare.`) + console.log(`You must have a minimum of ${chalk.blue('2')} services registered to compare.`) + return } const answers = await inquirer.prompt([ @@ -36,32 +37,28 @@ export class CompareCommand extends Command { return { source, destination } } - async execute (): Promise { - try { - const { source, destination } = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const { source, destination } = beforeExecuteReturnValue - 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()) - } - } catch (error) { - console.log(error.message) + if (differentVariables.length > 0) { + console.log(table.toString()) } } } diff --git a/lib/command/commandTypes/LsCommand.ts b/lib/command/commandTypes/LsCommand.ts index afd0f6b..5b9ab10 100644 --- a/lib/command/commandTypes/LsCommand.ts +++ b/lib/command/commandTypes/LsCommand.ts @@ -6,7 +6,7 @@ import chalk from 'chalk' import inquirer from 'inquirer' export class LsCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const files = await getEnvFilesRecursively({ directory: this.baseFolder }) if (files.length === 0) { @@ -23,26 +23,24 @@ export class LsCommand extends Command { return targetPath } - async execute (): Promise { - try { - const targetPath: string = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const targetPath: string = beforeExecuteReturnValue - 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 terminalWidth = process.stdout.columns - data.forEach(row => { - table.push(row) - }) + const table = new Table({ + head: ['ENV', 'VALUE'], + colWidths: [Math.floor(terminalWidth / 2 - 5), Math.floor(terminalWidth / 2 - 5)], + wrapOnWordBoundary: false, + wordWrap: true + }) - console.log(table.toString()) - } catch (error) { - console.log(error.message) - } + data.forEach(row => { + table.push(row) + }) + + console.log(table.toString()) } } diff --git a/lib/command/commandTypes/RestoreCommand.ts b/lib/command/commandTypes/RestoreCommand.ts index a93f6d7..7792129 100644 --- a/lib/command/commandTypes/RestoreCommand.ts +++ b/lib/command/commandTypes/RestoreCommand.ts @@ -3,7 +3,7 @@ import { restoreEnvFile } from '../../handler/envHandler' import chalk from 'chalk' export class RestoreCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const isConfirmed = await this.askForConfirmation() if (!isConfirmed) { @@ -11,9 +11,7 @@ export class RestoreCommand extends Command { } } - async execute (): Promise { - await this.beforeExecute() - + protected async onExecute (beforeExecuteReturnValue: any): Promise { const isSuccess = await restoreEnvFile() isSuccess diff --git a/lib/command/commandTypes/RevertCommand.ts b/lib/command/commandTypes/RevertCommand.ts index ea1bab4..4197984 100644 --- a/lib/command/commandTypes/RevertCommand.ts +++ b/lib/command/commandTypes/RevertCommand.ts @@ -7,7 +7,7 @@ import inquirer from 'inquirer' import { format } from 'date-fns' export class RevertCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const files = await getEnvFilesRecursively({ directory: this.baseFolder }) const { targetPath } = await inquirer.prompt({ @@ -53,8 +53,8 @@ export class RevertCommand extends Command { return { targetPath, envValue, newValue: version.changes[0].oldValue } } - async execute (): Promise { - const { targetPath, envValue, newValue } = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const { targetPath, envValue, newValue } = beforeExecuteReturnValue await updateEnvFile({ file: targetPath, envValue, newValue }) console.log(`Environment variables restored in "${chalk.blue(targetPath)}"`) diff --git a/lib/command/commandTypes/SyncCommand.ts b/lib/command/commandTypes/SyncCommand.ts index 9636a22..7541742 100644 --- a/lib/command/commandTypes/SyncCommand.ts +++ b/lib/command/commandTypes/SyncCommand.ts @@ -3,12 +3,12 @@ import { syncEnvFile } from '../../handler/envHandler' import chalk from 'chalk' export class SyncCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { return await syncEnvFile() } - async execute (): Promise { - const isSuccess: boolean = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const isSuccess: boolean = beforeExecuteReturnValue ;(isSuccess) ? console.log(`Synchronization was ${chalk.blue('successful')}. You are ready to go!`) diff --git a/lib/command/commandTypes/UpdateAllCommand.ts b/lib/command/commandTypes/UpdateAllCommand.ts index 6f03506..318e928 100644 --- a/lib/command/commandTypes/UpdateAllCommand.ts +++ b/lib/command/commandTypes/UpdateAllCommand.ts @@ -4,7 +4,7 @@ import chalk from 'chalk' import inquirer from 'inquirer' export class UpdateAllCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const envOptions = await promptForEnvVariable() const { envValue, newValue } = await inquirer.prompt([ @@ -37,8 +37,8 @@ export class UpdateAllCommand extends Command { return await updateAllEnvFile({ envValue, newValue }) } - async execute (): Promise { - const effectedServices: [] = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const effectedServices: [] = beforeExecuteReturnValue effectedServices.forEach((service) => { console.log(`Environment variables updated in "${chalk.blue(service)}"`) diff --git a/lib/command/commandTypes/UpdateCommand.ts b/lib/command/commandTypes/UpdateCommand.ts index feed3c8..1b6de69 100644 --- a/lib/command/commandTypes/UpdateCommand.ts +++ b/lib/command/commandTypes/UpdateCommand.ts @@ -5,7 +5,7 @@ import chalk from 'chalk' import inquirer from 'inquirer' export class UpdateCommand extends Command { - async beforeExecute (): Promise { + protected async beforeExecute (): Promise { const files = await getEnvFilesRecursively({ directory: this.baseFolder }) const { targetPath } = await inquirer.prompt({ @@ -40,8 +40,8 @@ export class UpdateCommand extends Command { return { targetPath, envValue, newValue } } - async execute (): Promise { - const { targetPath, envValue, newValue } = await this.beforeExecute() + protected async onExecute (beforeExecuteReturnValue: any): Promise { + const { targetPath, envValue, newValue } = beforeExecuteReturnValue await updateEnvFile({ file: targetPath, envValue, newValue }) console.log(`Environment variables updated in "${chalk.blue(targetPath)}"`) diff --git a/lib/handler/envHandler.ts b/lib/handler/envHandler.ts index e87b473..c48cd4c 100644 --- a/lib/handler/envHandler.ts +++ b/lib/handler/envHandler.ts @@ -22,7 +22,7 @@ function getServiceNameFromUrl ({ targetPath }: { targetPath: string }): string return parts[parts.length - 2] } -function splitEnvLine (line: string): [string, string] { +function extractEnvVariable (line: string): [string, string] { const indexOfFirstEqualSign = line.indexOf('=') if (indexOfFirstEqualSign >= 0) { const envName = line.substring(0, indexOfFirstEqualSign) @@ -83,7 +83,7 @@ export async function updateEnvFile ({ if (updatedFileContent !== undefined) { const updatedLines = updatedFileContent.split('\n').map(line => { // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [currentEnvName, currentEnvValue] = splitEnvLine(line) + const [currentEnvName, currentEnvValue] = extractEnvVariable(line) if (currentEnvName === envValue) { return `${currentEnvName}=${newValue}` } @@ -151,7 +151,7 @@ export async function getValuesInEnv ({ for (const line of lines) { if (line.trim() !== '') { - const [envName, envValue] = splitEnvLine(line) + const [envName, envValue] = extractEnvVariable(line) data.push([envName, envValue]) } } @@ -192,17 +192,17 @@ export async function compareEnvFiles ({ const differentVariables: string[][] = []; (sourceLines ?? []).forEach((sourceLine: string) => { - const sourceLineParts = splitEnvLine(sourceLine) + const sourceLineParts = extractEnvVariable(sourceLine) const variableName: string = sourceLineParts[0] const sourceValue: string = sourceLineParts[1] const matchingDestinationLine: string | undefined = (destinationLines ?? []).find((destinationLine) => { - const destinationLineParts = splitEnvLine(destinationLine) + const destinationLineParts = extractEnvVariable(destinationLine) return destinationLineParts[0] === variableName }) if (matchingDestinationLine != null) { - const destinationValue = splitEnvLine(matchingDestinationLine)[1] + const destinationValue = extractEnvVariable(matchingDestinationLine)[1] if (sourceValue !== destinationValue) { differentVariables.push([variableName, sourceValue, destinationValue]) } @@ -251,7 +251,7 @@ export async function promptForEnvVariable (): Promise { for (const line of sourceLines) { if (line.trim() !== '') { - const [envName] = splitEnvLine(line) + const [envName] = extractEnvVariable(line) variables.add(envName) } } @@ -271,7 +271,7 @@ export async function getUniqueEnvNames (targetFolder: string): Promise { const currentDirectory = process.cwd() const directoryName = currentDirectory.split('/').pop() ?? '' const serviceFolderPath = path.join(getBaseFolder(), directoryName) - const versionFilePath = path.join(serviceFolderPath, 'version.json') + const versionFilePath = path.join(serviceFolderPath, '.version.json') const versionFileContent = await readFile({ file: versionFilePath }) diff --git a/lib/handler/historyHandler.ts b/lib/handler/historyHandler.ts index 075ff4a..ad3a5fd 100644 --- a/lib/handler/historyHandler.ts +++ b/lib/handler/historyHandler.ts @@ -13,7 +13,7 @@ import { } from './envHandler' export async function saveFieldVersion (targetPath: string, fieldName: string, value: string): Promise { - const versionFilePath = path.join(path.dirname(targetPath), 'version.json') + const versionFilePath = path.join(path.dirname(targetPath), '.version.json') await createFileIfNotExists(versionFilePath) @@ -50,7 +50,7 @@ export async function saveFieldVersionsInSync (serviceFolderPath: string, envVal return } - const versionFilePath = path.join(serviceFolderPath, 'version.json') + const versionFilePath = path.join(serviceFolderPath, '.version.json') await createFileIfNotExists(versionFilePath) const versionFileContent = await readFile({ file: versionFilePath }) @@ -79,7 +79,7 @@ export async function saveFieldVersionsInSync (serviceFolderPath: string, envVal } export async function getEnvVersions (targetPath: string, fieldName: string): Promise { - const versionFilePath = path.join(path.dirname(targetPath), 'version.json') + const versionFilePath = path.join(path.dirname(targetPath), '.version.json') const versionFileContent = await readFile({ file: versionFilePath }) if (versionFileContent !== undefined) {