Skip to content

Commit

Permalink
tidy up writing files to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii committed Dec 13, 2023
1 parent e3349b0 commit aa0f386
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
6 changes: 3 additions & 3 deletions scripts/upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ It's written in python because Google does not offer a JS SDK.

## Running locally
```bash
$ cd scripts/upload-to-gam
$ cd scripts/upload
$ cp .env.example .env # fill this in
$ pip install -r requirements.txt
$ python upload.py
$ pipenv install
$ pipenv run python upload.py
```
25 changes: 25 additions & 0 deletions src/lib/writeTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { copyFileSync, existsSync, mkdirSync, writeFileSync } from 'fs';

/**
* Write the template to the build-static directory so that it can be uploaded to GAM
*
* @param template The name of the template
* @param html The HTML to write
* @param css The CSS to write
*/
const writeTemplate = (template: string, 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`;

if (existsSync(adJSON)) {
copyFileSync(adJSON, `${outDir}/ad.json`);
}
};

export { writeTemplate };
24 changes: 2 additions & 22 deletions src/routes/csr/[template].json.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
} from 'fs';
import { existsSync, readFileSync } from 'fs';
import type { RequestHandler } from '@sveltejs/kit/types';
import { marked } from 'marked';
import { getCommit } from '$lib/git';
import { build } from '$lib/rollup';
import { getProps } from '$lib/svelte';
import { writeTemplate } from '$lib/writeTemplate';

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

const writeTemplate = (template: string, 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`;

if (existsSync(adJSON)) {
copyFileSync(adJSON, `${outDir}/ad.json`);
}
};

export const GET: RequestHandler = async ({ params }) => {
const template = params.template ?? 'unknown';

Expand Down
24 changes: 2 additions & 22 deletions src/routes/ssr/[template].json.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {
copyFileSync,
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
} from 'fs';
import { existsSync, readFileSync } from 'fs';
import vm from 'vm';
import type { RequestHandler } from '@sveltejs/kit/types';
import { marked } from 'marked';
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';

type Output = {
html?: string;
Expand Down Expand Up @@ -46,21 +41,6 @@ const prerender = (code: string): Output => {
const isChunk = (output: OutputChunk | OutputAsset): output is OutputChunk =>
output.type === 'chunk';

const writeTemplate = (template: string, 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/ssr/${template}/ad.json`;

if (existsSync(adJSON)) {
copyFileSync(adJSON, `${outDir}/ad.json`);
}
};

export const GET: RequestHandler = async ({ params }) => {
const template = params.template ?? 'unknown';

Expand Down

0 comments on commit aa0f386

Please sign in to comment.