@jakubmazanec/template 2.1.0-unstable.d6c542d → 3.0.0-next.398ed97fe

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/README.md CHANGED
@@ -4,7 +4,6 @@
4
4
  # @jakubmazanec/template
5
5
 
6
6
  TypeScript-first library for working with templates using EJS and Zod.
7
-
8
7
  </div>
9
8
  <!-- header -->
10
9
 
@@ -14,13 +13,14 @@ TypeScript-first library for working with templates using EJS and Zod.
14
13
  npm install @jakubmazanec/template
15
14
  ```
16
15
 
17
- ⚠️ This is an [ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) package!
18
- It cannot be required from a CommonJS module.
16
+ <!-- prerequisites -->
19
17
 
20
18
  #### Prerequisites
21
19
 
22
- - Node.js 20 or later
23
- - TypeScript 5 or later
20
+ - Node.js 26 or later
21
+ - TypeScript 6 or later
22
+
23
+ <!-- prerequisites -->
24
24
 
25
25
  ## Usage
26
26
 
@@ -1,8 +1,8 @@
1
1
  import prettier from 'prettier';
2
- import { type AnyZodObject, type z } from 'zod';
2
+ import { type z, type ZodObject } from 'zod';
3
3
  import { type TemplateAttributes } from './TemplateAttributes.js';
4
4
  import { type TemplateRenders } from './TemplateRenders.js';
5
- export type TemplateOptions<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> = {
5
+ export type TemplateOptions<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined> = {
6
6
  /** Template path. */
7
7
  path?: string;
8
8
  /** Template content. */
@@ -14,13 +14,13 @@ export type TemplateOptions<A extends AnyZodObject | undefined = undefined, D ex
14
14
  /** Zod schema for template data. */
15
15
  dataSchema?: D | undefined;
16
16
  };
17
- export type TemplateReadOptions<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> = {
17
+ export type TemplateReadOptions<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined> = {
18
18
  /** Zod schema for custom template attributes. */
19
19
  attributesSchema?: A | undefined;
20
20
  /** Zod schema for template data. */
21
21
  dataSchema?: D | undefined;
22
22
  };
23
- export type TemplateReadAndRenderOptions<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> = {
23
+ export type TemplateReadAndRenderOptions<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined> = {
24
24
  /** Prettier options. */
25
25
  prettierOptions?: prettier.Options | undefined;
26
26
  /** Disable formatting the rendered template using Prettier. */
@@ -37,7 +37,8 @@ export type TemplateRenderOptions = {
37
37
  disablePrettier?: boolean | undefined;
38
38
  };
39
39
  /**
40
- * The `Template` object stores all information about a template and has methods for reading and rendering.
40
+ * The `Template` object stores all information about a template and has methods for reading and
41
+ * rendering.
41
42
  *
42
43
  * ```TypeScript
