@jakubmazanec/template 3.0.0-unstable.a43a6cfaf → 3.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/build/Template.js CHANGED
@@ -1,6 +1,3 @@
1
- import { NO_INFERRED_PARSER_REGEXP } from "./constants.js";
2
- import { templateAttributesSchema } from "./TemplateAttributes.js";
3
- import { TemplateError } from "./TemplateError.js";
4
1
  import ejs from "ejs";
5
2
  import fs from "fs-extra";
6
3
  import matter from "gray-matter";
@@ -8,145 +5,158 @@ import _ from "lodash";
8
5
  import path from "node:path";
9
6
  import prettier from "prettier";
10
7
  import yaml from "yaml";
11
- // source/Template.ts
12
- /**
13
- * The `Template` object stores all information about a template and has methods for reading and
14
- * rendering.
15
- *
16
- * ```TypeScript
17
- * let template = new Template({
18
- * content: '<%- value %>',
19
- * attributes: {
20
- * to: 'value.txt',
21
- * },
22
- * dataSchema: z.object({value: z.number()}),
23
- * });
24
- *
25
- * console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]
26
- * ```
27
- *
28
- * @typeParam A A Zod schema type used to define the custom template attributes type.
29
- * @typeParam D A Zod schema type used to define the template datatype.
30
- */
31
- var Template = class Template {
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. */
33
- attributes;
34
- /** Zod schema for custom template attributes. */
35
- attributesSchema;
36
- /** Template content. Templates are written in [EJS](https://ejs.co/). */
37
- content;
38
- /** Zod schema for template data. */
39
- dataSchema;
40
- /** Template path. */
41
- path;
42
- /** Template function. */
43
- fn;
44
- constructor({ path: templatePath, content, attributes, attributesSchema, dataSchema }) {
45
- if (templatePath) this.path = templatePath;
46
- this.content = content;
47
- this.attributes = attributes;
48
- this.fn = ejs.compile(this.content);
49
- if (attributesSchema) this.attributesSchema = attributesSchema;
50
- else this.attributesSchema = null;
51
- if (dataSchema) this.dataSchema = dataSchema;
52
- else this.dataSchema = null;
53
- }
54
- /**
55
- * Reads a template from a file.
56
- *
57
- * @param templatePath Path to the template file.
58
- */
59
- static async read(templatePath, options) {
60
- let { data: rawAttributes, content } = matter(await fs.readFile(templatePath, "utf8"));
61
- if (typeof rawAttributes.extends === "string" && rawAttributes.extends) {
62
- let extendedTemplate = await Template.read(path.join(path.dirname(templatePath), rawAttributes.extends), options);
63
- rawAttributes = _.merge(extendedTemplate.attributes, rawAttributes);
64
- if (!content.trim()) content = extendedTemplate.content;
65
- }
66
- let attributesParseResult = (options?.attributesSchema ? templateAttributesSchema.merge(options.attributesSchema) : templateAttributesSchema).safeParse(rawAttributes);
67
- if (!attributesParseResult.success) throw new TemplateError("INVALID_ATTRIBUTES", {
68
- cause: attributesParseResult.error,
69
- data: {
70
- path: templatePath,
71
- zodIssues: attributesParseResult.error.issues
72
- }
73
- });
74
- let attributes = attributesParseResult.data;
75
- return new this({
76
- path: templatePath,
77
- attributes,
78
- content,
79
- attributesSchema: options?.attributesSchema,
80
- dataSchema: options?.dataSchema
81
- });
82
- }
83
- /**
84
- * Reads a template from a file and then renders it.
85
- *
86
- * @param templatePath Path to the template file.
87
- * @param data Data used for rendering the template.
88
- * @param options Options object.
89
- * @return Template renders.
90
- */
91
- static async readAndRender(templatePath, data, options) {
92
- return (await Template.read(templatePath, {
93
- attributesSchema: options?.attributesSchema,
94
- dataSchema: options?.dataSchema
95
- })).render(data, {
96
- prettierOptions: options?.prettierOptions,
97
- disablePrettier: options?.disablePrettier
98
- });
99
- }
100
- /**
101
- * Renders the template using data.
102
- *
103
- * @param data Data used for rendering the template.
104
- * @param options Options object.
105
- * @return Template renders.
106
- */
107
- async render(data, options) {
108
- let renders = [];
109
- for (let variables of Array.isArray(this.attributes.variables) ? this.attributes.variables : [this.attributes.variables]) {
110
- let renderedAttributes = _.omit(this.attributes, ["if"]);
111
- let renderedVariables = variables;
112
- if (renderedVariables) {
113
- for (let [variableName, variableValue] of Object.entries(renderedVariables)) if (typeof variableValue === "string") renderedVariables[variableName] = ejs.render(variableValue, { ...data });
114
- else renderedVariables[variableName] = variableValue;
115
- renderedAttributes.variables = renderedVariables;
116
- }
117
- let dataWithAttributes = {
118
- ...data,
119
- attributes: renderedAttributes
120
- };
121
- if (this.attributes.if) {
122
- let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);
123
- if (yaml.parse(renderedIf) === false) continue;
124
- }
125
- renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);
126
- dataWithAttributes.attributes = renderedAttributes;
127
- let content;
128
- try {
129
- content = this.fn(dataWithAttributes);
130
- } catch (error) {
131
- throw new TemplateError("FAILED_RENDER", { cause: error });
132
- }
133
- if (!options?.disablePrettier) try {
134
- content = await prettier.format(content, {
135
- ...options?.prettierOptions,
136
- filepath: renderedAttributes.to
137
- });
138
- } catch (error) {
139
- if (!NO_INFERRED_PARSER_REGEXP.test(error.message)) throw new TemplateError("FAILED_FORMAT", { cause: error });
140
- }
141
- renders.push({
142
- attributes: renderedAttributes,
143
- data: data ?? null,
144
- content
145
- });
146
- }
147
- return renders;
148
- }
149
- };
150
- export { Template };
151
-
152
- //# sourceMappingURL=Template.js.map
8
+ import { NO_INFERRED_PARSER_REGEXP } from "./constants.js";
9
+ import { templateAttributesSchema } from "./TemplateAttributes.js";
10
+ import { TemplateError } from "./TemplateError.js";
11
+ export class Template {
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. */
13
+ attributes;
14
+ /** Zod schema for custom template attributes. */
15
+ attributesSchema;
16
+ /** Template content. Templates are written in [EJS](https://ejs.co/). */
17
+ content;
18
+ /** Zod schema for template data. */
19
+ dataSchema;
20
+ /** Template path. */
21
+ path;
22
+ /** Template function. */
23
+ fn;
24
+ constructor({
25
+ path: templatePath,
26
+ content,
27
+ attributes,
28
+ attributesSchema,
29
+ dataSchema
30
+ }) {
31
+ if (templatePath) {
32
+ this.path = templatePath;
33
+ }
34
+ this.content = content;
35
+ this.attributes = attributes;
36
+ this.fn = ejs.compile(this.content);
37
+ if (attributesSchema) {
38
+ this.attributesSchema = attributesSchema;
39
+ } else {
40
+ this.attributesSchema = null;
41
+ }
42
+ if (dataSchema) {
43
+ this.dataSchema = dataSchema;
44
+ } else {
45
+ this.dataSchema = null;
46
+ }
47
+ }
48
+ /**
49
+ * Reads a template from a file.
50
+ *
51
+ * @param templatePath Path to the template file.
52
+ */
53
+ static async read(templatePath, options) {
54
+ let { data: rawAttributes, content } = matter(await fs.readFile(templatePath, "utf8"));
55
+ if (typeof rawAttributes.extends === "string" && rawAttributes.extends) {
56
+ let extendedTemplate = await Template.read(
57
+ path.join(path.dirname(templatePath), rawAttributes.extends),
58
+ options
59
+ );
60
+ rawAttributes = _.merge(extendedTemplate.attributes, rawAttributes);
61
+ if (!content.trim()) {
62
+ content = extendedTemplate.content;
63
+ }
64
+ }
65
+ let attributesSchema = options?.attributesSchema ? templateAttributesSchema.merge(options.attributesSchema) : templateAttributesSchema;
66
+ let attributesParseResult = attributesSchema.safeParse(rawAttributes);
67
+ if (!attributesParseResult.success) {
68
+ throw new TemplateError("INVALID_ATTRIBUTES", {
69
+ cause: attributesParseResult.error,
70
+ data: {
71
+ path: templatePath,
72
+ zodIssues: attributesParseResult.error.issues
73
+ }
74
+ });
75
+ }
76
+ let attributes = attributesParseResult.data;
77
+ return new this({
78
+ path: templatePath,
79
+ attributes,
80
+ content,
81
+ attributesSchema: options?.attributesSchema,
82
+ dataSchema: options?.dataSchema
83
+ });
84
+ }
85
+ /**
86
+ * Reads a template from a file and then renders it.
87
+ *
88
+ * @param templatePath Path to the template file.
89
+ * @param data Data used for rendering the template.
90
+ * @param options Options object.
91
+ * @return Template renders.
92
+ */
93
+ static async readAndRender(templatePath, data, options) {
94
+ let template = await Template.read(templatePath, {
95
+ attributesSchema: options?.attributesSchema,
96
+ dataSchema: options?.dataSchema
97
+ });
98
+ return template.render(data, {
99
+ prettierOptions: options?.prettierOptions,
100
+ disablePrettier: options?.disablePrettier
101
+ });
102
+ }
103
+ /**
104
+ * Renders the template using data.
105
+ *
106
+ * @param data Data used for rendering the template.
107
+ * @param options Options object.
108
+ * @return Template renders.
109
+ */
110
+ async render(data, options) {
111
+ let renders = [];
112
+ for (let variables of Array.isArray(this.attributes.variables) ? this.attributes.variables : [this.attributes.variables]) {
113
+ let renderedAttributes = _.omit(this.attributes, ["if"]);
114
+ let renderedVariables = variables;
115
+ if (renderedVariables) {
116
+ for (let [variableName, variableValue] of Object.entries(renderedVariables)) {
117
+ if (typeof variableValue === "string") {
118
+ renderedVariables[variableName] = ejs.render(variableValue, { ...data });
119
+ } else {
120
+ renderedVariables[variableName] = variableValue;
121
+ }
122
+ }
123
+ renderedAttributes.variables = renderedVariables;
124
+ }
125
+ let dataWithAttributes = { ...data, attributes: renderedAttributes };
126
+ if (this.attributes.if) {
127
+ let renderedIf = ejs.render(this.attributes.if, dataWithAttributes);
128
+ let parsedIf = yaml.parse(renderedIf);
129
+ if (parsedIf === false) {
130
+ continue;
131
+ }
132
+ }
133
+ renderedAttributes.to = ejs.render(renderedAttributes.to, dataWithAttributes);
134
+ dataWithAttributes.attributes = renderedAttributes;
135
+ let content;
136
+ try {
137
+ content = this.fn(dataWithAttributes);
138
+ } catch (error) {
139
+ throw new TemplateError("FAILED_RENDER", { cause: error });
140
+ }
141
+ if (!options?.disablePrettier) {
142
+ try {
143
+ content = await prettier.format(content, {
144
+ ...options?.prettierOptions,
145
+ filepath: renderedAttributes.to
146
+ });
147
+ } catch (error) {
148
+ if (!NO_INFERRED_PARSER_REGEXP.test(error.message)) {
149
+ throw new TemplateError("FAILED_FORMAT", { cause: error });
150
+ }
151
+ }
152
+ }
153
+ renders.push({
154
+ attributes: renderedAttributes,
155
+ data: data ?? null,
156
+ content
157
+ });
158
+ }
159
+ return renders;
160
+ }
161
+ }
162
+ //# sourceMappingURL=Template.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"Template.js","names":[],"sources":["../source/Template.ts"],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4FA,IAAa,WAAb,MAAa,SAGX;;CAEA;;CAIA;;CAGA;;CAGA;;CAGA;;CAGA;CAEA,YAAY,EACV,MAAM,cACN,SACA,YACA,kBACA,cACwB;EACxB,IAAI,cACF,KAAK,OAAO;EAGd,KAAK,UAAU;EACf,KAAK,aAAa;EAClB,KAAK,KAAK,IAAI,QAAQ,KAAK,OAAO;EAElC,IAAI,kBACF,KAAK,mBAAmB;OAExB,KAAK,mBAAmB;EAG1B,IAAI,YACF,KAAK,aAAa;OAElB,KAAK,aAAa;CAEtB;;;;;;CAOA,aAAa,KAGX,cAAsB,SAA8D;EACpF,IAAI,EAAC,MAAM,eAAe,YAAW,OAAO,MAAM,GAAG,SAAS,cAAc,MAAM,CAAC;EAGnF,IAAI,OAAO,cAAc,YAAY,YAAY,cAAc,SAAS;GACtE,IAAI,mBAAmB,MAAM,SAAS,KACpC,KAAK,KAAK,KAAK,QAAQ,YAAY,GAAG,cAAc,OAAO,GAC3D,OACF;GAGA,gBAAgB,EAAE,MAAM,iBAAiB,YAAY,aAAa;GAGlE,IAAI,CAAC,QAAQ,KAAK,GAChB,UAAU,iBAAiB;EAE/B;EAMA,IAAI,yBAHF,SAAS,mBACP,yBAAyB,MAAM,QAAQ,gBAAgB,IACvD,yBAAA,CACyC,UAAU,aAAa;EAEpE,IAAI,CAAC,sBAAsB,SACzB,MAAM,IAAI,cAAc,sBAAsB;GAC5C,OAAO,sBAAsB;GAC7B,MAAM;IACJ,MAAM;IACN,WAAW,sBAAsB,MAAM;GACzC;EACF,CAAC;EAGH,IAAI,aAAa,sBAAsB;EAEvC,OAAO,IAAI,KAAK;GACd,MAAM;GACN;GACA;GACA,kBAAkB,SAAS;GAC3B,YAAY,SAAS;EACvB,CAA0B;CAC5B;;;;;;;;;CAUA,aAA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I,CAAC,0BAA0B,KAAM,MAAgB,OAAO,GAC1D,MAAM,IAAI,cAAc,iBAAiB,EAAC,OAAO,MAAK,CAAC;GAE3D;GAGF,QAAQ,KAAK;IACX,YAAY;IACZ,MAAM,QAAQ;IACd;GACF,CAAyB;EAC3B;EAEA,OAAO;CACT;AACF"}
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 _ 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;AACh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AY,IAAI;AAAA,UACpC;AAAA,QACF;AAEA,2BAAmB,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
+ "names": []
7
+ }
@@ -1,15 +1,9 @@
1
- import { templateVariablesSchema } from "./TemplateVariables.js";
2
1
  import { z } from "zod";
