@codeandmoney/jargal 0.0.0-dev.11 → 0.0.0-dev.12

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.
@@ -4,7 +4,11 @@ import type { Action } from "../types.ts";
4
4
  export function combine(...actions: Action[]): Action {
5
5
  return async function execute(params) {
6
6
  for (const action of actions) {
7
- await executeAction({ action, context: params.context, renderer: params.renderer });
7
+ await executeAction({
8
+ action,
9
+ context: params.context,
10
+ renderer: params.renderer,
11
+ });
8
12
  }
9
13
  };
10
14
  }
@@ -2,7 +2,12 @@ import assert from "node:assert";
2
2
  import { readdir, readFile } from "node:fs/promises";
3
3
  import path, { resolve } from "node:path";
4
4
 
5
- export type TemplateData = { templatePath: string; savePath: string; templateContent: string; metadata?: Record<string, any> };
5
+ export type TemplateData = {
6
+ templatePath: string;
7
+ savePath: string;
8
+ templateContent: string;
9
+ metadata?: Record<string, any>;
10
+ };
6
11
 
7
12
  export type TemplatesMap = Record<string, TemplateData>;
8
13
 
@@ -12,7 +17,9 @@ export async function templates<Scope extends string | undefined = undefined>(pa
12
17
  path: string;
13
18
  scope?: Scope;
14
19
  engine?: "handlebars";
15
- hooks?: { onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[] };
20
+ hooks?: {
21
+ onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
22
+ };
16
23
  }): Promise<(ctx: Record<string, any>) => Templates<Scope extends undefined ? "default" : Scope>> {
17
24
  const engine = params?.engine ?? "handlebars";
18
25
  const removeExtension = engine === "handlebars" ? ".hbs" : "";
@@ -30,7 +37,11 @@ export async function templates<Scope extends string | undefined = undefined>(pa
30
37
  const savePath = path.relative(resolvedPath, templatePath).replace(removeExtension, "");
31
38
 
32
39
  const contentRaw = await readFile(path.resolve(resolvedPath, templatePath));
33
- const data: TemplateData = { templateContent: new TextDecoder().decode(contentRaw), templatePath, savePath };
40
+ const data: TemplateData = {
41
+ templateContent: new TextDecoder().decode(contentRaw),
42
+ templatePath,
43
+ savePath,
44
+ };
34
45
 
35
46
  if (!params.hooks) {
36
47
  assert(record[scopeKey]);
@@ -2,13 +2,19 @@ import type { ContextAction } from "../types.ts";
2
2
  import { readdir, readFile } from "node:fs/promises";
3
3
  import path, { resolve } from "node:path";
4
4
 
5
- type TemplateData = { templatePath: string; realativePath: string; templateContent: string };
5
+ type TemplateData = {
6
+ templatePath: string;
7
+ realativePath: string;
8
+ templateContent: string;
9
+ };
6
10
 
7
11
  export function loadTemplates(
8
12
  templatesPath: string,
9
13
  options: {
10
14
  scope?: string;
11
- hooks?: { onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[] };
15
+ hooks?: {
16
+ onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
17
+ };
12
18
  },
13
19
  ): ContextAction<{ templates: Record<string, Map<string, TemplateData>> }> {
14
20
  return async function execute(params) {
@@ -22,7 +28,11 @@ export function loadTemplates(
22
28
  const realativePath = path.relative(templatesPath, templatePath);
23
29
 
24
30
  const contentRaw = await readFile(path.resolve(templatesPath, templatePath));
25
- const data: TemplateData = { templateContent: new TextDecoder().decode(contentRaw), templatePath, realativePath };
31
+ const data: TemplateData = {
32
+ templateContent: new TextDecoder().decode(contentRaw),
33
+ templatePath,
34
+ realativePath,
35
+ };
26
36
 
27
37
  if (!options.hooks) {
28
38
  record[options.scope ? options.scope : "templates"]?.set(realativePath, data);
@@ -48,7 +58,9 @@ export function loadTemplates(
48
58
  export async function templates<Scope extends string | undefined = undefined>(params: {
49
59
  path: string;
50
60
  scope?: Scope;
51
- hooks?: { onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[] };
61
+ hooks?: {
62
+ onDataReady?: ((path: string, data: TemplateData) => [path: string, data: TemplateData])[];
63
+ };
52
64
  }): Promise<
53
65
  (ctx: Record<string, any>) => {
54
66
  templates: Record<Scope extends undefined ? "default" : Scope, Map<string, TemplateData>>;
@@ -68,7 +80,11 @@ export async function templates<Scope extends string | undefined = undefined>(pa
68
80
  const realativePath = path.relative(resolvedPath, templatePath);
69
81
 
70
82
  const contentRaw = await readFile(path.resolve(resolvedPath, templatePath));
71
- const data: TemplateData = { templateContent: new TextDecoder().decode(contentRaw), templatePath, realativePath };
83
+ const data: TemplateData = {
84
+ templateContent: new TextDecoder().decode(contentRaw),
85
+ templatePath,
86
+ realativePath,
87
+ };
72
88
 
73
89
  if (!params.hooks) {
74
90
  record[params.scope ? params.scope : "default"]?.set(realativePath, data);
@@ -87,7 +103,9 @@ export async function templates<Scope extends string | undefined = undefined>(pa
87
103
  }
88
104
  }
89
105
 
90
- return function setter(_ctx: Record<string, any>): { templates: Record<string, Map<string, TemplateData>> } {
106
+ return function setter(_ctx: Record<string, any>): {
107
+ templates: Record<string, Map<string, TemplateData>>;
108
+ } {
91
109
  return { templates: record };
92
110
  };
93
111
  }
@@ -3,6 +3,14 @@ import type { Action } from "../types.ts";
3
3
 
4
4
  export function parallel(actions: Action[]): Action {
5
5
  return async function execute(params) {
6
- await Promise.all(actions.map((action) => executeAction({ action, context: params.context, renderer: params.renderer })));
6
+ await Promise.all(
7
+ actions.map((action) =>
8
+ executeAction({
9
+ action,
10
+ context: params.context,
11
+ renderer: params.renderer,
12
+ }),
13
+ ),
14
+ );
7
15
  };
8
16
  }
@@ -20,10 +20,20 @@ export function renderTemplate({
20
20
 
21
21
  const renderedTemplate = params.renderer.renderString({ template, data });
22
22
 
23
- const renderedPath = params.renderer.renderString({ template: templatePath, data });
23
+ const renderedPath = params.renderer.renderString({
24
+ template: templatePath,
25
+ data,
26
+ });
24
27
 
25
28
  if (save) {
26
- return save({ ...params, context: { ...params.context, templateContent: renderedTemplate, destination: renderedPath } });
29
+ return save({
30
+ ...params,
31
+ context: {
32
+ ...params.context,
33
+ templateContent: renderedTemplate,
34
+ destination: renderedPath,
35
+ },
36
+ });
27
37
  }
28
38
  };
29
39
  }
@@ -18,7 +18,11 @@ export function selectGenerator(config: Config): Action {
18
18
 
19
19
  assert(generator);
20
20
 
21
- return runGenerator({ context: { errors: [], answers: {} }, renderer: new Renderer(), generator });
21
+ return runGenerator({
22
+ context: { errors: [], answers: {} },
23
+ renderer: new Renderer(),
24
+ generator,
25
+ });
22
26
  },
23
27
  ];
24
28
  }
package/actions/write.ts CHANGED
@@ -10,11 +10,10 @@ export type WriteActionConfig = {
10
10
  mode?: "force" | "skip-if-exists";
11
11
  };
12
12
 
13
- export function write({
14
- destination: writeDestination,
15
- templateContent: writeContent,
16
- mode,
17
- }: WriteActionConfig): Action<{ templateContent?: string; destination?: string }> {
13
+ export function write({ destination: writeDestination, templateContent: writeContent, mode }: WriteActionConfig): Action<{
14
+ templateContent?: string;
15
+ destination?: string;
16
+ }> {
18
17
  return async function execute({ context: { templateContent: executeContent, destination: executeDestination } }) {
19
18
  const destination = executeDestination || writeDestination;
20
19
  const templateContent = executeContent || writeContent;
package/jargal.ts CHANGED
@@ -10,7 +10,14 @@ import { context } from "./actions/jargal-context";
10
10
 
11
11
  export { templates, context };
12
12
 
13
- export type RenderEntry = { control: "user"; data: any; pathTemplate: string; contentTemplate: string } | { control?: "auto"; data: any; destination: string };
13
+ export type RenderEntry =
14
+ | {
15
+ control: "user";
16
+ data: any;
17
+ pathTemplate: string;
18
+ contentTemplate: string;
19
+ }
20
+ | { control?: "auto"; data: any; destination: string };
14
21
 
15
22
  export class Jargal<const in out Context> {
16
23
  #context = {
@@ -65,8 +72,14 @@ export class Jargal<const in out Context> {
65
72
  for (const [filename, template] of Object.entries(templatesToRender)) {
66
73
  // @ts-expect-error
67
74
  this.#context.renderedContent.push({
68
- savePath: this.#renderer.renderString({ template: join(renderEntry.destination, template.savePath), data: renderEntry.data }),
69
- content: this.#renderer.renderString({ template: template.templateContent, data: renderEntry.data }),
75
+ savePath: this.#renderer.renderString({
76
+ template: join(renderEntry.destination, template.savePath),
77
+ data: renderEntry.data,
78
+ }),
79
+ content: this.#renderer.renderString({
80
+ template: template.templateContent,
81
+ data: renderEntry.data,
82
+ }),
70
83
  });
71
84
  }
72
85
 
@@ -76,8 +89,14 @@ export class Jargal<const in out Context> {
76
89
  if (renderEntry.control === "user") {
77
90
  // @ts-expect-error
78
91
  this.#context.renderedContent.push({
79
- savePath: this.#renderer.renderString({ template: renderEntry.pathTemplate, data: renderEntry.data }),
80
- content: this.#renderer.renderString({ template: renderEntry.contentTemplate, data: renderEntry.data }),
92
+ savePath: this.#renderer.renderString({
93
+ template: renderEntry.pathTemplate,
94
+ data: renderEntry.data,
95
+ }),
96
+ content: this.#renderer.renderString({
97
+ template: renderEntry.contentTemplate,
98
+ data: renderEntry.data,
99
+ }),
81
100
  });
82
101
 
83
102
  continue;
@@ -131,7 +150,10 @@ export class Jargal<const in out Context> {
131
150
  ): Promise<void> {
132
151
  if (typeof write === "function") {
133
152
  // @ts-expect-error
134
- for (const renderConfig of this.#context.renderedContent as { savePath: string; content: string }[]) {
153
+ for (const renderConfig of this.#context.renderedContent as {
154
+ savePath: string;
155
+ content: string;
156
+ }[]) {
135
157
  await write({ ...renderConfig, defultWrite });
136
158
  }
137
159
 
@@ -140,7 +162,10 @@ export class Jargal<const in out Context> {
140
162
 
141
163
  // console.log({ renderedContent: this.#context.renderedContent})
142
164
  // @ts-expect-error
143
- for (const renderConfig of this.#context.renderedContent as { savePath: string; content: string }[]) {
165
+ for (const renderConfig of this.#context.renderedContent as {
166
+ savePath: string;
167
+ content: string;
168
+ }[]) {
144
169
  defultWrite(renderConfig);
145
170
  }
146
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeandmoney/jargal",
3
- "version": "0.0.0-dev.11",
3
+ "version": "0.0.0-dev.12",
4
4
  "description": "Renderer",
5
5
  "license": "MIT",
6
6
  "author": "Code & Money Team",