@jakubmazanec/template 1.0.1-265acad → 1.0.1-2d1145f

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.
Files changed (43) hide show
  1. package/dist/Template.d.ts +11 -6
  2. package/dist/Template.js +14 -9
  3. package/dist/Template.js.map +1 -1
  4. package/dist/{types/TemplateAttributes.d.ts → TemplateAttributes.d.ts} +1 -1
  5. package/dist/{types/TemplateAttributes.js → TemplateAttributes.js} +1 -1
  6. package/dist/TemplateAttributes.js.map +1 -0
  7. package/dist/TemplateContent.js.map +1 -0
  8. package/dist/TemplateFunction.js.map +1 -0
  9. package/dist/TemplateRender.js.map +1 -0
  10. package/dist/TemplateRenders.js.map +1 -0
  11. package/dist/{types/TemplateVariables.d.ts → TemplateVariables.d.ts} +2 -2
  12. package/dist/{types/TemplateVariables.js → TemplateVariables.js} +1 -1
  13. package/dist/TemplateVariables.js.map +1 -0
  14. package/dist/main.d.ts +6 -1
  15. package/dist/main.js +6 -1
  16. package/dist/main.js.map +1 -1
  17. package/package.json +2 -2
  18. package/src/Template.ts +23 -15
  19. package/src/{types/TemplateAttributes.ts → TemplateAttributes.ts} +1 -1
  20. package/src/{types/TemplateVariables.ts → TemplateVariables.ts} +2 -2
  21. package/src/main.ts +6 -1
  22. package/dist/types/TemplateAttributes.js.map +0 -1
  23. package/dist/types/TemplateContent.js.map +0 -1
  24. package/dist/types/TemplateFunction.js.map +0 -1
  25. package/dist/types/TemplateRender.js.map +0 -1
  26. package/dist/types/TemplateRenders.js.map +0 -1
  27. package/dist/types/TemplateVariables.js.map +0 -1
  28. package/dist/types.d.ts +0 -5
  29. package/dist/types.js +0 -7
  30. package/dist/types.js.map +0 -1
  31. package/src/types.ts +0 -5
  32. /package/dist/{types/TemplateContent.d.ts → TemplateContent.d.ts} +0 -0
  33. /package/dist/{types/TemplateContent.js → TemplateContent.js} +0 -0
  34. /package/dist/{types/TemplateFunction.d.ts → TemplateFunction.d.ts} +0 -0
  35. /package/dist/{types/TemplateFunction.js → TemplateFunction.js} +0 -0
  36. /package/dist/{types/TemplateRender.d.ts → TemplateRender.d.ts} +0 -0
  37. /package/dist/{types/TemplateRender.js → TemplateRender.js} +0 -0
  38. /package/dist/{types/TemplateRenders.d.ts → TemplateRenders.d.ts} +0 -0
  39. /package/dist/{types/TemplateRenders.js → TemplateRenders.js} +0 -0
  40. /package/src/{types/TemplateContent.ts → TemplateContent.ts} +0 -0
  41. /package/src/{types/TemplateFunction.ts → TemplateFunction.ts} +0 -0
  42. /package/src/{types/TemplateRender.ts → TemplateRender.ts} +0 -0
  43. /package/src/{types/TemplateRenders.ts → TemplateRenders.ts} +0 -0
@@ -1,7 +1,10 @@
1
1
  import prettier from 'prettier';
2
2
  import { type z, type AnyZodObject } from 'zod';
