From efd74cd2238f90d6f052920b8a7fb7967850f8ad Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Thu, 11 Apr 2024 16:51:03 +0200 Subject: [PATCH] fix(layer): make base dir configurable --- src/packLambdaFromPath.ts | 11 +++++++++-- src/packLayer.ts | 20 ++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/packLambdaFromPath.ts b/src/packLambdaFromPath.ts index 44f4695..02d8d3c 100644 --- a/src/packLambdaFromPath.ts +++ b/src/packLambdaFromPath.ts @@ -6,16 +6,23 @@ export const packLambdaFromPath = async ( id: string, sourceFile: string, handlerFunction = 'handler', + /** + * @default process.cwd() + */ baseDir = process.cwd(), + /** + * @default ${baseDir}/dist/lambdas + */ + distDir: string = path.join(process.cwd(), 'dist', 'lambdas'), ): Promise => { try { - await mkdir(path.join(process.cwd(), 'dist', 'lambdas'), { + await mkdir(distDir, { recursive: true, }) } catch { // Directory exists } - const zipFile = path.join(process.cwd(), 'dist', 'lambdas', `${id}.zip`) + const zipFile = path.join(distDir, `${id}.zip`) const { handler, hash } = await packLambda({ sourceFile: path.join(baseDir, sourceFile), zipFile, diff --git a/src/packLayer.ts b/src/packLayer.ts index b352c40..0a80bf0 100644 --- a/src/packLayer.ts +++ b/src/packLayer.ts @@ -12,17 +12,29 @@ export type PackedLayer = { layerZipFile: string; hash: string } export const packLayer = async ({ id, dependencies, + baseDir, + distDir, }: { id: string dependencies: string[] + /** + * @default process.cwd() + */ + baseDir?: string + /** + * @default ${baseDir}/dist/layers + */ + distDir?: string }): Promise => { - const packageJsonFile = path.join(process.cwd(), 'package.json') - const packageLockJsonFile = path.join(process.cwd(), 'package-lock.json') + const base = baseDir ?? process.cwd() + const dist = distDir ?? path.join(base, 'dist', 'layers') + const packageJsonFile = path.join(base, 'package.json') + const packageLockJsonFile = path.join(base, 'package-lock.json') const { dependencies: deps, devDependencies: devDeps } = JSON.parse( await readFile(packageJsonFile, 'utf-8'), ) - const layerDir = path.join(process.cwd(), 'dist', 'layers', id) + const layerDir = path.join(dist, id) const nodejsDir = path.join(layerDir, 'nodejs') try { @@ -91,7 +103,7 @@ export const packLayer = async ({ }) const zipFileName = await new Promise((resolve) => { - const zipFileName = path.join(process.cwd(), 'dist', 'layers', `${id}.zip`) + const zipFileName = path.join(base, 'dist', 'layers', `${id}.zip`) zipfile.outputStream .pipe(createWriteStream(zipFileName)) .on('close', () => {