@jakubmazanec/template 2.0.14 → 2.0.15-unstable.aad0af37
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/build/Template.d.ts +9 -9
- package/build/Template.js.map +1 -1
- package/build/TemplateAttributes.d.ts +2 -12
- package/build/TemplateRender.d.ts +1 -1
- package/build/TemplateRenders.d.ts +1 -1
- package/build/TemplateVariables.d.ts +1 -1
- package/build/TemplateVariables.js +2 -2
- package/build/TemplateVariables.js.map +2 -2
- package/package.json +3 -3
- package/source/Template.ts +19 -19
- package/source/TemplateRender.ts +2 -2
- package/source/TemplateRenders.ts +2 -2
- package/source/TemplateVariables.ts +2 -2
package/build/Template.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import prettier from 'prettier';
|
|
2
|
-
import { type
|
|
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
|
|
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
|
|
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
|
|
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. */
|
|
@@ -54,7 +54,7 @@ export type TemplateRenderOptions = {
|
|
|
54
54
|
* @typeParam A A Zod schema type used to define the custom template attributes type.
|
|
55
55
|
* @typeParam D A Zod schema type used to define the template datatype.
|
|
56
56
|
*/
|
|
57
|
-
export declare class Template<A extends
|
|
57
|
+
export declare class Template<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined> {
|
|
58
58
|
/** Template path. */
|
|
59
59
|
path?: string;
|
|
60
60
|
/** Template content. Templates are written in [EJS](https://ejs.co/). */
|
|
@@ -64,16 +64,16 @@ export declare class Template<A extends AnyZodObject | undefined = undefined, D
|
|
|
64
64
|
/** Template function. */
|
|
65
65
|
private readonly fn;
|
|
66
66
|
/** Zod schema for custom template attributes. */
|
|
67
|
-
readonly attributesSchema: A extends
|
|
67
|
+
readonly attributesSchema: A extends ZodObject ? A : null;
|
|
68
68
|
/** Zod schema for template data. */
|
|
69
|
-
readonly dataSchema: D extends
|
|
69
|
+
readonly dataSchema: D extends ZodObject ? D : null;
|
|
70
70
|
constructor({ path: templatePath, content, attributes, attributesSchema, dataSchema, }: TemplateOptions<A, D>);
|
|
71
71
|
/**
|
|
72
72
|
* Reads a template from a file.
|
|
73
73
|
*
|
|
74
74
|
* @param templatePath Path to the template file.
|
|
75
75
|
*/
|
|
76
|
-
static read<A extends
|
|
76
|
+
static read<A extends ZodObject | undefined = undefined, D extends ZodObject | undefined = undefined>(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>>;
|
|
77
77
|
/**
|
|
78
78
|
* Reads a template from a file and then renders it.
|
|
79
79
|
*
|
|
@@ -82,7 +82,7 @@ export declare class Template<A extends AnyZodObject | undefined = undefined, D
|
|
|
82
82
|
* @param options Options object.
|
|
83
83
|
* @return Template renders.
|
|
84
84
|
*/
|
|
85
|
-
static readAndRender<A extends
|
|
85
|
+
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
86
|
/**
|
|
87
87
|
* Renders the template using data.
|
|
88
88
|
*
|
package/build/Template.js.map
CHANGED
|
@@ -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"],
|
|
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 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 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 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 ZodObject ? A : null;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends ZodObject ? 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 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
5
|
"mappings": ";AAAA,OAAO,SAAS;AAChB,OAAO,QAAQ;AACf,OAAO,YAAY;AACnB,OAAO,WAAW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mB,YAAY;AAAA,MACjC;AAEA,UAAI,qBAAqB,EAAC,GAAG,MAAM,YAAY,mBAAkB;AAEjE,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;",
|
|
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
|
|
10
|
-
},
|
|
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
|
*/
|
|
@@ -6,7 +6,7 @@ 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.
|
|
9
|
+
export type TemplateRender<A extends z.ZodObject | undefined = undefined, D extends z.ZodObject | undefined = undefined> = {
|
|
10
10
|
/** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.
|
|
11
11
|
*/
|
|
12
12
|
attributes: A extends undefined ? Omit<TemplateAttributes, 'if'> : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;
|
|
@@ -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.
|
|
3
|
+
export type TemplateRenders<A extends z.ZodObject | undefined = undefined, D extends z.ZodObject | undefined = undefined> = Array<TemplateRender<A, D>>;
|
|
@@ -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
|
|
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,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import * as z from "zod";
|
|
3
3
|
export const templateVariablesSchema = z.union([
|
|
4
|
-
z.record(z.unknown()),
|
|
5
|
-
z.array(z.record(z.unknown()))
|
|
4
|
+
z.record(z.string(), z.unknown()),
|
|
5
|
+
z.array(z.record(z.string(), z.unknown()))
|
|
6
6
|
]);
|
|
7
7
|
//# 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,
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jakubmazanec/template",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.15-unstable.aad0af37",
|
|
4
4
|
"description": "TypeScript-first library for working with templates using EJS and Zod.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"typecheck": "tsc --project tsconfig.typecheck.json"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jakubmazanec/error": "
|
|
30
|
+
"@jakubmazanec/error": "3.0.14-unstable.aad0af37",
|
|
31
31
|
"ejs": "^3.1.10",
|
|
32
32
|
"fs-extra": "^11.3.0",
|
|
33
33
|
"gray-matter": "^4.0.3",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"lodash.omit": "^4.5.0",
|
|
36
36
|
"prettier": "^3.6.0",
|
|
37
37
|
"yaml": "^2.8.0",
|
|
38
|
-
"zod": "^
|
|
38
|
+
"zod": "^4.0.17"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@jakubmazanec/eslint-config": "^4.1.0",
|
package/source/Template.ts
CHANGED
|
@@ -6,7 +6,7 @@ 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 z, type ZodObject} from 'zod';
|
|
10
10
|
|
|
11
11
|
import {NO_INFERRED_PARSER_REGEXP} from './constants.js';
|
|
12
12
|
import {type TemplateAttributes, templateAttributesSchema} from './TemplateAttributes.js';
|
|
@@ -16,8 +16,8 @@ import {type TemplateRender} from './TemplateRender.js';
|
|
|
16
16
|
import {type TemplateRenders} from './TemplateRenders.js';
|
|
17
17
|
|
|
18
18
|
export type TemplateOptions<
|
|
19
|
-
A extends
|
|
20
|
-
D extends
|
|
19
|
+
A extends ZodObject | undefined = undefined,
|
|
20
|
+
D extends ZodObject | undefined = undefined,
|
|
21
21
|
> = {
|
|
22
22
|
/** Template path. */
|
|
23
23
|
path?: string;
|
|
@@ -37,8 +37,8 @@ export type TemplateOptions<
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
export type TemplateReadOptions<
|
|
40
|
-
A extends
|
|
41
|
-
D extends
|
|
40
|
+
A extends ZodObject | undefined = undefined,
|
|
41
|
+
D extends ZodObject | undefined = undefined,
|
|
42
42
|
> = {
|
|
43
43
|
/** Zod schema for custom template attributes. */
|
|
44
44
|
attributesSchema?: A | undefined;
|
|
@@ -48,8 +48,8 @@ export type TemplateReadOptions<
|
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
export type TemplateReadAndRenderOptions<
|
|
51
|
-
A extends
|
|
52
|
-
D extends
|
|
51
|
+
A extends ZodObject | undefined = undefined,
|
|
52
|
+
D extends ZodObject | undefined = undefined,
|
|
53
53
|
> = {
|
|
54
54
|
/** Prettier options. */
|
|
55
55
|
prettierOptions?: prettier.Options | undefined;
|
|
@@ -91,8 +91,8 @@ 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
|
|
95
|
-
D extends
|
|
94
|
+
A extends ZodObject | undefined = undefined,
|
|
95
|
+
D extends ZodObject | undefined = undefined,
|
|
96
96
|
> {
|
|
97
97
|
/** Template path. */
|
|
98
98
|
path?: string;
|
|
@@ -108,10 +108,10 @@ export class Template<
|
|
|
108
108
|
private readonly fn: TemplateFunction;
|
|
109
109
|
|
|
110
110
|
/** Zod schema for custom template attributes. */
|
|
111
|
-
readonly attributesSchema: A extends
|
|
111
|
+
readonly attributesSchema: A extends ZodObject ? A : null;
|
|
112
112
|
|
|
113
113
|
/** Zod schema for template data. */
|
|
114
|
-
readonly dataSchema: D extends
|
|
114
|
+
readonly dataSchema: D extends ZodObject ? D : null;
|
|
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
|
|
132
|
+
this.attributesSchema = attributesSchema as A extends ZodObject ? A : null;
|
|
133
133
|
} else {
|
|
134
|
-
this.attributesSchema = null as A extends
|
|
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
|
|
138
|
+
this.dataSchema = dataSchema as D extends ZodObject ? D : null;
|
|
139
139
|
} else {
|
|
140
|
-
this.dataSchema = null as D extends
|
|
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
|
|
151
|
-
D extends
|
|
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
|
|
|
@@ -204,8 +204,8 @@ export class Template<
|
|
|
204
204
|
* @return Template renders.
|
|
205
205
|
*/
|
|
206
206
|
static async readAndRender<
|
|
207
|
-
A extends
|
|
208
|
-
D extends
|
|
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>>,
|
package/source/TemplateRender.ts
CHANGED
|
@@ -9,8 +9,8 @@ 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.
|
|
13
|
-
D extends z.
|
|
12
|
+
A extends z.ZodObject | undefined = undefined,
|
|
13
|
+
D extends z.ZodObject | undefined = undefined,
|
|
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
|
*/
|
|
@@ -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.
|
|
7
|
-
D extends z.
|
|
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
|
/**
|