@jakubmazanec/carson 1.3.0 → 1.4.0-unstable.c5a26408

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.
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * A subclass of `Error` that indicates a Carson template failure.
3
3
  */
4
- export declare const CarsonTemplateError: new (code: "INVALID_CARSON_TEMPLATE_ID" | "CARSON_TEMPLATE_NOT_FOUND" | "STRATEGY_UNSUPPORTED_EXTENSION" | "STRATEGY_MISSING_FILE" | "INVALID_SNAPSHOT", options?: import("@jakubmazanec/error").CustomErrorWithDataOptions<{
4
+ export declare const CarsonTemplateError: new (code: "INVALID_CARSON_TEMPLATE_ID" | "CARSON_TEMPLATE_NOT_FOUND" | "STRATEGY_UNSUPPORTED_EXTENSION" | "STRATEGY_MISSING_FILE" | "INVALID_SNAPSHOT" | "FAILED_RENDER", options?: import("@jakubmazanec/error").CustomErrorWithDataOptions<{
5
5
  path?: string;
6
- }> | undefined) => import("@jakubmazanec/error").CustomErrorWithData<"INVALID_CARSON_TEMPLATE_ID" | "CARSON_TEMPLATE_NOT_FOUND" | "STRATEGY_UNSUPPORTED_EXTENSION" | "STRATEGY_MISSING_FILE" | "INVALID_SNAPSHOT" | "UNKNOWN_ERROR", {
6
+ }> | undefined) => import("@jakubmazanec/error").CustomErrorWithData<"INVALID_CARSON_TEMPLATE_ID" | "CARSON_TEMPLATE_NOT_FOUND" | "STRATEGY_UNSUPPORTED_EXTENSION" | "STRATEGY_MISSING_FILE" | "INVALID_SNAPSHOT" | "FAILED_RENDER" | "UNKNOWN_ERROR", {
7
7
  path?: string;
8
8
  }>;
@@ -7,7 +7,8 @@ export const CarsonTemplateError = createCustomErrorWithData(
7
7
  CARSON_TEMPLATE_NOT_FOUND: 'Carson template "{0}" was not found in module "{1}".',
8
8
  STRATEGY_UNSUPPORTED_EXTENSION: `Strategy "{0}" in the template file "{1}" doesn't support extension "{2}".`,
9
9
  STRATEGY_MISSING_FILE: 'Strategy "{0}" in the template file "{1}" is missing file "{2}".',
10
- INVALID_SNAPSHOT: "Invalid Carson template render snapshot."
10
+ INVALID_SNAPSHOT: "Invalid Carson template render snapshot.",
11
+ FAILED_RENDER: 'Template "{0}" rendering has failed.'
11
12
  },
12
13
  class CarsonTemplateErrorData {
13
14
  path;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../source/template/CarsonTemplateError.ts"],
4
- "sourcesContent": ["import {createCustomErrorWithData} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a Carson template failure.\n */\nexport const CarsonTemplateError = createCustomErrorWithData(\n 'CarsonTemplateError',\n {\n INVALID_CARSON_TEMPLATE_ID:\n 'Invalid Carson template ID \"{0}\". Carson template ID must consist of a module ID and a template name separated by a colon.',\n CARSON_TEMPLATE_NOT_FOUND: 'Carson template \"{0}\" was not found in module \"{1}\".',\n STRATEGY_UNSUPPORTED_EXTENSION:\n 'Strategy \"{0}\" in the template file \"{1}\" doesn\\'t support extension \"{2}\".',\n STRATEGY_MISSING_FILE: 'Strategy \"{0}\" in the template file \"{1}\" is missing file \"{2}\".',\n INVALID_SNAPSHOT: 'Invalid Carson template render snapshot.',\n },\n class CarsonTemplateErrorData {\n path?: string;\n },\n);\n"],
5
- "mappings": ";AAAA,SAAQ,iCAAgC;AAKjC,aAAM,sBAAsB;AAAA,EACjC;AAAA,EACA;AAAA,IACE,4BACE;AAAA,IACF,2BAA2B;AAAA,IAC3B,gCACE;AAAA,IACF,uBAAuB;AAAA,IACvB,kBAAkB;AAAA,EACpB;AAAA,EACA,MAAM,wBAAwB;AAAA,IAC5B;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import {createCustomErrorWithData} from '@jakubmazanec/error';\n\n/**\n * A subclass of `Error` that indicates a Carson template failure.\n */\nexport const CarsonTemplateError = createCustomErrorWithData(\n 'CarsonTemplateError',\n {\n INVALID_CARSON_TEMPLATE_ID:\n 'Invalid Carson template ID \"{0}\". Carson template ID must consist of a module ID and a template name separated by a colon.',\n CARSON_TEMPLATE_NOT_FOUND: 'Carson template \"{0}\" was not found in module \"{1}\".',\n STRATEGY_UNSUPPORTED_EXTENSION:\n 'Strategy \"{0}\" in the template file \"{1}\" doesn\\'t support extension \"{2}\".',\n STRATEGY_MISSING_FILE: 'Strategy \"{0}\" in the template file \"{1}\" is missing file \"{2}\".',\n INVALID_SNAPSHOT: 'Invalid Carson template render snapshot.',\n FAILED_RENDER: 'Template \"{0}\" rendering has failed.',\n },\n class CarsonTemplateErrorData {\n path?: string;\n },\n);\n"],
5
+ "mappings": ";AAAA,SAAQ,iCAAgC;AAKjC,aAAM,sBAAsB;AAAA,EACjC;AAAA,EACA;AAAA,IACE,4BACE;AAAA,IACF,2BAA2B;AAAA,IAC3B,gCACE;AAAA,IACF,uBAAuB;AAAA,IACvB,kBAAkB;AAAA,IAClB,eAAe;AAAA,EACjB;AAAA,EACA,MAAM,wBAAwB;AAAA,IAC5B;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -15,7 +15,7 @@ export declare const templateRenderSnapshotSchema: z.ZodObject<{
15
15
  to: string;
16
16
  }>;
