-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.ts
74 lines (72 loc) · 2.29 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {
basename,
join,
resolve,
} from "https://deno.land/std@0.146.0/path/mod.ts";
import {
compile,
renderAsync,
templates,
} from "https://deno.land/x/eta@v1.12.3/mod.ts";
import { getBranch, getCommit, getRepository, getTag } from "./git.ts";
/**
* Template rendering function.
* Uses Eta under the hood.
*
* ```ts
* import { render } from "./render.ts"
* const output = await render("/path/to/.README.template.md", "/path/to/", "# my-project", "/path/to/templates/", (errorMsg: string, error: Error) => {})
* ```
*
* @param {string} readmeFilePath The path of the readme we want to compile.
* @param {string} readmeDirectoryPath The directory where the readme resides.
* @param {string} readmeTemplate The contents of the readme we are compiling.
* @param {string} templatesPath The directory where the templates used for rendering are.
* @param {function(string, Error)} error Handler for errors.
*/
export const render = async (
readmeFilePath: string,
readmeDirectoryPath: string,
readmeTemplate: string,
templatesPath: string,
error: (message: string, error?: Error) => void,
): Promise<string | void> => {
for await (const template of Deno.readDir(templatesPath)) {
if (
template.isFile && template.name.endsWith(".template.md") &&
!template.name.endsWith("README.template.md") &&
!template.name.startsWith(".")
) {
templates.define(
template.name,
compile(
await Deno.readTextFile(resolve(join(templatesPath, template.name))),
),
);
}
}
try {
return renderAsync(
await Deno.readTextFile(
resolve(join(templatesPath, "README.template.md")),
),
{
name: basename(readmeDirectoryPath),
readmeFilePath,
readmeDirectoryPath,
readme: readmeTemplate,
gitURL: (await getRepository(readmeDirectoryPath, error)).trim(),
gitBranch: (await getBranch(readmeDirectoryPath, error)).trim(),
gitTag: (await getTag(readmeDirectoryPath, error)).trim(),
gitCommit: (await getCommit(readmeDirectoryPath, error)).trim(),
},
{
async: true,
autoEscape: false,
autoTrim: false,
},
) as Promise<string>;
} catch (e) {
error(`Something went wrong rendering the README ${readmeFilePath}`, e);
}
};