3
- // source/TemplateAttributes.ts
4
- /**
5
- * Zod schema for {@link TemplateAttributes}.
6
- */
7
- const templateAttributesSchema = z.object({
8
- extends: z.string().optional(),
9
- to: z.string(),
10
- if: z.string().optional(),
11
- variables: templateVariablesSchema.optional()
2
+ import { templateVariablesSchema } from "./TemplateVariables.js";
3
+ export const templateAttributesSchema = z.object({
4
+ extends: z.string().optional(),
5
+ to: z.string(),
6
+ if: z.string().optional(),
7
+ variables: templateVariablesSchema.optional()
12
8
  }).strict();
13
- export { templateAttributesSchema };
14
-
15
- //# sourceMappingURL=TemplateAttributes.js.map
9
+ //# sourceMappingURL=TemplateAttributes.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"TemplateAttributes.js","names":[],"sources":["../source/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"],"mappings":";;;;;;AAOA,MAAa,2BAA2B,EACrC,OAAO;CACN,SAAS,EAAE,OAAO,CAAC,CAAC,SAAS;CAC7B,IAAI,EAAE,OAAO;CACb,IAAI,EAAE,OAAO,CAAC,CAAC,SAAS;CACxB,WAAW,wBAAwB,SAAS;AAC9C,CAAC,CAAC,CACD,OAAO"}
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 @@
1
+ //# sourceMappingURL=TemplateContent.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -1,17 +1,15 @@
1
1
  import { createCustomErrorWithData } from "@jakubmazanec/error";
2
- // source/TemplateError.ts
3
- /**
4
- * A subclass of `Error` that indicates a template failure.
5
- */
6
- const TemplateError = createCustomErrorWithData("TemplateError", {
7
- INVALID_ATTRIBUTES: "Invalid template attributes.",
8
- FAILED_FORMAT: "Template render formatting has failed.",
9
- FAILED_RENDER: "Template rendering has failed."
10
- }, class TemplateErrorData {
11
- path;
12
- template;
13
- zodIssues;
14
- });
15
- export { TemplateError };
16
-
17
- //# sourceMappingURL=TemplateError.js.map
2
+ export const TemplateError = createCustomErrorWithData(
3
+ "TemplateError",
4
+ {
5
+ INVALID_ATTRIBUTES: "Invalid template attributes.",
6
+ FAILED_FORMAT: "Template render formatting has failed.",
7
+ FAILED_RENDER: "Template rendering has failed."
8
+ },
9
+ class TemplateErrorData {
10
+ path;
11
+ template;
12
+ zodIssues;
13
+ }
14
+ );
15
+ //# sourceMappingURL=TemplateError.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"TemplateError.js","names":[],"sources":["../source/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 path?: string;\n template?: Template;\n zodIssues?: ZodIssue[];\n },\n);\n"],"mappings":";;;;;AAQA,MAAa,gBAAgB,0BAC3B,iBACA;CACE,oBAAoB;CACpB,eAAe;CACf,eAAe;AACjB,GACA,MAAM,kBAAkB;CACtB;CACA;CACA;AACF,CACF"}
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 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
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=TemplateFunction.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=TemplateRender.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=TemplateRenders.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -1,9 +1,6 @@
1
- import * as z$1 from "zod";
2
- // source/TemplateVariables.ts
3
- /**
4
- * Zod schema for {@link TemplateAttributesVariables}.
5
- */
6
- const templateVariablesSchema = z$1.union([z$1.record(z$1.string(), z$1.unknown()), z$1.array(z$1.record(z$1.string(), z$1.unknown()))]);
7
- export { templateVariablesSchema };
8
-
9
- //# sourceMappingURL=TemplateVariables.js.map
1
+ import * as z from "zod";
2
+ export const templateVariablesSchema = z.union([
3
+ z.record(z.string(), z.unknown()),
4
+ z.array(z.record(z.string(), z.unknown()))
5
+ ]);
6
+ //# sourceMappingURL=TemplateVariables.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"TemplateVariables.js","names":["z"],"sources":["../source/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.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"],"mappings":";;;;;AAKA,MAAa,0BAA0BA,IAAE,MAAM,CAC7CA,IAAE,OAAOA,IAAE,OAAO,GAAGA,IAAE,QAAQ,CAAC,GAChCA,IAAE,MAAMA,IAAE,OAAOA,IAAE,OAAO,GAAGA,IAAE,QAAQ,CAAC,CAAC,CAC3C,CAAC"}
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.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
+ "names": []
7
+ }
@@ -1,5 +1,2 @@
1
- // source/constants.ts
2
- const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;
3
- export { NO_INFERRED_PARSER_REGEXP };
4
-
5
- //# sourceMappingURL=constants.js.map
1
+ export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;
2
+ //# sourceMappingURL=constants.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"constants.js","names":[],"sources":["../source/constants.ts"],"sourcesContent":["export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;\n"],"mappings":";AAAA,MAAa,4BAA4B"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../source/constants.ts"],
4
+ "sourcesContent": ["export const NO_INFERRED_PARSER_REGEXP = /parser.*inferred/i;\n"],
5
+ "mappings": "AAAO,aAAM,4BAA4B;",
6
+ "names": []
7
+ }
package/build/main.js CHANGED
@@ -1,5 +1,5 @@
1
- import { Template } from "./Template.js";
2
- import { templateAttributesSchema } from "./TemplateAttributes.js";
3
- import { TemplateError } from "./TemplateError.js";
4
- import { templateVariablesSchema } from "./TemplateVariables.js";
5
- export { Template, TemplateError, templateAttributesSchema, templateVariablesSchema };
1
+ export * from "./Template.js";
2
+ export * from "./TemplateAttributes.js";
3
+ export * from "./TemplateError.js";
4
+ export * from "./TemplateVariables.js";
5
+ //# 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 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
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/template",
3
- "version": "3.0.0-unstable.a43a6cfaf",
3
+ "version": "3.0.0",
4
4
  "description": "TypeScript-first library for working with templates using EJS and Zod.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,9 +17,9 @@
