Skip to content

Commit

Permalink
fix(build): fix AWS Lambdas and GCP Functions builds
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Feb 27, 2024
1 parent 4600433 commit 140c13d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 105 deletions.
81 changes: 33 additions & 48 deletions packages/whook-aws-lambda/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -127,16 +128,7 @@ export type WhookAPIOperationAWSLambdaConfig<
T extends Record<string, unknown> = Record<string, unknown>,
> = WhookAWSLambdaBaseConfiguration & WhookAWSLambdaConfiguration<T>;

const readFileAsync = util.promisify(fs.readFile) as (
path: string,
encoding: string,
) => Promise<string>;
const writeFileAsync = util.promisify(fs.writeFile) as (
path: string,
content: string,
encoding: string,
) => Promise<void>;
const cprAsync = util.promisify(cpr) as (
const cprAsync = promisify(cpr) as (
source: string,
destination: string,
options: CprOptions,
Expand Down Expand Up @@ -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}`,
);
Expand All @@ -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) {
Expand Down Expand Up @@ -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(),
]);
Expand All @@ -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),
),
),
);
Expand All @@ -427,13 +410,16 @@ async function copyFiles(
source: string,
destination: string,
): Promise<void> {
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) {
Expand All @@ -445,23 +431,22 @@ async function copyFiles(
}
}

async function ensureFileAsync(
async function ensureFile(
{ log }: { log: LogService },
path: string,
content: string,
encoding = 'utf-8',
): Promise<void> {
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);
}
4 changes: 2 additions & 2 deletions packages/whook-cors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
);
```
Expand Down
6 changes: 3 additions & 3 deletions packages/whook-example/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export async function prepareBuildEnvironment<T extends Knifecycle>(
$.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',
}),
);

Expand Down
83 changes: 34 additions & 49 deletions packages/whook-gcp-functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -48,16 +49,7 @@ export type WhookAPIOperationGCPFunctionConfig = {
suffix?: string;
};

const readFileAsync = util.promisify(fs.readFile) as (
path: string,
encoding: string,
) => Promise<string>;
const writeFileAsync = util.promisify(fs.writeFile) as (
path: string,
content: string,
encoding: string,
) => Promise<void>;
const cprAsync = util.promisify(cpr) as (
const cprAsync = promisify(cpr) as (
source: string,
destination: string,
options: CprOptions,
Expand Down Expand Up @@ -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}`,
);
Expand All @@ -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) {
Expand Down Expand Up @@ -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(),
]);
Expand All @@ -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),
),
),
);
Expand All @@ -340,13 +323,16 @@ async function copyFiles(
source: string,
destination: string,
): Promise<void> {
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) {
Expand All @@ -358,23 +344,22 @@ async function copyFiles(
}
}

async function ensureFileAsync(
async function ensureFile(
{ log }: { log: LogService },
path: string,
content: string,
encoding = 'utf-8',
): Promise<void> {
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);
}
6 changes: 3 additions & 3 deletions packages/whook/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 140c13d

Please sign in to comment.