From 140c13d17cff835f09779bb1813cabc724ef80d1 Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Tue, 27 Feb 2024 17:20:53 +0100 Subject: [PATCH] fix(build): fix AWS Lambdas and GCP Functions builds --- packages/whook-aws-lambda/src/index.ts | 81 +++++++++------------- packages/whook-cors/README.md | 4 +- packages/whook-example/src/build.ts | 6 +- packages/whook-gcp-functions/src/index.ts | 83 ++++++++++------------- packages/whook/src/build.ts | 6 +- 5 files changed, 75 insertions(+), 105 deletions(-) diff --git a/packages/whook-aws-lambda/src/index.ts b/packages/whook-aws-lambda/src/index.ts index 7ab26760..2144d6eb 100644 --- a/packages/whook-aws-lambda/src/index.ts +++ b/packages/whook-aws-lambda/src/index.ts @@ -1,7 +1,8 @@ import { exit, stderr } from 'node:process'; -import fs from 'fs'; -import util from 'util'; -import path from 'path'; +import { readFile, writeFile } from 'node:fs/promises'; +import { promisify } from 'node:util'; +import { join, relative } from 'node:path'; +import { pathToFileURL } from 'node:url'; import { mkdirp } from 'mkdirp'; import cpr from 'cpr'; import { printStackTrace, YError } from 'yerror'; @@ -127,16 +128,7 @@ export type WhookAPIOperationAWSLambdaConfig< T extends Record = Record, > = WhookAWSLambdaBaseConfiguration & WhookAWSLambdaConfiguration; -const readFileAsync = util.promisify(fs.readFile) as ( - path: string, - encoding: string, -) => Promise; -const writeFileAsync = util.promisify(fs.writeFile) as ( - path: string, - content: string, - encoding: string, -) => Promise; -const cprAsync = util.promisify(cpr) as ( +const cprAsync = promisify(cpr) as ( source: string, destination: string, options: CprOptions, @@ -320,16 +312,13 @@ async function buildAnyLambda( log('warning', `🏗 - Building ${operationType} "${finalEntryPoint}"...`); - const lambdaPath = path.join( - PROJECT_DIR, - 'builds', - APP_ENV, - finalEntryPoint, - ); + const lambdaPath = join(PROJECT_DIR, 'builds', APP_ENV, finalEntryPoint); + const srcPath = join(PROJECT_DIR, 'src'); + const srcRelativePath = relative(lambdaPath, srcPath); - const initializerContent = await buildInitializer([ - `OPERATION_HANDLER_${finalEntryPoint}`, - ]); + const initializerContent = ( + await buildInitializer([`OPERATION_HANDLER_${finalEntryPoint}`]) + ).replaceAll(pathToFileURL(srcPath).toString(), srcRelativePath); const indexContent = await buildLambdaIndex( `OPERATION_HANDLER_${finalEntryPoint}`, ); @@ -341,12 +330,12 @@ async function buildAnyLambda( lambdaPath, whookConfig.staticFiles || [], ), - ensureFileAsync( + ensureFile( { log }, - path.join(lambdaPath, 'initialize.js'), + join(lambdaPath, 'initialize.js'), initializerContent, ), - ensureFileAsync({ log }, path.join(lambdaPath, 'main.js'), indexContent), + ensureFile({ log }, join(lambdaPath, 'main.js'), indexContent), ]); await buildFinalLambda({ compiler, log }, lambdaPath, whookConfig); } catch (err) { @@ -388,18 +377,12 @@ async function buildFinalLambda( ); await Promise.all([ - ensureFileAsync( - { log }, - path.join(lambdaPath, `/index${extension}`), - contents, - 'utf-8', - ), + ensureFile({ log }, join(lambdaPath, `/index${extension}`), contents), mappings - ? ensureFileAsync( + ? ensureFile( { log }, - path.join(lambdaPath, `/index${extension}.map`), + join(lambdaPath, `/index${extension}.map`), mappings, - 'utf-8', ) : Promise.resolve(), ]); @@ -415,8 +398,8 @@ async function copyStaticFiles( async (staticFile) => await copyFiles( { log }, - path.join(PROJECT_DIR, 'node_modules', staticFile), - path.join(lambdaPath, 'node_modules', staticFile), + join(PROJECT_DIR, 'node_modules', staticFile), + join(lambdaPath, 'node_modules', staticFile), ), ), ); @@ -427,13 +410,16 @@ async function copyFiles( source: string, destination: string, ): Promise { - let theError; + let theError: YError | undefined = undefined; + try { await mkdirp(destination); - const data = await readFileAsync(source, 'utf-8'); - await ensureFileAsync({ log }, destination, data, 'utf-8'); + + const data = await readFile(source); + + await ensureFile({ log }, destination, data.toString()); } catch (err) { - theError = err; + theError = err as YError; } if (theError) { if ('EISDIR' !== theError.code) { @@ -445,23 +431,22 @@ async function copyFiles( } } -async function ensureFileAsync( +async function ensureFile( { log }: { log: LogService }, path: string, content: string, - encoding = 'utf-8', ): Promise { try { - const oldContent = await readFileAsync(path, encoding); + const oldContent = (await readFile(path)).toString(); if (oldContent === content) { - log('debug', `Ignore unchanged file: "${path}".`); + log('debug', `🗀 - Ignore unchanged file: "${path}".`); return; } } catch (err) { - log('debug', `Write new file: "${path}".`); - return await writeFileAsync(path, content, encoding); + log('debug', `🗀 - Write new file: "${path}".`); + return await writeFile(path, content); } - log('debug', `Write changed file: "${path}".`); - return await writeFileAsync(path, content, encoding); + log('debug', `🗀 - Write changed file: "${path}".`); + return await writeFile(path, content); } diff --git a/packages/whook-cors/README.md b/packages/whook-cors/README.md index ae1ea2b2..a09e16a3 100644 --- a/packages/whook-cors/README.md +++ b/packages/whook-cors/README.md @@ -143,8 +143,8 @@ According to the kind of build you use, you may also declare it in your constant('INITIALIZER_PATH_MAP', { ...DEFAULT_BUILD_INITIALIZER_PATH_MAP, // MY_SERVICE: '@my/service_module_name', - jwtToken: 'jwt-service/dist/index', -+ errorHandler: '@whook/cors/dist/services/errorHandler', + jwtToken: 'jwt-service/dist/index.js', ++ errorHandler: '@whook/cors/dist/services/errorHandler.js', }), ); ``` diff --git a/packages/whook-example/src/build.ts b/packages/whook-example/src/build.ts index 1a1f219a..0e52d634 100644 --- a/packages/whook-example/src/build.ts +++ b/packages/whook-example/src/build.ts @@ -48,9 +48,9 @@ export async function prepareBuildEnvironment( $.register( constant('INITIALIZER_PATH_MAP', { ...DEFAULT_BUILD_INITIALIZER_PATH_MAP, - // MY_SERVICE: '@my/service_module_name', - jwtToken: 'jwt-service/dist/index', - errorHandler: '@whook/cors/dist/services/errorHandler', + // MY_SERVICE: '@my/service_module_name/afile.js', + jwtToken: 'jwt-service/dist/index.js', + errorHandler: '@whook/cors/dist/services/errorHandler.js', }), ); diff --git a/packages/whook-gcp-functions/src/index.ts b/packages/whook-gcp-functions/src/index.ts index 3f22e35e..3414bba5 100644 --- a/packages/whook-gcp-functions/src/index.ts +++ b/packages/whook-gcp-functions/src/index.ts @@ -1,8 +1,9 @@ -import fs from 'fs'; -import util from 'util'; -import path from 'path'; -import { mkdirp } from 'mkdirp'; import { argv, exit, stderr } from 'node:process'; +import { readFile, writeFile } from 'node:fs/promises'; +import { promisify } from 'node:util'; +import { join, relative } from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { mkdirp } from 'mkdirp'; import cpr from 'cpr'; import { printStackTrace, YError } from 'yerror'; import { Knifecycle, constant, initInitializerBuilder } from 'knifecycle'; @@ -48,16 +49,7 @@ export type WhookAPIOperationGCPFunctionConfig = { suffix?: string; }; -const readFileAsync = util.promisify(fs.readFile) as ( - path: string, - encoding: string, -) => Promise; -const writeFileAsync = util.promisify(fs.writeFile) as ( - path: string, - content: string, - encoding: string, -) => Promise; -const cprAsync = util.promisify(cpr) as ( +const cprAsync = promisify(cpr) as ( source: string, destination: string, options: CprOptions, @@ -240,16 +232,13 @@ async function buildAnyLambda( log('warning', `🏗 - Building ${operationType} "${finalEntryPoint}"...`); - const lambdaPath = path.join( - PROJECT_DIR, - 'builds', - APP_ENV, - finalEntryPoint, - ); + const lambdaPath = join(PROJECT_DIR, 'builds', APP_ENV, finalEntryPoint); + const srcPath = join(PROJECT_DIR, 'src'); + const srcRelativePath = relative(lambdaPath, srcPath); - const initializerContent = await buildInitializer([ - `OPERATION_HANDLER_${finalEntryPoint}`, - ]); + const initializerContent = ( + await buildInitializer([`OPERATION_HANDLER_${finalEntryPoint}`]) + ).replaceAll(pathToFileURL(srcPath).toString(), srcRelativePath); const indexContent = await buildLambdaIndex( `OPERATION_HANDLER_${finalEntryPoint}`, ); @@ -261,12 +250,12 @@ async function buildAnyLambda( lambdaPath, whookConfig.staticFiles || [], ), - ensureFileAsync( + ensureFile( { log }, - path.join(lambdaPath, 'initialize.js'), + join(lambdaPath, 'initialize.js'), initializerContent, ), - ensureFileAsync({ log }, path.join(lambdaPath, 'main.js'), indexContent), + ensureFile({ log }, join(lambdaPath, 'main.js'), indexContent), ]); await buildFinalLambda({ compiler, log }, lambdaPath, whookConfig); } catch (err) { @@ -301,18 +290,12 @@ async function buildFinalLambda( ); await Promise.all([ - ensureFileAsync( - { log }, - path.join(lambdaPath, `/index${extension}`), - contents, - 'utf-8', - ), + ensureFile({ log }, join(lambdaPath, `/index${extension}`), contents), mappings - ? ensureFileAsync( + ? ensureFile( { log }, - path.join(lambdaPath, `/index${extension}.map`), + join(lambdaPath, `/index${extension}.map`), mappings, - 'utf-8', ) : Promise.resolve(), ]); @@ -328,8 +311,8 @@ async function copyStaticFiles( async (staticFile) => await copyFiles( { log }, - path.join(PROJECT_DIR, 'node_modules', staticFile), - path.join(lambdaPath, 'node_modules', staticFile), + join(PROJECT_DIR, 'node_modules', staticFile), + join(lambdaPath, 'node_modules', staticFile), ), ), ); @@ -340,13 +323,16 @@ async function copyFiles( source: string, destination: string, ): Promise { - let theError; + let theError: YError | undefined = undefined; + try { await mkdirp(destination); - const data = await readFileAsync(source, 'utf-8'); - await ensureFileAsync({ log }, destination, data, 'utf-8'); + + const data = await readFile(source); + + await ensureFile({ log }, destination, data.toString()); } catch (err) { - theError = err; + theError = err as YError; } if (theError) { if ('EISDIR' !== theError.code) { @@ -358,23 +344,22 @@ async function copyFiles( } } -async function ensureFileAsync( +async function ensureFile( { log }: { log: LogService }, path: string, content: string, - encoding = 'utf-8', ): Promise { try { - const oldContent = await readFileAsync(path, encoding); + const oldContent = (await readFile(path)).toString(); if (oldContent === content) { - log('debug', `Ignore unchanged file: "${path}".`); + log('debug', `🗀 - Ignore unchanged file: "${path}".`); return; } } catch (err) { - log('debug', `Write new file: "${path}".`); - return await writeFileAsync(path, content, encoding); + log('debug', `🗀 - Write new file: "${path}".`); + return await writeFile(path, content); } - log('debug', `Write changed file: "${path}".`); - return await writeFileAsync(path, content, encoding); + log('debug', `🗀 - Write changed file: "${path}".`); + return await writeFile(path, content); } diff --git a/packages/whook/src/build.ts b/packages/whook/src/build.ts index ee03e182..85cbbb53 100644 --- a/packages/whook/src/build.ts +++ b/packages/whook/src/build.ts @@ -138,14 +138,14 @@ async function ensureFile( const oldContent = (await readFile(path)).toString(); if (oldContent === content) { - log('debug', `Ignore unchanged file: "${path}".`); + log('debug', `🗀 - Ignore unchanged file: "${path}".`); return; } } catch (err) { - log('debug', `Write new file: "${path}".`); + log('debug', `🗀 - Write new file: "${path}".`); return await writeFile(path, content); } - log('debug', `Write changed file: "${path}".`); + log('debug', `🗀 - Write changed file: "${path}".`); return await writeFile(path, content); }