Skip to content

Commit

Permalink
feat: fixed templates in test and bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
marnixdessing committed Oct 12, 2022
1 parent 265f3f9 commit 2e4c00b
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 90 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 3 additions & 36 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const project = new awscdk.AwsCdkTypeScriptApp({
'copyfiles',
'@playwright/test',
'aws-sdk-client-mock',
'jest-raw-loader',
], /* Build dependencies for this module. */
depsUpgradeOptions: {
workflowOptions: {
Expand All @@ -47,15 +48,17 @@ const project = new awscdk.AwsCdkTypeScriptApp({
jestOptions: {
jestConfig: {
setupFiles: ['dotenv/config'],
moduleFileExtensions: [
'js', 'json', 'jsx', 'ts', 'tsx', 'node', 'mustache',
],
transform: {
'\\.[jt]sx?$': 'ts-jest',
'^.+\\.mustache$': 'jest-raw-loader',
},
testPathIgnorePatterns: ['/node_modules/', '/cdk.out', '/test/playwright'],
roots: ['src', 'test'],
},
},
scripts: {
'install:login': 'copyfiles -f src/app/templates/* assets/app/login/login.lambda/templates && copyfiles -f src/app/templates/* src/app/login/templates',
'install:issue': 'copyfiles -f src/app/templates/* assets/app/issue/issue.lambda/templates && copyfiles -f src/app/templates/* src/app/issue/templates',
'install:logout': 'copyfiles -f src/app/templates/* assets/app/logout/logout.lambda/templates && copyfiles -f src/app/templates/* src/app/logout/templates',
},
eslintOptions: {
devdirs: ['src/app/logout/tests', '/test', '/build-tools'],
},
Expand All @@ -71,8 +74,11 @@ const project = new awscdk.AwsCdkTypeScriptApp({
});

// During bundling copy the templates to lambda deployment directories
project.tasks.tryFind('bundle:app/issue/issue.lambda').exec('npx projen install:issue');
project.tasks.tryFind('bundle:app/login/login.lambda').exec('npx projen install:login');
project.tasks.tryFind('bundle:app/logout/logout.lambda').exec('npx projen install:logout');
project.tasks.tryFind('bundle:app/issue/issue.lambda').reset();
project.tasks.tryFind('bundle:app/login/login.lambda').reset();
project.tasks.tryFind('bundle:app/logout/logout.lambda').reset();
project.tasks.tryFind('bundle:app/issue/issue.lambda').exec('esbuild --bundle src/app/issue/issue.lambda.ts --target=\"node14\" --platform=\"node\" --outfile=\"assets/app/issue/issue.lambda/index.js\" --tsconfig=\"tsconfig.dev.json\" --external:aws-sdk --loader:.mustache=text');
project.tasks.tryFind('bundle:app/login/login.lambda').exec('esbuild --bundle src/app/login/login.lambda.ts --target=\"node14\" --platform=\"node\" --outfile=\"assets/app/login/login.lambda/index.js\" --tsconfig=\"tsconfig.dev.json\" --external:aws-sdk --loader:.mustache=text');
project.tasks.tryFind('bundle:app/logout/logout.lambda').exec('esbuild --bundle src/app/logout/logout.lambda.ts --target=\"node14\" --platform=\"node\" --outfile=\"assets/app/logout/logout.lambda/index.js\" --tsconfig=\"tsconfig.dev.json\" --external:aws-sdk --loader:.mustache=text');

project.synth();
17 changes: 14 additions & 3 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions src/app/code/Render.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
import fs from 'fs/promises';
import Mustache from 'mustache';
import * as footer from '../templates/footer.mustache';
import * as header from '../templates/header.mustache';

/**
* Render data in a mustache template
*
* @param {object} data An object of data to use in the template
* @param {string} templatePath the path to the mustache template
* @param {string} template the main mustache template as a string
* @param {{[key:string] : string} | undefined} partials name and template string
* @returns string
*/
export default async (data: any, templatePath: string, partials: any) => {
const template = await fs.readFile(templatePath, 'utf8');
let partialTemplates: {[key: string]: string } = {};
if (partials) {
const keys = Object.keys(partials);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const partial = await fs.readFile(partials[key], 'utf8');
partialTemplates[key] = partial;
}
}
export default async (data: any, template: string, partials?: {[key: string]: string }) => {
const fullPartials = {
header: header.default,
footer: footer.default,
...partials,
};

data.name = 'IRMA'; // TODO later veranderen bij naam switch

return Mustache.render(template.toString(), data, partialTemplates);
return Mustache.render(template, data, fullPartials);
};
7 changes: 3 additions & 4 deletions src/app/issue/homeRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { ApiClient } from '@gemeentenijmegen/apiclient';
import { Session } from '@gemeentenijmegen/session';
import { IrmaApi } from '../code/IrmaApi';
import render from '../code/Render';
import * as template from '../templates/issue.mustache';
import { BrpApi } from './BrpApi';


function redirectResponse(location: string, code = 302) {
return {
statusCode: code,
Expand Down Expand Up @@ -47,10 +49,7 @@ async function handleLoggedinRequest(session: Session, brpClient: ApiClient, irm
};

// render page
const html = await render(data, __dirname + '/templates/issue.mustache', {
header: `${__dirname}/templates/header.mustache`,
footer: `${__dirname}/templates/footer.mustache`,
});
const html = await render(data, template.default);

return {
statusCode: 200,
Expand Down
7 changes: 3 additions & 4 deletions src/app/login/loginRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Session } from '@gemeentenijmegen/session';
import { OpenIDConnect } from '../code/OpenIDConnect';
import render from '../code/Render';

import * as template from '../templates/login.mustache';

function redirectResponse(location: string, status = 302) {
const response = {
statusCode: status,
Expand Down Expand Up @@ -43,10 +45,7 @@ export async function handleLoginRequest(cookies: string, dynamoDBClient: Dynamo
title: 'Inloggen',
authUrl: authUrl,
};
const html = await render(data, __dirname + '/templates/login.mustache', {
header: `${__dirname}/templates/header.mustache`,
footer: `${__dirname}/templates/footer.mustache`,
});
const html = await render(data, template.default);
const newCookies = [session.getCookie()];
return htmlResponse(html, newCookies);
}
7 changes: 3 additions & 4 deletions src/app/logout/handleLogoutRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Session } from '@gemeentenijmegen/session';
import * as cookie from 'cookie';
import render from '../code/Render';

import * as template from '../templates/logout.mustache';

function htmlResponse(body: string, cookies: string[] = []) {
const response = {
statusCode: 200,
Expand All @@ -23,10 +25,7 @@ export async function handleLogoutRequest(cookies: string, dynamoDBClient: Dynam
});
}

const html = await render({ title: 'Uitgelogd' }, __dirname + '/templates/logout.mustache', {
header: `${__dirname}/templates/header.mustache`,
footer: `${__dirname}/templates/footer.mustache`,
});
const html = await render({ title: 'Uitgelogd' }, template.default);
const emptyCookie = cookie.serialize('session', '', {
httpOnly: true,
secure: true,
Expand Down
14 changes: 0 additions & 14 deletions src/app/logout/templates/logout.mustache

This file was deleted.

6 changes: 3 additions & 3 deletions test/__snapshots__/main.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e4c00b

Please sign in to comment.