17
17
  content: z.ZodOptional<z.ZodString>;
18
- }, "strict", z.ZodTypeAny, {
18
+ }, "strip", z.ZodTypeAny, {
19
19
  attributes: {
20
20
  strategy: "create" | "ensure" | "overwrite" | "merge" | "check" | ["insert", string];
21
21
  to: string;
@@ -5,5 +5,5 @@ import { templateAttributesSchema } from "../TemplateAttributes.js";
5
5
  export const templateRenderSnapshotSchema = z.object({
6
6
  attributes: baseTemplateAttributesSchema.pick({ to: true }).merge(templateAttributesSchema),
7
7
  content: z.string().optional()
8
- }).strict();
8
+ });
9
9
  //# sourceMappingURL=TemplateRenderSnapshot.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../source/template/internals/TemplateRenderSnapshot.ts"],
4
- "sourcesContent": ["import {templateAttributesSchema as baseTemplateAttributesSchema} from '@jakubmazanec/template';\nimport {z} from 'zod';\n\nimport {templateAttributesSchema} from '../TemplateAttributes.js';\n\nexport const templateRenderSnapshotSchema = z\n .object({\n attributes: baseTemplateAttributesSchema.pick({to: true}).merge(templateAttributesSchema),\n content: z.string().optional(),\n })\n .strict();\n\nexport type TemplateRenderSnapshot = z.infer<typeof templateRenderSnapshotSchema>;\n"],
5
- "mappings": ";AAAA,SAAQ,4BAA4B,oCAAmC;AACvE,SAAQ,SAAQ;AAEhB,SAAQ,gCAA+B;AAEhC,aAAM,+BAA+B,EACzC,OAAO;AAAA,EACN,YAAY,6BAA6B,KAAK,EAAC,IAAI,KAAI,CAAC,EAAE,MAAM,wBAAwB;AAAA,EACxF,SAAS,EAAE,OAAO,EAAE,SAAS;AAC/B,CAAC,EACA,OAAO;",
4
+ "sourcesContent": ["import {templateAttributesSchema as baseTemplateAttributesSchema} from '@jakubmazanec/template';\nimport {z} from 'zod';\n\nimport {templateAttributesSchema} from '../TemplateAttributes.js';\n\nexport const templateRenderSnapshotSchema = z.object({\n attributes: baseTemplateAttributesSchema.pick({to: true}).merge(templateAttributesSchema),\n content: z.string().optional(),\n});\n\nexport type TemplateRenderSnapshot = z.infer<typeof templateRenderSnapshotSchema>;\n"],
5
+ "mappings": ";AAAA,SAAQ,4BAA4B,oCAAmC;AACvE,SAAQ,SAAQ;AAEhB,SAAQ,gCAA+B;AAEhC,aAAM,+BAA+B,EAAE,OAAO;AAAA,EACnD,YAAY,6BAA6B,KAAK,EAAC,IAAI,KAAI,CAAC,EAAE,MAAM,wBAAwB;AAAA,EACxF,SAAS,EAAE,OAAO,EAAE,SAAS;AAC/B,CAAC;",
6
6
  "names": []
