This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
forked from segmentio/typewriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.ts
72 lines (63 loc) · 2.47 KB
/
templates.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
import * as fs from 'fs'
import * as Handlebars from 'handlebars'
import { promisify } from 'util'
import { resolve } from 'path'
const readFile = promisify(fs.readFile)
/**
* Header used to mark generated files that are safe to remove during generation.
* This note needs to be in every generated file (except plan.json), otherwise
* that file will not be cleaned up before client generation. This can be placed
* anywhere in a file, though prefer placing it at the top of every file.
*
* If you change this, make sure to update the Build step to recognize previous
* versions of this header when identifying files safe to remove.
*/
export const SEGMENT_AUTOGENERATED_FILE_WARNING =
'This client was automatically generated by Segment Typewriter. ** Do Not Edit **'
// Renders a string generated from a template using the provided context.
// The template path is relative to the `src` directory.
export async function generateFromTemplate<Context extends Record<string, unknown>>(
templatePath: string,
context: Context,
needsWarning?: boolean
): Promise<string> {
const path = resolve(__dirname, templatePath)
const template = await readFile(path, {
encoding: 'utf-8',
})
const templater = Handlebars.compile(template, {
noEscape: true,
})
const content = templater(context)
if (needsWarning && !content.includes(SEGMENT_AUTOGENERATED_FILE_WARNING)) {
throw new Error(
`This autogenerated file (${templatePath}) is missing a warning, and therefore will not be cleaned up in future runs.`
)
}
return content
}
export async function registerPartial(partialPath: string, partialName: string): Promise<void> {
const path = resolve(__dirname, partialPath)
const template = await readFile(path, {
encoding: 'utf-8',
})
const templater = Handlebars.compile(template, {
noEscape: true,
})
Handlebars.registerPartial(partialName, templater)
}
export async function registerStandardHelpers(): Promise<void> {
// Register a helper for indenting multi-line output from other helpers.
Handlebars.registerHelper('indent', (indentation: string, content: string) => {
return content
.split('\n')
.join(`\n${indentation}`)
.trim()
})
// Register a helper to output a warning that a given file was automatically
// generated by Typewriter. Note that the exact phrasing is important, since
// it is used to clear generated files. See `clearFolder` in `commands.ts`.
Handlebars.registerHelper('autogeneratedFileWarning', () => {
return SEGMENT_AUTOGENERATED_FILE_WARNING
})
}