UNPKG

lemon-devkit

Version:

Lemon Serverless Micro-Service Platform for local development

182 lines 7.32 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 __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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncCredentials = exports.hasCredentials = 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 <steve@lemoncloud.io> * @date 2025-05-20 optimize for `lemon-core#v4` * * @copyright (C) lemoncloud.io 2025 - All Rights Reserved. */ const fs_1 = __importDefault(require("fs")); const yaml = __importStar(require("js-yaml")); const credential_providers_1 = require("@aws-sdk/credential-providers"); /** * 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; 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. const STAGE = (options === null || options === void 0 ? void 0 : options.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 !== null && val !== void 0 ? val : ''}`; // as string. } else { //* ignore!. } return $O; }, {}); //* make sure STAGE. $new.STAGE = $new.STAGE || STAGE; return Object.assign($det, $new); })($env); }; exports.loadEnviron = loadEnviron; /** * 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`) * @returns profile-name defined as `NAME` in environment (none is ignored). */ const loadProfile = ($proc, options) => { var _a; $proc = $proc === undefined ? process : $proc; const $info = (options === null || options === void 0 ? void 0 : options.info) === undefined ? console.info : options.info; const $env = (0, exports.loadEnviron)($proc); const NAME = (_a = $env === null || $env === void 0 ? void 0 : $env['NAME']) !== null && _a !== void 0 ? _a : ''; const PROFILE = `${NAME !== 'none' ? NAME : ''}`; // ignore 'none'. if (PROFILE && typeof $info === 'function') $info('! PROFILE =', PROFILE); return PROFILE; }; exports.loadProfile = loadProfile; /** * 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. * * @deprecated use `asyncCredentials()` instead. */ const credentials = (profile) => { if (!profile) return ''; throw new Error('WARN! credentials() is deprecated. use `asyncCredentials()` instead!'); }; exports.credentials = credentials; /** * return whether AWS credentials set * * @deprecated use `asyncCredentials` instead. */ const hasCredentials = () => { return false; }; exports.hasCredentials = hasCredentials; /** * dynamic loading credentials by profile. (search PROFILE -> NAME) * * @returns {Promise<CrendentialForAWS>} - AWS credentials */ const asyncCredentials = (profile) => __awaiter(void 0, void 0, void 0, function* () { const provider = (0, credential_providers_1.fromIni)({ profile }); const $res = yield provider(); return Object.assign(Object.assign({}, $res), { profile }); }); exports.asyncCredentials = asyncCredentials; //# sourceMappingURL=environ.js.map