UNPKG

lemon-core

Version:
152 lines 5.67 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadProfile = exports.loadEnviron = void 0; /** * `environ.ts` * - override environ with `env/<profile>.yml` * - **NOTE** seperated file from index due to initialization sequence. * * usage (javascript): * ```js * const environ = require('lemon-core/dist/environ').default; * process.env = environ(process) * ``` * * usage (typescript): * ```ts * import environ from 'lemon-core/dist/environ'; * const $env = environ(process); * process.env = $env; * ``` * * @author Steve Jung <steve@lemoncloud.io> * @date 2019-08-09 initial typescript version. * @date 2019-11-26 cleanup and optimized for `lemon-core#v2` * * @copyright (C) 2019 LemonCloud Co Ltd. - All Rights Reserved. */ const fs_1 = __importDefault(require("fs")); const yaml = __importStar(require("js-yaml")); const aws_sdk_1 = __importDefault(require("aws-sdk")); /** * loader `<profile>.yml` * * **Determine Environ Target** * 1. ENV 로부터, 로딩할 `env.yml` 파일을 지정함. * 2. STAGE 로부터, `env.yml`내 로딩할 환경 그룹을 지정함. * * example: * `$ ENV=lemon STAGE=dev nodemon express.js --port 8081` * * @param process the main process instance. * @param options (optional) default option. */ const loadEnviron = (process, options) => { options = options || {}; const { ENV, ENV_PATH } = options; let { STAGE } = options; const $env = (process && process.env) || {}; const QUIET = 0 ? 0 : $env['LS'] === '1'; // LOG SILENT - PRINT NO LOG MESSAGE const PROFILE = ENV || $env['PROFILE'] || $env['ENV'] || 'none'; // Environment Profile Name. STAGE = STAGE || $env['STAGE'] || $env['NODE_ENV'] || 'local'; // Global STAGE/NODE_ENV For selecting. const _log = QUIET ? (...a) => { } : console.log; const isLocal = STAGE === 'local'; if (!isLocal) _log(`! PROFILE=${PROFILE} STAGE=${STAGE}`); //! initialize environment via 'env.yml' return ($det => { const file = PROFILE; const path = `${ENV_PATH || './env'}/` + file + (file.endsWith('.yml') ? '' : '.yml'); if (!fs_1.default.existsSync(path)) throw new Error('FILE NOT FOUND:' + path); if (!isLocal) _log(`! loading yml-file: "${path}"`); const $doc = yaml.load(fs_1.default.readFileSync(path, 'utf8')); const $src = ($doc && $doc[STAGE]) || {}; const $new = Object.keys($src).reduce(($O, key) => { const val = $src[key]; if (typeof val == 'string' && val.startsWith('!')) { //! force to update environ. $O[key] = val.substring(1); } else if (typeof val == 'object' && Array.isArray(val)) { //! join array with ', '. $O[key] = val.join(', '); } else if ($det[key] === undefined) { //! override only if undefined. $O[key] = `${val}`; // as string. } else { //! ignore!. } return $O; }, {}); //! make sure STAGE. $new.STAGE = $new.STAGE || STAGE; return Object.assign($det, $new); })($env); }; exports.loadEnviron = loadEnviron; /** * dynamic loading credentials by profile. (search PROFILE -> NAME) * !WARN! - could not catch AWS.Error `Profile null not found` via callback. * * @param profile profile name of AWS. */ const credentials = (profile) => { if (!profile) return ''; const credentials = new aws_sdk_1.default.SharedIniFileCredentials({ profile }); aws_sdk_1.default.config.credentials = credentials; return `${profile}`; }; /** * load AWS credential profile via env.NAME * * ```sh * # load AWS 'lemon' profile, and run test. * $ NAME=lemon npm run test * ```` * @param $proc process (default `global.process`) * @param $info info logger (default `console.info`) */ const loadProfile = ($proc, $info) => { $proc = $proc === undefined ? process : $proc; $info = $info === undefined ? console.info : $info; const $env = (0, exports.loadEnviron)($proc); const PROFILE = `${$env['NAME'] != 'none' ? $env['NAME'] || '' : ''}`; if (PROFILE && $info) $info('! PROFILE =', PROFILE); return credentials(PROFILE); }; exports.loadProfile = loadProfile; //! export default. exports.default = exports.loadEnviron; //# sourceMappingURL=environ.js.map