@jakubmazanec/template 2.0.0-unstable.e1ae458 → 2.0.0
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/{dist → build}/Template.d.ts +1 -1
- package/build/Template.js +164 -0
- package/build/Template.js.map +7 -0
- package/build/TemplateAttributes.js +10 -0
- package/build/TemplateAttributes.js.map +7 -0
- package/build/TemplateContent.js +2 -0
- package/build/TemplateContent.js.map +7 -0
- package/build/TemplateError.js +16 -0
- package/build/TemplateError.js.map +7 -0
- package/build/TemplateFunction.js +2 -0
- package/build/TemplateFunction.js.map +7 -0
- package/build/TemplateRender.js +2 -0
- package/build/TemplateRender.js.map +7 -0
- package/build/TemplateRenders.js +2 -0
- package/build/TemplateRenders.js.map +7 -0
- package/build/TemplateVariables.js +7 -0
- package/build/TemplateVariables.js.map +7 -0
- package/{dist → build}/constants.js +2 -2
- package/build/constants.js.map +7 -0
- package/build/main.js +10 -0
- package/build/main.js.map +7 -0
- package/package.json +31 -20
- package/{src → source}/Template.ts +12 -13
- package/{src → source}/TemplateRender.ts +2 -3
- package/dist/Template.js +0 -179
- package/dist/Template.js.map +0 -1
- package/dist/TemplateAttributes.js +0 -12
- package/dist/TemplateAttributes.js.map +0 -1
- package/dist/TemplateContent.js +0 -5
- package/dist/TemplateContent.js.map +0 -1
- package/dist/TemplateError.js +0 -14
- package/dist/TemplateError.js.map +0 -1
- package/dist/TemplateFunction.js +0 -5
- package/dist/TemplateFunction.js.map +0 -1
- package/dist/TemplateRender.js +0 -8
- package/dist/TemplateRender.js.map +0 -1
- package/dist/TemplateRenders.js +0 -3
- package/dist/TemplateRenders.js.map +0 -1
- package/dist/TemplateVariables.js +0 -9
- package/dist/TemplateVariables.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/main.js +0 -10
- package/dist/main.js.map +0 -1
- /package/{dist → build}/TemplateAttributes.d.ts +0 -0
- /package/{dist → build}/TemplateContent.d.ts +0 -0
- /package/{dist → build}/TemplateError.d.ts +0 -0
- /package/{dist → build}/TemplateFunction.d.ts +0 -0
- /package/{dist → build}/TemplateRender.d.ts +0 -0
- /package/{dist → build}/TemplateRenders.d.ts +0 -0
- /package/{dist → build}/TemplateVariables.d.ts +0 -0
- /package/{dist → build}/constants.d.ts +0 -0
- /package/{dist → build}/main.d.ts +0 -0
- /package/{src → source}/TemplateAttributes.ts +0 -0
- /package/{src → source}/TemplateContent.ts +0 -0
- /package/{src → source}/TemplateError.ts +0 -0
- /package/{src → source}/TemplateFunction.ts +0 -0
- /package/{src → source}/TemplateRenders.ts +0 -0
- /package/{src → source}/TemplateVariables.ts +0 -0
- /package/{src → source}/constants.ts +0 -0
- /package/{src → source}/main.ts +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import prettier from 'prettier';
|
|
2
|
-
import { type
|
|
2
|
+
import { type AnyZodObject, type z } from 'zod';
|
|
3
3
|
import { type TemplateAttributes } from './TemplateAttributes.js';
|
|
4
4
|
import { type TemplateRenders } from './TemplateRenders.js';
|
|
5
5
|
export type TemplateOptions<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> = {
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import ejs from "ejs";
|
|
3
|
+
import fs from "fs-extra";
|
|
4
|
+
import matter from "gray-matter";
|
|
5
|
+
import merge from "lodash.merge";
|
|
6
|
+
import omit from "lodash.omit";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import prettier from "prettier";
|
|
9
|
+
import yaml from "yaml";
|
|
10
|
+
import { NO_INFERRED_PARSER_REGEXP } from "./constants.js";
|
|
11
|
+
import { templateAttributesSchema } from "./TemplateAttributes.js";
|
|
12
|
+
import { TemplateError } from "./TemplateError.js";
|
|
13
|
+
export class Template {
|
|
14
|
+
/** Template path. */
|
|
15
|
+
path;
|
|
16
|
+
/** Template content. Templates are written in [EJS](https://ejs.co/). */
|
|
17
|
+
content;
|
|
18
|
+
/** 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. */
|
|
19
|
+
attributes;
|
|
20
|
+
/** Template function. */
|
|
21
|
+
fn;
|
|
22
|
+
/** Zod schema for custom template attributes. */
|
|
23
|
+
attributesSchema;
|
|
24
|
+
/** Zod schema for template data. */
|
|
25
|
+
dataSchema;
|
|
26
|
+
constructor({
|
|
27
|
+
path: templatePath,
|
|
28
|
+
content,
|
|
29
|
+
attributes,
|
|
30
|
+
attributesSchema,
|
|
31
|
+
dataSchema
|
|
32
|
+
}) {
|
|
33
|
+
if (templatePath) {
|
|
34
|
+
this.path = templatePath;
|
|
35
|
+
}
|
|
36
|
+
this.content = content;
|
|
37
|
+
this.attributes = attributes;
|
|
38
|
+
this.fn = ejs.compile(this.content);
|
|
39
|
+
if (attributesSchema) {
|
|
40
|
+
this.attributesSchema = attributesSchema;
|
|
41
|
+
} else {
|
|
42
|
+
this.attributesSchema = null;
|
|
43
|
+
}
|
|
44
|
+
if (dataSchema) {
|
|
45
|
+
this.dataSchema = dataSchema;
|
|
46
|
+
} else {
|
|
47
|
+
this.dataSchema = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Reads a template from a file.
|
|
52
|
+
*
|
|
53
|
+
* @param templatePath Path to the template file.
|
|
54
|
+
*/
|
|
55
|
+
static async read(templatePath, options) {
|
|
56
|
+
let { data: rawAttributes, content } = matter(await fs.readFile(templatePath, "utf8"));
|
|
57
|
+
if (typeof rawAttributes.extends === "string" && rawAttributes.extends) {
|
|
58
|
+
let extendedTemplate = await Template.read(
|
|
59
|
+
path.join(path.dirname(templatePath), rawAttributes.extends),
|
|
60
|
+
options
|
|
61
|
+
);
|
|
62
|
+
rawAttributes = merge(extendedTemplate.attributes, rawAttributes);
|
|
63
|
+
if (!content.trim()) {
|
|
64
|
+
content = extendedTemplate.content;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let attributesSchema = options?.attributesSchema ? templateAttributesSchema.merge(options.attributesSchema) : templateAttributesSchema;
|
|
68
|
+
let attributesParseResult = attributesSchema.safeParse(rawAttributes);
|
|
69
|
+
if (!attributesParseResult.success) {
|
|
70
|
+
throw new TemplateError("INVALID_ATTRIBUTES", {
|
|
71
|
+
cause: attributesParseResult.error,
|
|
72
|
+
data: {
|
|
73
|
+
path: templatePath,
|
|
74
|
+
zodIssues: attributesParseResult.error.issues
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
let attributes = attributesParseResult.data;
|
|
79
|
+
return new this({
|
|
80
|
+
path: templatePath,
|
|
81
|
+
attributes,
|
|
82
|
+
content,
|
|
83
|
+
attributesSchema: options?.attributesSchema,
|
|
84
|
+
dataSchema: options?.dataSchema
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Reads a template from a file and then renders it.
|
|
89
|
+
*
|
|
90
|
+
* @param templatePath Path to the template file.
|
|
91
|
+
* @param data Data used for rendering the template.
|
|
92
|
+
* @param options Options object.
|
|
93
|
+
* @return Template renders.
|
|
94
|
+
*/
|
|
95
|
+
static async readAndRender(templatePath, data, options) {
|
|
96
|
+
let template = await Template.read(templatePath, {
|
|
97
|
+
attributesSchema: options?.attributesSchema,
|
|
98
|
+
dataSchema: options?.dataSchema
|
|
99
|
+
});
|
|
100
|
+
return template.render(data, {
|
|
101
|
+
prettierOptions: options?.prettierOptions,
|
|
102
|
+
disablePrettier: options?.disablePrettier
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Renders the template using data.
|
|
107
|
+
*
|
|
108
|
+
* @param data Data used for rendering the template.
|
|
109
|
+
* @param options Options object.
|
|
110
|
+
* @return Template renders.
|
|
111
|
+
*/
|
|
112
|
+
async render(data, options) {
|
|
113
|
+
let renders = [];
|
|
114
|
+
for (let variables of Array.isArray(this.attributes.variables) ? this.attributes.variables : [this.attributes.variables]) {
|
|
115
|
+
let renderedAttributes = omit(this.attributes, ["if"]);
|
|
116
|
+
let renderedVariables = variables;
|
|
117
|
+
if (renderedVariables) {
|
|
118
|
+
for (let [variableName, variableValue] of Object.entries(renderedVariables)) {
|
|
119
|
+
if (typeof variableValue === "string") {
|
|
120
|
+
renderedVariables[variableName] = ejs.render(variableValue, { ...data });
|
|
121
|
+
} else {
|
|
122
|
+
renderedVariables[variableName] = variableValue;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
renderedAttributes.variables = renderedVariables;
|
|
126
|
+
}
|
|
127
|
+
let dataWithAttributes = { ...data, attributes: renderedAttributes };
|
|
128
|
+
if (this.attributes.if) {
|
|
129
|
+
let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);
|
|
130
|
+
let parsedIf = yaml.parse(renderedIf);
|
|
131
|
+
if (parsedIf === false) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);
|
|
136
|
+
dataWithAttributes.attributes = renderedAttributes;
|
|
137
|
+
let content;
|
|
138
|
+
try {
|
|
139
|
+
content = this.fn(dataWithAttributes);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
throw new TemplateError("FAILED_RENDER", { cause: error });
|
|
142
|
+
}
|
|
143
|
+
if (!options?.disablePrettier) {
|
|
144
|
+
try {
|
|
145
|
+
content = await prettier.format(content, {
|
|
146
|
+
...options?.prettierOptions,
|
|
147
|
+
filepath: renderedAttributes.to
|
|
148
|
+
});
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (!NO_INFERRED_PARSER_REGEXP.test(error.message)) {
|
|
151
|
+
throw new TemplateError("FAILED_FORMAT", { cause: error });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
renders.push({
|
|
156
|
+
attributes: renderedAttributes,
|
|
157
|
+
data: data ?? null,
|
|
158
|
+
content
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return renders;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=Template.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/Template.ts"],
|
|
4
|
+
"sourcesContent": ["import ejs from 'ejs';\nimport fs from 'fs-extra';\nimport matter from 'gray-matter';\nimport merge from 'lodash.merge';\nimport omit from 'lodash.omit';\nimport path from 'node:path';\nimport prettier from 'prettier';\nimport yaml from 'yaml';\nimport {type AnyZodObject, type z} from 'zod';\n\nimport {NO_INFERRED_PARSER_REGEXP} from './constants.js';\nimport {type TemplateAttributes, templateAttributesSchema} from './TemplateAttributes.js';\nimport {TemplateError} from './TemplateError.js';\nimport {type TemplateFunction} from './TemplateFunction.js';\nimport {type TemplateRender} from './TemplateRender.js';\nimport {type TemplateRenders} from './TemplateRenders.js';\n\nexport type TemplateOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Template path. */\n path?: string;\n\n /** Template content. */\n content: string;\n\n /** Template attributes. */\n attributes: A extends undefined ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadAndRenderOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateRenderOptions = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n};\n\n/**\n * The `Template` object stores all information about a template and has methods for reading and rendering.\n *\n * ```TypeScript\n * let template = new Template({\n * content: '<%- value %>',\n * attributes: {\n * to: 'value.txt',\n * },\n * dataSchema: z.object({value: z.number()}),\n * });\n *\n * console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]\n * ```\n *\n * @typeParam A A Zod schema type used to define the custom template attributes type.\n * @typeParam D A Zod schema type used to define the template datatype.\n */\nexport class Template<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> {\n /** Template path. */\n path?: string;\n\n /** Template content. Templates are written in [EJS](https://ejs.co/). */\n readonly content: string;\n\n /** 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. */\n readonly attributes: A extends undefined ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Template function. */\n private readonly fn: TemplateFunction;\n\n /** Zod schema for custom template attributes. */\n readonly attributesSchema: A extends AnyZodObject ? A : null;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends AnyZodObject ? D : null;\n\n constructor({\n path: templatePath,\n content,\n attributes,\n attributesSchema,\n dataSchema,\n }: TemplateOptions<A, D>) {\n if (templatePath) {\n this.path = templatePath;\n }\n\n this.content = content;\n this.attributes = attributes;\n this.fn = ejs.compile(this.content);\n\n if (attributesSchema) {\n this.attributesSchema = attributesSchema as A extends AnyZodObject ? A : null;\n } else {\n this.attributesSchema = null as A extends AnyZodObject ? A : null;\n }\n\n if (dataSchema) {\n this.dataSchema = dataSchema as D extends AnyZodObject ? D : null;\n } else {\n this.dataSchema = null as D extends AnyZodObject ? D : null;\n }\n }\n\n /**\n * Reads a template from a file.\n *\n * @param templatePath Path to the template file.\n */\n static async read<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n >(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {\n let {data: rawAttributes, content} = matter(await fs.readFile(templatePath, 'utf8'));\n\n // extending another template\n if (typeof rawAttributes.extends === 'string' && rawAttributes.extends) {\n let extendedTemplate = await Template.read(\n path.join(path.dirname(templatePath), rawAttributes.extends),\n options,\n ); // you can point to any template, but it will be parsed with the same options, including attribute or data schema\n\n // attributes are recursively merged\n rawAttributes = merge(extendedTemplate.attributes, rawAttributes);\n\n // if content is empty, use content from the extended template\n if (!content.trim()) {\n content = extendedTemplate.content;\n }\n }\n\n let attributesSchema =\n options?.attributesSchema ?\n templateAttributesSchema.merge(options.attributesSchema)\n : templateAttributesSchema;\n let attributesParseResult = attributesSchema.safeParse(rawAttributes);\n\n if (!attributesParseResult.success) {\n throw new TemplateError('INVALID_ATTRIBUTES', {\n cause: attributesParseResult.error,\n data: {\n path: templatePath,\n zodIssues: attributesParseResult.error.issues,\n },\n });\n }\n\n let attributes = attributesParseResult.data;\n\n return new this({\n path: templatePath,\n attributes,\n content,\n attributesSchema: options?.attributesSchema,\n dataSchema: options?.dataSchema,\n } as TemplateOptions<A, D>);\n }\n\n /**\n * Reads a template from a file and then renders it.\n *\n * @param templatePath Path to the template file.\n * @param data Data used for rendering the template.\n * @param options Options object.\n * @return Template renders.\n */\n static async readAndRender<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n >(\n templatePath: string,\n data: D extends undefined ? null : z.infer<NonNullable<D>>,\n options?: TemplateReadAndRenderOptions<A, D>,\n ) {\n let template = await Template.read<A, D>(templatePath, {\n attributesSchema: options?.attributesSchema,\n dataSchema: options?.dataSchema,\n });\n\n return template.render(data, {\n prettierOptions: options?.prettierOptions,\n disablePrettier: options?.disablePrettier,\n });\n }\n\n /**\n * Renders the template using data.\n *\n * @param data Data used for rendering the template.\n * @param options Options object.\n * @return Template renders.\n */\n async render(\n data: D extends undefined ? null : z.infer<NonNullable<D>>,\n options?: TemplateRenderOptions,\n ): Promise<TemplateRenders<A, D>> {\n let renders: TemplateRenders<A, D> = [];\n\n for (let variables of Array.isArray(this.attributes.variables) ?\n this.attributes.variables\n : [this.attributes.variables]) {\n let renderedAttributes = omit(this.attributes, ['if']);\n let renderedVariables = variables;\n\n if (renderedVariables) {\n for (let [variableName, variableValue] of Object.entries(renderedVariables)) {\n if (typeof variableValue === 'string') {\n renderedVariables[variableName] = ejs.render(variableValue, {...data});\n } else {\n renderedVariables[variableName] = variableValue;\n }\n }\n\n renderedAttributes.variables = renderedVariables;\n }\n\n let dataWithAttributes = {...data, attributes: renderedAttributes};\n\n if (this.attributes.if) {\n let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);\n let parsedIf = yaml.parse(renderedIf) as unknown;\n\n if (parsedIf === false) {\n continue;\n }\n }\n\n renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);\n dataWithAttributes.attributes = renderedAttributes;\n\n let content;\n\n try {\n content = this.fn(dataWithAttributes);\n } catch (error: unknown) {\n throw new TemplateError('FAILED_RENDER', {cause: error});\n }\n\n if (!options?.disablePrettier) {\n try {\n content = await prettier.format(content, {\n ...options?.prettierOptions,\n filepath: renderedAttributes.to,\n });\n } catch (error: unknown) {\n if (!NO_INFERRED_PARSER_REGEXP.test((error as Error).message)) {\n throw new TemplateError('FAILED_FORMAT', {cause: error});\n }\n }\n }\n\n renders.push({\n attributes: renderedAttributes,\n data: data ?? null,\n content,\n } as TemplateRender<A, D>);\n }\n\n return renders;\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,SAAS;AAChB,OAAO,QAAQ;AACf,OAAO,YAAY;AACnB,OAAO,WAAW;AAClB,OAAO,UAAU;AACjB,OAAO,UAAU;AACjB,OAAO,cAAc;AACrB,OAAO,UAAU;AAGjB,SAAQ,iCAAgC;AACxC,SAAiC,gCAA+B;AAChE,SAAQ,qBAAoB;AAgFrB,aAAM,SAGX;AAAA;AAAA,EAEA;AAAA;AAAA,EAGS;AAAA;AAAA,EAGA;AAAA;AAAA,EAIQ;AAAA;AAAA,EAGR;AAAA;AAAA,EAGA;AAAA,EAET,YAAY;AAAA,IACV,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAA0B;AACxB,QAAI,cAAc;AAChB,WAAK,OAAO;AAAA,IACd;AAEA,SAAK,UAAU;AACf,SAAK,aAAa;AAClB,SAAK,KAAK,IAAI,QAAQ,KAAK,OAAO;AAElC,QAAI,kBAAkB;AACpB,WAAK,mBAAmB;AAAA,IAC1B,OAAO;AACL,WAAK,mBAAmB;AAAA,IAC1B;AAEA,QAAI,YAAY;AACd,WAAK,aAAa;AAAA,IACpB,OAAO;AACL,WAAK,aAAa;AAAA,IACpB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,aAAa,KAGX,cAAsB,SAA8D;AACpF,QAAI,EAAC,MAAM,eAAe,QAAO,IAAI,OAAO,MAAM,GAAG,SAAS,cAAc,MAAM,CAAC;AAGnF,QAAI,OAAO,cAAc,YAAY,YAAY,cAAc,SAAS;AACtE,UAAI,mBAAmB,MAAM,SAAS;AAAA,QACpC,KAAK,KAAK,KAAK,QAAQ,YAAY,GAAG,cAAc,OAAO;AAAA,QAC3D;AAAA,MACF;AAGA,sBAAg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use strict";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { templateVariablesSchema } from "./TemplateVariables.js";
|
|
4
|
+
export const templateAttributesSchema = z.object({
|
|
5
|
+
extends: z.string().optional(),
|
|
6
|
+
to: z.string(),
|
|
7
|
+
if: z.string().optional(),
|
|
8
|
+
variables: templateVariablesSchema.optional()
|
|
9
|
+
}).strict();
|
|
10
|
+
//# sourceMappingURL=TemplateAttributes.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/TemplateAttributes.ts"],
|
|
4
|
+
"sourcesContent": ["import {z} from 'zod';\n\nimport {templateVariablesSchema} from './TemplateVariables.js';\n\n/**\n * Zod schema for {@link TemplateAttributes}.\n */\nexport const templateAttributesSchema = z\n .object({\n extends: z.string().optional(),\n to: z.string(),\n if: z.string().optional(),\n variables: templateVariablesSchema.optional(),\n })\n .strict();\n\n/**\n * Type representing template attributes.\n */\nexport type TemplateAttributes = z.infer<typeof templateAttributesSchema>;\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAQ,SAAQ;AAEhB,SAAQ,+BAA8B;AAK/B,aAAM,2BAA2B,EACrC,OAAO;AAAA,EACN,SAAS,EAAE,OAAO,EAAE,SAAS;AAAA,EAC7B,IAAI,EAAE,OAAO;AAAA,EACb,IAAI,EAAE,OAAO,EAAE,SAAS;AAAA,EACxB,WAAW,wBAAwB,SAAS;AAC9C,CAAC,EACA,OAAO;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { createCustomErrorWithData } from "@jakubmazanec/error";
|
|
3
|
+
export const TemplateError = createCustomErrorWithData(
|
|
4
|
+
"TemplateError",
|
|
5
|
+
{
|
|
6
|
+
INVALID_ATTRIBUTES: "Invalid template attributes.",
|
|
7
|
+
FAILED_FORMAT: "Template render formatting has failed.",
|
|
8
|
+
FAILED_RENDER: "Template rendering has failed."
|
|
9
|
+
},
|
|
10
|
+
class TemplateErrorData {
|
|
11
|
+
template;
|
|
12
|
+
path;
|
|
13
|
+
zodIssues;
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
//# sourceMappingURL=TemplateError.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/TemplateError.ts"],
|
|
4
|
+
"sourcesContent": ["import {createCustomErrorWithData} from '@jakubmazanec/error';\nimport {type ZodIssue} from 'zod';\n\nimport {type Template} from './Template.js';\n\n/**\n * A subclass of `Error` that indicates a template failure.\n */\nexport const TemplateError = createCustomErrorWithData(\n 'TemplateError',\n {\n INVALID_ATTRIBUTES: 'Invalid template attributes.',\n FAILED_FORMAT: 'Template render formatting has failed.',\n FAILED_RENDER: 'Template rendering has failed.',\n },\n class TemplateErrorData {\n template?: Template;\n path?: string;\n zodIssues?: ZodIssue[];\n },\n);\n"],
|
|
5
|
+
"mappings": ";AAAA,SAAQ,iCAAgC;AAQjC,aAAM,gBAAgB;AAAA,EAC3B;AAAA,EACA;AAAA,IACE,oBAAoB;AAAA,IACpB,eAAe;AAAA,IACf,eAAe;AAAA,EACjB;AAAA,EACA,MAAM,kBAAkB;AAAA,IACtB;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/TemplateVariables.ts"],
|
|
4
|
+
"sourcesContent": ["import * as z from 'zod';\n\n/**\n * Zod schema for {@link TemplateAttributesVariables}.\n */\nexport const templateVariablesSchema = z.union([\n z.record(z.unknown()),\n z.array(z.record(z.unknown())),\n]);\n\n/**\n * Type of `variables`` property of {@link TemplateAttributes}.\n */\nexport type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;\n"],
|
|
5
|
+
"mappings": ";AAAA,YAAY,OAAO;AAKZ,aAAM,0BAA0B,EAAE,MAAM;AAAA,EAC7C,EAAE,OAAO,EAAE,QAAQ,CAAC;AAAA,EACpB,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC/B,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/main.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export * from "./Template.js";
|
|
3
|
+
export * from "./TemplateAttributes.js";
|
|
4
|
+
export * from "./TemplateContent.js";
|
|
5
|
+
export * from "./TemplateError.js";
|
|
6
|
+
export * from "./TemplateFunction.js";
|
|
7
|
+
export * from "./TemplateRender.js";
|
|
8
|
+
export * from "./TemplateRenders.js";
|
|
9
|
+
export * from "./TemplateVariables.js";
|
|
10
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../source/main.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './Template.js';\nexport * from './TemplateAttributes.js';\nexport * from './TemplateContent.js';\nexport * from './TemplateError.js';\nexport * from './TemplateFunction.js';\nexport * from './TemplateRender.js';\nexport * from './TemplateRenders.js';\nexport * from './TemplateVariables.js';\n"],
|
|
5
|
+
"mappings": ";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/template",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "TypeScript-first library for working with templates using EJS and Zod.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,38 +11,49 @@
|
|
|
11
11
|
"author": "Jakub Mazanec <jakub@mazanec.dev>",
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"type": "module",
|
|
14
|
-
"exports": "./
|
|
14
|
+
"exports": "./build/main.js",
|
|
15
15
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
"
|
|
16
|
+
"build",
|
|
17
|
+
"source"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"clean": "del-cli coverage
|
|
22
|
-
"
|
|
23
|
-
"
|
|
20
|
+
"build": "del-cli build tsconfig.tsbuildinfo && esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap && tsc",
|
|
21
|
+
"clean": "del-cli coverage build *.tsbuildinfo",
|
|
22
|
+
"develop": "concurrently \"esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap --watch\" \"tsc --watch\"",
|
|
23
|
+
"document": "del-cli docs && typedoc --options typedoc.config.cjs",
|
|
24
|
+
"format": "prettier . --write --ignore-unknown",
|
|
24
25
|
"lint": "eslint .",
|
|
25
|
-
"test": "
|
|
26
|
+
"test": "del-cli coverage && vitest run --coverage --passWithNoTests",
|
|
26
27
|
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
|
-
"@jakubmazanec/error": "3.0.0
|
|
30
|
+
"@jakubmazanec/error": "^3.0.0",
|
|
30
31
|
"ejs": "^3.1.9",
|
|
31
|
-
"fs-extra": "^11.
|
|
32
|
+
"fs-extra": "^11.2.0",
|
|
32
33
|
"gray-matter": "^4.0.3",
|
|
33
34
|
"lodash.merge": "^4.6.2",
|
|
34
35
|
"lodash.omit": "^4.5.0",
|
|
35
|
-
"prettier": "^3.
|
|
36
|
-
"yaml": "^2.
|
|
37
|
-
"zod": "^3.
|
|
36
|
+
"prettier": "^3.2.5",
|
|
37
|
+
"yaml": "^2.3.4",
|
|
38
|
+
"zod": "^3.22.4"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"@types/
|
|
42
|
-
"@types/
|
|
43
|
-
"@types/lodash.
|
|
44
|
-
"@types/
|
|
45
|
-
"@
|
|
41
|
+
"@types/ejs": "^3.1.5",
|
|
42
|
+
"@types/fs-extra": "^11.0.4",
|
|
43
|
+
"@types/lodash.merge": "^4.6.9",
|
|
44
|
+
"@types/lodash.omit": "^4.5.9",
|
|
45
|
+
"@types/node": "^20.8.5",
|
|
46
|
+
"@vitest/coverage-v8": "^1.2.2",
|
|
47
|
+
"concurrently": "^8.2.2",
|
|
48
|
+
"del-cli": "^5.1.0",
|
|
49
|
+
"esbuild": "^0.20.0",
|
|
50
|
+
"eslint": "^8.56.0",
|
|
51
|
+
"prettier": "^3.2.5",
|
|
52
|
+
"prettier-plugin-packagejson": "^2.4.10",
|
|
53
|
+
"typedoc": "^0.25.7",
|
|
54
|
+
"typedoc-plugin-markdown": "^3.17.1",
|
|
55
|
+
"typescript": "^5.3.3",
|
|
56
|
+
"vitest": "^1.2.2"
|
|
46
57
|
},
|
|
47
58
|
"packageManager": "npm@10.1.0",
|
|
48
59
|
"engines": {
|
|
@@ -6,10 +6,10 @@ import omit from 'lodash.omit';
|
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import prettier from 'prettier';
|
|
8
8
|
import yaml from 'yaml';
|
|
9
|
-
import {type
|
|
9
|
+
import {type AnyZodObject, type z} from 'zod';
|
|
10
10
|
|
|
11
11
|
import {NO_INFERRED_PARSER_REGEXP} from './constants.js';
|
|
12
|
-
import {
|
|
12
|
+
import {type TemplateAttributes, templateAttributesSchema} from './TemplateAttributes.js';
|
|
13
13
|
import {TemplateError} from './TemplateError.js';
|
|
14
14
|
import {type TemplateFunction} from './TemplateFunction.js';
|
|
15
15
|
import {type TemplateRender} from './TemplateRender.js';
|
|
@@ -26,9 +26,8 @@ export type TemplateOptions<
|
|
|
26
26
|
content: string;
|
|
27
27
|
|
|
28
28
|
/** Template attributes. */
|
|
29
|
-
attributes: A extends undefined
|
|
30
|
-
|
|
31
|
-
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
29
|
+
attributes: A extends undefined ? TemplateAttributes
|
|
30
|
+
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
32
31
|
|
|
33
32
|
/** Zod schema for custom template attributes. */
|
|
34
33
|
attributesSchema?: A | undefined;
|
|
@@ -102,9 +101,8 @@ export class Template<
|
|
|
102
101
|
readonly content: string;
|
|
103
102
|
|
|
104
103
|
/** 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. */
|
|
105
|
-
readonly attributes: A extends undefined
|
|
106
|
-
|
|
107
|
-
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
104
|
+
readonly attributes: A extends undefined ? TemplateAttributes
|
|
105
|
+
: TemplateAttributes & z.infer<NonNullable<A>>;
|
|
108
106
|
|
|
109
107
|
/** Template function. */
|
|
110
108
|
private readonly fn: TemplateFunction;
|
|
@@ -170,8 +168,9 @@ export class Template<
|
|
|
170
168
|
}
|
|
171
169
|
}
|
|
172
170
|
|
|
173
|
-
let attributesSchema =
|
|
174
|
-
|
|
171
|
+
let attributesSchema =
|
|
172
|
+
options?.attributesSchema ?
|
|
173
|
+
templateAttributesSchema.merge(options.attributesSchema)
|
|
175
174
|
: templateAttributesSchema;
|
|
176
175
|
let attributesParseResult = attributesSchema.safeParse(rawAttributes);
|
|
177
176
|
|
|
@@ -236,9 +235,9 @@ export class Template<
|
|
|
236
235
|
): Promise<TemplateRenders<A, D>> {
|
|
237
236
|
let renders: TemplateRenders<A, D> = [];
|
|
238
237
|
|
|
239
|
-
for (let variables of Array.isArray(this.attributes.variables)
|
|
240
|
-
|
|
241
|
-
|
|
238
|
+
for (let variables of Array.isArray(this.attributes.variables) ?
|
|
239
|
+
this.attributes.variables
|
|
240
|
+
: [this.attributes.variables]) {
|
|
242
241
|
let renderedAttributes = omit(this.attributes, ['if']);
|
|
243
242
|
let renderedVariables = variables;
|
|
244
243
|
|
|
@@ -14,9 +14,8 @@ export type TemplateRender<
|
|
|
14
14
|
> = {
|
|
15
15
|
/** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.
|
|
16
16
|
*/
|
|
17
|
-
attributes: A extends undefined
|
|
18
|
-
|
|
19
|
-
: Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
|
|
17
|
+
attributes: A extends undefined ? Omit<TemplateAttributes, 'if'>
|
|
18
|
+
: Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
|
|
20
19
|
|
|
21
20
|
/** Data used to render the template. */
|
|
22
21
|
data: D extends undefined ? null : z.infer<NonNullable<D>>;
|
package/dist/Template.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import ejs from 'ejs';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
import matter from 'gray-matter';
|
|
4
|
-
import merge from 'lodash.merge';
|
|
5
|
-
import omit from 'lodash.omit';
|
|
6
|
-
import path from 'node:path';
|
|
7
|
-
import prettier from 'prettier';
|
|
8
|
-
import yaml from 'yaml';
|
|
9
|
-
import { NO_INFERRED_PARSER_REGEXP } from './constants.js';
|
|
10
|
-
import { templateAttributesSchema } from './TemplateAttributes.js';
|
|
11
|
-
import { TemplateError } from './TemplateError.js';
|
|
12
|
-
/**
|
|
13
|
-
* The `Template` object stores all information about a template and has methods for reading and rendering.
|
|
14
|
-
*
|
|
15
|
-
* ```TypeScript
|
|
16
|
-
* let template = new Template({
|
|
17
|
-
* content: '<%- value %>',
|
|
18
|
-
* attributes: {
|
|
19
|
-
* to: 'value.txt',
|
|
20
|
-
* },
|
|
21
|
-
* dataSchema: z.object({value: z.number()}),
|
|
22
|
-
* });
|
|
23
|
-
*
|
|
24
|
-
* console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @typeParam A A Zod schema type used to define the custom template attributes type.
|
|
28
|
-
* @typeParam D A Zod schema type used to define the template datatype.
|
|
29
|
-
*/ export class Template {
|
|
30
|
-
/** Template path. */ path;
|
|
31
|
-
/** Template content. Templates are written in [EJS](https://ejs.co/). */ content;
|
|
32
|
-
/** 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. */ attributes;
|
|
33
|
-
/** Template function. */ fn;
|
|
34
|
-
/** Zod schema for custom template attributes. */ attributesSchema;
|
|
35
|
-
/** Zod schema for template data. */ dataSchema;
|
|
36
|
-
constructor({ path: templatePath, content, attributes, attributesSchema, dataSchema }){
|
|
37
|
-
if (templatePath) {
|
|
38
|
-
this.path = templatePath;
|
|
39
|
-
}
|
|
40
|
-
this.content = content;
|
|
41
|
-
this.attributes = attributes;
|
|
42
|
-
this.fn = ejs.compile(this.content);
|
|
43
|
-
if (attributesSchema) {
|
|
44
|
-
this.attributesSchema = attributesSchema;
|
|
45
|
-
} else {
|
|
46
|
-
this.attributesSchema = null;
|
|
47
|
-
}
|
|
48
|
-
if (dataSchema) {
|
|
49
|
-
this.dataSchema = dataSchema;
|
|
50
|
-
} else {
|
|
51
|
-
this.dataSchema = null;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Reads a template from a file.
|
|
56
|
-
*
|
|
57
|
-
* @param templatePath Path to the template file.
|
|
58
|
-
*/ static async read(templatePath, options) {
|
|
59
|
-
let { data: rawAttributes, content } = matter(await fs.readFile(templatePath, 'utf8'));
|
|
60
|
-
// extending another template
|
|
61
|
-
if (typeof rawAttributes.extends === 'string' && rawAttributes.extends) {
|
|
62
|
-
let extendedTemplate = await Template.read(path.join(path.dirname(templatePath), rawAttributes.extends), options); // you can point to any template, but it will be parsed with the same options, including attribute or data schema
|
|
63
|
-
// attributes are recursively merged
|
|
64
|
-
rawAttributes = merge(extendedTemplate.attributes, rawAttributes);
|
|
65
|
-
// if content is empty, use content from the extended template
|
|
66
|
-
if (!content.trim()) {
|
|
67
|
-
content = extendedTemplate.content;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
let attributesSchema = options?.attributesSchema ? templateAttributesSchema.merge(options.attributesSchema) : templateAttributesSchema;
|
|
71
|
-
let attributesParseResult = attributesSchema.safeParse(rawAttributes);
|
|
72
|
-
if (!attributesParseResult.success) {
|
|
73
|
-
throw new TemplateError('INVALID_ATTRIBUTES', {
|
|
74
|
-
cause: attributesParseResult.error,
|
|
75
|
-
data: {
|
|
76
|
-
path: templatePath,
|
|
77
|
-
zodIssues: attributesParseResult.error.issues
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
let attributes = attributesParseResult.data;
|
|
82
|
-
return new this({
|
|
83
|
-
path: templatePath,
|
|
84
|
-
attributes,
|
|
85
|
-
content,
|
|
86
|
-
attributesSchema: options?.attributesSchema,
|
|
87
|
-
dataSchema: options?.dataSchema
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Reads a template from a file and then renders it.
|
|
92
|
-
*
|
|
93
|
-
* @param templatePath Path to the template file.
|
|
94
|
-
* @param data Data used for rendering the template.
|
|
95
|
-
* @param options Options object.
|
|
96
|
-
* @return Template renders.
|
|
97
|
-
*/ static async readAndRender(templatePath, data, options) {
|
|
98
|
-
let template = await Template.read(templatePath, {
|
|
99
|
-
attributesSchema: options?.attributesSchema,
|
|
100
|
-
dataSchema: options?.dataSchema
|
|
101
|
-
});
|
|
102
|
-
return template.render(data, {
|
|
103
|
-
prettierOptions: options?.prettierOptions,
|
|
104
|
-
disablePrettier: options?.disablePrettier
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Renders the template using data.
|
|
109
|
-
*
|
|
110
|
-
* @param data Data used for rendering the template.
|
|
111
|
-
* @param options Options object.
|
|
112
|
-
* @return Template renders.
|
|
113
|
-
*/ async render(data, options) {
|
|
114
|
-
let renders = [];
|
|
115
|
-
for (let variables of Array.isArray(this.attributes.variables) ? this.attributes.variables : [
|
|
116
|
-
this.attributes.variables
|
|
117
|
-
]){
|
|
118
|
-
let renderedAttributes = omit(this.attributes, [
|
|
119
|
-
'if'
|
|
120
|
-
]);
|
|
121
|
-
let renderedVariables = variables;
|
|
122
|
-
if (renderedVariables) {
|
|
123
|
-
for (let [variableName, variableValue] of Object.entries(renderedVariables)){
|
|
124
|
-
if (typeof variableValue === 'string') {
|
|
125
|
-
renderedVariables[variableName] = ejs.render(variableValue, {
|
|
126
|
-
...data
|
|
127
|
-
});
|
|
128
|
-
} else {
|
|
129
|
-
renderedVariables[variableName] = variableValue;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
renderedAttributes.variables = renderedVariables;
|
|
133
|
-
}
|
|
134
|
-
let dataWithAttributes = {
|
|
135
|
-
...data,
|
|
136
|
-
attributes: renderedAttributes
|
|
137
|
-
};
|
|
138
|
-
if (this.attributes.if) {
|
|
139
|
-
let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);
|
|
140
|
-
let parsedIf = yaml.parse(renderedIf);
|
|
141
|
-
if (parsedIf === false) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);
|
|
146
|
-
dataWithAttributes.attributes = renderedAttributes;
|
|
147
|
-
let content;
|
|
148
|
-
try {
|
|
149
|
-
content = this.fn(dataWithAttributes);
|
|
150
|
-
} catch (error) {
|
|
151
|
-
throw new TemplateError('FAILED_RENDER', {
|
|
152
|
-
cause: error
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
if (!options?.disablePrettier) {
|
|
156
|
-
try {
|
|
157
|
-
content = await prettier.format(content, {
|
|
158
|
-
...options?.prettierOptions,
|
|
159
|
-
filepath: renderedAttributes.to
|
|
160
|
-
});
|
|
161
|
-
} catch (error) {
|
|
162
|
-
if (!NO_INFERRED_PARSER_REGEXP.test(error.message)) {
|
|
163
|
-
throw new TemplateError('FAILED_FORMAT', {
|
|
164
|
-
cause: error
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
renders.push({
|
|
170
|
-
attributes: renderedAttributes,
|
|
171
|
-
data: data ?? null,
|
|
172
|
-
content
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
return renders;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
//# sourceMappingURL=Template.js.map
|
package/dist/Template.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Template.ts"],"sourcesContent":["import ejs from 'ejs';\nimport fs from 'fs-extra';\nimport matter from 'gray-matter';\nimport merge from 'lodash.merge';\nimport omit from 'lodash.omit';\nimport path from 'node:path';\nimport prettier from 'prettier';\nimport yaml from 'yaml';\nimport {type z, type AnyZodObject} from 'zod';\n\nimport {NO_INFERRED_PARSER_REGEXP} from './constants.js';\nimport {templateAttributesSchema, type TemplateAttributes} from './TemplateAttributes.js';\nimport {TemplateError} from './TemplateError.js';\nimport {type TemplateFunction} from './TemplateFunction.js';\nimport {type TemplateRender} from './TemplateRender.js';\nimport {type TemplateRenders} from './TemplateRenders.js';\n\nexport type TemplateOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Template path. */\n path?: string;\n\n /** Template content. */\n content: string;\n\n /** Template attributes. */\n attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadAndRenderOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateRenderOptions = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n};\n\n/**\n * The `Template` object stores all information about a template and has methods for reading and rendering.\n *\n * ```TypeScript\n * let template = new Template({\n * content: '<%- value %>',\n * attributes: {\n * to: 'value.txt',\n * },\n * dataSchema: z.object({value: z.number()}),\n * });\n *\n * console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]\n * ```\n *\n * @typeParam A A Zod schema type used to define the custom template attributes type.\n * @typeParam D A Zod schema type used to define the template datatype.\n */\nexport class Template<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> {\n /** Template path. */\n path?: string;\n\n /** Template content. Templates are written in [EJS](https://ejs.co/). */\n readonly content: string;\n\n /** 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. */\n readonly attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Template function. */\n private readonly fn: TemplateFunction;\n\n /** Zod schema for custom template attributes. */\n readonly attributesSchema: A extends AnyZodObject ? A : null;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends AnyZodObject ? D : null;\n\n constructor({\n path: templatePath,\n content,\n attributes,\n attributesSchema,\n dataSchema,\n }: TemplateOptions<A, D>) {\n if (templatePath) {\n this.path = templatePath;\n }\n\n this.content = content;\n this.attributes = attributes;\n this.fn = ejs.compile(this.content);\n\n if (attributesSchema) {\n this.attributesSchema = attributesSchema as A extends AnyZodObject ? A : null;\n } else {\n this.attributesSchema = null as A extends AnyZodObject ? A : null;\n }\n\n if (dataSchema) {\n this.dataSchema = dataSchema as D extends AnyZodObject ? D : null;\n } else {\n this.dataSchema = null as D extends AnyZodObject ? D : null;\n }\n }\n\n /**\n * Reads a template from a file.\n *\n * @param templatePath Path to the template file.\n */\n static async read<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n >(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {\n let {data: rawAttributes, content} = matter(await fs.readFile(templatePath, 'utf8'));\n\n // extending another template\n if (typeof rawAttributes.extends === 'string' && rawAttributes.extends) {\n let extendedTemplate = await Template.read(\n path.join(path.dirname(templatePath), rawAttributes.extends),\n options,\n ); // you can point to any template, but it will be parsed with the same options, including attribute or data schema\n\n // attributes are recursively merged\n rawAttributes = merge(extendedTemplate.attributes, rawAttributes);\n\n // if content is empty, use content from the extended template\n if (!content.trim()) {\n content = extendedTemplate.content;\n }\n }\n\n let attributesSchema = options?.attributesSchema\n ? templateAttributesSchema.merge(options.attributesSchema)\n : templateAttributesSchema;\n let attributesParseResult = attributesSchema.safeParse(rawAttributes);\n\n if (!attributesParseResult.success) {\n throw new TemplateError('INVALID_ATTRIBUTES', {\n cause: attributesParseResult.error,\n data: {\n path: templatePath,\n zodIssues: attributesParseResult.error.issues,\n },\n });\n }\n\n let attributes = attributesParseResult.data;\n\n return new this({\n path: templatePath,\n attributes,\n content,\n attributesSchema: options?.attributesSchema,\n dataSchema: options?.dataSchema,\n } as TemplateOptions<A, D>);\n }\n\n /**\n * Reads a template from a file and then renders it.\n *\n * @param templatePath Path to the template file.\n * @param data Data used for rendering the template.\n * @param options Options object.\n * @return Template renders.\n */\n static async readAndRender<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n >(\n templatePath: string,\n data: D extends undefined ? null : z.infer<NonNullable<D>>,\n options?: TemplateReadAndRenderOptions<A, D>,\n ) {\n let template = await Template.read<A, D>(templatePath, {\n attributesSchema: options?.attributesSchema,\n dataSchema: options?.dataSchema,\n });\n\n return template.render(data, {\n prettierOptions: options?.prettierOptions,\n disablePrettier: options?.disablePrettier,\n });\n }\n\n /**\n * Renders the template using data.\n *\n * @param data Data used for rendering the template.\n * @param options Options object.\n * @return Template renders.\n */\n async render(\n data: D extends undefined ? null : z.infer<NonNullable<D>>,\n options?: TemplateRenderOptions,\n ): Promise<TemplateRenders<A, D>> {\n let renders: TemplateRenders<A, D> = [];\n\n for (let variables of Array.isArray(this.attributes.variables)\n ? this.attributes.variables\n : [this.attributes.variables]) {\n let renderedAttributes = omit(this.attributes, ['if']);\n let renderedVariables = variables;\n\n if (renderedVariables) {\n for (let [variableName, variableValue] of Object.entries(renderedVariables)) {\n if (typeof variableValue === 'string') {\n renderedVariables[variableName] = ejs.render(variableValue, {...data});\n } else {\n renderedVariables[variableName] = variableValue;\n }\n }\n\n renderedAttributes.variables = renderedVariables;\n }\n\n let dataWithAttributes = {...data, attributes: renderedAttributes};\n\n if (this.attributes.if) {\n let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);\n let parsedIf = yaml.parse(renderedIf) as unknown;\n\n if (parsedIf === false) {\n continue;\n }\n }\n\n renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);\n dataWithAttributes.attributes = renderedAttributes;\n\n let content;\n\n try {\n content = this.fn(dataWithAttributes);\n } catch (error: unknown) {\n throw new TemplateError('FAILED_RENDER', {cause: error});\n }\n\n if (!options?.disablePrettier) {\n try {\n content = await prettier.format(content, {\n ...options?.prettierOptions,\n filepath: renderedAttributes.to,\n });\n } catch (error: unknown) {\n if (!NO_INFERRED_PARSER_REGEXP.test((error as Error).message)) {\n throw new TemplateError('FAILED_FORMAT', {cause: error});\n }\n }\n }\n\n renders.push({\n attributes: renderedAttributes,\n data: data ?? null,\n content,\n } as TemplateRender<A, D>);\n }\n\n return renders;\n }\n}\n"],"names":["ejs","fs","matter","merge","omit","path","prettier","yaml","NO_INFERRED_PARSER_REGEXP","templateAttributesSchema","TemplateError","Template","content","attributes","fn","attributesSchema","dataSchema","constructor","templatePath","compile","read","options","data","rawAttributes","readFile","extends","extendedTemplate","join","dirname","trim","attributesParseResult","safeParse","success","cause","error","zodIssues","issues","readAndRender","template","render","prettierOptions","disablePrettier","renders","variables","Array","isArray","renderedAttributes","renderedVariables","variableName","variableValue","Object","entries","dataWithAttributes","if","renderedIf","parsedIf","parse","to","format","filepath","test","message","push"],"mappings":"AAAA,OAAOA,SAAS,MAAM;AACtB,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,YAAY,cAAc;AACjC,OAAOC,WAAW,eAAe;AACjC,OAAOC,UAAU,cAAc;AAC/B,OAAOC,UAAU,YAAY;AAC7B,OAAOC,cAAc,WAAW;AAChC,OAAOC,UAAU,OAAO;AAGxB,SAAQC,yBAAyB,QAAO,iBAAiB;AACzD,SAAQC,wBAAwB,QAAgC,0BAA0B;AAC1F,SAAQC,aAAa,QAAO,qBAAqB;AA+DjD;;;;;;;;;;;;;;;;;CAiBC,GACD,O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BACdV;YACF;QACF;QAEA,OAAO8B;IACT;AACF"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { templateVariablesSchema } from './TemplateVariables.js';
|
|
3
|
-
/**
|
|
4
|
-
* Zod schema for {@link TemplateAttributes}.
|
|
5
|
-
*/ export const templateAttributesSchema = z.object({
|
|
6
|
-
extends: z.string().optional(),
|
|
7
|
-
to: z.string(),
|
|
8
|
-
if: z.string().optional(),
|
|
9
|
-
variables: templateVariablesSchema.optional()
|
|
10
|
-
}).strict();
|
|
11
|
-
|
|
12
|
-
//# sourceMappingURL=TemplateAttributes.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateAttributes.ts"],"sourcesContent":["import {z} from 'zod';\n\nimport {templateVariablesSchema} from './TemplateVariables.js';\n\n/**\n * Zod schema for {@link TemplateAttributes}.\n */\nexport const templateAttributesSchema = z\n .object({\n extends: z.string().optional(),\n to: z.string(),\n if: z.string().optional(),\n variables: templateVariablesSchema.optional(),\n })\n .strict();\n\n/**\n * Type representing template attributes.\n */\nexport type TemplateAttributes = z.infer<typeof templateAttributesSchema>;\n"],"names":["z","templateVariablesSchema","templateAttributesSchema","object","extends","string","optional","to","if","variables","strict"],"mappings":"AAAA,SAAQA,CAAC,QAAO,MAAM;AAEtB,SAAQC,uBAAuB,QAAO,yBAAyB;AAE/D;;CAEC,GACD,OAAO,MAAMC,2BAA2BF,EACrCG,MAAM,CAAC;IACNC,SAASJ,EAAEK,MAAM,GAAGC,QAAQ;IAC5BC,IAAIP,EAAEK,MAAM;IACZG,IAAIR,EAAEK,MAAM,GAAGC,QAAQ;IACvBG,WAAWR,wBAAwBK,QAAQ;AAC7C,GACCI,MAAM,GAAG"}
|
package/dist/TemplateContent.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateContent.ts"],"sourcesContent":["/**\n * Type representing a template content.\n */\nexport type TemplateContent = string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAAqC"}
|
package/dist/TemplateError.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createCustomErrorWithData } from '@jakubmazanec/error';
|
|
2
|
-
/**
|
|
3
|
-
* A subclass of `Error` that indicates a template failure.
|
|
4
|
-
*/ export const TemplateError = createCustomErrorWithData('TemplateError', {
|
|
5
|
-
INVALID_ATTRIBUTES: 'Invalid template attributes.',
|
|
6
|
-
FAILED_FORMAT: 'Template render formatting has failed.',
|
|
7
|
-
FAILED_RENDER: 'Template rendering has failed.'
|
|
8
|
-
}, class TemplateErrorData {
|
|
9
|
-
template;
|
|
10
|
-
path;
|
|
11
|
-
zodIssues;
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
//# sourceMappingURL=TemplateError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateError.ts"],"sourcesContent":["import {createCustomErrorWithData} from '@jakubmazanec/error';\nimport {type ZodIssue} from 'zod';\n\nimport {type Template} from './Template.js';\n\n/**\n * A subclass of `Error` that indicates a template failure.\n */\nexport const TemplateError = createCustomErrorWithData(\n 'TemplateError',\n {\n INVALID_ATTRIBUTES: 'Invalid template attributes.',\n FAILED_FORMAT: 'Template render formatting has failed.',\n FAILED_RENDER: 'Template rendering has failed.',\n },\n class TemplateErrorData {\n template?: Template;\n path?: string;\n zodIssues?: ZodIssue[];\n },\n);\n"],"names":["createCustomErrorWithData","TemplateError","INVALID_ATTRIBUTES","FAILED_FORMAT","FAILED_RENDER","TemplateErrorData","template","path","zodIssues"],"mappings":"AAAA,SAAQA,yBAAyB,QAAO,sBAAsB;AAK9D;;CAEC,GACD,OAAO,MAAMC,gBAAgBD,0BAC3B,iBACA;IACEE,oBAAoB;IACpBC,eAAe;IACfC,eAAe;AACjB,GACA,MAAMC;IACJC,SAAoB;IACpBC,KAAc;IACdC,UAAuB;AACzB,GACA"}
|
package/dist/TemplateFunction.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateFunction.ts"],"sourcesContent":["/**\n * Type representing a template function that takes data and returns rendered string.\n */\nexport type TemplateFunction = (data?: Record<string, unknown>) => string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAA0E"}
|
package/dist/TemplateRender.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* An object type representing the result of a template render.
|
|
3
|
-
*
|
|
4
|
-
* @typeParam A Type of Zod schema used to validate the custom template attributes.
|
|
5
|
-
* @typeParam D Type of Zod schema used to define the type of template data.
|
|
6
|
-
*/ export { };
|
|
7
|
-
|
|
8
|
-
//# sourceMappingURL=TemplateRender.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateRender.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateAttributes} from './TemplateAttributes.js';\n\n/**\n * An object type representing the result of a template render.\n *\n * @typeParam A Type of Zod schema used to validate the custom template attributes.\n * @typeParam D Type of Zod schema used to define the type of template data.\n */\nexport type TemplateRender<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = {\n /** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.\n */\n attributes: A extends undefined\n ? Omit<TemplateAttributes, 'if'>\n : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;\n\n /** Data used to render the template. */\n data: D extends undefined ? null : z.infer<NonNullable<D>>;\n\n /** Rendered template content. */\n content: string;\n};\n"],"names":[],"mappings":"AAIA;;;;;CAKC,GACD,WAeE"}
|
package/dist/TemplateRenders.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateRenders.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateRender} from './TemplateRender.js';\n\nexport type TemplateRenders<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = Array<TemplateRender<A, D>>;\n"],"names":[],"mappings":"AAIA,WAGgC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/TemplateVariables.ts"],"sourcesContent":["import * as z from 'zod';\n\n/**\n * Zod schema for {@link TemplateAttributesVariables}.\n */\nexport const templateVariablesSchema = z.union([\n z.record(z.unknown()),\n z.array(z.record(z.unknown())),\n]);\n\n/**\n * Type of `variables`` property of {@link TemplateAttributes}.\n */\nexport type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;\n"],"names":["z","templateVariablesSchema","union","record","unknown","array"],"mappings":"AAAA,YAAYA,OAAO,MAAM;AAEzB;;CAEC,GACD,OAAO,MAAMC,0BAA0BD,EAAEE,KAAK,CAAC;IAC7CF,EAAEG,MAAM,CAACH,EAAEI,OAAO;IAClBJ,EAAEK,KAAK,CAACL,EAAEG,MAAM,CAACH,EAAEI,OAAO;CAC3B,EAAE"}
|
package/dist/constants.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/constants.ts"],"sourcesContent":["export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;\n"],"names":["NO_INFERRED_PARSER_REGEXP"],"mappings":"AAAA,OAAO,MAAMA,4BAA4B,oBAAoB"}
|
package/dist/main.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './Template.js';
|
|
2
|
-
export * from './TemplateAttributes.js';
|
|
3
|
-
export * from './TemplateContent.js';
|
|
4
|
-
export * from './TemplateError.js';
|
|
5
|
-
export * from './TemplateFunction.js';
|
|
6
|
-
export * from './TemplateRender.js';
|
|
7
|
-
export * from './TemplateRenders.js';
|
|
8
|
-
export * from './TemplateVariables.js';
|
|
9
|
-
|
|
10
|
-
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './Template.js';\nexport * from './TemplateAttributes.js';\nexport * from './TemplateContent.js';\nexport * from './TemplateError.js';\nexport * from './TemplateFunction.js';\nexport * from './TemplateRender.js';\nexport * from './TemplateRenders.js';\nexport * from './TemplateVariables.js';\n"],"names":[],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,0BAA0B;AACxC,cAAc,uBAAuB;AACrC,cAAc,qBAAqB;AACnC,cAAc,wBAAwB;AACtC,cAAc,sBAAsB;AACpC,cAAc,uBAAuB;AACrC,cAAc,yBAAyB"}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{src → source}/main.ts
RENAMED
|
File without changes
|