UNPKG

@aminya/dotenv-vault

Version:

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

97 lines (96 loc) 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BuildService = 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 BuildService { constructor(attrs = {}) { this.cmd = attrs.cmd; 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() { new append_to_ignore_service_1.AppendToIgnoreService().run(); if (vars_1.vars.missingEnvVault) { this.abort.missingEnvVault(); } if (vars_1.vars.emptyEnvVault) { this.abort.emptyEnvVault(); } if (vars_1.vars.missingEnvMe(this.dotenvMe)) { await this.login.login(false); } if (vars_1.vars.emptyEnvMe(this.dotenvMe)) { await this.login.login(false); } const buildMsg = 'Securely building .env.vault'; core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextRemote)}${buildMsg}`); await this.build(); } async build() { const options = { method: 'POST', headers: { 'content-type': 'application/json' }, data: { DOTENV_VAULT: vars_1.vars.vaultValue, DOTENV_ME: this.meUid, }, url: this.url, }; try { const resp = await (0, axios_1.default)(options); const envName = resp.data.data.envName; const newData = resp.data.data.dotenv; core_1.CliUx.ux.action.stop(); // write to .env.vault (0, fs_1.writeFileSync)(envName, newData); this.log.remote('Securely built .env.vault'); this.log.plain(''); this.log.plain('Next:'); this.log.plain('1. Commit .env.vault to code'); this.log.plain('2. Set DOTENV_KEY on server'); this.log.plain('3. Deploy your code'); this.log.plain(''); this.log.plain(`(run ${chalk_1.default.bold(`${vars_1.vars.cli} keys`)} to view DOTENV_KEYs)`); } catch (error) { core_1.CliUx.ux.action.stop('aborting'); let errorMessage = null; let errorCode = 'BUILD_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 + '/build'; } get meUid() { return this.dotenvMe || vars_1.vars.meValue; } } exports.BuildService = BuildService;