Skip to content

Commit

Permalink
Merge pull request #10 from dotenv-org/custom-envs
Browse files Browse the repository at this point in the history
Add support for custom environments
  • Loading branch information
motdotla committed Apr 11, 2022
2 parents f0c8290 + 83606a5 commit 3aecb73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
2 changes: 0 additions & 2 deletions src/commands/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default class Pull extends Command {
required: false,
description: 'Pull .env.ci, .env.staging, and .env.production',
hidden: false,
default: 'development',
options: ['development', 'ci', 'staging', 'production'],
},
{
name: 'filename',
Expand Down
50 changes: 19 additions & 31 deletions src/services/pull-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as dotenv from 'dotenv'
import * as crypto from 'node:crypto'
import axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import {vars} from '../vars'
import {existsSync, writeFileSync, readFileSync} from 'node:fs'
import {existsSync, writeFileSync} from 'node:fs'
import {CliUx} from '@oclif/core'
import {AppendToGitignoreService} from '../services/append-to-gitignore-service'

Expand Down Expand Up @@ -50,18 +50,10 @@ class PullService {
return !(this.projectUid && this.projectUid.toString().length > 1)
}

get envContent(): string {
return readFileSync(this.smartFilename, 'utf8')
}

get emptyEnvMe(): boolean {
return !(this.meUid && this.meUid.toString().length > 1)
}

get existingEnv(): boolean {
return existsSync(this.smartFilename)
}

get existingEnvMe(): boolean {
if (this.dotenvMe) {
return true
Expand All @@ -70,19 +62,6 @@ class PullService {
return existsSync('.env.me')
}

get smartFilename(): string {
// if user has set a filename for output then use that
if (this.filename) {
return this.filename
}

if (this.environment === 'development') {
return '.env'
}

return `.env.${this.environment}`
}

get envProjectConfig(): any {
return dotenv.config({path: '.env.project'}).parsed || {}
}
Expand Down Expand Up @@ -145,12 +124,6 @@ class PullService {
this.cmd.log('You must have DOTENV_PROJECT set to some value in your .env.project file. Try deleting your .env.project file and running npx doten-vault new')
}

_logEmptyEnv(): void {
this.cmd.log('Aborted.')
this.cmd.log('')
this.cmd.log(`Your ${this.smartFilename} file is empty. Please populate it with value(s)`)
}

_logCheckingForEnvMe(): void {
this.cmd.log('local: Checking for .env.me')
}
Expand Down Expand Up @@ -216,8 +189,6 @@ class PullService {

async _pull(): Promise<void> {
this.cmd.log('remote:')
this.cmd.log(`remote: Securely pulling ${this.environment} to ${this.smartFilename}`)
this.cmd.log('remote:')

const options: AxiosRequestConfig = {
method: 'POST',
Expand All @@ -232,14 +203,31 @@ class PullService {

try {
const resp: AxiosResponse = await axios(options)
const environment = resp.data.data.environment
const envName = resp.data.data.envName
const newData = resp.data.data.dotenv
writeFileSync(this.smartFilename, newData)

const outputFilename = this._smartFilename(envName)

this.cmd.log(`remote: Securely pulling ${environment} to ${outputFilename}`)
this.cmd.log('remote:')

writeFileSync(outputFilename, newData)
this._logCompleted()
} catch (error) {
this._logError(error)
}
}

_smartFilename(envName: string): string {
// if user has set a filename for output then use that else use envName
if (this.filename) {
return this.filename
}

return envName
}

_logCompleted(): void {
this.cmd.log('Done.')
this.cmd.log('')
Expand Down

0 comments on commit 3aecb73

Please sign in to comment.