@goldstack/utils-aws-lambda
Version:
Utilities for deploying AWS Lambda functions
56 lines • 3.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildFunctions = exports.getOutFileForLambda = exports.getOutDirForLambda = void 0;
const utils_sh_1 = require("@goldstack/utils-sh");
const esbuild_1 = require("esbuild");
const path_1 = __importDefault(require("path"));
const generateFunctionName_1 = require("./generate/generateFunctionName");
const getOutDirForLambda = (packageRootDir, config) => {
if (config.path === '$default') {
return path_1.default.join(packageRootDir, `distLambda/${config.path}`);
}
if (config.path.endsWith('/')) {
return path_1.default.join(packageRootDir, `distLambda${config.path}index-lambda`);
}
return path_1.default.join(packageRootDir, `distLambda${config.path}-lambda`);
};
exports.getOutDirForLambda = getOutDirForLambda;
const getOutFileForLambda = (packageRootDir, config) => {
return path_1.default.join((0, exports.getOutDirForLambda)(packageRootDir, config), 'lambda.js');
};
exports.getOutFileForLambda = getOutFileForLambda;
const buildFunctions = async ({ routesDir, configs, deploymentName, buildOptions, lambdaNamePrefix, routeFilter, packageRootDir, }) => {
const buildConfig = (0, utils_sh_1.readToType)(path_1.default.join(packageRootDir, 'esbuild.config.json'));
// if we have a filter, we don't remove previous outputs
if (!routeFilter) {
await (0, utils_sh_1.rmSafe)(path_1.default.join(packageRootDir, 'distLambda'));
(0, utils_sh_1.mkdir)('-p', path_1.default.join(packageRootDir, 'distLambda/zips'));
}
for await (const config of configs) {
(0, utils_sh_1.mkdir)('-p', (0, exports.getOutDirForLambda)(packageRootDir, config));
const esbuildLocalPath = (0, utils_sh_1.changeExtension)(`${routesDir}/${config.relativeFilePath}`, '.esbuild.config.json');
const functionName = (0, generateFunctionName_1.generateFunctionName)(lambdaNamePrefix, config);
const localBuildConfig = (0, utils_sh_1.readToType)(esbuildLocalPath);
const onCSSGenerated = (css) => {
generatedCss.push(css);
};
const generatedCss = [];
const res = await (0, esbuild_1.build)({
...buildOptions({ onCSSGenerated, deploymentName }),
entryPoints: [`${routesDir}/${config.relativeFilePath}`],
outfile: (0, exports.getOutFileForLambda)(packageRootDir, config),
...buildConfig,
...localBuildConfig,
});
if (res.metafile) {
(0, utils_sh_1.write)(JSON.stringify(res.metafile), path_1.default.join(packageRootDir, `distLambda/zips/${functionName}.meta.json`));
}
// provide CSS for initial load
(0, utils_sh_1.write)(generatedCss.join('\n'), `${(0, exports.getOutDirForLambda)(packageRootDir, config)}/client.bundle.css`);
}
};
exports.buildFunctions = buildFunctions;
//# sourceMappingURL=buildFunctions.js.map