17
17
  "source"
18
18
  ],
19
19
  "scripts": {
20
- "build": "del-cli build tsconfig.tsbuildinfo && tsdown && 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 \"tsdown --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
25
  "lint": "eslint . --quiet",
@@ -27,7 +27,7 @@
27
27
  "typecheck": "tsc --project tsconfig.typecheck.json"
28
28
  },
29
29
  "dependencies": {
30
- "@jakubmazanec/error": "4.0.0-unstable.a43a6cfaf",
30
+ "@jakubmazanec/error": "^4.0.0",
31
31
  "ejs": "^7.0.2",
32
32
  "fs-extra": "^11.4.1",
33
33
  "gray-matter": "^4.0.3",
@@ -37,7 +37,7 @@
37
37
  "zod": "^4.6.7"
38
38
  },
39
39
  "devDependencies": {
40
- "@jakubmazanec/eslint-config": "6.0.0-unstable.a43a6cfaf",
40
+ "@jakubmazanec/eslint-config": "^6.0.0",
41
41
  "@types/ejs": "^3.1.5",
42
42
  "@types/fs-extra": "^11.0.4",
43
43
  "@types/lodash": "^4.17.27",
@@ -45,9 +45,9 @@
45
45
  "@vitest/coverage-v8": "^5.0.5",
46
46
  "concurrently": "^10.0.6",
47
47
  "del-cli": "^8.0.1",
48
+ "esbuild": "^0.28.2",
48
49
  "eslint": "^9.39.5",
49
50
  "prettier-plugin-packagejson": "^3.1.0",
50
- "tsdown": "^0.22.0",
51
51
  "typedoc": "^0.28.20",
52
52
  "typedoc-plugin-markdown": "^4.14.1",
53
53
  "typescript": "^6.0.3",