43
44
  * let template = new Template({
@@ -54,26 +55,26 @@ export type TemplateRenderOptions = {
54
55
  * @typeParam A A Zod schema type used to define the custom template attributes type.
55
56
  * @typeParam D A Zod schema type used to define the template datatype.
56
57
  */
57
- export declare class Template<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> {
58
- /** Template path. */
59
- path?: string;
60
- /** Template content. Templates are written in [EJS](https://ejs.co/). */
61
- readonly content: string;
58
+ export declare class Template<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined> {
62
59
  /** 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. */
63
60
  readonly attributes: A extends undefined ? TemplateAttributes : TemplateAttributes & z.infer<NonNullable<A>>;
64
- /** Template function. */
65
- private readonly fn;
66
61
  /** Zod schema for custom template attributes. */
67
- readonly attributesSchema: A extends AnyZodObject ? A : null;
62
+ readonly attributesSchema: A extends ZodObject ? A : null;
63
+ /** Template content. Templates are written in [EJS](https://ejs.co/). */
64
+ readonly content: string;
68
65
  /** Zod schema for template data. */
69
- readonly dataSchema: D extends AnyZodObject ? D : null;
66
+ readonly dataSchema: D extends ZodObject ? D : null;
67
+ /** Template path. */
68
+ path?: string;
69
+ /** Template function. */
70
+ private readonly fn;
70
71
  constructor({ path: templatePath, content, attributes, attributesSchema, dataSchema, }: TemplateOptions<A, D>);
71
72
  /**
72
73
  * Reads a template from a file.
73
74
  *
74
75
  * @param templatePath Path to the template file.
75
76
  */
76
- static read<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined>(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>>;
77
+ static read<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined>(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>>;
77
78
  /**
78
79
  * Reads a template from a file and then renders it.
79
80
  *
@@ -82,7 +83,7 @@ export declare class Template<A extends AnyZodObject | undefined = undefined, D
82
83
  * @param options Options object.
83
84
  * @return Template renders.
84
85
  */
85
- static readAndRender<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined>(templatePath: string, data: D extends undefined ? null : z.infer<NonNullable<D>>, options?: TemplateReadAndRenderOptions<A, D>): Promise<TemplateRenders<A, D>>;
86
+ static readAndRender<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined>(templatePath: string, data: D extends undefined ? null : z.infer<NonNullable<D>>, options?: TemplateReadAndRenderOptions<A, D>): Promise<TemplateRenders<A, D>>;
86
87
  /**
87
88
  * Renders the template using data.
88
89
  *
package/build/Template.js CHANGED
@@ -1,9 +1,7 @@
1
- "use strict";
2
1
  import ejs from "ejs";
3
2
  import fs from "fs-extra";
4
3
  import matter from "gray-matter";
5
- import merge from "lodash.merge";
6
- import omit from "lodash.omit";
4
+ import _ from "lodash";
7
5
  import path from "node:path";
8
6
  import prettier from "prettier";
9
7
  import yaml from "yaml";
@@ -11,18 +9,18 @@ import { NO_INFERRED_PARSER_REGEXP } from "./constants.js";
11
9
  import { templateAttributesSchema } from "./TemplateAttributes.js";
12
10
  import { TemplateError } from "./TemplateError.js";
13
11
  export class Template {
14
- /** Template path. */
15
- path;
16
- /** Template content. Templates are written in [EJS](https://ejs.co/). */
17
- content;
18
12
  /** 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
13
  attributes;
20
- /** Template function. */
21
- fn;
22
14
  /** Zod schema for custom template attributes. */
23
15
  attributesSchema;
16
+ /** Template content. Templates are written in [EJS](https://ejs.co/). */
17
+ content;
24
18
  /** Zod schema for template data. */
25
19
  dataSchema;
20
+ /** Template path. */
21
+ path;
22
+ /** Template function. */
23
+ fn;
26
24
  constructor({
27
25
  path: templatePath,
28
26
  content,
@@ -59,7 +57,7 @@ export class Template {
59
57
  path.join(path.dirname(templatePath), rawAttributes.extends),
60
58
  options
61
59
  );
62
- rawAttributes = merge(extendedTemplate.attributes, rawAttributes);
60
+ rawAttributes = _.merge(extendedTemplate.attributes, rawAttributes);
63
61
  if (!content.trim()) {
64
62
  content = extendedTemplate.content;
65
63
  }
@@ -112,7 +110,7 @@ export class Template {
112
110
  async render(data, options) {
113
111
  let renders = [];
114
112
  for (let variables of Array.isArray(this.attributes.variables) ? this.attributes.variables : [this.attributes.variables]) {
115
- let renderedAttributes = omit(this.attributes, ["if"]);
113
+ let renderedAttributes = _.omit(this.attributes, ["if"]);
116
114
  let renderedVariables = variables;
117
115
  if (renderedVariables) {
118
116
  for (let [variableName, variableValue] of Object.entries(renderedVariables)) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;AAG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EjE,UAAI,KAAK,WAAW,IAAI;AACtB,YAAI,aAAa,IAAI,OAAO,KAAK,WAAW,IAAI,kBAAkB;AAClE,YAAI,WAAW,KAAK,MAAM,UAAU;AAEpC,YAAI,aAAa,OAAO;AACtB;AAAA,QACF;AAAA,MACF;AAEA,yBAAmB,KAAK,IAAI,OAAO,mBAAmB,IAAI,kBAAkB;AAC5E,yBAAmB,aAAa;AAEhC,UAAI;AAEJ,UAAI;AACF,kBAAU,KAAK,GAAG,kBAAkB;AAAA,MACtC,SAAS,OAAgB;AACvB,cAAM,IAAI,cAAc,iBAAiB,EAAC,OAAO,MAAK,CAAC;AAAA,MACzD;AAEA,UAAI,CAAC,SAAS,iBAAiB;AAC7B,YAAI;AACF,oBAAU,MAAM,SAAS,OAAO,SAAS;AAAA,YACvC,GAAG,SAAS;AAAA,YACZ,UAAU,mBAAmB;AAAA,UAC/B,CAAC;AAAA,QACH,SAAS,OAAgB;AACvB,cAAI,CAAC,0BAA0B,KAAM,MAAgB,OAAO,GAAG;AAC7D,kBAAM,IAAI,cAAc,iBAAiB,EAAC,OAAO,MAAK,CAAC;AAAA,UACzD;AAAA,QACF;AAAA,MACF;AAEA,cAAQ,KAAK;AAAA,QACX,YAAY;AAAA,QACZ,MAAM,QAAQ;AAAA,QACd;AAAA,MACF,CAAyB;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT;AACF;",
4
+ "sourcesContent": ["import ejs from 'ejs';\nimport fs from 'fs-extra';\nimport matter from 'gray-matter';\nimport _ from 'lodash';\nimport path from 'node:path';\nimport prettier from 'prettier';\nimport yaml from 'yaml';\nimport {type z, type ZodObject} 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 ZodObject | undefined = undefined,\n D extends ZodObject | 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 ZodObject | undefined = undefined,\n D extends ZodObject | 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 ZodObject | undefined = undefined,\n D extends ZodObject | 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\n * 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 ZodObject | undefined = undefined,\n D extends ZodObject | undefined = undefined,\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 /** Zod schema for custom template attributes. */\n readonly attributesSchema: A extends ZodObject ? A : null;\n\n /** Template content. Templates are written in [EJS](https://ejs.co/). */\n readonly content: string;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends ZodObject ? D : null;\n\n /** Template path. */\n path?: string;\n\n /** Template function. */\n private readonly fn: TemplateFunction;\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 ZodObject ? A : null;\n } else {\n this.attributesSchema = null as A extends ZodObject ? A : null;\n }\n\n if (dataSchema) {\n this.dataSchema = dataSchema as D extends ZodObject ? D : null;\n } else {\n this.dataSchema = null as D extends ZodObject ? 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 ZodObject | undefined = undefined,\n D extends ZodObject | 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 ZodObject | undefined = undefined,\n D extends ZodObject | 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,OAAO;AACd,OAAO,UAAU;AACjB,OAAO,cAAc;AACrB,OAAO,UAAU;AAGjB,SAAQ,iCAAgC;AACxC,SAAiC,gCAA+B;AAChE,SAAQ,qBAAoB;AAiFrB,aAAM,SAGX;AAAA;AAAA,EAES;AAAA;AAAA,EAIA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA,EAGT;AAAA;AAAA,EAGiB;AAAA,EAEjB,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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GAAG,SAAS;AAAA,YACZ,UAAU,mBAAmB;AAAA,UAC/B,CAAC;AAAA,QACH,SAAS,OAAgB;AACvB,cAAI,CAAC,0BAA0B,KAAM,MAAgB,OAAO,GAAG;AAC7D,kBAAM,IAAI,cAAc,iBAAiB,EAAC,OAAO,MAAK,CAAC;AAAA,UACzD;AAAA,QACF;AAAA,MACF;AAEA,cAAQ,KAAK;AAAA,QACX,YAAY;AAAA,QACZ,MAAM,QAAQ;AAAA,QACd;AAAA,MACF,CAAyB;AAAA,IAC3B;AAEA,WAAO;AAAA,EACT;AACF;",
6
6
  "names": []
7
7
  }
@@ -6,18 +6,8 @@ export declare const templateAttributesSchema: z.ZodObject<{
6
6
  extends: z.ZodOptional<z.ZodString>;
7
7
  to: z.ZodString;
8
8
  if: z.ZodOptional<z.ZodString>;
9
- variables: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">]>>;
10
- }, "strict", z.ZodTypeAny, {
11
- to: string;
12
- extends?: string | undefined;
13
- if?: string | undefined;
14
- variables?: Record<string, unknown> | Record<string, unknown>[] | undefined;
15
- }, {
16
- to: string;
17
- extends?: string | undefined;
18
- if?: string | undefined;
19
- variables?: Record<string, unknown> | Record<string, unknown>[] | undefined;
20
- }>;
9
+ variables: z.ZodOptional<z.ZodUnion<readonly [z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>]>>;
10
+ }, z.core.$strict>;
21
11
  /**
22
12
  * Type representing template attributes.
23
13
  */
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  import { z } from "zod";
3
2
  import { templateVariablesSchema } from "./TemplateVariables.js";
4
3
  export const templateAttributesSchema = z.object({
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../source/TemplateAttributes.ts"],
4
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;",
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
6
  "names": []
7
7
  }
@@ -1,2 +1 @@
1
- "use strict";
2
1
  //# sourceMappingURL=TemplateContent.js.map
@@ -4,11 +4,11 @@ import { type Template } from './Template.js';
4
4
  * A subclass of `Error` that indicates a template failure.
5
5
  */
6
6
  export declare const TemplateError: new (code: "INVALID_ATTRIBUTES" | "FAILED_FORMAT" | "FAILED_RENDER", options?: import("@jakubmazanec/error").CustomErrorWithDataOptions<{
7
- template?: Template;
8
7
  path?: string;
8
+ template?: Template;
9
9
  zodIssues?: ZodIssue[];
10
10
  }> | undefined) => import("@jakubmazanec/error").CustomErrorWithData<"INVALID_ATTRIBUTES" | "FAILED_FORMAT" | "FAILED_RENDER" | "UNKNOWN_ERROR", {
11
- template?: Template;
12
11
  path?: string;
12
+ template?: Template;
13
13
  zodIssues?: ZodIssue[];
14
14
  }>;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  import { createCustomErrorWithData } from "@jakubmazanec/error";
3
2
  export const TemplateError = createCustomErrorWithData(
4
3
  "TemplateError",
@@ -8,8 +7,8 @@ export const TemplateError = createCustomErrorWithData(
8
7
  FAILED_RENDER: "Template rendering has failed."
9
8
  },
10
9
  class TemplateErrorData {
11
- template;
12
10
  path;
11
+ template;
13
12
  zodIssues;
14
13
  }
15
14
  );
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;",
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 path?: string;\n template?: Template;\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
6
  "names": []
7
7
  }
@@ -1,2 +1 @@
1
- "use strict";
2
1
  //# sourceMappingURL=TemplateFunction.js.map
@@ -6,8 +6,10 @@ import { type TemplateAttributes } from './TemplateAttributes.js';
6
6
  * @typeParam A Type of Zod schema used to validate the custom template attributes.
7
7
  * @typeParam D Type of Zod schema used to define the type of template data.
8
8
  */
9
- export type TemplateRender<A extends z.AnyZodObject | undefined = undefined, D extends z.AnyZodObject | undefined = undefined> = {
10
- /** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.
9
+ export type TemplateRender<A extends z.ZodObject | undefined = undefined, D extends z.ZodObject | undefined = undefined> = {
10
+ /**
11
+ * Rendered template attributes. They created from the `to` and `variables` attributes, and from
12
+ * all of the custom template attributes.
11
13
  */
12
14
  attributes: A extends undefined ? Omit<TemplateAttributes, 'if'> : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
13
15
  /** Data used to render the template. */
@@ -1,2 +1 @@
1
- "use strict";
2
1
  //# sourceMappingURL=TemplateRender.js.map
@@ -1,3 +1,3 @@
1
1
  import { type z } from 'zod';
2
2
  import { type TemplateRender } from './TemplateRender.js';
3
- export type TemplateRenders<A extends z.AnyZodObject | undefined = undefined, D extends z.AnyZodObject | undefined = undefined> = Array<TemplateRender<A, D>>;
3
+ export type TemplateRenders<A extends z.ZodObject | undefined = undefined, D extends z.ZodObject | undefined = undefined> = Array<TemplateRender<A, D>>;
@@ -1,2 +1 @@
1
- "use strict";
2
1
  //# sourceMappingURL=TemplateRenders.js.map
@@ -2,7 +2,7 @@ import * as z from 'zod';
2
2
  /**
3
3
  * Zod schema for {@link TemplateAttributesVariables}.
4
4
  */
5
- export declare const templateVariablesSchema: z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">]>;
5
+ export declare const templateVariablesSchema: z.ZodUnion<readonly [z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>]>;
6
6
  /**
7
7
  * Type of `variables`` property of {@link TemplateAttributes}.
8
8
  */
@@ -1,7 +1,6 @@
1
- "use strict";
2
1
  import * as z from "zod";
3
2
  export const templateVariablesSchema = z.union([
4
- z.record(z.unknown()),
5
- z.array(z.record(z.unknown()))
3
+ z.record(z.string(), z.unknown()),
4
+ z.array(z.record(z.string(), z.unknown()))
6
5
  ]);
7
6
  //# sourceMappingURL=TemplateVariables.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;",
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.string(), z.unknown()),\n z.array(z.record(z.string(), 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,OAAO,GAAG,EAAE,QAAQ,CAAC;AAAA,EAChC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,CAAC;AAC3C,CAAC;",
6
6
  "names": []
7
7
  }
@@ -1,3 +1,2 @@
1
- "use strict";
2
1
  export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;
3
2
  //# sourceMappingURL=constants.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../source/constants.ts"],
4
4
  "sourcesContent": ["export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;\n"],
5
- "mappings": ";AAAO,aAAM,4BAA4B;",
5
+ "mappings": "AAAO,aAAM,4BAA4B;",
6
6
  "names": []
7
7
  }
package/build/main.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './Template.js';
2
2
  export * from './TemplateAttributes.js';
3
- export * from './TemplateContent.js';
3
+ export type * from './TemplateContent.js';
4
4
  export * from './TemplateError.js';
5
- export * from './TemplateFunction.js';
6
- export * from './TemplateRender.js';
7
- export * from './TemplateRenders.js';
5
+ export type * from './TemplateFunction.js';
6
+ export type * from './TemplateRender.js';
7
+ export type * from './TemplateRenders.js';
8
8
  export * from './TemplateVariables.js';
package/build/main.js CHANGED
@@ -1,10 +1,5 @@
1
- "use strict";
2
1
  export * from "./Template.js";
3
2
  export * from "./TemplateAttributes.js";
4
- export * from "./TemplateContent.js";
5
3
  export * from "./TemplateError.js";
6
- export * from "./TemplateFunction.js";
7
- export * from "./TemplateRender.js";
8
- export * from "./TemplateRenders.js";
9
4
  export * from "./TemplateVariables.js";
10
5
  //# sourceMappingURL=main.js.map
package/build/main.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
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;",
4
+ "sourcesContent": ["export * from './Template.js';\nexport * from './TemplateAttributes.js';\nexport type * from './TemplateContent.js';\nexport * from './TemplateError.js';\nexport type * from './TemplateFunction.js';\nexport type * from './TemplateRender.js';\nexport type * from './TemplateRenders.js';\nexport * from './TemplateVariables.js';\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;AAEd,cAAc;AAId,cAAc;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/template",
3
- "version": "2.1.0-unstable.d6c542d",
3
+ "version": "3.0.0-next.398ed97fe",
4
4
  "description": "TypeScript-first library for working with templates using EJS and Zod.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,49 +17,45 @@
17
17
  "source"
18
18
  ],
19
19
  "scripts": {
20
- "build": "del-cli build tsconfig.tsbuildinfo && esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap && tsc",
20
+ "build": "del-cli build tsconfig.tsbuildinfo && esbuild \"source/**/*\" --platform=node --target=node26.13 --outdir=build --sourcemap && tsc",
21
21
  "clean": "del-cli coverage build *.tsbuildinfo",
22
- "develop": "concurrently \"esbuild \"source/**/*\" --platform=node --target=node20.8 --outdir=build --sourcemap --watch\" \"tsc --watch\"",
22
+ "develop": "concurrently \"esbuild \"source/**/*\" --platform=node --target=node26.13 --outdir=build --sourcemap --watch\" \"tsc --watch\"",
23
23
  "document": "del-cli docs && typedoc --options typedoc.config.cjs",
24
24
  "format": "prettier . --write --ignore-unknown",
25
- "lint": "eslint .",
25
+ "lint": "eslint . --quiet",
26
26
  "test": "del-cli coverage && vitest run --coverage --passWithNoTests",
27
27
  "typecheck": "tsc --project tsconfig.typecheck.json"
28
28
  },
29
29
  "dependencies": {
30
- "@jakubmazanec/error": "3.1.0-unstable.d6c542d",
31
- "ejs": "^3.1.10",
32
- "fs-extra": "^11.2.0",
30
+ "@jakubmazanec/error": "4.0.0-next.398ed97fe",
31
+ "ejs": "^7.0.2",
32
+ "fs-extra": "^11.4.1",
33
33
  "gray-matter": "^4.0.3",
34
- "lodash.merge": "^4.6.2",
35
- "lodash.omit": "^4.5.0",
36
- "prettier": "^3.3.3",
37
- "yaml": "^2.6.0",
38
- "zod": "^3.23.8"
34
+ "lodash": "^4.18.1",
35
+ "prettier": "^3.9.10",
36
+ "yaml": "^2.9.1",
37
+ "zod": "^4.6.7"
39
38
  },
40
39
  "devDependencies": {
41
- "@jakubmazanec/eslint-config": "4.0.0-unstable.d6c542d",
40
+ "@jakubmazanec/eslint-config": "6.0.0-next.398ed97fe",
42
41
  "@types/ejs": "^3.1.5",
43
42
  "@types/fs-extra": "^11.0.4",
44
- "@types/lodash": "^4.17.13",
45
- "@types/lodash.merge": "^4.6.9",
46
- "@types/lodash.omit": "^4.5.9",
47
- "@types/node": "^22.9.0",
48
- "@vitest/coverage-v8": "^2.1.4",
49
- "concurrently": "^9.1.0",
50
- "del-cli": "^6.0.0",
51
- "esbuild": "^0.24.0",
52
- "eslint": "^9.14.0",
53
- "lodash": "^4.17.21",
54
- "prettier": "^3.3.3",
55
- "prettier-plugin-packagejson": "^2.5.3",
56
- "typedoc": "^0.26.11",
57
- "typedoc-plugin-markdown": "^4.2.10",
58
- "typescript": "^5.6.3",
59
- "vitest": "^2.1.4"
43
+ "@types/lodash": "^4.17.27",
44
+ "@types/node": "^26.6.6",
45
+ "@vitest/coverage-v8": "^5.0.5",
46
+ "concurrently": "^10.0.6",
47
+ "del-cli": "^8.0.1",
48
+ "esbuild": "^0.28.2",
49
+ "eslint": "^9.39.5",
50
+ "prettier-plugin-packagejson": "^3.1.0",
51
+ "typedoc": "^0.28.20",
52
+ "typedoc-plugin-markdown": "^4.14.1",
53
+ "typescript": "^6.0.3",
54
+ "vitest": "^5.0.5"
60
55
  },
61
- "packageManager": "npm@10.9.0",
56
+ "packageManager": "npm@12.2.1",
62
57
  "engines": {
63
- "node": "^22.11.0"
58
+ "node": "^26.13.0",
59
+ "npm": "^12.2.1"
64
60
  }
65
61
  }
@@ -1,12 +1,11 @@
1
1
  import ejs from 'ejs';
2
2
  import fs from 'fs-extra';
3
3
  import matter from 'gray-matter';
4
- import merge from 'lodash.merge';
5
- import omit from 'lodash.omit';
4
+ import _ from 'lodash';
6
5
  import path from 'node:path';
7
6
  import prettier from 'prettier';
8
7
  import yaml from 'yaml';
9
- import {type AnyZodObject, type z} from 'zod';
8
+ import {type z, type ZodObject} from 'zod';
10
9
 
11
10
  import {NO_INFERRED_PARSER_REGEXP} from './constants.js';
12
11
  import {type TemplateAttributes, templateAttributesSchema} from './TemplateAttributes.js';
@@ -16,8 +15,8 @@ import {type TemplateRender} from './TemplateRender.js';
16
15
  import {type TemplateRenders} from './TemplateRenders.js';
17
16
 
18
17
  export type TemplateOptions<
19
- A extends AnyZodObject | undefined = undefined,
20
- D extends AnyZodObject | undefined = undefined,
18
+ A extends ZodObject | undefined = undefined,
19
+ D extends ZodObject | undefined = undefined,
21
20
  > = {
22
21
  /** Template path. */
23
22
  path?: string;
@@ -37,8 +36,8 @@ export type TemplateOptions<
37
36
  };
38
37
 
39
38
  export type TemplateReadOptions<
40
- A extends AnyZodObject | undefined = undefined,
41
- D extends AnyZodObject | undefined = undefined,
39
+ A extends ZodObject | undefined = undefined,
40
+ D extends ZodObject | undefined = undefined,
42
41
  > = {
43
42
  /** Zod schema for custom template attributes. */
44
43
  attributesSchema?: A | undefined;
@@ -48,8 +47,8 @@ export type TemplateReadOptions<
48
47
  };
49
48
 
50
49
  export type TemplateReadAndRenderOptions<
51
- A extends AnyZodObject | undefined = undefined,
52
- D extends AnyZodObject | undefined = undefined,
50
+ A extends ZodObject | undefined = undefined,
51
+ D extends ZodObject | undefined = undefined,
53
52
  > = {
54
53
  /** Prettier options. */
55
54
  prettierOptions?: prettier.Options | undefined;
@@ -73,7 +72,8 @@ export type TemplateRenderOptions = {
73
72
  };
74
73
 
75
74
  /**
76
- * The `Template` object stores all information about a template and has methods for reading and rendering.
75
+ * The `Template` object stores all information about a template and has methods for reading and
76
+ * rendering.
77
77
  *
78
78
  * ```TypeScript
79
79
  * let template = new Template({
@@ -91,27 +91,27 @@ export type TemplateRenderOptions = {
91
91
  * @typeParam D A Zod schema type used to define the template datatype.
92
92
  */
93
93
  export class Template<
94
- A extends AnyZodObject | undefined = undefined,
95
- D extends AnyZodObject | undefined = undefined,
94
+ A extends ZodObject | undefined = undefined,
95
+ D extends ZodObject | undefined = undefined,
96
96
  > {
97
- /** Template path. */
98
- path?: string;
99
-
100
- /** Template content. Templates are written in [EJS](https://ejs.co/). */
101
- readonly content: string;
102
-
103
97
  /** 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. */
104
98
  readonly attributes: A extends undefined ? TemplateAttributes
105
99
  : TemplateAttributes & z.infer<NonNullable<A>>;
106
100
 
107
- /** Template function. */
108
- private readonly fn: TemplateFunction;
109
-
110
101
  /** Zod schema for custom template attributes. */
111
- readonly attributesSchema: A extends AnyZodObject ? A : null;
102
+ readonly attributesSchema: A extends ZodObject ? A : null;
103
+
104
+ /** Template content. Templates are written in [EJS](https://ejs.co/). */
105
+ readonly content: string;
112
106
 
113
107
  /** Zod schema for template data. */
114
- readonly dataSchema: D extends AnyZodObject ? D : null;
108
+ readonly dataSchema: D extends ZodObject ? D : null;
109
+
110
+ /** Template path. */
111
+ path?: string;
112
+
113
+ /** Template function. */
114
+ private readonly fn: TemplateFunction;
115
115
 
116
116
  constructor({
117
117
  path: templatePath,
@@ -129,15 +129,15 @@ export class Template<
129
129
  this.fn = ejs.compile(this.content);
130
130
 
131
131
  if (attributesSchema) {
132
- this.attributesSchema = attributesSchema as A extends AnyZodObject ? A : null;
132
+ this.attributesSchema = attributesSchema as A extends ZodObject ? A : null;
133
133
  } else {
134
- this.attributesSchema = null as A extends AnyZodObject ? A : null;
134
+ this.attributesSchema = null as A extends ZodObject ? A : null;
135
135
  }
136
136
 
137
137
  if (dataSchema) {
138
- this.dataSchema = dataSchema as D extends AnyZodObject ? D : null;
138
+ this.dataSchema = dataSchema as D extends ZodObject ? D : null;
139
139
  } else {
140
- this.dataSchema = null as D extends AnyZodObject ? D : null;
140
+ this.dataSchema = null as D extends ZodObject ? D : null;
141
141
  }
142
142
  }
143
143
 
@@ -147,8 +147,8 @@ export class Template<
147
147
  * @param templatePath Path to the template file.
148
148
  */
149
149
  static async read<
150
- A extends AnyZodObject | undefined = undefined,
151
- D extends AnyZodObject | undefined = undefined,
150
+ A extends ZodObject | undefined = undefined,
151
+ D extends ZodObject | undefined = undefined,
152
152
  >(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {
153
153
  let {data: rawAttributes, content} = matter(await fs.readFile(templatePath, 'utf8'));
154
154
 
@@ -160,7 +160,7 @@ export class Template<
160
160
  ); // you can point to any template, but it will be parsed with the same options, including attribute or data schema
161
161
 
162
162
  // attributes are recursively merged
163
- rawAttributes = merge(extendedTemplate.attributes, rawAttributes);
163
+ rawAttributes = _.merge(extendedTemplate.attributes, rawAttributes);
164
164
 
165
165
  // if content is empty, use content from the extended template
166
166
  if (!content.trim()) {
@@ -204,8 +204,8 @@ export class Template<
204
204
  * @return Template renders.
205
205
  */
206
206
  static async readAndRender<
207
- A extends AnyZodObject | undefined = undefined,
208
- D extends AnyZodObject | undefined = undefined,
207
+ A extends ZodObject | undefined = undefined,
208
+ D extends ZodObject | undefined = undefined,
209
209
  >(
210
210
  templatePath: string,
211
211
  data: D extends undefined ? null : z.infer<NonNullable<D>>,
@@ -238,7 +238,7 @@ export class Template<
238
238
  for (let variables of Array.isArray(this.attributes.variables) ?
239
239
  this.attributes.variables
240
240
  : [this.attributes.variables]) {
241
- let renderedAttributes = omit(this.attributes, ['if']);
241
+ let renderedAttributes = _.omit(this.attributes, ['if']);
242
242
  let renderedVariables = variables;
243
243
 
244
244
  if (renderedVariables) {
@@ -14,8 +14,8 @@ export const TemplateError = createCustomErrorWithData(
14
14
  FAILED_RENDER: 'Template rendering has failed.',
15
15
  },
16
16
  class TemplateErrorData {
17
- template?: Template;
18
17
  path?: string;
18
+ template?: Template;
19
19
  zodIssues?: ZodIssue[];
20
20
  },
21
21
  );
@@ -9,10 +9,12 @@ import {type TemplateAttributes} from './TemplateAttributes.js';
9
9
  * @typeParam D Type of Zod schema used to define the type of template data.
10
10
  */
11
11
  export type TemplateRender<
12
- A extends z.AnyZodObject | undefined = undefined,
13
- D extends z.AnyZodObject | undefined = undefined,
12
+ A extends z.ZodObject | undefined = undefined,
13
+ D extends z.ZodObject | undefined = undefined,
14
14
  > = {
15
- /** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.
15
+ /**
16
+ * Rendered template attributes. They created from the `to` and `variables` attributes, and from
17
+ * all of the custom template attributes.
16
18
  */
17
19
  attributes: A extends undefined ? Omit<TemplateAttributes, 'if'>
18
20
  : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
@@ -3,6 +3,6 @@ import {type z} from 'zod';
3
3
  import {type TemplateRender} from './TemplateRender.js';
4
4
 
5
5
  export type TemplateRenders<
6
- A extends z.AnyZodObject | undefined = undefined,
7
- D extends z.AnyZodObject | undefined = undefined,
6
+ A extends z.ZodObject | undefined = undefined,
7
+ D extends z.ZodObject | undefined = undefined,
8
8
  > = Array<TemplateRender<A, D>>;
@@ -4,8 +4,8 @@ import * as z from 'zod';
4
4
  * Zod schema for {@link TemplateAttributesVariables}.
5
5
  */
6
6
  export const templateVariablesSchema = z.union([
7
- z.record(z.unknown()),
8
- z.array(z.record(z.unknown())),
7
+ z.record(z.string(), z.unknown()),
8
+ z.array(z.record(z.string(), z.unknown())),
9
9
  ]);
10
10
 
11
11
  /**
package/source/main.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export * from './Template.js';
2
2
  export * from './TemplateAttributes.js';
3
- export * from './TemplateContent.js';
3
+ export type * from './TemplateContent.js';
4
4
  export * from './TemplateError.js';
5
- export * from './TemplateFunction.js';
6
- export * from './TemplateRender.js';
7
- export * from './TemplateRenders.js';
5
+ export type * from './TemplateFunction.js';
6
+ export type * from './TemplateRender.js';
7
+ export type * from './TemplateRenders.js';
8
8
  export * from './TemplateVariables.js';