Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom environments #10

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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