UNPKG

@geek-fun/serverlessinsight

Version:

Full life cycle cross providers serverless application management for your fast-growing business.

73 lines (72 loc) 3.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getContext = exports.setContext = exports.getIacLocation = void 0; const node_path_1 = __importDefault(require("node:path")); const node_fs_1 = __importDefault(require("node:fs")); const providerEnum_1 = require("./providerEnum"); const node_async_hooks_1 = require("node:async_hooks"); const imsClient_1 = require("./imsClient"); const asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage(); const getIacLocation = (location) => { const projectRoot = node_path_1.default.resolve(process.cwd()); if (location) { const candidate = node_path_1.default.isAbsolute(location) ? location : node_path_1.default.resolve(projectRoot, location); if (node_fs_1.default.existsSync(candidate)) { return candidate; } throw new Error(`IaC file not found at '${candidate}'`); } const candidates = [ 'serverlessinsight.yml', 'serverlessInsight.yml', 'ServerlessInsight.yml', 'serverless-insight.yml', ]; for (const name of candidates) { const candidate = node_path_1.default.resolve(projectRoot, name); if (node_fs_1.default.existsSync(candidate)) { return candidate; } } throw new Error(`No IaC file found. Tried: ${candidates.map((n) => `'${node_path_1.default.resolve(projectRoot, n)}'`).join(', ')}`); }; exports.getIacLocation = getIacLocation; const setContext = async (config, reaValToken = false) => { const region = config.region ?? config.iacProvider?.region ?? process.env.ROS_REGION_ID ?? process.env.ALIYUN_REGION ?? 'cn-hangzhou'; const context = { stage: config.stage ?? 'default', stackName: config.stackName ?? '', provider: (config.provider ?? config.iacProvider?.name ?? providerEnum_1.ProviderEnum.ALIYUN), region, accessKeyId: config.accessKeyId ?? process.env.ALIYUN_ACCESS_KEY_ID, accessKeySecret: config.accessKeySecret ?? process.env.ALIYUN_ACCESS_KEY_SECRET, securityToken: config.securityToken ?? process.env.ALIYUN_SECURITY_TOKEN, iacLocation: (0, exports.getIacLocation)(config.location), parameters: Object.entries(config.parameters ?? {}).map(([key, value]) => ({ key, value })), stages: Object.entries(config.stages ?? {}).reduce((acc, [stage, parameters]) => ({ ...acc, [stage]: Object.entries(parameters).map(([key, value]) => ({ key, value })), }), {}), }; if (reaValToken) { const iamInfo = await (0, imsClient_1.getIamInfo)(context); context.accountId = iamInfo?.accountId; } asyncLocalStorage.enterWith(context); }; exports.setContext = setContext; const getContext = () => { const context = asyncLocalStorage.getStore(); if (!context) { throw new Error('No context found'); } return context; }; exports.getContext = getContext;