Skip to content

Commit ee7027c

Browse files
committed
feat: add template engine
1 parent d66d4a6 commit ee7027c

File tree

5 files changed

+142
-13
lines changed

5 files changed

+142
-13
lines changed

package-lock.json

Lines changed: 63 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@types/showdown": "^1.9.3",
2222
"dotenv": "^8.0.0",
23+
"handlebars": "^4.1.2",
2324
"node-fetch": "^2.6.0",
2425
"showdown": "^1.9.0"
2526
},

src/index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
<link rel=canonical href=https://codebreeze.fi/ >
99
<base href=/>
1010
<title>Codebreeze · Hike, Talk, Think, Connect</title>
11+
<meta name=gitRev content="{{gitRev}}">
1112
</head>
1213
<body>
1314
<main>
14-
{{content}}
15+
{{{content}}}
1516
</main>
17+
<footer>
18+
<section class="meta">
19+
<p>Generated on {{timestamp}}.</p>
20+
<p><a href="https://github.com/codefreezefi/codebreeze.fi/commit/{{gitRev}}">{{gitRev}}</a></p>
21+
</section>
22+
</footer>
1623
</body>
1724
</html>

src/render-website.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { config } from 'dotenv'
33
import * as showdown from 'showdown'
44
import { promises as fs } from 'fs'
55
import * as path from 'path'
6+
import { execSync } from 'child_process'
7+
import * as handlebars from 'handlebars'
68

79
config()
810

@@ -34,11 +36,16 @@ fetchContentFromTrello()
3436

3537
const tpl = await fs.readFile(path.join(srcDir, 'index.html'), 'utf-8')
3638
const targetFile = path.join(webDir, 'index.html')
37-
await fs.writeFile(
38-
targetFile,
39-
tpl.replace('{{content}}', contentAsMarkdown),
40-
'utf-8',
41-
)
39+
40+
const content = {
41+
content: contentAsMarkdown,
42+
gitRev: execSync('git rev-parse HEAD')
43+
.toString()
44+
.trim(),
45+
timestamp: new Date().toISOString(),
46+
} as const
47+
48+
await fs.writeFile(targetFile, handlebars.compile(tpl)(content), 'utf-8')
4249
console.log(`${targetFile} written`)
4350
})
4451
.then(() => {

src/trello/api.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import fetch from 'node-fetch'
1+
import fetch, { RequestInit } from 'node-fetch'
2+
import * as querystring from 'querystring'
23

34
export const trelloApi = ({
45
apiKey,
@@ -7,16 +8,68 @@ export const trelloApi = ({
78
apiKey: string
89
apiToken: string,
910
}) => {
10-
const query = ({ res }: { res: string }) =>
11+
const apiEndpoint = 'https://api.trello.com/1'
12+
13+
const f = ({
14+
res,
15+
query,
16+
options,
17+
}: {
18+
res: string
19+
query?: { [key: string]: any }
20+
options?: RequestInit,
21+
}) =>
1122
fetch(
12-
`https://api.trello.com/1/${res}?key=${apiKey}&token=${apiToken}`,
13-
).then(res => res.json())
23+
`${apiEndpoint}/${res}?${querystring.stringify({
24+
...query,
25+
key: apiKey,
26+
token: apiToken,
27+
})}`,
28+
options,
29+
)
30+
31+
const query = (args: { res: string }) => f(args).then(res => res.json())
32+
33+
const del = (args: { res: string }) =>
34+
f({
35+
...args,
36+
options: { method: 'DELETE' },
37+
})
38+
const post = (args: { res: string; query?: { [key: string]: any } }) =>
39+
f({
40+
...args,
41+
options: { method: 'POST' },
42+
})
43+
1444
return {
1545
lists: {
1646
cards: ({ list }: { list: string }) =>
1747
query({ res: `lists/${list}/cards` }) as Promise<
18-
{ name: string; desc: string }[]
48+
{ id: string; name: string; desc: string }[]
1949
>,
2050
},
51+
tokens: {
52+
token: ({ token }: { token: string }) => ({
53+
webhooks: () =>
54+
query({ res: `tokens/${token}/webhooks` }) as Promise<
55+
{ id: string }[]
56+
>,
57+
}),
58+
},
59+
webhook: ({ id }: { id: string }) => ({
60+
delete: () => del({ res: `webhook/${id}` }),
61+
}),
62+
webhooks: {
63+
create: (query: {
64+
description?: string
65+
callbackURL: string
66+
idModel: string
67+
active: boolean,
68+
}) =>
69+
post({
70+
res: 'webhooks',
71+
query,
72+
}),
73+
},
2174
}
2275
}

0 commit comments

Comments
 (0)