7
7
  }
@@ -15,7 +15,7 @@ export declare const templateRenderSnapshotsSchema: z.ZodArray<z.ZodObject<{
15
15
  to: string;
16
16
  }>;
17
17
  content: z.ZodOptional<z.ZodString>;
18
- }, "strict", z.ZodTypeAny, {
18
+ }, "strip", z.ZodTypeAny, {
19
19
  attributes: {
20
20
  strategy: "create" | "ensure" | "overwrite" | "merge" | "check" | ["insert", string];
21
21
  to: string;
@@ -39,7 +39,7 @@ export async function readCarsonTemplate(templateId) {
39
39
  return carsonTemplate;
40
40
  }
41
41
  throw new CarsonTemplateError("CARSON_TEMPLATE_NOT_FOUND", {
42
- messageParameters: [templateId, moduleId]
42
+ messageParameters: [templateName, moduleId]
43
43
  });
44
44
  }
45
45
  //# sourceMappingURL=readCarsonTemplate.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../source/template/internals/readCarsonTemplate.ts"],
4
- "sourcesContent": ["import {Template} from '@jakubmazanec/template';\nimport {readFile} from '@jakubmazanec/zod-utils';\nimport glob from 'fast-glob';\nimport fs from 'fs-extra';\nimport path from 'node:path';\n\nimport {TEMPLATE_CONFIG_FILENAME, TEMPLATES_PATH} from '../../constants.js';\nimport {type CarsonTemplate} from '../CarsonTemplate.js';\nimport {carsonTemplateConfigSchema} from '../CarsonTemplateConfig.js';\nimport {CarsonTemplateError} from '../CarsonTemplateError.js';\nimport {templateAttributesSchema} from '../TemplateAttributes.js';\nimport {parseCarsonTemplateId} from './parseCarsonTemplateId.js';\nimport {resolveModule} from './resolveModule.js';\n\nexport async function readCarsonTemplate(templateId: string): Promise<CarsonTemplate> {\n let [moduleId, templateName] = parseCarsonTemplateId(templateId);\n let modulePath = await resolveModule(moduleId);\n let carsonTemplatePath = path.join(modulePath, TEMPLATES_PATH, templateName);\n\n if (await fs.pathExists(carsonTemplatePath)) {\n let templateConfigPath = path.join(carsonTemplatePath, TEMPLATE_CONFIG_FILENAME);\n let templateConfig = await readFile(templateConfigPath, carsonTemplateConfigSchema, {\n throwOnZodError: true,\n });\n let templateFilePaths = await glob('**/*.ejs', {\n cwd: carsonTemplatePath,\n dot: true,\n });\n let carsonTemplate: CarsonTemplate = {\n id: templateId,\n name: templateName,\n config: templateConfig,\n files: [],\n };\n\n for (let templateFilePath of templateFilePaths) {\n carsonTemplate.files.push(\n await Template.read(path.join(carsonTemplatePath, templateFilePath), {\n attributesSchema: templateAttributesSchema,\n }),\n );\n }\n\n return carsonTemplate;\n }\n\n throw new CarsonTemplateError('CARSON_TEMPLATE_NOT_FOUND', {\n messageParameters: [templateId, moduleId],\n });\n}\n"],
5
- "mappings": ";AAAA,SAAQ,gBAAe;AACvB,SAAQ,gBAAe;AACvB,OAAO,UAAU;AACjB,OAAO,QAAQ;AACf,OAAO,UAAU;AAEjB,SAAQ,0BAA0B,sBAAqB;AAEvD,SAAQ,kCAAiC;AACzC,SAAQ,2BAA0B;AAClC,SAAQ,gCAA+B;AACvC,SAAQ,6BAA4B;AACpC,SAAQ,qBAAoB;AAE5B,sBAAsB,mBAAmB,YAA6C;AACpF,MAAI,CAAC,UAAU,YAAY,IAAI,sBAAsB,UAAU;AAC/D,MAAI,aAAa,MAAM,cAAc,QAAQ;AAC7C,MAAI,qBAAqB,KAAK,KAAK,YAAY,gBAAgB,YAAY;AAE3E,MAAI,MAAM,GAAG,WAAW,kBAAkB,GAAG;AAC3C,QAAI,qBAAqB,KAAK,KAAK,oBAAoB,wBAAwB;AAC/E,QAAI,iBAAiB,MAAM,SAAS,oBAAoB,4BAA4B;AAAA,MAClF,iBAAiB;AAAA,IACnB,CAAC;AACD,QAAI,oBAAoB,MAAM,KAAK,YAAY;AAAA,MAC7C,KAAK;AAAA,MACL,KAAK;AAAA,IACP,CAAC;AACD,QAAI,iBAAiC;AAAA,MACnC,IAAI;AAAA,MACJ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,CAAC;AAAA,IACV;AAEA,aAAS,oBAAoB,mBAAmB;AAC9C,qBAAe,MAAM;AAAA,QACnB,MAAM,SAAS,KAAK,KAAK,KAAK,oBAAoB,gBAAgB,GAAG;AAAA,UACnE,kBAAkB;AAAA,QACpB,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,IAAI,oBAAoB,6BAA6B;AAAA,IACzD,mBAAmB,CAAC,YAAY,QAAQ;AAAA,EAC1C,CAAC;AACH;",
4
+ "sourcesContent": ["import {Template} from '@jakubmazanec/template';\nimport {readFile} from '@jakubmazanec/zod-utils';\nimport glob from 'fast-glob';\nimport fs from 'fs-extra';\nimport path from 'node:path';\n\nimport {TEMPLATE_CONFIG_FILENAME, TEMPLATES_PATH} from '../../constants.js';\nimport {type CarsonTemplate} from '../CarsonTemplate.js';\nimport {carsonTemplateConfigSchema} from '../CarsonTemplateConfig.js';\nimport {CarsonTemplateError} from '../CarsonTemplateError.js';\nimport {templateAttributesSchema} from '../TemplateAttributes.js';\nimport {parseCarsonTemplateId} from './parseCarsonTemplateId.js';\nimport {resolveModule} from './resolveModule.js';\n\nexport async function readCarsonTemplate(templateId: string): Promise<CarsonTemplate> {\n let [moduleId, templateName] = parseCarsonTemplateId(templateId);\n let modulePath = await resolveModule(moduleId);\n let carsonTemplatePath = path.join(modulePath, TEMPLATES_PATH, templateName);\n\n if (await fs.pathExists(carsonTemplatePath)) {\n let templateConfigPath = path.join(carsonTemplatePath, TEMPLATE_CONFIG_FILENAME);\n let templateConfig = await readFile(templateConfigPath, carsonTemplateConfigSchema, {\n throwOnZodError: true,\n });\n let templateFilePaths = await glob('**/*.ejs', {\n cwd: carsonTemplatePath,\n dot: true,\n });\n let carsonTemplate: CarsonTemplate = {\n id: templateId,\n name: templateName,\n config: templateConfig,\n files: [],\n };\n\n for (let templateFilePath of templateFilePaths) {\n carsonTemplate.files.push(\n await Template.read(path.join(carsonTemplatePath, templateFilePath), {\n attributesSchema: templateAttributesSchema,\n }),\n );\n }\n\n return carsonTemplate;\n }\n\n throw new CarsonTemplateError('CARSON_TEMPLATE_NOT_FOUND', {\n messageParameters: [templateName, moduleId],\n });\n}\n"],
5
+ "mappings": ";AAAA,SAAQ,gBAAe;AACvB,SAAQ,gBAAe;AACvB,OAAO,UAAU;AACjB,OAAO,QAAQ;AACf,OAAO,UAAU;AAEjB,SAAQ,0BAA0B,sBAAqB;AAEvD,SAAQ,kCAAiC;AACzC,SAAQ,2BAA0B;AAClC,SAAQ,gCAA+B;AACvC,SAAQ,6BAA4B;AACpC,SAAQ,qBAAoB;AAE5B,sBAAsB,mBAAmB,YAA6C;AACpF,MAAI,CAAC,UAAU,YAAY,IAAI,sBAAsB,UAAU;AAC/D,MAAI,aAAa,MAAM,cAAc,QAAQ;AAC7C,MAAI,qBAAqB,KAAK,KAAK,YAAY,gBAAgB,YAAY;AAE3E,MAAI,MAAM,GAAG,WAAW,kBAAkB,GAAG;AAC3C,QAAI,qBAAqB,KAAK,KAAK,oBAAoB,wBAAwB;AAC/E,QAAI,iBAAiB,MAAM,SAAS,oBAAoB,4BAA4B;AAAA,MAClF,iBAAiB;AAAA,IACnB,CAAC;AACD,QAAI,oBAAoB,MAAM,KAAK,YAAY;AAAA,MAC7C,KAAK;AAAA,MACL,KAAK;AAAA,IACP,CAAC;AACD,QAAI,iBAAiC;AAAA,MACnC,IAAI;AAAA,MACJ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,CAAC;AAAA,IACV;AAEA,aAAS,oBAAoB,mBAAmB;AAC9C,qBAAe,MAAM;AAAA,QACnB,MAAM,SAAS,KAAK,KAAK,KAAK,oBAAoB,gBAAgB,GAAG;AAAA,UACnE,kBAAkB;AAAA,QACpB,CAAC;AAAA,MACH;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,IAAI,oBAAoB,6BAA6B;AAAA,IACzD,mBAAmB,CAAC,cAAc,QAAQ;AAAA,EAC5C,CAAC;AACH;",
6
6
  "names": []
7
7
  }
