UNPKG

@geek-fun/serverlessinsight

Version:

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

91 lines (90 loc) 3.75 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 DEFAULT_IAC_FILES = [ 'serverlessinsight.yml', 'serverlessInsight.yml', 'ServerlessInsight.yml', 'serverless-insight.yml', ]; const getIacLocation = (location) => { const projectRoot = node_path_1.default.resolve(process.cwd()); const searchTargets = location ? [location] : DEFAULT_IAC_FILES; const attempted = new Set(); const toAbsolutePath = (target) => node_path_1.default.isAbsolute(target) ? target : node_path_1.default.resolve(projectRoot, target); const tryResolveCandidate = (target) => { const resolved = toAbsolutePath(target); attempted.add(resolved); if (!node_fs_1.default.existsSync(resolved)) { return undefined; } const stats = node_fs_1.default.statSync(resolved); if (stats.isDirectory()) { for (const fileName of DEFAULT_IAC_FILES) { const nested = node_path_1.default.join(resolved, fileName); attempted.add(nested); if (node_fs_1.default.existsSync(nested) && node_fs_1.default.statSync(nested).isFile()) { return nested; } } return undefined; } return resolved; }; for (const candidate of searchTargets) { const match = tryResolveCandidate(candidate); if (match) { return match; } } const attemptedList = Array.from(attempted) .map((n) => `'${n}'`) .join(', '); throw new Error(`No IaC file found. Tried: ${attemptedList}`); }; 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;