Skip to content

Commit

Permalink
[feat] Added new failed cases control for initial start
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkosk committed Nov 6, 2023
1 parent da0a8ee commit 8f8528e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
18 changes: 17 additions & 1 deletion bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ program
.action(async () => {
const files = await getFilesRecursively({ directory: getBaseFolder() })

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
}

const { targetPath } = await inquirer.prompt({
type: 'list',
name: 'targetPath',
Expand Down Expand Up @@ -61,7 +67,11 @@ 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 () => {
await syncEnvFile()
const isSuccess = await syncEnvFile()

isSuccess
? console.log(`Synchronization was ${chalk.blue('successful')}. You are ready to go!`)
: console.log(`There was a ${chalk.red('problem')} synchronizing . Make sure you are on the correct file path and that your file contains an .env file`)
})

program
Expand Down Expand Up @@ -101,6 +111,12 @@ program
.action(async () => {
const files: string [] = await getFilesRecursively({ directory: getBaseFolder() })

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

return
}

const answers = await inquirer.prompt([
{
type: 'list',
Expand Down
13 changes: 11 additions & 2 deletions lib/env-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
generateSymlink,
copyFile,
deleteFile,
getEnvFiles
getEnvFiles,
doesFileExist
} from './file-operations'

function getServiceNameFromUrl ({ targetPath }: { targetPath: string }): string {
Expand Down Expand Up @@ -195,16 +196,24 @@ async function compareEnvFiles ({
}
}

async function syncEnvFile (): Promise<void> {
async function syncEnvFile (): Promise<boolean> {
const currentDirectory = process.cwd()
const directoryName = currentDirectory.split('/').pop() ?? ''
const serviceFolderPath = path.join(getBaseFolder(), directoryName)

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

if (!currentPathDoesContainEnvFile) {
return false
}

await createFolderIfDoesNotExist(serviceFolderPath)

await copyFile(path.join(currentDirectory, '.env'), path.join(serviceFolderPath, '.env'))
await deleteFile(path.join(currentDirectory, '.env'))
await createSymlink({ targetPath: path.join(serviceFolderPath, '.env') })

return true
}

async function promptForEnvVariable (): Promise<string[]> {
Expand Down
16 changes: 15 additions & 1 deletion lib/file-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ async function getEnvFiles (baseFolder: string): Promise<string[]> {
return envFiles
}

async function doesFileExist (filePath: string): Promise<boolean> {
try {
await fs.promises.access(filePath, fs.constants.F_OK)
return true
} catch (err) {
if (err.code === 'ENOENT') {
return false
} else {
throw err
}
}
}

export {
getBaseFolder,
readFile,
Expand All @@ -107,5 +120,6 @@ export {
generateSymlink,
copyFile,
deleteFile,
getEnvFiles
getEnvFiles,
doesFileExist
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "envolve",
"version": "1.0.6",
"version": "1.0.7",
"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 Down

0 comments on commit 8f8528e

Please sign in to comment.