@@ -2,6 +2,7 @@
2
2
  import { pathToPosixPath } from "@jakubmazanec/fs-utils";
3
3
  import _ from "lodash";
4
4
  import path from "node:path";
5
+ import { CarsonTemplateError } from "./CarsonTemplateError.js";
5
6
  import { importCarsonTemplate, parseCarsonTemplateId, resolveModule } from "./internals.js";
6
7
  export async function renderCarsonTemplate(options) {
7
8
  let [moduleId] = parseCarsonTemplateId(options.template.id);
@@ -9,22 +10,34 @@ export async function renderCarsonTemplate(options) {
9
10
  let templateModule = await importCarsonTemplate(options.template.id);
10
11
  let templateRenders = [];
11
12
  for (let file of options.template.files) {
12
- templateRenders.push(
13
- ...(await file.render(
14
- {
15
- ...templateModule,
16
- ...options.templateData
17
- },
18
- { disablePrettier: true }
19
- )).map((templateRender) => ({
20
- ...templateRender,
21
- template: {
22
- path: pathToPosixPath(
23
- `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`
24
- )
25
- }
26
- }))
13
+ let filePath = pathToPosixPath(
14
+ `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`
27
15
  );
16
+ try {
17
+ templateRenders.push(
18
+ ...(await file.render(
19
+ {
20
+ ...templateModule,
21
+ ...options.templateData
22
+ },
23
+ { disablePrettier: true }
24
+ )).map((templateRender) => ({
25
+ ...templateRender,
26
+ template: {
27
+ path: filePath
28
+ }
29
+ }))
30
+ );
31
+ } catch (error) {
32
+ if (error.code === "FAILED_RENDER") {
33
+ throw new CarsonTemplateError("FAILED_RENDER", {
34
+ messageParameters: [filePath],
35
+ cause: error.cause
36
+ });
37
+ } else {
38
+ throw error;
39
+ }
40
+ }
28
41
  }
29
42
  templateRenders = _.sortBy(templateRenders, [
30
43
  (templateRender) => templateRender.attributes.to,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../source/template/renderCarsonTemplate.ts"],
4
- "sourcesContent": ["import {pathToPosixPath} from '@jakubmazanec/fs-utils';\nimport _ from 'lodash';\nimport path from 'node:path';\n\nimport {type CarsonTemplate} from './CarsonTemplate.js';\nimport {importCarsonTemplate, parseCarsonTemplateId, resolveModule} from './internals.js';\nimport {type TemplateData} from './TemplateData.js';\nimport {type TemplateRenders} from './TemplateRenders.js';\n\nexport type RenderCarsonTemplateOptions = {\n template: CarsonTemplate;\n templateData: TemplateData;\n};\n\n/**\n * Renders a Carson template with provided template data.\n */\nexport async function renderCarsonTemplate(\n options: RenderCarsonTemplateOptions,\n): Promise<TemplateRenders> {\n let [moduleId] = parseCarsonTemplateId(options.template.id);\n let modulePath = await resolveModule(moduleId);\n let templateModule = await importCarsonTemplate(options.template.id);\n let templateRenders: TemplateRenders = [];\n\n for (let file of options.template.files) {\n templateRenders.push(\n ...(\n await file.render(\n {\n ...templateModule,\n ...options.templateData,\n },\n {disablePrettier: true},\n )\n ).map((templateRender) => ({\n ...templateRender,\n template: {\n path: pathToPosixPath(\n `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`,\n ),\n },\n })),\n );\n }\n\n templateRenders = _.sortBy(templateRenders, [\n (templateRender) => templateRender.attributes.to,\n (templateRender) => templateRender.attributes.strategy,\n (templateRender) => templateRender.content,\n ]);\n\n return templateRenders;\n}\n"],
5
- "mappings": ";AAAA,SAAQ,uBAAsB;AAC9B,OAAO,OAAO;AACd,OAAO,UAAU;AAGjB,SAAQ,sBAAsB,uBAAuB,qBAAoB;AAYzE,sBAAsB,qBACpB,SAC0B;AAC1B,MAAI,CAAC,QAAQ,IAAI,sBAAsB,QAAQ,SAAS,EAAE;AAC1D,MAAI,aAAa,MAAM,cAAc,QAAQ;AAC7C,MAAI,iBAAiB,MAAM,qBAAqB,QAAQ,SAAS,EAAE;AACnE,MAAI,kBAAmC,CAAC;AAExC,WAAS,QAAQ,QAAQ,SAAS,OAAO;AACvC,oBAAgB;AAAA,MACd,IACE,MAAM,KAAK;AAAA,QACT;AAAA,UACE,GAAG;AAAA,UACH,GAAG,QAAQ;AAAA,QACb;AAAA,QACA,EAAC,iBAAiB,KAAI;AAAA,MACxB,GACA,IAAI,CAAC,oBAAoB;AAAA,QACzB,GAAG;AAAA,QACH,UAAU;AAAA,UACR,MAAM;AAAA,YACJ,GAAG,QAAQ,IAAI,KAAK,SAAS,YAAY,KAAK,QAAQ,UAAU,CAAC;AAAA,UACnE;AAAA,QACF;AAAA,MACF,EAAE;AAAA,IACJ;AAAA,EACF;AAEA,oBAAkB,EAAE,OAAO,iBAAiB;AAAA,IAC1C,CAAC,mBAAmB,eAAe,WAAW;AAAA,IAC9C,CAAC,mBAAmB,eAAe,WAAW;AAAA,IAC9C,CAAC,mBAAmB,eAAe;AAAA,EACrC,CAAC;AAED,SAAO;AACT;",
4
+ "sourcesContent": ["import {pathToPosixPath} from '@jakubmazanec/fs-utils';\nimport {type TemplateError} from '@jakubmazanec/template';\nimport _ from 'lodash';\nimport path from 'node:path';\n\nimport {type CarsonTemplate} from './CarsonTemplate.js';\nimport {CarsonTemplateError} from './CarsonTemplateError.js';\nimport {importCarsonTemplate, parseCarsonTemplateId, resolveModule} from './internals.js';\nimport {type TemplateData} from './TemplateData.js';\nimport {type TemplateRenders} from './TemplateRenders.js';\n\nexport type RenderCarsonTemplateOptions = {\n template: CarsonTemplate;\n templateData: TemplateData;\n};\n\n/**\n * Renders a Carson template with provided template data.\n */\nexport async function renderCarsonTemplate(\n options: RenderCarsonTemplateOptions,\n): Promise<TemplateRenders> {\n let [moduleId] = parseCarsonTemplateId(options.template.id);\n let modulePath = await resolveModule(moduleId);\n let templateModule = await importCarsonTemplate(options.template.id);\n let templateRenders: TemplateRenders = [];\n\n for (let file of options.template.files) {\n let filePath = pathToPosixPath(\n `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`,\n );\n\n try {\n templateRenders.push(\n ...(\n await file.render(\n {\n ...templateModule,\n ...options.templateData,\n },\n {disablePrettier: true},\n )\n ).map((templateRender) => ({\n ...templateRender,\n template: {\n path: filePath,\n },\n })),\n );\n } catch (error: unknown) {\n if ((error as InstanceType<typeof TemplateError>).code === 'FAILED_RENDER') {\n // rethrowing original FAILED_RENDER with more informative description\n throw new CarsonTemplateError('FAILED_RENDER', {\n messageParameters: [filePath],\n cause: (error as InstanceType<typeof TemplateError>).cause,\n });\n } else {\n throw error;\n }\n }\n }\n\n templateRenders = _.sortBy(templateRenders, [\n (templateRender) => templateRender.attributes.to,\n (templateRender) => templateRender.attributes.strategy,\n (templateRender) => templateRender.content,\n ]);\n\n return templateRenders;\n}\n"],
5
+ "mappings": ";AAAA,SAAQ,uBAAsB;AAE9B,OAAO,OAAO;AACd,OAAO,UAAU;AAGjB,SAAQ,2BAA0B;AAClC,SAAQ,sBAAsB,uBAAuB,qBAAoB;AAYzE,sBAAsB,qBACpB,SAC0B;AAC1B,MAAI,CAAC,QAAQ,IAAI,sBAAsB,QAAQ,SAAS,EAAE;AAC1D,MAAI,aAAa,MAAM,cAAc,QAAQ;AAC7C,MAAI,iBAAiB,MAAM,qBAAqB,QAAQ,SAAS,EAAE;AACnE,MAAI,kBAAmC,CAAC;AAExC,WAAS,QAAQ,QAAQ,SAAS,OAAO;AACvC,QAAI,WAAW;AAAA,MACb,GAAG,QAAQ,IAAI,KAAK,SAAS,YAAY,KAAK,QAAQ,UAAU,CAAC;AAAA,IACnE;AAEA,QAAI;AACF,sBAAgB;AAAA,QACd,IACE,MAAM,KAAK;AAAA,UACT;AAAA,YACE,GAAG;AAAA,YACH,GAAG,QAAQ;AAAA,UACb;AAAA,UACA,EAAC,iBAAiB,KAAI;AAAA,QACxB,GACA,IAAI,CAAC,oBAAoB;AAAA,UACzB,GAAG;AAAA,UACH,UAAU;AAAA,YACR,MAAM;AAAA,UACR;AAAA,QACF,EAAE;AAAA,MACJ;AAAA,IACF,SAAS,OAAgB;AACvB,UAAK,MAA6C,SAAS,iBAAiB;AAE1E,cAAM,IAAI,oBAAoB,iBAAiB;AAAA,UAC7C,mBAAmB,CAAC,QAAQ;AAAA,UAC5B,OAAQ,MAA6C;AAAA,QACvD,CAAC;AAAA,MACH,OAAO;AACL,cAAM;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,oBAAkB,EAAE,OAAO,iBAAiB;AAAA,IAC1C,CAAC,mBAAmB,eAAe,WAAW;AAAA,IAC9C,CAAC,mBAAmB,eAAe,WAAW;AAAA,IAC9C,CAAC,mBAAmB,eAAe;AAAA,EACrC,CAAC;AAED,SAAO;AACT;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/carson",
3
- "version": "1.3.0",
3
+ "version": "1.4.0-unstable.c5a26408",
4
4
  "description": "Tool for managing workspace and project configuration.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,10 +33,10 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@jakubmazanec/args": "^2.0.0",
36
- "@jakubmazanec/cli": "^0.3.0",
36
+ "@jakubmazanec/cli": "0.3.9-unstable.c5a26408",
37
37
  "@jakubmazanec/error": "^3.0.0",
38
38
  "@jakubmazanec/fs-utils": "^0.2.0",
39
- "@jakubmazanec/template": "^2.0.0",
39
+ "@jakubmazanec/template": "2.0.10-unstable.c5a26408",
40
40
  "@jakubmazanec/ts-utils": "^3.0.0",
41
41
  "@jakubmazanec/zod-utils": "^2.0.0",
42
42
  "chalk": "^5.4.1",
@@ -69,9 +69,6 @@
69
69
  "esbuild": "^0.24.2",
70
70
  "eslint": "^9.17.0",
71
71
  "ink": "^5.1.0",
72
- "lodash": "^4.17.21",
73
- "prettier": "^3.4.2",
74
- "prettier-plugin-packagejson": "^2.5.6",
75
72
  "react": "^18.3.1",
76
73
  "ts-node": "^10.9.2",
77
74
  "typedoc": "^0.27.6",
@@ -13,6 +13,7 @@ export const CarsonTemplateError = createCustomErrorWithData(
13
13
  'Strategy "{0}" in the template file "{1}" doesn\'t support extension "{2}".',
14
14
  STRATEGY_MISSING_FILE: 'Strategy "{0}" in the template file "{1}" is missing file "{2}".',
15
15
  INVALID_SNAPSHOT: 'Invalid Carson template render snapshot.',
16
+ FAILED_RENDER: 'Template "{0}" rendering has failed.',
16
17
  },
17
18
  class CarsonTemplateErrorData {
18
19
  path?: string;
@@ -3,11 +3,9 @@ import {z} from 'zod';
3
3
 
4
4
  import {templateAttributesSchema} from '../TemplateAttributes.js';
5
5
 
6
- export const templateRenderSnapshotSchema = z
7
- .object({
8
- attributes: baseTemplateAttributesSchema.pick({to: true}).merge(templateAttributesSchema),
9
- content: z.string().optional(),
10
- })
11
- .strict();
6
+ export const templateRenderSnapshotSchema = z.object({
7
+ attributes: baseTemplateAttributesSchema.pick({to: true}).merge(templateAttributesSchema),
8
+ content: z.string().optional(),
9
+ });
12
10
 
13
11
  export type TemplateRenderSnapshot = z.infer<typeof templateRenderSnapshotSchema>;
@@ -45,6 +45,6 @@ export async function readCarsonTemplate(templateId: string): Promise<CarsonTemp
45
45
  }
46
46
 
47
47
  throw new CarsonTemplateError('CARSON_TEMPLATE_NOT_FOUND', {
48
- messageParameters: [templateId, moduleId],
48
+ messageParameters: [templateName, moduleId],
49
49
  });
50
50
  }
@@ -1,8 +1,10 @@
1
1
  import {pathToPosixPath} from '@jakubmazanec/fs-utils';
2
+ import {type TemplateError} from '@jakubmazanec/template';
2
3
  import _ from 'lodash';
3
4
  import path from 'node:path';
4
5
 
5
6
  import {type CarsonTemplate} from './CarsonTemplate.js';
7
+ import {CarsonTemplateError} from './CarsonTemplateError.js';
6
8
  import {importCarsonTemplate, parseCarsonTemplateId, resolveModule} from './internals.js';
7
9
  import {type TemplateData} from './TemplateData.js';
8
10
  import {type TemplateRenders} from './TemplateRenders.js';
@@ -24,24 +26,38 @@ export async function renderCarsonTemplate(
24
26
  let templateRenders: TemplateRenders = [];
25
27
 
26
28
  for (let file of options.template.files) {
27
- templateRenders.push(
28
- ...(
29
- await file.render(
30
- {
31
- ...templateModule,
32
- ...options.templateData,
33
- },
34
- {disablePrettier: true},
35
- )
36
- ).map((templateRender) => ({
37
- ...templateRender,
38
- template: {
39
- path: pathToPosixPath(
40
- `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`,
41
- ),
42
- },
43
- })),
29
+ let filePath = pathToPosixPath(
30
+ `${moduleId}/${path.relative(modulePath, file.path ?? modulePath)}`,
44
31
  );
32
+
33
+ try {
34
+ templateRenders.push(
35
+ ...(
36
+ await file.render(
37
+ {
38
+ ...templateModule,
39
+ ...options.templateData,
40
+ },
41
+ {disablePrettier: true},
42
+ )
43
+ ).map((templateRender) => ({
44
+ ...templateRender,
45
+ template: {
46
+ path: filePath,
47
+ },
48
+ })),
49
+ );
50
+ } catch (error: unknown) {
51
+ if ((error as InstanceType<typeof TemplateError>).code === 'FAILED_RENDER') {
52
+ // rethrowing original FAILED_RENDER with more informative description
53
+ throw new CarsonTemplateError('FAILED_RENDER', {
54
+ messageParameters: [filePath],
55
+ cause: (error as InstanceType<typeof TemplateError>).cause,
56
+ });
57
+ } else {
58
+ throw error;
59
+ }
60
+ }
45
61
  }
46
62
 
47
63
  templateRenders = _.sortBy(templateRenders, [