Skip to content

Commit

Permalink
Merge pull request #8 from erdemkosk/issue-7-_feat_Need_to_add_auto-c…
Browse files Browse the repository at this point in the history
…omplate_on_update_all

[feat] Added auto-comp on update all side
  • Loading branch information
erdemkosk authored Nov 6, 2023
2 parents 61bc809 + 1d028ea commit f7c317d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
14 changes: 11 additions & 3 deletions bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from 'commander'
import inquirer from 'inquirer'
import inquirerPrompt from 'inquirer-autocomplete-prompt'
import chalk from 'chalk'
import { table } from 'table'
import packages from '../package.json'
Expand All @@ -16,10 +17,12 @@ import {
createSymlink,
getValuesInEnv,
compareEnvFiles,
syncEnvFile
syncEnvFile,
promptForEnvVariable
} from '../lib/env-operations'

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

program
.version(packages.version)
Expand Down Expand Up @@ -55,11 +58,16 @@ program
.description(`${chalk.yellow('UPDATE-ALL')} command is a handy utility for updating a specific environment variable across multiple service-specific .env files.`)
.alias('ua')
.action(async () => {
const oldValueOptions = await promptForEnvVariable()

const { oldValue, newValue } = await inquirer.prompt([
{
type: 'input',
type: 'autocomplete',
name: 'oldValue',
message: 'Enter the old value to change:'
message: 'Select the old value to change:',
source: (answers: any, input: string) => {
return oldValueOptions.filter(option => option.includes(input))
}
},
{
type: 'input',
Expand Down
36 changes: 31 additions & 5 deletions lib/env-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
writeFile,
generateSymlink,
copyFile,
deleteFile
deleteFile,
getEnvFiles
} from './file-operations'

async function createEnvFile ({ serviceName, content }: { serviceName: string, content: string }): Promise<void> {
Expand Down Expand Up @@ -73,9 +74,6 @@ async function getValuesInEnv ({ targetPath }: { targetPath: string }): Promise<
const directoryName: string = getServiceNameFromUrl({ targetPath })

const config = {
columnDefault: {
width: 50
},
header: {
alignment: 'center',
content: directoryName
Expand Down Expand Up @@ -159,12 +157,40 @@ async function syncEnvFile (): Promise<void> {
await createSymlink({ targetPath: path.join(serviceFolderPath, '.env') })
}

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

const variables = new Set<string>()

for (const file of files) {
const fileVariables = await readFile({ file })
if (fileVariables != null) {
const sourceLines: string[] = fileVariables.split('\n')

for (const line of sourceLines) {
if (line.trim() !== '') {
const parts: string[] = line.split('=')
if (parts.length === 2) {
variables.add(parts[0])
}
}
}
}
}
const uniqueVariables = Array.from(variables)
uniqueVariables.sort()

return uniqueVariables
}

export {
createEnvFile,
updateEnvFile,
updateAllEnvFile,
createSymlink,
getValuesInEnv,
compareEnvFiles,
syncEnvFile
syncEnvFile,
promptForEnvVariable
}
24 changes: 23 additions & 1 deletion lib/file-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ async function copyFile (sourcePath: string, destinationPath: string): Promise<v
})
}

async function getEnvFiles (baseFolder: string): Promise<string[]> {
const entries = await fs.promises.readdir(baseFolder, { withFileTypes: true })
const envFiles: string[] = []

for (const entry of entries) {
if (entry.isDirectory()) {
const folderPath = path.join(baseFolder, entry.name)
const envFilePath = path.join(folderPath, '.env')

try {
await readFile({ file: envFilePath })
envFiles.push(envFilePath)
} catch (error) {
continue
}
}
}

return envFiles
}

export {
getBaseFolder,
readFile,
Expand All @@ -78,5 +99,6 @@ export {
createFolderIfDoesNotExist,
generateSymlink,
copyFile,
deleteFile
deleteFile,
getEnvFiles
}
36 changes: 32 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "envolve",
"version": "1.0.1",
"version": "1.0.2",
"description": "Envolve CLI is a powerful tool for managing environment variables in your projects. It allows you to easily create, update, compare, and sync environment files across different services.",
"main": "index.ts",
"scripts": {
Expand All @@ -25,12 +25,14 @@
"chalk": "4.0.0",
"commander": "^11.1.0",
"inquirer": "^8.2.6",
"inquirer-autocomplete-prompt": "^2.0.1",
"table": "^6.8.1",
"zx": "^7.2.3"
},
"devDependencies": {
"@types/eslint": "^8.44.6",
"@types/inquirer": "^9.0.6",
"@types/inquirer-autocomplete-prompt": "^3.0.2",
"@types/jest": "^29.5.6",
"@types/node": "^20.8.9",
"@typescript-eslint/eslint-plugin": "^6.9.1",
Expand Down

0 comments on commit f7c317d

Please sign in to comment.