Skip to content

Commit

Permalink
[refactor] Refactoring on codes for reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Nov 14, 2023
1 parent f07f969 commit 2448ed1
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 212 deletions.
35 changes: 31 additions & 4 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import packages from '../package.json'
import {
getBaseFolder,
getEnvFilesRecursively
} from '../lib/file-operations'
} from '../lib/fileHandler'

import {
updateEnvFile,
Expand All @@ -18,14 +18,27 @@ import {
promptForEnvVariable,
getUniqueEnvNames,
restoreEnvFile
} from '../lib/env-operations'
} from '../lib/envHandler'

import {
getEnvVersions
} from '../lib/history-operations'
} from '../lib/historyHandler'

import { format } from 'date-fns'

async function askForConfirmation (): Promise<boolean> {
const answer = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmation',
message: 'Are you sure you want to perform this operation?',
default: false
}
])

return answer.confirmation
}

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

Expand Down Expand Up @@ -81,7 +94,7 @@ program

program
.command('update-all')
.description(`${chalk.yellow('UPDATE-ALL')} command is a handy utility for updating a specific environment variable across multiple service-specific .env files.`)
.description(`${chalk.yellow('UPDATE-ALL')} occurrences of a specific environment variable across multiple service-specific .env files.`)
.alias('ua')
.action(async () => {
const envOptions = await promptForEnvVariable()
Expand All @@ -106,6 +119,13 @@ program
}
])

const isConfirmed = await askForConfirmation()

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

const effectedServices = await updateAllEnvFile({ envValue, newValue })

effectedServices.forEach((service) => {
Expand Down Expand Up @@ -271,6 +291,13 @@ program
.command('restore-env')
.description(`${chalk.yellow('RESTORE')} the .env file based on the latest changes in the version.json file.`)
.action(async () => {
const isConfirmed = await askForConfirmation()

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

const isSuccess = await restoreEnvFile()

isSuccess
Expand Down
112 changes: 41 additions & 71 deletions lib/env-operations.ts → lib/envHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import {
deleteFile,
getEnvFiles,
doesFileExist
} from './file-operations'
} from './fileHandler'

import {
saveFieldVersion,
saveFieldVersionsInSync
} from './history-operations'
} from './historyHandler'

function getServiceNameFromUrl ({ targetPath }: { targetPath: string }): string {
const parts = targetPath.split('/')
Expand All @@ -32,21 +32,41 @@ function splitEnvLine (line: string): [string, string] {
return ['', '']
}

async function createEnvFile ({
serviceName,
content
function changeValuesInEnv ({
contents,
envValue,
newValue
}: {
serviceName: string
content: string
}): Promise<void> {
const serviceFolderPath = path.join(getBaseFolder(), serviceName)
await createFolderIfDoesNotExist(serviceFolderPath)
contents: string
envValue: string
newValue: string
}): string {
const lines = contents.split('\n')
const newLines = []

for (const line of lines) {
const parts = line.split('=')
if (parts[0] === envValue) {
newLines.push(`${envValue}=${newValue}`)
} else {
newLines.push(line)
}
}

const filePath = path.join(serviceFolderPath, '.env')
await writeFile({ file: filePath, newFileContents: content })
return newLines.join('\n')
}

async function updateEnvFile ({
async function createSymlink ({
targetPath
}: {
targetPath: string
}): Promise<string> {
const symlinkPath = path.join(process.cwd(), '.env')
await generateSymlink({ targetPath: path.join(targetPath), symlinkPath })
return symlinkPath
}

export async function updateEnvFile ({
file,
envValue,
newValue
Expand Down Expand Up @@ -84,7 +104,7 @@ async function updateEnvFile ({
}
}

async function updateAllEnvFile ({
export async function updateAllEnvFile ({
envValue,
newValue
}: {
Expand Down Expand Up @@ -115,17 +135,7 @@ async function updateAllEnvFile ({
return effectedServices
}

async function createSymlink ({
targetPath
}: {
targetPath: string
}): Promise<string> {
const symlinkPath = path.join(process.cwd(), '.env')
await generateSymlink({ targetPath: path.join(targetPath), symlinkPath })
return symlinkPath
}

async function getValuesInEnv ({
export async function getValuesInEnv ({
targetPath
}: {
targetPath: string
Expand All @@ -151,31 +161,7 @@ async function getValuesInEnv ({
}
}

function changeValuesInEnv ({
contents,
envValue,
newValue
}: {
contents: string
envValue: string
newValue: string
}): string {
const lines = contents.split('\n')
const newLines = []

for (const line of lines) {
const parts = line.split('=')
if (parts[0] === envValue) {
newLines.push(`${envValue}=${newValue}`)
} else {
newLines.push(line)
}
}

return newLines.join('\n')
}

async function compareEnvFiles ({
export async function compareEnvFiles ({
source,
destination
}: {
Expand Down Expand Up @@ -230,7 +216,7 @@ async function compareEnvFiles ({
}
}

async function syncEnvFile (): Promise<boolean> {
export async function syncEnvFile (): Promise<boolean> {
const currentDirectory = process.cwd()
const directoryName = currentDirectory.split('/').pop() ?? ''
const serviceFolderPath = path.join(getBaseFolder(), directoryName)
Expand All @@ -252,7 +238,7 @@ async function syncEnvFile (): Promise<boolean> {
return true
}

async function promptForEnvVariable (): Promise<string[]> {
export async function promptForEnvVariable (): Promise<string[]> {
const baseFolder = getBaseFolder()
const files = await getEnvFiles(baseFolder)

Expand All @@ -276,7 +262,7 @@ async function promptForEnvVariable (): Promise<string[]> {
return uniqueVariables
}

async function getUniqueEnvNames (targetFolder: string): Promise<string[]> {
export async function getUniqueEnvNames (targetFolder: string): Promise<string[]> {
const envNames = new Set<string>()

const fileContent = await readFile({ file: targetFolder })
Expand All @@ -295,7 +281,7 @@ async function getUniqueEnvNames (targetFolder: string): Promise<string[]> {
return uniqueEnvNames
}

async function getEnvValue (targetFolder: string, envName: string): Promise<string | undefined> {
export async function getEnvValue (targetFolder: string, envName: string): Promise<string | undefined> {
const fileContent = await readFile({ file: targetFolder })
if (fileContent != null) {
const sourceLines = fileContent.split('\n')
Expand All @@ -313,7 +299,7 @@ async function getEnvValue (targetFolder: string, envName: string): Promise<stri
return undefined
}

async function restoreEnvFile (): Promise<boolean> {
export async function restoreEnvFile (): Promise<boolean> {
const currentDirectory = process.cwd()
const directoryName = currentDirectory.split('/').pop() ?? ''
const serviceFolderPath = path.join(getBaseFolder(), directoryName)
Expand Down Expand Up @@ -360,19 +346,3 @@ async function restoreEnvFile (): Promise<boolean> {

return true
}

export {
createEnvFile,
updateEnvFile,
updateAllEnvFile,
createSymlink,
getValuesInEnv,
compareEnvFiles,
syncEnvFile,
promptForEnvVariable,
getServiceNameFromUrl,
splitEnvLine,
getUniqueEnvNames,
getEnvValue,
restoreEnvFile
}
129 changes: 0 additions & 129 deletions lib/file-operations.ts

This file was deleted.

Loading

0 comments on commit 2448ed1

Please sign in to comment.