UNPKG

@aminya/dotenv-vault

Version:

A secrets manager for .env files – from the same people that pioneered dotenv.

125 lines (124 loc) 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PullService = void 0; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const axios_1 = tslib_1.__importDefault(require("axios")); const vars_1 = require("../vars"); const fs_1 = require("fs"); const core_1 = require("@oclif/core"); const append_to_ignore_service_1 = require("../services/append-to-ignore-service"); const log_service_1 = require("../services/log-service"); const abort_service_1 = require("../services/abort-service"); const login_service_1 = require("../services/login-service"); class PullService { constructor(attrs = {}) { this.cmd = attrs.cmd; this.environment = attrs.environment; this.filename = attrs.filename; this.dotenvMe = attrs.dotenvMe; this.yes = attrs.yes; this.log = new log_service_1.LogService({ cmd: attrs.cmd }); this.abort = new abort_service_1.AbortService({ cmd: attrs.cmd }); this.login = new login_service_1.LoginService({ cmd: attrs.cmd, dotenvMe: null, yes: this.yes }); } async run(writeToFile = true) { new append_to_ignore_service_1.AppendToIgnoreService().run(); if (vars_1.vars.missingEnvVault) { this.abort.missingEnvVault(); } if (vars_1.vars.emptyEnvVault) { this.abort.emptyEnvVault(); } // special case for pulling example - no auth needed if (this.pullingExample) { return await this.pull(writeToFile); } if (vars_1.vars.missingEnvMe(this.dotenvMe)) { await this.login.login(false); } if (vars_1.vars.emptyEnvMe(this.dotenvMe)) { await this.login.login(false); } let pullingMsg = 'Securely pulling'; if (this.environment) { pullingMsg = `Securely pulling ${this.environment}`; } core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextRemote)}${pullingMsg}`); return await this.pull(writeToFile); } async pull(writeToFile) { const options = { method: 'POST', headers: { 'content-type': 'application/json' }, data: { environment: this.environment, DOTENV_VAULT: vars_1.vars.vaultValue, DOTENV_ME: this.meUid, }, url: this.url, }; try { const resp = await (0, axios_1.default)(options); const environment = resp.data.data.environment; const envName = resp.data.data.envName; const newData = resp.data.data.dotenv; const newVaultData = resp.data.data.dotenvVault; const outputFilename = this.displayFilename(envName); core_1.CliUx.ux.action.stop(); // backup current file to .previous if ((0, fs_1.existsSync)(outputFilename)) { (0, fs_1.renameSync)(outputFilename, `${outputFilename}.previous`); } // write to new current file if (!writeToFile) { return newData; } (0, fs_1.writeFileSync)(outputFilename, newData); this.log.remote(`Securely pulled ${environment} (${outputFilename})`); // write .env.vault file if (newVaultData) { (0, fs_1.writeFileSync)('.env.vault', newVaultData); this.log.remote('Securely built vault (.env.vault)'); } } catch (error) { core_1.CliUx.ux.action.stop('aborting'); let errorMessage = null; let errorCode = 'PULL_ERROR'; let suggestions = []; errorMessage = error; if (error.response) { errorMessage = error.response.data; if (error.response.data && error.response.data.errors && error.response.data.errors[0]) { const error1 = error.response.data.errors[0]; errorMessage = error1.message; if (error1.code) { errorCode = error1.code; } if (error1.suggestions) { suggestions = error1.suggestions; } } } this.abort.error(errorMessage, { code: errorCode, ref: '', suggestions: suggestions }); } } get url() { return vars_1.vars.apiUrl + '/pull'; } get meUid() { return this.dotenvMe || vars_1.vars.meValue; } get pullingExample() { return this.environment === 'example'; } displayFilename(envName) { // if user has set a filename for output then use that else use envName if (this.filename) { return this.filename; } return envName; } } exports.PullService = PullService;