UNPKG

@aminya/dotenv-vault

Version:

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

73 lines (72 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExampleService = void 0; const tslib_1 = require("tslib"); const fs_1 = require("fs"); const core_1 = require("@oclif/core"); const log_service_1 = require("./log-service"); const abort_service_1 = require("./abort-service"); const pull_service_1 = require("./pull-service"); const chalk_1 = tslib_1.__importDefault(require("chalk")); const dotenv = tslib_1.__importStar(require("dotenv")); class ExampleService { constructor(attrs = {}) { var _a; this.cmd = attrs.cmd; this.yes = attrs.yes; this.environment = (_a = attrs.environment) !== null && _a !== void 0 ? _a : 'production'; this.fileName = attrs.filename; this.log = new log_service_1.LogService({ cmd: attrs.cmd }); this.abort = new abort_service_1.AbortService({ cmd: attrs.cmd }); this.pull = new pull_service_1.PullService({ cmd: attrs.cmd, yes: this.yes, environment: this.environment, filename: undefined, dotenvMe: attrs.dotenvMe }); } async run() { var _a; core_1.CliUx.ux.action.start(`${chalk_1.default.dim(this.log.pretextRemote)}Pulling ${this.environment} environment`); const outputFile = (_a = this.fileName) !== null && _a !== void 0 ? _a : '.env.example'; // Check if output file exists if ((0, fs_1.existsSync)(outputFile) && !this.yes) { const overwrite = await core_1.CliUx.ux.confirm(`${outputFile} already exists. Overwrite?`); if (!overwrite) { this.log.plain('Operation cancelled.'); return; } } // Pull environment data let envData; try { // Get environment data without writing to file envData = await this.pull.run(false); } catch (error) { core_1.CliUx.ux.action.stop('failed'); this.log.remote(`Failed to pull ${this.environment} environment: ${error.message}`); return; } if (!envData) { core_1.CliUx.ux.action.stop('failed'); this.log.remote('No data received from pull service'); return; } // Get keys from the environment data const parsed = dotenv.parse(envData); const keys = Object.keys(parsed); // Generate the example content const exampleLines = [`# Example .env file generated from the '${this.environment}' environment`]; for (const key of keys) { exampleLines.push(`${key}=${key.toLowerCase()}`); } const exampleContent = exampleLines.join('\n'); // Write example file (0, fs_1.writeFileSync)(outputFile, exampleContent); this.log.plain(`Generated ${outputFile} successfully with keys from ${this.environment} environment.`); core_1.CliUx.ux.action.stop(); } } exports.ExampleService = ExampleService;