@geek-fun/serverlessinsight
Version:
Full life cycle cross providers serverless application management for your fast-growing business.
52 lines (51 loc) • 2.43 kB
JavaScript
;
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 providerEnum_1 = require("./providerEnum");
const node_async_hooks_1 = require("node:async_hooks");
const asyncLocalStorage = new node_async_hooks_1.AsyncLocalStorage();
const getIacLocation = (location) => {
const projectRoot = node_path_1.default.resolve(process.cwd());
return location
? node_path_1.default.resolve(projectRoot, location)
: node_path_1.default.resolve(projectRoot, 'serverlessinsight.yml') ||
node_path_1.default.resolve(projectRoot, 'serverlessInsight.yml') ||
node_path_1.default.resolve(projectRoot, 'ServerlessInsight.yml') ||
node_path_1.default.resolve(projectRoot, 'serverless-insight.yml');
};
exports.getIacLocation = getIacLocation;
const setContext = (config) => {
const context = {
stage: config.stage ?? 'default',
stackName: config.stackName ?? '',
provider: (config.provider ?? config.iacProvider?.name ?? providerEnum_1.ProviderEnum.ALIYUN),
region: config.region ??
config.iacProvider?.region ??
process.env.ROS_REGION_ID ??
process.env.ALIYUN_REGION ??
'cn-hangzhou',
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 })),
}), {}),
};
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;