3
- import { type TemplateAttributes, type TemplateRenders } from './types.js';
3
+ import { type TemplateAttributes } from './TemplateAttributes.js';
4
+ import { type TemplateRenders } from './TemplateRenders.js';
4
5
  export type TemplateOptions<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> = {
6
+ /** Template path. */
7
+ path?: string;
5
8
  /** Template content. */
6
9
  content: string;
7
10
  /** Template attributes. */
@@ -52,6 +55,8 @@ export type TemplateRenderOptions = {
52
55
  * @typeParam D A Zod schema type used to define the template datatype.
53
56
  */
54
57
  export declare class Template<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined> {
58
+ /** Template path. */
59
+ path?: string;
55
60
  /** Template content. Templates are written in [EJS](https://ejs.co/). */
56
61
  readonly content: string;
57
62
  /** 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. */
@@ -62,22 +67,22 @@ export declare class Template<A extends AnyZodObject | undefined = undefined, D
62
67
  readonly attributesSchema: A extends AnyZodObject ? A : null;
63
68
  /** Zod schema for template data. */
64
69
  readonly dataSchema: D extends AnyZodObject ? D : null;
65
- constructor({ content, attributes, attributesSchema, dataSchema }: TemplateOptions<A, D>);
70
+ constructor({ path, content, attributes, attributesSchema, dataSchema }: TemplateOptions<A, D>);
66
71
  /**
67
72
  * Reads a template from a file.
68
73
  *
69
- * @param templateFilePath Path to the template file.
74
+ * @param templatePath Path to the template file.
70
75
  */
71
- static read<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined>(templateFilePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>>;
76
+ static read<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined>(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>>;
72
77
  /**
73
78
  * Reads a template from a file and then renders it.
74
79
  *
75
- * @param templateFilePath Path to the template file.
80
+ * @param templatePath Path to the template file.
76
81
  * @param data Data used for rendering the template.
77
82
  * @param options Options object.
78
83
  * @return Template renders.
79
84
  */
80
- static readAndRender<A extends AnyZodObject | undefined = undefined, D extends AnyZodObject | undefined = undefined>(templateFilePath: string, data: D extends undefined ? null : z.infer<NonNullable<D>>, options?: TemplateReadAndRenderOptions<A, D>): Promise<TemplateRenders<A, D>>;
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>>;
81
86
  /**
82
87
  * Renders the template using data.
83
88
  *
package/dist/Template.js CHANGED
@@ -5,8 +5,8 @@ import omit from 'lodash.omit';
5
5
  import prettier from 'prettier';
6
6
  import yaml from 'yaml';
7
7
  import { NO_INFERRED_PARSER_REGEXP } from './constants.js';
8
+ import { templateAttributesSchema } from './TemplateAttributes.js';
8
9
  import { TemplateError } from './TemplateError.js';
9
- import { templateAttributesSchema } from './types.js';
10
10
  /**
11
11
  * The `Template` object stores all information about a template and has methods for reading and rendering.
12
12
  *
@@ -25,12 +25,16 @@ import { templateAttributesSchema } from './types.js';
25
25
  * @typeParam A A Zod schema type used to define the custom template attributes type.
26
26
  * @typeParam D A Zod schema type used to define the template datatype.
27
27
  */ export class Template {
28
+ /** Template path. */ path;
28
29
  /** Template content. Templates are written in [EJS](https://ejs.co/). */ content;
29
30
  /** Template attributes. These control how the template is rendered. Some attributes can be written in [EJS](https://ejs.co/) and are rendered before the template content is rendered. It is expected that attribute `if` renders to a string that can be parsed as YAML value; if that value is `false`, the rendering is cancelled. Attribute `to` is also rendered and it represents the path of file the resulting template render is supposed to be written into (by the user). Attribute `variables` is an object that is merged with the data object before rendering. If attribute `variables` is an array of objects, that leads to multiple renders, each with different variables object merged with the data. Each variable that is a string is also rendered. */ attributes;
30
31
  /** Template function. */ fn;
31
32
  /** Zod schema for custom template attributes. */ attributesSchema;
32
33
  /** Zod schema for template data. */ dataSchema;
33
- constructor({ content, attributes, attributesSchema, dataSchema }){
34
+ constructor({ path, content, attributes, attributesSchema, dataSchema }){
35
+ if (path) {
36
+ this.path = path;
37
+ }
34
38
  this.content = content;
35
39
  this.attributes = attributes;
36
40
  this.fn = ejs.compile(this.content);
@@ -48,22 +52,23 @@ import { templateAttributesSchema } from './types.js';
48
52
  /**
49
53
  * Reads a template from a file.
50
54
  *
51
- * @param templateFilePath Path to the template file.
52
- */ static async read(templateFilePath, options) {
55
+ * @param templatePath Path to the template file.
56
+ */ static async read(templatePath, options) {
53
57
  var _options, _options1, _options2;
54
- let { data: attributes, content } = matter(await fs.readFile(templateFilePath, 'utf8'));
58
+ let { data: attributes, content } = matter(await fs.readFile(templatePath, 'utf8'));
55
59
  let attributesSchema = ((_options = options) === null || _options === void 0 ? void 0 : _options.attributesSchema) ? templateAttributesSchema.merge(options.attributesSchema) : templateAttributesSchema;
56
60
  let attributesParseResult = attributesSchema.safeParse(attributes);
57
61
  if (!attributesParseResult.success) {
58
62
  throw new TemplateError('INVALID_ATTRIBUTES', {
59
63
  cause: attributesParseResult.error,
60
64
  data: {
61
- path: templateFilePath,
65
+ path: templatePath,
62
66
  zodIssues: attributesParseResult.error.issues
63
67
  }
64
68
  });
65
69
  }
66
70
  return new this({
71
+ path: templatePath,
67
72
  attributes,
68
73
  content,
69
74
  attributesSchema: (_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.attributesSchema,
@@ -73,13 +78,13 @@ import { templateAttributesSchema } from './types.js';
73
78
  /**
74
79
  * Reads a template from a file and then renders it.
75
80
  *
76
- * @param templateFilePath Path to the template file.
81
+ * @param templatePath Path to the template file.
77
82
  * @param data Data used for rendering the template.
78
83
  * @param options Options object.
79
84
  * @return Template renders.
80
- */ static async readAndRender(templateFilePath, data, options) {
85
+ */ static async readAndRender(templatePath, data, options) {
81
86
  var _options, _options1, _options2, _options3;
82
- let template = await Template.read(templateFilePath, {
87
+ let template = await Template.read(templatePath, {
83
88
  attributesSchema: (_options = options) === null || _options === void 0 ? void 0 : _options.attributesSchema,
84
89
  dataSchema: (_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.dataSchema
85
90
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/Template.ts"],"sourcesContent":["import ejs from 'ejs';\nimport fs from 'fs-extra';\nimport matter from 'gray-matter';\nimport omit from 'lodash.omit';\nimport prettier from 'prettier';\nimport yaml from 'yaml';\nimport {type z, type AnyZodObject} from 'zod';\n\nimport {NO_INFERRED_PARSER_REGEXP} from './constants.js';\nimport {TemplateError} from './TemplateError.js';\nimport {\n templateAttributesSchema,\n type TemplateAttributes,\n type TemplateFunction,\n type TemplateRender,\n type TemplateRenders,\n} from './types.js';\n\nexport type TemplateOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Template content. */\n content: string;\n\n /** Template attributes. */\n attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadAndRenderOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateRenderOptions = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n};\n\n/**\n * The `Template` object stores all information about a template and has methods for reading and rendering.\n *\n * ```TypeScript\n * let template = new Template({\n * content: '<%- value %>',\n * attributes: {\n * to: 'value.txt',\n * },\n * dataSchema: z.object({value: z.number()}),\n * });\n *\n * console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]\n * ```\n *\n * @typeParam A A Zod schema type used to define the custom template attributes type.\n * @typeParam D A Zod schema type used to define the template datatype.\n */\nexport class Template<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> {\n /** Template content. Templates are written in [EJS](https://ejs.co/). */\n readonly content: string;\n\n /** Template attributes. These control how the template is rendered. Some attributes can be written in [EJS](https://ejs.co/) and are rendered before the template content is rendered. It is expected that attribute `if` renders to a string that can be parsed as YAML value; if that value is `false`, the rendering is cancelled. Attribute `to` is also rendered and it represents the path of file the resulting template render is supposed to be written into (by the user). Attribute `variables` is an object that is merged with the data object before rendering. If attribute `variables` is an array of objects, that leads to multiple renders, each with different variables object merged with the data. Each variable that is a string is also rendered. */\n readonly attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Template function. */\n private readonly fn: TemplateFunction;\n\n /** Zod schema for custom template attributes. */\n readonly attributesSchema: A extends AnyZodObject ? A : null;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends AnyZodObject ? D : null;\n\n constructor({content, attributes, attributesSchema, dataSchema}: TemplateOptions<A, D>) {\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 templateFilePath Path to the template file.\n */\n static async read<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n >(templateFilePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {\n let {data: attributes, content} = matter(await fs.readFile(templateFilePath, 'utf8'));\n let attributesSchema = options?.attributesSchema\n ? templateAttributesSchema.merge(options.attributesSchema)\n : templateAttributesSchema;\n let attributesParseResult = attributesSchema.safeParse(attributes);\n\n if (!attributesParseResult.success) {\n throw new TemplateError('INVALID_ATTRIBUTES', {\n cause: attributesParseResult.error,\n data: {\n path: templateFilePath,\n zodIssues: attributesParseResult.error.issues,\n },\n });\n }\n\n return new this({\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 templateFilePath 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 templateFilePath: 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>(templateFilePath, {\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 = prettier.format(content, {\n ...options?.prettierOptions,\n filepath: renderedAttributes.to,\n });\n } catch (error: unknown) {\n if (!NO_INFERRED_PARSER_REGEXP.test((error as Error).message)) {\n throw new TemplateError('FAILED_FORMAT', {cause: error});\n }\n }\n }\n\n renders.push({\n attributes: renderedAttributes,\n data: data ?? null,\n content,\n } as TemplateRender<A, D>);\n }\n\n return renders;\n }\n}\n"],"names":["ejs","fs","matter","omit","prettier","yaml","NO_INFERRED_PARSER_REGEXP","TemplateError","templateAttributesSchema","Template","content","attributes","fn","attributesSchema","dataSchema","constructor","compile","read","templateFilePath","options","data","readFile","merge","attributesParseResult","safeParse","success","cause","error","path","zodIssues","issues","readAndRender","template","render","prettierOptions","disablePrettier","renders","variables","Array","isArray","renderedAttributes","renderedVariables","variableName","variableValue","Object","entries","dataWithAttributes","if","renderedIf","parsedIf","parse","to","format","filepath","test","message","push"],"mappings":"AAAA,OAAOA,SAAS,MAAM;AACtB,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,YAAY,cAAc;AACjC,OAAOC,UAAU,cAAc;AAC/B,OAAOC,cAAc,WAAW;AAChC,OAAOC,UAAU,OAAO;AAGxB,SAAQC,yBAAyB,QAAO,iBAAiB;AACzD,SAAQC,aAAa,QAAO,qBAAqB;AACjD,SACEC,wBAAwB,QAKnB,aAAa;AAyDpB;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,MAAMC;IAIX,uEAAuE,GACvE,AAASC,QAAgB;IAEzB,4uBAA4uB,GAC5uB,AAASC,WAEwC;IAEjD,uBAAuB,GACvB,AAAiBC,GAAqB;IAEtC,+CAA+C,GAC/C,AAASC,iBAAoD;IAE7D,kCAAkC,GAClC,AAASC,WAA8C;IAEvDC,YAAY,EAACL,OAAO,EAAEC,UAAU,EAAEE,gBAAgB,EAAEC,UAAU,EAAwB,CAAE;QACtF,IAAI,CAACJ,OAAO,GAAGA;QACf,IAAI,CAACC,UAAU,GAAGA;QAClB,IAAI,CAACC,EAAE,GAAGZ,IAAIgB,OAAO,CAAC,IAAI,CAACN,OAAO;QAElC,IAAIG,kBAAkB;YACpB,IAAI,CAACA,gBAAgB,GAAGA;QAC1B,OAAO;YACL,IAAI,CAACA,gBAAgB,GAAG;QAC1B;QA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EAAE,OAAOnB,OAAgB;gBACvB,MAAM,IAAIpB,cAAc,iBAAiB;oBAACmB,OAAOC;gBAAK;YACxD;YAEA,IAAI,GAACR,WAAAA,qBAAAA,+BAAAA,SAASgB,eAAe,GAAE;gBAC7B,IAAI;wBAEGhB;oBADLT,UAAUN,SAASgD,MAAM,CAAC1C,SAAS;4BAC9BS,YAAAA,qBAAAA,gCAAAA,UAASe,eAAe,AAA3B;wBACAmB,UAAUb,mBAAmBW,EAAE;oBACjC;gBACF,EAAE,OAAOxB,OAAgB;oBACvB,IAAI,CAACrB,0BAA0BgD,IAAI,CAAC,AAAC3B,MAAgB4B,OAAO,GAAG;wBAC7D,MAAM,IAAIhD,cAAc,iBAAiB;4BAACmB,OAAOC;wBAAK;oBACxD;gBACF;YACF;YAEAS,QAAQoB,IAAI,CAAC;gBACX7C,YAAY6B;gBACZpB,MAAMA,QAAQ;gBACdV;YACF;QACF;QAEA,OAAO0B;IACT;AACF"}
1
+ {"version":3,"sources":["../src/Template.ts"],"sourcesContent":["import ejs from 'ejs';\nimport fs from 'fs-extra';\nimport matter from 'gray-matter';\nimport omit from 'lodash.omit';\nimport prettier from 'prettier';\nimport yaml from 'yaml';\nimport {type z, type AnyZodObject} from 'zod';\n\nimport {NO_INFERRED_PARSER_REGEXP} from './constants.js';\nimport {templateAttributesSchema, type TemplateAttributes} from './TemplateAttributes.js';\nimport {TemplateError} from './TemplateError.js';\nimport {type TemplateFunction} from './TemplateFunction.js';\nimport {type TemplateRender} from './TemplateRender.js';\nimport {type TemplateRenders} from './TemplateRenders.js';\n\nexport type TemplateOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Template path. */\n path?: string;\n\n /** Template content. */\n content: string;\n\n /** Template attributes. */\n attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateReadAndRenderOptions<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n\n /** Zod schema for custom template attributes. */\n attributesSchema?: A | undefined;\n\n /** Zod schema for template data. */\n dataSchema?: D | undefined;\n};\n\nexport type TemplateRenderOptions = {\n /** Prettier options. */\n prettierOptions?: prettier.Options | undefined;\n\n /** Disable formatting the rendered template using Prettier. */\n disablePrettier?: boolean | undefined;\n};\n\n/**\n * The `Template` object stores all information about a template and has methods for reading and rendering.\n *\n * ```TypeScript\n * let template = new Template({\n * content: '<%- value %>',\n * attributes: {\n * to: 'value.txt',\n * },\n * dataSchema: z.object({value: z.number()}),\n * });\n *\n * console.log(await template.render({value: 42})); // -> [{attributes: {to: 'value.txt'}, data: {value: 42}, content: '42'}]\n * ```\n *\n * @typeParam A A Zod schema type used to define the custom template attributes type.\n * @typeParam D A Zod schema type used to define the template datatype.\n */\nexport class Template<\n A extends AnyZodObject | undefined = undefined,\n D extends AnyZodObject | undefined = undefined,\n> {\n /** Template path. */\n path?: string;\n\n /** Template content. Templates are written in [EJS](https://ejs.co/). */\n readonly content: string;\n\n /** Template attributes. These control how the template is rendered. Some attributes can be written in [EJS](https://ejs.co/) and are rendered before the template content is rendered. It is expected that attribute `if` renders to a string that can be parsed as YAML value; if that value is `false`, the rendering is cancelled. Attribute `to` is also rendered and it represents the path of file the resulting template render is supposed to be written into (by the user). Attribute `variables` is an object that is merged with the data object before rendering. If attribute `variables` is an array of objects, that leads to multiple renders, each with different variables object merged with the data. Each variable that is a string is also rendered. */\n readonly attributes: A extends undefined\n ? TemplateAttributes\n : TemplateAttributes & z.infer<NonNullable<A>>;\n\n /** Template function. */\n private readonly fn: TemplateFunction;\n\n /** Zod schema for custom template attributes. */\n readonly attributesSchema: A extends AnyZodObject ? A : null;\n\n /** Zod schema for template data. */\n readonly dataSchema: D extends AnyZodObject ? D : null;\n\n constructor({path, content, attributes, attributesSchema, dataSchema}: TemplateOptions<A, D>) {\n if (path) {\n this.path = path;\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: attributes, content} = matter(await fs.readFile(templatePath, 'utf8'));\n let attributesSchema = options?.attributesSchema\n ? templateAttributesSchema.merge(options.attributesSchema)\n : templateAttributesSchema;\n let attributesParseResult = attributesSchema.safeParse(attributes);\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 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 = prettier.format(content, {\n ...options?.prettierOptions,\n filepath: renderedAttributes.to,\n });\n } catch (error: unknown) {\n if (!NO_INFERRED_PARSER_REGEXP.test((error as Error).message)) {\n throw new TemplateError('FAILED_FORMAT', {cause: error});\n }\n }\n }\n\n renders.push({\n attributes: renderedAttributes,\n data: data ?? null,\n content,\n } as TemplateRender<A, D>);\n }\n\n return renders;\n }\n}\n"],"names":["ejs","fs","matter","omit","prettier","yaml","NO_INFERRED_PARSER_REGEXP","templateAttributesSchema","TemplateError","Template","path","content","attributes","fn","attributesSchema","dataSchema","constructor","compile","read","templatePath","options","data","readFile","merge","attributesParseResult","safeParse","success","cause","error","zodIssues","issues","readAndRender","template","render","prettierOptions","disablePrettier","renders","variables","Array","isArray","renderedAttributes","renderedVariables","variableName","variableValue","Object","entries","dataWithAttributes","if","renderedIf","parsedIf","parse","to","format","filepath","test","message","push"],"mappings":"AAAA,OAAOA,SAAS,MAAM;AACtB,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,YAAY,cAAc;AACjC,OAAOC,UAAU,cAAc;AAC/B,OAAOC,cAAc,WAAW;AAChC,OAAOC,UAAU,OAAO;AAGxB,SAAQC,yBAAyB,QAAO,iBAAiB;AACzD,SAAQC,wBAAwB,QAAgC,0BAA0B;AAC1F,SAAQC,aAAa,QAAO,qBAAqB;AA+DjD;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,MAAMC;IAIX,mBAAmB,GACnBC,KAAc;IAEd,uEAAuE,GACvE,AAASC,QAAgB;IAEzB,4uBAA4uB,GAC5uB,AAASC,WAEwC;IAEjD,uBAAuB,GACvB,AAAiBC,GAAqB;IAEtC,+CAA+C,GAC/C,AAASC,iBAAoD;IAE7D,kCAAkC,GAClC,AAASC,WAA8C;IAEvDC,YAAY,EAACN,IAAI,EAAEC,OAAO,EAAEC,UAAU,EAAEE,gBAAgB,EAAEC,UAAU,EAAwB,CAAE;QAC5F,IAAIL,MAAM;YACR,IAAI,CAACA,IAAI,GAAGA;QACd;QAEA,IAAI,CAACC,OAAO,GAAGA;QACf,IAAI,CAACC,UAAU,GAAGA;QAClB,IAAI,CAACC,EAAE,GAAGb,IAAIiB,OAAO,CAAC,IAAI,CAACN,OAAO;QAElC,IAAIG,kBAAkB;YACpB,IAAI,CAACA,gBAAgB,GAAGA;QAC1B,OAAO;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,UAAU,IAAI,CAACE,EAAE,CAACiC;YACpB,EAAE,OAAOlB,OAAgB;gBACvB,MAAM,IAAIpB,cAAc,iBAAiB;oBAACmB,OAAOC;gBAAK;YACxD;YAEA,IAAI,GAACR,WAAAA,qBAAAA,+BAAAA,SAASe,eAAe,GAAE;gBAC7B,IAAI;wBAEGf;oBADLT,UAAUP,SAASgD,MAAM,CAACzC,SAAS;4BAC9BS,YAAAA,qBAAAA,gCAAAA,UAASc,eAAe,AAA3B;wBACAmB,UAAUb,mBAAmBW,EAAE;oBACjC;gBACF,EAAE,OAAOvB,OAAgB;oBACvB,IAAI,CAACtB,0BAA0BgD,IAAI,CAAC,AAAC1B,MAAgB2B,OAAO,GAAG;wBAC7D,MAAM,IAAI/C,cAAc,iBAAiB;4BAACmB,OAAOC;wBAAK;oBACxD;gBACF;YACF;YAEAQ,QAAQoB,IAAI,CAAC;gBACX5C,YAAY4B;gBACZnB,MAAMA,QAAQ;gBACdV;YACF;QACF;QAEA,OAAOyB;IACT;AACF"}
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  /**
3
- * Zod schema for {@linkcode TemplateAttributes}.
3
+ * Zod schema for {@link TemplateAttributes}.
4
4
  */
5
5
  export declare const templateAttributesSchema: z.ZodObject<{
6
6
  to: z.ZodString;
@@ -1,7 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { templateVariablesSchema } from './TemplateVariables.js';
3
3
  /**
4
- * Zod schema for {@linkcode TemplateAttributes}.
4
+ * Zod schema for {@link TemplateAttributes}.
5
5
  */ export const templateAttributesSchema = z.object({
6
6
  to: z.string(),
7
7
  if: z.string().optional(),
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateAttributes.ts"],"sourcesContent":["import {z} from 'zod';\n\nimport {templateVariablesSchema} from './TemplateVariables.js';\n\n/**\n * Zod schema for {@link TemplateAttributes}.\n */\nexport const templateAttributesSchema = z\n .object({\n to: z.string(),\n if: z.string().optional(),\n variables: templateVariablesSchema.optional(),\n })\n .strict();\n\n/**\n * Type representing template attributes.\n */\nexport type TemplateAttributes = z.infer<typeof templateAttributesSchema>;\n"],"names":["z","templateVariablesSchema","templateAttributesSchema","object","to","string","if","optional","variables","strict"],"mappings":"AAAA,SAAQA,CAAC,QAAO,MAAM;AAEtB,SAAQC,uBAAuB,QAAO,yBAAyB;AAE/D;;CAEC,GACD,OAAO,MAAMC,2BAA2BF,EACrCG,MAAM,CAAC;IACNC,IAAIJ,EAAEK,MAAM;IACZC,IAAIN,EAAEK,MAAM,GAAGE,QAAQ;IACvBC,WAAWP,wBAAwBM,QAAQ;AAC7C,GACCE,MAAM,GAAG"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateContent.ts"],"sourcesContent":["/**\n * Type representing a template content.\n */\nexport type TemplateContent = string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAAqC"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateFunction.ts"],"sourcesContent":["/**\n * Type representing a template function that takes data and returns rendered string.\n */\nexport type TemplateFunction = (data?: Record<string, unknown>) => string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAA0E"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateRender.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateAttributes} from './TemplateAttributes.js';\n\n/**\n * An object type representing the result of a template render.\n *\n * @typeParam A Type of Zod schema used to validate the custom template attributes.\n * @typeParam D Type of Zod schema used to define the type of template data.\n */\nexport type TemplateRender<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = {\n /** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.\n */\n attributes: A extends undefined\n ? Omit<TemplateAttributes, 'if'>\n : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;\n\n /** Data used to render the template. */\n data: D extends undefined ? null : z.infer<NonNullable<D>>;\n\n /** Rendered template content. */\n content: string;\n};\n"],"names":[],"mappings":"AAAA,WAyBE"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateRenders.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateRender} from './TemplateRender.js';\n\nexport type TemplateRenders<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = Array<TemplateRender<A, D>>;\n"],"names":[],"mappings":"AAAA,WAOgC"}
@@ -1,9 +1,9 @@
1
1
  import * as z from 'zod';
2
2
  /**
3
- * Zod schema for {@linkcode TemplateAttributesVariables}.
3
+ * Zod schema for {@link TemplateAttributesVariables}.
4
4
  */
5
5
  export declare const templateVariablesSchema: z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>, "many">]>;
6
6
  /**
7
- * Type of `variables`` property of {@linkcode TemplateAttributes}.
7
+ * Type of `variables`` property of {@link TemplateAttributes}.
8
8
  */
9
9
  export type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;
@@ -1,6 +1,6 @@
1
1
  import * as z from 'zod';
2
2
  /**
3
- * Zod schema for {@linkcode TemplateAttributesVariables}.
3
+ * Zod schema for {@link TemplateAttributesVariables}.
4
4
  */ export const templateVariablesSchema = z.union([
5
5
  z.record(z.unknown()),
6
6
  z.array(z.record(z.unknown()))
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/TemplateVariables.ts"],"sourcesContent":["import * as z from 'zod';\n\n/**\n * Zod schema for {@link TemplateAttributesVariables}.\n */\nexport const templateVariablesSchema = z.union([\n z.record(z.unknown()),\n z.array(z.record(z.unknown())),\n]);\n\n/**\n * Type of `variables`` property of {@link TemplateAttributes}.\n */\nexport type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;\n"],"names":["z","templateVariablesSchema","union","record","unknown","array"],"mappings":"AAAA,YAAYA,OAAO,MAAM;AAEzB;;CAEC,GACD,OAAO,MAAMC,0BAA0BD,EAAEE,KAAK,CAAC;IAC7CF,EAAEG,MAAM,CAACH,EAAEI,OAAO;IAClBJ,EAAEK,KAAK,CAACL,EAAEG,MAAM,CAACH,EAAEI,OAAO;CAC3B,EAAE"}
package/dist/main.d.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  export * from './Template.js';
2
+ export * from './TemplateAttributes.js';
3
+ export * from './TemplateContent.js';
2
4
  export * from './TemplateError.js';
3
- export * from './types.js';
5
+ export * from './TemplateFunction.js';
6
+ export * from './TemplateRender.js';
7
+ export * from './TemplateRenders.js';
8
+ export * from './TemplateVariables.js';
package/dist/main.js CHANGED
@@ -1,5 +1,10 @@
1
1
  export * from './Template.js';
2
+ export * from './TemplateAttributes.js';
3
+ export * from './TemplateContent.js';
2
4
  export * from './TemplateError.js';
3
- export * from './types.js';
5
+ export * from './TemplateFunction.js';
6
+ export * from './TemplateRender.js';
7
+ export * from './TemplateRenders.js';
8
+ export * from './TemplateVariables.js';
4
9
 
5
10
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './Template.js';\nexport * from './TemplateError.js';\nexport * from './types.js';\n"],"names":[],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,qBAAqB;AACnC,cAAc,aAAa"}
1
+ {"version":3,"sources":["../src/main.ts"],"sourcesContent":["export * from './Template.js';\nexport * from './TemplateAttributes.js';\nexport * from './TemplateContent.js';\nexport * from './TemplateError.js';\nexport * from './TemplateFunction.js';\nexport * from './TemplateRender.js';\nexport * from './TemplateRenders.js';\nexport * from './TemplateVariables.js';\n"],"names":[],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,0BAA0B;AACxC,cAAc,uBAAuB;AACrC,cAAc,qBAAqB;AACnC,cAAc,wBAAwB;AACtC,cAAc,sBAAsB;AACpC,cAAc,uBAAuB;AACrC,cAAc,yBAAyB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/template",
3
- "version": "1.0.1-265acad",
3
+ "version": "1.0.1-2d1145f",
4
4
  "description": "TypeScript-first library for working with templates using EJS and Zod.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,7 +26,7 @@
26
26
  "typecheck": "tsc --project tsconfig.typecheck.json"
27
27
  },
28
28
  "dependencies": {
29
- "@jakubmazanec/error": "2.0.1-265acad",
29
+ "@jakubmazanec/error": "2.0.1-2d1145f",
30
30
  "ejs": "^3.1.9",
31
31
  "fs-extra": "^11.1.1",
32
32
  "gray-matter": "^4.0.3",
package/src/Template.ts CHANGED
@@ -7,19 +7,19 @@ import yaml from 'yaml';
7
7
  import {type z, type AnyZodObject} from 'zod';
8
8
 
9
9
  import {NO_INFERRED_PARSER_REGEXP} from './constants.js';
10
+ import {templateAttributesSchema, type TemplateAttributes} from './TemplateAttributes.js';
10
11
  import {TemplateError} from './TemplateError.js';
11
- import {
12
- templateAttributesSchema,
13
- type TemplateAttributes,
14
- type TemplateFunction,
15
- type TemplateRender,
16
- type TemplateRenders,
17
- } from './types.js';
12
+ import {type TemplateFunction} from './TemplateFunction.js';
13
+ import {type TemplateRender} from './TemplateRender.js';
14
+ import {type TemplateRenders} from './TemplateRenders.js';
18
15
 
19
16
  export type TemplateOptions<
20
17
  A extends AnyZodObject | undefined = undefined,
21
18
  D extends AnyZodObject | undefined = undefined,
22
19
  > = {
20
+ /** Template path. */
21
+ path?: string;
22
+
23
23
  /** Template content. */
24
24
  content: string;
25
25
 
@@ -93,6 +93,9 @@ export class Template<
93
93
  A extends AnyZodObject | undefined = undefined,
94
94
  D extends AnyZodObject | undefined = undefined,
95
95
  > {
96
+ /** Template path. */
97
+ path?: string;
98
+
96
99
  /** Template content. Templates are written in [EJS](https://ejs.co/). */
97
100
  readonly content: string;
98
101
 
@@ -110,7 +113,11 @@ export class Template<
110
113
  /** Zod schema for template data. */
111
114
  readonly dataSchema: D extends AnyZodObject ? D : null;
112
115
 
113
- constructor({content, attributes, attributesSchema, dataSchema}: TemplateOptions<A, D>) {
116
+ constructor({path, content, attributes, attributesSchema, dataSchema}: TemplateOptions<A, D>) {
117
+ if (path) {
118
+ this.path = path;
119
+ }
120
+
114
121
  this.content = content;
115
122
  this.attributes = attributes;
116
123
  this.fn = ejs.compile(this.content);
@@ -131,13 +138,13 @@ export class Template<
131
138
  /**
132
139
  * Reads a template from a file.
133
140
  *
134
- * @param templateFilePath Path to the template file.
141
+ * @param templatePath Path to the template file.
135
142
  */
136
143
  static async read<
137
144
  A extends AnyZodObject | undefined = undefined,
138
145
  D extends AnyZodObject | undefined = undefined,
139
- >(templateFilePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {
140
- let {data: attributes, content} = matter(await fs.readFile(templateFilePath, 'utf8'));
146
+ >(templatePath: string, options?: TemplateReadOptions<A, D>): Promise<Template<A, D>> {
147
+ let {data: attributes, content} = matter(await fs.readFile(templatePath, 'utf8'));
141
148
  let attributesSchema = options?.attributesSchema
142
149
  ? templateAttributesSchema.merge(options.attributesSchema)
143
150
  : templateAttributesSchema;
@@ -147,13 +154,14 @@ export class Template<
147
154
  throw new TemplateError('INVALID_ATTRIBUTES', {
148
155
  cause: attributesParseResult.error,
149
156
  data: {
150
- path: templateFilePath,
157
+ path: templatePath,
151
158
  zodIssues: attributesParseResult.error.issues,
152
159
  },
153
160
  });
154
161
  }
155
162
 
156
163
  return new this({
164
+ path: templatePath,
157
165
  attributes,
158
166
  content,
159
167
  attributesSchema: options?.attributesSchema,
@@ -164,7 +172,7 @@ export class Template<
164
172
  /**
165
173
  * Reads a template from a file and then renders it.
166
174
  *
167
- * @param templateFilePath Path to the template file.
175
+ * @param templatePath Path to the template file.
168
176
  * @param data Data used for rendering the template.
169
177
  * @param options Options object.
170
178
  * @return Template renders.
@@ -173,11 +181,11 @@ export class Template<
173
181
  A extends AnyZodObject | undefined = undefined,
174
182
  D extends AnyZodObject | undefined = undefined,
175
183
  >(
176
- templateFilePath: string,
184
+ templatePath: string,
177
185
  data: D extends undefined ? null : z.infer<NonNullable<D>>,
178
186
  options?: TemplateReadAndRenderOptions<A, D>,
179
187
  ) {
180
- let template = await Template.read<A, D>(templateFilePath, {
188
+ let template = await Template.read<A, D>(templatePath, {
181
189
  attributesSchema: options?.attributesSchema,
182
190
  dataSchema: options?.dataSchema,
183
191
  });
@@ -3,7 +3,7 @@ import {z} from 'zod';
3
3
  import {templateVariablesSchema} from './TemplateVariables.js';
4
4
 
5
5
  /**
6
- * Zod schema for {@linkcode TemplateAttributes}.
6
+ * Zod schema for {@link TemplateAttributes}.
7
7
  */
8
8
  export const templateAttributesSchema = z
9
9
  .object({
@@ -1,7 +1,7 @@
1
1
  import * as z from 'zod';
2
2
 
3
3
  /**
4
- * Zod schema for {@linkcode TemplateAttributesVariables}.
4
+ * Zod schema for {@link TemplateAttributesVariables}.
5
5
  */
6
6
  export const templateVariablesSchema = z.union([
7
7
  z.record(z.unknown()),
@@ -9,6 +9,6 @@ export const templateVariablesSchema = z.union([
9
9
  ]);
10
10
 
11
11
  /**
12
- * Type of `variables`` property of {@linkcode TemplateAttributes}.
12
+ * Type of `variables`` property of {@link TemplateAttributes}.
13
13
  */
14
14
  export type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;
package/src/main.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  export * from './Template.js';
2
+ export * from './TemplateAttributes.js';
3
+ export * from './TemplateContent.js';
2
4
  export * from './TemplateError.js';
3
- export * from './types.js';
5
+ export * from './TemplateFunction.js';
6
+ export * from './TemplateRender.js';
7
+ export * from './TemplateRenders.js';
8
+ export * from './TemplateVariables.js';
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateAttributes.ts"],"sourcesContent":["import {z} from 'zod';\n\nimport {templateVariablesSchema} from './TemplateVariables.js';\n\n/**\n * Zod schema for {@linkcode TemplateAttributes}.\n */\nexport const templateAttributesSchema = z\n .object({\n to: z.string(),\n if: z.string().optional(),\n variables: templateVariablesSchema.optional(),\n })\n .strict();\n\n/**\n * Type representing template attributes.\n */\nexport type TemplateAttributes = z.infer<typeof templateAttributesSchema>;\n"],"names":["z","templateVariablesSchema","templateAttributesSchema","object","to","string","if","optional","variables","strict"],"mappings":"AAAA,SAAQA,CAAC,QAAO,MAAM;AAEtB,SAAQC,uBAAuB,QAAO,yBAAyB;AAE/D;;CAEC,GACD,OAAO,MAAMC,2BAA2BF,EACrCG,MAAM,CAAC;IACNC,IAAIJ,EAAEK,MAAM;IACZC,IAAIN,EAAEK,MAAM,GAAGE,QAAQ;IACvBC,WAAWP,wBAAwBM,QAAQ;AAC7C,GACCE,MAAM,GAAG"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateContent.ts"],"sourcesContent":["/**\n * Type representing a template content.\n */\nexport type TemplateContent = string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAAqC"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateFunction.ts"],"sourcesContent":["/**\n * Type representing a template function that takes data and returns rendered string.\n */\nexport type TemplateFunction = (data?: Record<string, unknown>) => string;\n"],"names":[],"mappings":"AAAA;;CAEC,GACD,WAA0E"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateRender.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateAttributes} from './TemplateAttributes.js';\n\n/**\n * An object type representing the result of a template render.\n *\n * @typeParam A Type of Zod schema used to validate the custom template attributes.\n * @typeParam D Type of Zod schema used to define the type of template data.\n */\nexport type TemplateRender<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = {\n /** Rendered template attributes. They created from the `to` and `variables` attributes, and from all of the custom template attributes.\n */\n attributes: A extends undefined\n ? Omit<TemplateAttributes, 'if'>\n : Omit<TemplateAttributes, 'if'> & z.infer<NonNullable<A>>;\n\n /** Data used to render the template. */\n data: D extends undefined ? null : z.infer<NonNullable<D>>;\n\n /** Rendered template content. */\n content: string;\n};\n"],"names":[],"mappings":"AAAA,WAyBE"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateRenders.ts"],"sourcesContent":["import {type z} from 'zod';\n\nimport {type TemplateRender} from './TemplateRender.js';\n\nexport type TemplateRenders<\n A extends z.AnyZodObject | undefined = undefined,\n D extends z.AnyZodObject | undefined = undefined,\n> = Array<TemplateRender<A, D>>;\n"],"names":[],"mappings":"AAAA,WAOgC"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/types/TemplateVariables.ts"],"sourcesContent":["import * as z from 'zod';\n\n/**\n * Zod schema for {@linkcode 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 {@linkcode TemplateAttributes}.\n */\nexport type TemplateAttributesVariables = z.infer<typeof templateVariablesSchema>;\n"],"names":["z","templateVariablesSchema","union","record","unknown","array"],"mappings":"AAAA,YAAYA,OAAO,MAAM;AAEzB;;CAEC,GACD,OAAO,MAAMC,0BAA0BD,EAAEE,KAAK,CAAC;IAC7CF,EAAEG,MAAM,CAACH,EAAEI,OAAO;IAClBJ,EAAEK,KAAK,CAACL,EAAEG,MAAM,CAACH,EAAEI,OAAO;CAC3B,EAAE"}
package/dist/types.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './types/TemplateAttributes.js';
2
- export * from './types/TemplateVariables.js';
3
- export * from './types/TemplateFunction.js';
4
- export * from './types/TemplateRender.js';
5
- export * from './types/TemplateRenders.js';
package/dist/types.js DELETED
@@ -1,7 +0,0 @@
1
- export * from './types/TemplateAttributes.js';
2
- export * from './types/TemplateVariables.js';
3
- export * from './types/TemplateFunction.js';
4
- export * from './types/TemplateRender.js';
5
- export * from './types/TemplateRenders.js';
6
-
7
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["export * from './types/TemplateAttributes.js';\nexport * from './types/TemplateVariables.js';\nexport * from './types/TemplateFunction.js';\nexport * from './types/TemplateRender.js';\nexport * from './types/TemplateRenders.js';\n"],"names":[],"mappings":"AAAA,cAAc,gCAAgC;AAC9C,cAAc,+BAA+B;AAC7C,cAAc,8BAA8B;AAC5C,cAAc,4BAA4B;AAC1C,cAAc,6BAA6B"}
package/src/types.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './types/TemplateAttributes.js';
2
- export * from './types/TemplateVariables.js';
3
- export * from './types/TemplateFunction.js';
4
- export * from './types/TemplateRender.js';
5
- export * from './types/TemplateRenders.js';