@jakubmazanec/template 1.0.0-a3e1bca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +840 -0
- package/README.md +102 -0
- package/dist/Template.d.ts +89 -0
- package/dist/Template.js +156 -0
- package/dist/Template.js.map +1 -0
- package/dist/TemplateError.d.ts +14 -0
- package/dist/TemplateError.js +11 -0
- package/dist/TemplateError.js.map +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +3 -0
- package/dist/constants.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.js +5 -0
- package/dist/main.js.map +1 -0
- package/dist/types/TemplateAttributes.d.ts +21 -0
- package/dist/types/TemplateAttributes.js +11 -0
- package/dist/types/TemplateAttributes.js.map +1 -0
- package/dist/types/TemplateContent.d.ts +4 -0
- package/dist/types/TemplateContent.js +5 -0
- package/dist/types/TemplateContent.js.map +1 -0
- package/dist/types/TemplateFunction.d.ts +4 -0
- package/dist/types/TemplateFunction.js +5 -0
- package/dist/types/TemplateFunction.js.map +1 -0
- package/dist/types/TemplateRender.d.ts +17 -0
- package/dist/types/TemplateRender.js +3 -0
- package/dist/types/TemplateRender.js.map +1 -0
- package/dist/types/TemplateRenders.d.ts +3 -0
- package/dist/types/TemplateRenders.js +3 -0
- package/dist/types/TemplateRenders.js.map +1 -0
- package/dist/types/TemplateVariables.d.ts +9 -0
- package/dist/types/TemplateVariables.js +9 -0
- package/dist/types/TemplateVariables.js.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +49 -0
- package/src/Template.ts +266 -0
- package/src/TemplateError.ts +21 -0
- package/src/constants.ts +1 -0
- package/src/main.ts +3 -0
- package/src/types/TemplateAttributes.ts +19 -0
- package/src/types/TemplateContent.ts +4 -0
- package/src/types/TemplateFunction.ts +4 -0
- package/src/types/TemplateRender.ts +26 -0
- package/src/types/TemplateRenders.ts +8 -0
- package/src/types/TemplateVariables.ts +14 -0
- package/src/types.ts +5 -0
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jakubmazanec/template",
|
|
3
|
+
"version": "1.0.0-a3e1bca",
|
|
4
|
+
"description": "TypeScript-first library for working with templates using EJS and Zod.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git@github.com:jakubmazanec/js-tools.git",
|
|
8
|
+
"directory": "packages/template"
|
|
9
|
+
},
|
|
10
|
+
"license": "LGPL-3.0-only",
|
|
11
|
+
"author": "Jakub Mazanec <jakub@mazanec.dev>",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./dist/main.js",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "swc src -d dist && tsc",
|
|
21
|
+
"clean": "del-cli dist *.tsbuildinfo",
|
|
22
|
+
"document": "typedoc --options typedoc.config.cjs",
|
|
23
|
+
"format": "prettier . --write --ignore-unknown --ignore-path ../../.prettierignore",
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
|
|
26
|
+
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@jakubmazanec/error": "2.0.0-a3e1bca",
|
|
30
|
+
"ejs": "^3.1.9",
|
|
31
|
+
"fs-extra": "^11.1.1",
|
|
32
|
+
"gray-matter": "^4.0.3",
|
|
33
|
+
"lodash.omit": "^4.5.0",
|
|
34
|
+
"prettier": "^2.8.7",
|
|
35
|
+
"yaml": "^2.2.1",
|
|
36
|
+
"zod": "^3.21.4"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@jest/globals": "^29.5.0",
|
|
40
|
+
"@types/ejs": "^3.1.2",
|
|
41
|
+
"@types/fs-extra": "^11.0.1",
|
|
42
|
+
"@types/lodash.omit": "^4.5.7",
|
|
43
|
+
"@types/node": "^16.18.3"
|
|
44
|
+
},
|
|
45
|
+
"packageManager": "npm@8.19.4",
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=16.20.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/Template.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import ejs from 'ejs';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import matter from 'gray-matter';
|
|
4
|
+
import omit from 'lodash.omit';
|
|
5
|
+
import prettier from 'prettier';
|
|
6
|
+
import yaml from 'yaml';
|
|
7
|
+
import {type z, type AnyZodObject} from 'zod';
|
|
8
|
+
|
|
9
|
+
import {NO_INFERRED_PARSER_REGEXP} from './constants.js';
|
|
10
|
+
import {TemplateError} from './TemplateError.js';
|
|
11
|
+
import {
|
|
12
|
+
templateAttributesSchema,
|
|
13
|
+
type TemplateAttributes,
|
|
14
|
+
type TemplateFunction,
|
|
15
|
+
type TemplateRender,
|
|
16
|
+
type TemplateRenders,
|
|
17
|
+
} from './types.js';
|
|
18
|
+
|
|
19
|
+
export type TemplateOptions<
|
|
20
|
+
A extends AnyZodObject | undefined = undefined,
|
|
21
|
+
D extends AnyZodObject | undefined = undefined,
|
|
22
|
+
> = {
|
|
23
|
+
/** Template content. */
|
|
24
|
+
content: string;
|
|
25
|
+
|
|
26
|
+
/** Template attributes. */
|
|
27
|
+
attributes: A extends undefined
|
|
28
|
+
? TemplateAttributes
|
|
29
|
+
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
30
|
+
|
|
31
|
+
/** Zod schema for custom template attributes. */
|
|
32
|
+
attributesSchema?: A | undefined;
|
|
33
|
+
|
|
34
|
+
/** Zod schema for template data. */
|
|
35
|
+
dataSchema?: D | undefined;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type TemplateReadOptions<
|
|
39
|
+
A extends AnyZodObject | undefined = undefined,
|
|
40
|
+
D extends AnyZodObject | undefined = undefined,
|
|
41
|
+
> = {
|
|
42
|
+
/** Zod schema for custom template attributes. */
|
|
43
|
+
attributesSchema?: A | undefined;
|
|
44
|
+
|
|
45
|
+
/** Zod schema for template data. */
|
|
46
|
+
dataSchema?: D | undefined;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type TemplateReadAndRenderOptions<
|
|
50
|
+
A extends AnyZodObject | undefined = undefined,
|
|
51
|
+
D extends AnyZodObject | undefined = undefined,
|
|
52
|
+
> = {
|
|
53
|
+
/** Prettier options. */
|
|
54
|
+
prettierOptions?: prettier.Options | undefined;
|
|
55
|
+
|
|
56
|
+
/** Disable formatting the rendered template using Prettier. */
|
|
57
|
+
disablePrettier?: boolean | undefined;
|
|
58
|
+
|
|
59
|
+
/** Zod schema for custom template attributes. */
|
|
60
|
+
attributesSchema?: A | undefined;
|
|
61
|
+
|
|
62
|
+
/** Zod schema for template data. */
|
|
63
|
+
dataSchema?: D | undefined;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type TemplateRenderOptions = {
|
|
67
|
+
/** Prettier options. */
|
|
68
|
+
prettierOptions?: prettier.Options | undefined;
|
|
69
|
+
|
|
70
|
+
/** Disable formatting the rendered template using Prettier. */
|
|
71
|
+
disablePrettier?: boolean | undefined;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The `Template` object stores all information about a template and has methods for reading and rendering.
|
|
76
|
+
*
|
|
77
|
+
* ```TypeScript
|
|
78
|
+
* let template = new Template({
|
|
79
|
+
* content: '<%- value %>',
|
|
80
|
+
* attributes: {
|
|
81
|
+
* to: 'value.txt',
|
|
82
|
+
* },
|
|
83
|
+
* dataSchema: z.object({value: z.number()}),
|
|
84
|
+
* });
|
|
85
|
+
*
|
|
86
|
+
* console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @typeParam A A Zod schema type used to define the custom template attributes type.
|
|
90
|
+
* @typeParam D A Zod schema type used to define the template datatype.
|
|
91
|
+
*/
|
|
92
|
+
export class Template<
|
|
93
|
+
A extends AnyZodObject | undefined = undefined,
|
|
94
|
+
D extends AnyZodObject | undefined = undefined,
|
|
95
|
+
> {
|
|
96
|
+
/** Template content. Templates are written in [EJS](https://ejs.co/). */
|
|
97
|
+
readonly content: string;
|
|
98
|
+
|
|
99
|
+
/** Template attributes. These control how the template is rendered. Some attributes can be written in [EJS](https://ejs.co/) and are rendered before the template content is rendered. It is expected that attribute `if` renders to a string that can be parsed as YAML value; if that value is `false`, the rendering is cancelled. Attribute `to` is also rendered and it represents the path of file the resulting template render is supposed to be written into (by the user). Attribute `variables` is an object that is merged with the data object before rendering. If attribute `variables` is an array of objects, that leads to multiple renders, each with different variables object merged with the data. Each variable that is a string is also rendered. */
|
|
100
|
+
readonly attributes: A extends undefined
|
|
101
|
+
? TemplateAttributes
|
|
102
|
+
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
103
|
+
|
|
104
|
+
/** Template function. */
|
|
105
|
+
private readonly fn: TemplateFunction;
|
|
106
|
+
|
|
107
|
+
/** Zod schema for custom template attributes. */
|
|
108
|
+
readonly attributesSchema: A extends AnyZodObject ? A : null;
|
|
109
|
+
|
|
110
|
+
/** Zod schema for template data. */
|
|
111
|
+
readonly dataSchema: D extends AnyZodObject ? D : null;
|
|
112
|
+
|
|
113
|
+
constructor({content, attributes, attributesSchema, dataSchema}: TemplateOptions<A, D>) {
|
|
114
|
+
this.content = content;
|
|
115
|
+
this.attributes = attributes;
|
|
116
|
+
this.fn = ejs.compile(this.content);
|
|
117
|
+
|
|
118
|
+
if (attributesSchema) {
|
|
119
|
+
this.attributesSchema = attributesSchema as A extends AnyZodObject ? A : null;
|
|
120
|
+
} else {
|
|
121
|
+
this.attributesSchema = null as A extends AnyZodObject ? A : null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (dataSchema) {
|
|
125
|
+
this.dataSchema = dataSchema as D extends AnyZodObject ? D : null;
|
|
126
|
+
} else {
|
|
127
|
+
this.dataSchema = null as D extends AnyZodObject ? D : null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Reads a template from a file.
|
|
133
|
+
*
|
|
134
|
+
* @param templateFilePath Path to the template file.
|
|
135
|
+
*/
|
|
136
|
+
static async read<
|
|
137
|
+
A extends AnyZodObject | undefined = undefined,
|
|
138
|
+
D extends AnyZodObject | undefined = undefined,
|
|
139
|
+
>(templateFilePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {
|
|
140
|
+
let {data: attributes, content} = matter(await fs.readFile(templateFilePath, 'utf8'));
|
|
141
|
+
let attributesSchema = options?.attributesSchema
|
|
142
|
+
? templateAttributesSchema.merge(options.attributesSchema)
|
|
143
|
+
: templateAttributesSchema;
|
|
144
|
+
let attributesParseResult = attributesSchema.safeParse(attributes);
|
|
145
|
+
|
|
146
|
+
if (!attributesParseResult.success) {
|
|
147
|
+
throw new TemplateError('INVALID_ATTRIBUTES', {
|
|
148
|
+
cause: attributesParseResult.error,
|
|
149
|
+
data: {
|
|
150
|
+
path: templateFilePath,
|
|
151
|
+
zodIssues: attributesParseResult.error.issues,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return new this({
|
|
157
|
+
attributes,
|
|
158
|
+
content,
|
|
159
|
+
attributesSchema: options?.attributesSchema,
|
|
160
|
+
dataSchema: options?.dataSchema,
|
|
161
|
+
} as TemplateOptions<A, D>);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Reads a template from a file and then renders it.
|
|
166
|
+
*
|
|
167
|
+
* @param templateFilePath Path to the template file.
|
|
168
|
+
* @param data Data used for rendering the template.
|
|
169
|
+
* @param options Options object.
|
|
170
|
+
* @return Template renders.
|
|
171
|
+
*/
|
|
172
|
+
static async readAndRender<
|
|
173
|
+
A extends AnyZodObject | undefined = undefined,
|
|
174
|
+
D extends AnyZodObject | undefined = undefined,
|
|
175
|
+
>(
|
|
176
|
+
templateFilePath: string,
|
|
177
|
+
data: D extends undefined ? null : z.infer<NonNullable<D>>,
|
|
178
|
+
options?: TemplateReadAndRenderOptions<A, D>,
|
|
179
|
+
) {
|
|
180
|
+
let template = await Template.read<A, D>(templateFilePath, {
|
|
181
|
+
attributesSchema: options?.attributesSchema,
|
|
182
|
+
dataSchema: options?.dataSchema,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
return template.render(data, {
|
|
186
|
+
prettierOptions: options?.prettierOptions,
|
|
187
|
+
disablePrettier: options?.disablePrettier,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Renders the template using data.
|
|
193
|
+
*
|
|
194
|
+
* @param data Data used for rendering the template.
|
|
195
|
+
* @param options Options object.
|
|
196
|
+
* @return Template renders.
|
|
197
|
+
*/
|
|
198
|
+
async render(
|
|
199
|
+
data: D extends undefined ? null : z.infer<NonNullable<D>>,
|
|
200
|
+
options?: TemplateRenderOptions,
|
|
201
|
+
): Promise<TemplateRenders<A, D>> {
|
|
202
|
+
let renders: TemplateRenders<A, D> = [];
|
|
203
|
+
|
|
204
|
+
for (let variables of Array.isArray(this.attributes.variables)
|
|
205
|
+
? this.attributes.variables
|
|
206
|
+
: [this.attributes.variables]) {
|
|
207
|
+
let renderedAttributes = omit(this.attributes, ['if']);
|
|
208
|
+
let renderedVariables = variables;
|
|
209
|
+
|
|
210
|
+
if (renderedVariables) {
|
|
211
|
+
for (let [variableName, variableValue] of Object.entries(renderedVariables)) {
|
|
212
|
+
if (typeof variableValue === 'string') {
|
|
213
|
+
renderedVariables[variableName] = ejs.render(variableValue, {...data});
|
|
214
|
+
} else {
|
|
215
|
+
renderedVariables[variableName] = variableValue;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
renderedAttributes.variables = renderedVariables;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let dataWithAttributes = {...data, attributes: renderedAttributes};
|
|
223
|
+
|
|
224
|
+
if (this.attributes.if) {
|
|
225
|
+
let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);
|
|
226
|
+
let parsedIf = yaml.parse(renderedIf) as unknown;
|
|
227
|
+
|
|
228
|
+
if (parsedIf === false) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);
|
|
234
|
+
dataWithAttributes.attributes = renderedAttributes;
|
|
235
|
+
|
|
236
|
+
let content;
|
|
237
|
+
|
|
238
|
+
try {
|
|
239
|
+
content = this.fn(dataWithAttributes);
|
|
240
|
+
} catch (error: unknown) {
|
|
241
|
+
throw new TemplateError('FAILED_RENDER', {cause: error});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!options?.disablePrettier) {
|
|
245
|
+
try {
|
|
246
|
+
content = prettier.format(content, {
|
|
247
|
+
...options?.prettierOptions,
|
|
248
|
+
filepath: renderedAttributes.to,
|
|
249
|
+
});
|
|
250
|
+
} catch (error: unknown) {
|
|
251
|
+
if (!NO_INFERRED_PARSER_REGEXP.test((error as Error).message)) {
|
|
252
|
+
throw new TemplateError('FAILED_FORMAT', {cause: error});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
renders.push({
|
|
258
|
+
attributes: renderedAttributes,
|
|
259
|
+
data: data ?? null,
|
|
260
|
+
content,
|
|
261
|
+
} as TemplateRender<A, D>);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return renders;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {createCustomErrorWithData} from '@jakubmazanec/error';
|
|
2
|
+
import {type ZodIssue} from 'zod';
|
|
3
|
+
|
|
4
|
+
import {type Template} from './Template.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A subclass of `Error` that indicates a template failure.
|
|
8
|
+
*/
|
|
9
|
+
export const TemplateError = createCustomErrorWithData(
|
|
10
|
+
'TemplateError',
|
|
11
|
+
{
|
|
12
|
+
INVALID_ATTRIBUTES: 'Invalid template attributes.',
|
|
13
|
+
FAILED_FORMAT: 'Template render formatting has failed.',
|
|
14
|
+
FAILED_RENDER: 'Rendering has failed.',
|
|
15
|
+
},
|
|
16
|
+
class TemplateErrorData {
|
|
17
|
+
template?: Template;
|
|
18
|
+
path?: string;
|
|
19
|
+
zodIssues?: ZodIssue[];
|
|
20
|
+
},
|
|
21
|
+
);
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {z} from 'zod';
|
|
2
|
+
|
|
3
|
+
import {templateVariablesSchema} from './TemplateVariables.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Zod schema for {@linkcode TemplateAttributes}.
|
|
7
|
+
*/
|
|
8
|
+
export const templateAttributesSchema = z
|
|
9
|
+
.object({
|
|
10
|
+
to: z.string(),
|
|
11
|
+
if: z.string().optional(),
|
|
12
|
+
variables: templateVariablesSchema.optional(),
|
|
13
|
+
})
|
|
14
|
+
.strict();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Type representing template attributes.
|
|
18
|
+
*/
|
|
19
|
+
export type TemplateAttributes = z.infer<typeof templateAttributesSchema>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {type z} from 'zod';
|
|
2
|
+
|
|
3
|
+
import {type TemplateAttributes} from './TemplateAttributes.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An object type representing the result of a template render.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam A Type of Zod schema used to validate the custom template attributes.
|
|
9
|
+
* @typeParam D Type of Zod schema used to define the type of template data.
|
|
10
|
+
*/
|
|
11
|
+
export type TemplateRender<
|
|
12
|
+
A extends z.AnyZodObject | undefined = undefined,
|
|
13
|
+
D extends z.AnyZodObject | undefined = undefined,
|
|
14
|
+
> = {
|
|
15
|
+
/** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.
|
|
16
|
+
*/
|
|
17
|
+
attributes: A extends undefined
|
|
18
|
+
? Omit<TemplateAttributes, 'if'>
|
|
19
|
+
: Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
|
|
20
|
+
|
|
21
|
+
/** Data used to render the template. */
|
|
22
|
+
data: D extends undefined ? null : z.infer<NonNullable<D>>;
|
|
23
|
+
|
|
24
|
+
/** Rendered template content. */
|
|
25
|
+
content: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zod schema for {@linkcode TemplateAttributesVariables}.
|
|
5
|
+
*/
|
|
6
|
+
export const templateVariablesSchema = z.union([
|
|
7
|
+
z.record(z.unknown()),
|
|
8
|
+
z.array(z.record(z.unknown())),
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type of `variables`` property of {@linkcode TemplateAttributes}.
|
|
13
|
+
*/
|
|
14
|
+
export type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;
|