@sealcode/sealgen 0.14.3 → 0.14.5

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.
@@ -0,0 +1,52 @@
1
+ import _locreq from "locreq";
2
+ import * as prettier from "prettier";
3
+ import { promises as fs } from "fs";
4
+ import { getPrettierConfig } from "./utils/prettier.js";
5
+ import recursive from "recursive-readdir";
6
+ import path, { dirname } from "path";
7
+ import { toKebabCase, toPascalCase } from "js-convert-case";
8
+
9
+ const target_locreq = _locreq(process.cwd());
10
+
11
+ export async function generateStimulusControllers(): Promise<void> {
12
+ const output_file = "src/front/controllers.ts";
13
+ const stimulus_search_dir = target_locreq.resolve("src/");
14
+ const controller_names = (await recursive(stimulus_search_dir))
15
+ .filter((c) => c.endsWith(".stimulus.ts"))
16
+ .sort();
17
+ const controller_data = controller_names
18
+ .map(
19
+ (p) =>
20
+ "./" +
21
+ path
22
+ .relative(target_locreq.resolve(dirname(output_file)), p)
23
+ .replace(/\.[tj]sx?$/, ".js")
24
+ )
25
+ .map((relative_path) => {
26
+ const name = relative_path.match(/\/([^\/]+).stimulus/)?.[1];
27
+ return { name, relative_path };
28
+ });
29
+
30
+ const content = `// DO NOT EDIT! This file is generated automaticaly with 'npm run generate-stimulus'
31
+
32
+ import * as Turbo from "@hotwired/turbo";
33
+ import { Application } from "stimulus";
34
+ const application = Application.start();
35
+ ${controller_data
36
+ .map(
37
+ (c) => `
38
+ import { default as ${toPascalCase(c.name)} } from "${c.relative_path}";
39
+ application.register("${toKebabCase(c.name)}", ${toPascalCase(c.name)});`
40
+ )
41
+ .join("\n")}
42
+
43
+ export { Turbo };
44
+ `;
45
+
46
+ await fs.writeFile(
47
+ target_locreq.resolve(output_file),
48
+ prettier.format(content, await getPrettierConfig())
49
+ );
50
+ // eslint-disable-next-line no-console
51
+ console.log(`Successfuly generated new ${output_file} file`);
52
+ }
@@ -66,12 +66,17 @@ export abstract class StatefulPage<
66
66
  this.wrapInForm(
67
67
  state,
68
68
  await original_render(ctx, state, inputs)
69
- )
69
+ ),
70
+ state
70
71
  );
71
72
  };
72
73
  }
73
74
 
74
- abstract wrapInLayout(ctx: BaseContext, content: Templatable): Templatable;
75
+ abstract wrapInLayout(
76
+ ctx: BaseContext,
77
+ content: Templatable,
78
+ state: State
79
+ ): Templatable;
75
80
 
76
81
  wrapInForm(state: State, content: Templatable): Templatable {
77
82
  return tempstream/* HTML */ `<form action="./" method="POST">
@@ -218,7 +223,7 @@ export abstract class StatefulPage<
218
223
  options
219
224
  );
220
225
  } else if (target.indexOf(item) === -1) {
221
- destination.push(item);
226
+ destination[index] = item;
222
227
  }
223
228
  });
224
229
  return destination;
@@ -0,0 +1,18 @@
1
+ import { toKebabCase, toPascalCase } from "js-convert-case";
2
+
3
+ export function jddComponentTemplate(controller_name: string) {
4
+ return `import { Controller } from "stimulus";
5
+
6
+ export default class ${toPascalCase(controller_name)} extends Controller {
7
+
8
+ connect() {
9
+ console.warn(
10
+ "Remove ${toKebabCase(
11
+ controller_name
12
+ )}.stimulus.ts file if your JDD component doesn't need any browser-side logic"
13
+ );
14
+ }
15
+ }
16
+
17
+ `;
18
+ }