UNPKG

lemon-core

Version:
118 lines 4.49 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.credentials = 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 test_helper_1 = require("./common/test-helper"); /** * (internal) load module. */ const _load = (mod) => { var _a; try { return require(mod); } catch (e) { const error = `${(_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : (0, test_helper_1.GETERR)(e)}`.toLowerCase(); if (error.includes('cannot find module')) return null; throw e; } }; /** * (only for dev) 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. * * @deprecated use `loadEnviron()` from `lemon-devkit` instead. */ const loadEnviron = (process, options) => { const errScope = 'loadEnviron()'; const devkit = _load('lemon-devkit'); if (!(devkit === null || devkit === void 0 ? void 0 : devkit.loadEnviron)) throw new Error(`loadEnviron(function) is required (npm i -D lemon-devkit) - ${errScope}`); return devkit.loadEnviron(process, options); }; exports.loadEnviron = loadEnviron; /** * (only for dev) load AWS credential profile via `env.NAME` * * NOTE! ONLY FOR development purpose. * * ```sh * # load AWS 'lemon' profile, and run test. * $ NAME=lemon npm run test * ```` * @param $proc process (default `global.process`) * @param options (optional) parameters. * @returns {Promise<string>} - loaded profile name. */ const loadProfile = ($proc, options) => __awaiter(void 0, void 0, void 0, function* () { var _a; const errScope = 'loadProfile()'; const devkit = _load('lemon-devkit'); if (!(devkit === null || devkit === void 0 ? void 0 : devkit.loadProfile)) throw new Error(`loadProfile(function) is required (npm i -D lemon-devkit) - ${errScope}`); const $res = yield devkit.loadProfile($proc, options).catch((e) => { var _a; const error = `${(_a = e === null || e === void 0 ? void 0 : e.message) !== null && _a !== void 0 ? _a : (0, test_helper_1.GETERR)(e)}`.toLowerCase(); if (error.includes('could not resolve credentials') && error.includes('[default]')) return ''; throw e; }); return (_a = $res === null || $res === void 0 ? void 0 : $res.profile) !== null && _a !== void 0 ? _a : ''; }); exports.loadProfile = loadProfile; /** * (only for dev) dynamic loading credentials by profile. (search PROFILE -> NAME) * * @deprecated use `asyncCredentials` instead. */ const credentials = (profile) => { if (!profile) return; throw new Error('WARN! credentials() is deprecated. use `asyncCredentials()` instead!'); }; exports.credentials = credentials; //* export default. exports.default = exports.loadEnviron; //# sourceMappingURL=environ.js.map