@geek-fun/serverlessinsight
Version:
Full life cycle cross providers serverless application management for your fast-growing business.
138 lines (137 loc) • 6.04 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatRosId = exports.calcValue = exports.calcRefs = exports.getFileSource = exports.readCodeSize = exports.resolveCode = void 0;
const node_path_1 = __importDefault(require("node:path"));
const node_fs_1 = __importDefault(require("node:fs"));
const ros = __importStar(require("@alicloud/ros-cdk-core"));
const ossDeployment = __importStar(require("@alicloud/ros-cdk-ossdeployment"));
const node_crypto_1 = __importDefault(require("node:crypto"));
const lodash_1 = require("lodash");
const resolveCode = (location) => {
const filePath = node_path_1.default.resolve(process.cwd(), location);
const fileContent = node_fs_1.default.readFileSync(filePath);
return fileContent.toString('base64');
};
exports.resolveCode = resolveCode;
const readCodeSize = (location) => {
const filePath = node_path_1.default.resolve(process.cwd(), location);
const stats = node_fs_1.default.statSync(filePath);
return stats.size;
};
exports.readCodeSize = readCodeSize;
const getFileSource = (fcName, location) => {
const filePath = node_path_1.default.resolve(process.cwd(), location);
if (node_fs_1.default.lstatSync(filePath).isDirectory()) {
throw new Error('The provided path is a directory, not a file.');
}
const hash = node_crypto_1.default.createHash('md5').update(node_fs_1.default.readFileSync(filePath)).digest('hex');
const objectKey = `${fcName}/${hash}-${filePath.split('/').pop()}`;
const source = ossDeployment.Source.asset(filePath, { deployTime: true }, `${fcName}/${hash}-`);
return { source, objectKey };
};
exports.getFileSource = getFileSource;
const calcRefs = (rawValue, ctx) => {
if (typeof rawValue === 'string') {
const containsStage = rawValue.match(/\$\{ctx.\w+}/);
const matchVar = rawValue.match(/^\$\{vars\.(\w+)}$/);
const containsVar = rawValue.match(/\$\{vars\.(\w+)}/);
const matchMap = rawValue.match(/^\$\{stages\.(\w+)}$/);
const containsMap = rawValue.match(/\$\{stages\.(\w+)}/);
const matchFn = rawValue.match(/^\$\{functions\.(\w+(\.\w+)?)}$/);
let value = rawValue;
if (containsStage) {
value = value.replace(/\$\{ctx.stage}/g, ctx.stage);
}
if (matchVar?.length) {
return ros.Fn.ref(matchVar[1]);
}
if (matchMap?.length) {
return ros.Fn.findInMap('stages', ctx.stage, matchMap[1]);
}
if (matchFn?.length) {
return ros.Fn.getAtt(matchFn[1], 'FunctionName');
}
if (containsMap?.length || containsVar?.length) {
value = ros.Fn.sub(rawValue.replace(/\$\{stages\.(\w+)}/g, '${$1}').replace(/\$\{vars\.(\w+)}/g, '${$1}'));
}
return value;
}
if (Array.isArray(rawValue)) {
return rawValue.map((item) => (0, exports.calcRefs)(item, ctx));
}
if (typeof rawValue === 'object' && rawValue !== null) {
return Object.fromEntries(Object.entries(rawValue).map(([key, val]) => [key, (0, exports.calcRefs)(val, ctx)]));
}
return rawValue;
};
exports.calcRefs = calcRefs;
const getParam = (key, records) => {
return records?.find((param) => param.key === key)?.value;
};
const calcValue = (rawValue, ctx) => {
const containsStage = rawValue.match(/\$\{ctx.stage}/);
const containsVar = rawValue.match(/\$\{vars.\w+}/);
const containsMap = rawValue.match(/\$\{stages\.(\w+)}/);
let value = rawValue;
if (containsStage?.length) {
value = rawValue.replace(/\$\{ctx.stage}/g, ctx.stage);
}
if (containsVar?.length) {
value = value.replace(/\$\{vars\.(\w+)}/g, (_, key) => getParam(key, ctx.parameters));
}
if (containsMap?.length) {
value = value.replace(/\$\{stages\.(\w+)}/g, (_, key) => getParam(key, (0, lodash_1.get)(ctx.stages, `${ctx.stage}`)));
}
return value;
};
exports.calcValue = calcValue;
const formatRosId = (id) => {
// Insert underscore before uppercase letters, but only when they follow a lowercase letter
let result = id.replace(/([a-z])([A-Z])/g, '$1_$2');
// Convert to lowercase
result = result.toLowerCase();
// Replace special characters with underscores
result = result.replace(/[/*,/#,-]/g, '_');
// Remove any number of underscores to single one
result = result.replace(/_+/g, '_');
// Remove leading underscores
result = result.replace(/^_/, '');
return result;
};
exports.formatRosId = formatRosId;