diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 2f996e1..d3c99f0 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -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', diff --git a/src/services/pull-service.ts b/src/services/pull-service.ts index 2d0c3c7..f76af4d 100644 --- a/src/services/pull-service.ts +++ b/src/services/pull-service.ts @@ -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' @@ -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 @@ -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 || {} } @@ -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') } @@ -216,8 +189,6 @@ class PullService { async _pull(): Promise { this.cmd.log('remote:') - this.cmd.log(`remote: Securely pulling ${this.environment} to ${this.smartFilename}`) - this.cmd.log('remote:') const options: AxiosRequestConfig = { method: 'POST', @@ -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('')