Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ export interface <%= name.split('-').map(part => part.charAt(0).toUpperCase() +
description: string;
}

/**
* Entrypoint for the <%= name %> service
*
* Instantiate in a CDK Application Stage to deploy to AWS.
*
* Use 'resolve' helpers below when referencing lambda handlers, step function files etc.
*/
export class <%= name.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('') %>Stack extends Stack {
constructor(scope: Construct, id: typeof SERVICE_NAME | (string & {}), props?: <%= name.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('') %>StackProps) {
super(scope, id, props);

const STAGE = Stage.of(this)?.stageName;
if (!STAGE) {
throw new Error('This construct must be used within a CDK Stage');
}

Tags.of(this).add('SERVICE', id);
}
}

/**
* Resolves a path to infra assets relative to this stack
*
Expand All @@ -19,28 +39,27 @@ export function resolveAssetPath(assetPath: `${'infra/'}${string}`) {
}

/**
* Resolves a path to runtime assets relative to this stack
* Return an object with the default bundled code asset and
* handler property for use with the NodejsFunction construct.
*
* @param assetPath - The path to the asset.
* @returns The resolved bundled code path.
* @example
* ```ts
* new NodejsFunction(this, 'FetchData', {
* ...resolveLambdaHandler('runtime/handlers/fetch-data.ts'),
* });
* ```
*
* @param assetPath - The path to the typescript handler file.
* @returns The resolved bundled code path and handler name.
*/
export function resolveRuntimeAssetPath(assetPath: `${'runtime/'}${string}${'.ts'}`) {
const bundledPath = path.basename(assetPath).replace(path.extname(assetPath), '');
export function resolveLambdaHandler(assetPath: `${'runtime/handlers/'}${string}${'.ts'}`) {
// Replace 'runtime/handlers/' with '..dist/' and remove the file extension
const bundledPath = assetPath.replace(
/^runtime\/handlers\/(?<path>.*)\.ts$/,
'../dist/$<path>'
);
return {
code: Code.fromAsset(path.resolve(import.meta.dirname, '../dist', bundledPath)),
code: Code.fromAsset(path.resolve(import.meta.dirname, bundledPath)),
handler: 'index.handler',
};
}

export class <%= name.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('') %>Stack extends Stack {
constructor(scope: Construct, id: typeof SERVICE_NAME | (string & {}), props?: <%= name.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('') %>StackProps) {
super(scope, id, props);

const STAGE = Stage.of(this)?.stageName;
if (!STAGE) {
throw new Error('This construct must be used within a CDK Stage');
}

Tags.of(this).add('SERVICE', id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import { viteBaseConfig } from '../../vite.config.base.mjs';
const HANDLERS_PATH = 'src/runtime/handlers';

export default defineConfig(configEnv => {
const handlers = fg.sync(`${resolve(import.meta.dirname, HANDLERS_PATH)}/**/*.ts`);
const basePath = resolve(import.meta.dirname, HANDLERS_PATH);
const handlers = fg.sync(`${basePath}/**/*.ts`);
const input = Object.fromEntries(
handlers.map(handler => [basename(handler, extname(handler)), handler])
handlers.map(handler => {
const bundledPath = handler.replace(`${basePath}/`, '');
const entryName = bundledPath.replace(extname(bundledPath), '');
return [entryName, handler];
})
);

return mergeConfig(
Expand Down