@aminya/dotenv-vault
Version:
A secrets manager for .env files – from the same people that pioneered dotenv.
104 lines (103 loc) • 4.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeysService = 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 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 KeysService {
constructor(attrs = {}) {
this.cmd = attrs.cmd;
this.environment = attrs.environment;
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 keysMsg = 'Listing .env.vault decryption keys';
core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextRemote)}${keysMsg}`);
await this.keys();
}
async keys() {
const options = {
method: 'POST',
headers: { 'content-type': 'application/json' },
data: {
DOTENV_VAULT: vars_1.vars.vaultValue,
DOTENV_ME: this.meUid,
environment: this.environment,
},
url: this.url,
};
try {
const resp = await (0, axios_1.default)(options);
const keys = resp.data.data.keys;
core_1.CliUx.ux.action.stop();
if (this.environment && keys[0]) {
// if environment was passed and key exists then no truncation
this.log.plain(keys[0].key);
}
else {
// note that table truncates on smaller terminal windows
core_1.CliUx.ux.table(keys, {
environment: {
header: 'environment',
},
key: {
header: 'DOTENV_KEY',
},
});
this.log.plain('');
this.log.plain(`Set ${chalk_1.default.bold('DOTENV_KEY')} on your server`);
}
}
catch (error) {
core_1.CliUx.ux.action.stop('aborting');
let errorMessage = null;
let errorCode = 'KEYS_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 + '/keys';
}
get meUid() {
return this.dotenvMe || vars_1.vars.meValue;
}
}
exports.KeysService = KeysService;