@routineless/cdk
Version:
40 lines • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLambdaCode = void 0;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const aws_lambda_1 = require("aws-cdk-lib/aws-lambda");
const callsite_1 = tslib_1.__importDefault(require("callsite"));
const path_1 = tslib_1.__importDefault(require("path"));
const workspace_1 = require("../utils/workspace");
/**
* This function will try to resolve lambda code for cdk lambda by provided context.
* If project name was provided it will define it by retrieving project build output path from project graph.
* If filePath was provided it will try to resolve project by provided path replacing '-infra' postfix to '-runtime'
* and retrieve project build output path from project graph.
* If lambda code context was not provided if will use calling file path for resolution.
*
* @param lambdaCodeContext object with project name or file path
* @returns {@link Code} object with lambda code
*/
const getLambdaCode = (lambdaCodeContext) => {
const projectGraph = (0, devkit_1.readCachedProjectGraph)();
let resolvedProjectName = lambdaCodeContext?.projectName ||
(0, workspace_1.getProjectName)(lambdaCodeContext?.filePath || (0, callsite_1.default)()[1].getFileName(), projectGraph);
if (!resolvedProjectName) {
throw new Error('Could resolve project name');
}
// construct runtime project name if project name was not provided
// and was resolved from execution context
if (!lambdaCodeContext?.filePath) {
resolvedProjectName = resolvedProjectName.replace('infra', 'runtime');
}
const projectNode = projectGraph.nodes[resolvedProjectName];
const outputPath = projectNode?.data.targets?.['build']?.options.outputPath;
if (!outputPath) {
throw new Error(`Could not resolve ${resolvedProjectName} build target output path`);
}
return aws_lambda_1.Code.fromAsset(path_1.default.join(devkit_1.workspaceRoot, outputPath));
};
exports.getLambdaCode = getLambdaCode;
//# sourceMappingURL=code.js.map