Skip to content

Commit

Permalink
fix template writing to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii committed Dec 13, 2023
1 parent ac9de36 commit 8b6a4a2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/lib/writeTemplate.ts → src/lib/write-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'fs';
* @param html The HTML to write
* @param css The CSS to write
*/
const writeTemplate = (template: string, html: string, css: string) => {
const writeTemplate = (
template: string,
type: 'ssr' | 'csr',
html: string,
css: string,
) => {
const outDir = `build-static/${template}`;

mkdirSync(outDir, { recursive: true });

writeFileSync(`${outDir}/index.html`, html, 'utf-8');
writeFileSync(`${outDir}/style.css`, css, 'utf-8');

const adJSON = `src/templates/csr/${template}/ad.json`;
const adJSON = `src/templates/${type}/${template}/ad.json`;

if (existsSync(adJSON)) {
copyFileSync(adJSON, `${outDir}/ad.json`);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/csr/[template].json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { marked } from 'marked';
import { getCommit } from '$lib/git';
import { build } from '$lib/rollup';
import { getProps } from '$lib/svelte';
import { writeTemplate } from '$lib/writeTemplate';
import { writeTemplate } from '$lib/write-template';

const github = 'https://github.com/guardian/commercial-templates/blob';

Expand Down Expand Up @@ -59,7 +59,7 @@ export const GET: RequestHandler = async ({ params }) => {
? marked.parse(readFileSync(`${dir}/README.md`, 'utf-8'))
: `<p><em>no description provided</em></p>`;

writeTemplate(template, html, css);
writeTemplate(template, 'csr', html, css);

return {
body: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/ssr/[template].json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { OutputAsset, OutputChunk } from 'rollup';
import { getCommit } from '$lib/git';
import { build } from '$lib/rollup';
import { getProps } from '$lib/svelte';
import { writeTemplate } from '$lib/writeTemplate';
import { writeTemplate } from '$lib/write-template';

type Output = {
html?: string;
Expand Down Expand Up @@ -100,7 +100,7 @@ export const GET: RequestHandler = async ({ params }) => {
? marked.parse(readFileSync(`${dir}/README.md`, 'utf-8'))
: `<p><em>no description provided</em></p>`;

writeTemplate(template, html, css);
writeTemplate(template, 'ssr', html, css);

return {
body: {
Expand Down

0 comments on commit 8b6a4a2

Please sign in to comment.