diff --git a/cdk/backend.ts b/cdk/backend.ts index 6c9bd11..b11de4c 100644 --- a/cdk/backend.ts +++ b/cdk/backend.ts @@ -14,7 +14,7 @@ const packagesInLayer: string[] = [ ] const pack = async ( id: string, - handler = `${id}.handler`, + handlerFunction = 'handler', ): Promise => { try { await mkdir(path.join(process.cwd(), 'dist', 'lambdas'), { @@ -24,13 +24,13 @@ const pack = async ( // Directory exists } const zipFile = path.join(process.cwd(), 'dist', 'lambdas', `${id}.zip`) - await packLambda({ + const { handler } = await packLambda({ sourceFile: path.join(process.cwd(), 'lambda', `${id}.ts`), zipFile, }) return { lambdaZipFile: zipFile, - handler, + handler: handler.replace('.js', `.${handlerFunction}`), } } diff --git a/cdk/packLambda.ts b/cdk/packLambda.ts index 259e134..23d9af4 100644 --- a/cdk/packLambda.ts +++ b/cdk/packLambda.ts @@ -5,6 +5,21 @@ import * as yazl from 'yazl' import { commonParent } from './commonParent.js' import { findDependencies } from './findDependencies.js' +const removeCommonAncestor = + (parentDir: string) => + (file: string): string => { + const p = parse(file) + const jsFileName = [ + p.dir.replace(parentDir.slice(0, parentDir.length - 1), ''), + `${p.name}.js`, + ] + .join('/') + // Replace leading slash + .replace(/^\//, '') + + return jsFileName + } + /** * In the bundle we only include code that's not in the layer. */ @@ -18,12 +33,12 @@ export const packLambda = async ({ zipFile: string debug?: (label: string, info: string) => void progress?: (label: string, info: string) => void -}): Promise => { +}): Promise<{ handler: string }> => { const lambdaFiles = [sourceFile, ...findDependencies(sourceFile)] const zipfile = new yazl.ZipFile() - const parentDir = commonParent(lambdaFiles) + const stripCommon = removeCommonAncestor(commonParent(lambdaFiles)) for (const file of lambdaFiles) { const compiled = ( @@ -34,15 +49,7 @@ export const packLambda = async ({ }) ).code debug?.(`compiled`, compiled) - const p = parse(file) - const jsFileName = [ - p.dir.replace(parentDir.slice(0, parentDir.length - 1), ''), - `${p.name}.js`, - ] - .join('/') - // Replace leading slash - .replace(/^\//, '') - + const jsFileName = stripCommon(file) zipfile.addBuffer(Buffer.from(compiled, 'utf-8'), jsFileName) progress?.(`added`, jsFileName) } @@ -66,4 +73,6 @@ export const packLambda = async ({ zipfile.end() }) progress?.(`written`, zipFile) + + return { handler: stripCommon(sourceFile) } }