Skip to content

Commit

Permalink
feat: add template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 9, 2019
1 parent d66d4a6 commit ee7027c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 13 deletions.
65 changes: 63 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@types/showdown": "^1.9.3",
"dotenv": "^8.0.0",
"handlebars": "^4.1.2",
"node-fetch": "^2.6.0",
"showdown": "^1.9.0"
},
Expand Down
9 changes: 8 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@
<link rel=canonical href=https://codebreeze.fi/ >
<base href=/>
<title>Codebreeze · Hike, Talk, Think, Connect</title>
<meta name=gitRev content="{{gitRev}}">
</head>
<body>
<main>
{{content}}
{{{content}}}
</main>
<footer>
<section class="meta">
<p>Generated on {{timestamp}}.</p>
<p><a href="https://github.com/codefreezefi/codebreeze.fi/commit/{{gitRev}}">{{gitRev}}</a></p>
</section>
</footer>
</body>
</html>
17 changes: 12 additions & 5 deletions src/render-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { config } from 'dotenv'
import * as showdown from 'showdown'
import { promises as fs } from 'fs'
import * as path from 'path'
import { execSync } from 'child_process'
import * as handlebars from 'handlebars'

config()

Expand Down Expand Up @@ -34,11 +36,16 @@ fetchContentFromTrello()

const tpl = await fs.readFile(path.join(srcDir, 'index.html'), 'utf-8')
const targetFile = path.join(webDir, 'index.html')
await fs.writeFile(
targetFile,
tpl.replace('{{content}}', contentAsMarkdown),
'utf-8',
)

const content = {
content: contentAsMarkdown,
gitRev: execSync('git rev-parse HEAD')
.toString()
.trim(),
timestamp: new Date().toISOString(),
} as const

await fs.writeFile(targetFile, handlebars.compile(tpl)(content), 'utf-8')
console.log(`${targetFile} written`)
})
.then(() => {
Expand Down
63 changes: 58 additions & 5 deletions src/trello/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetch from 'node-fetch'
import fetch, { RequestInit } from 'node-fetch'
import * as querystring from 'querystring'

export const trelloApi = ({
apiKey,
Expand All @@ -7,16 +8,68 @@ export const trelloApi = ({
apiKey: string
apiToken: string,
}) => {
const query = ({ res }: { res: string }) =>
const apiEndpoint = 'https://api.trello.com/1'

const f = ({
res,
query,
options,
}: {
res: string
query?: { [key: string]: any }
options?: RequestInit,
}) =>
fetch(
`https://api.trello.com/1/${res}?key=${apiKey}&token=${apiToken}`,
).then(res => res.json())
`${apiEndpoint}/${res}?${querystring.stringify({
...query,
key: apiKey,
token: apiToken,
})}`,
options,
)

const query = (args: { res: string }) => f(args).then(res => res.json())

const del = (args: { res: string }) =>
f({
...args,
options: { method: 'DELETE' },
})
const post = (args: { res: string; query?: { [key: string]: any } }) =>
f({
...args,
options: { method: 'POST' },
})

return {
lists: {
cards: ({ list }: { list: string }) =>
query({ res: `lists/${list}/cards` }) as Promise<
{ name: string; desc: string }[]
{ id: string; name: string; desc: string }[]
>,
},
tokens: {
token: ({ token }: { token: string }) => ({
webhooks: () =>
query({ res: `tokens/${token}/webhooks` }) as Promise<
{ id: string }[]
>,
}),
},
webhook: ({ id }: { id: string }) => ({
delete: () => del({ res: `webhook/${id}` }),
}),
webhooks: {
create: (query: {
description?: string
callbackURL: string
idModel: string
active: boolean,
}) =>
post({
res: 'webhooks',
query,
}),
},
}
}

0 comments on commit ee7027c

Please sign in to comment.