Page MenuHomeSealhub

generate-stimulus.ts
No OneTemporary

generate-stimulus.ts

import _locreq from "locreq";
import * as prettier from "prettier";
import { promises as fs } from "fs";
import { getPrettierConfig } from "./utils/prettier.js";
import recursive from "recursive-readdir";
import path, { dirname } from "path";
import { toKebabCase, toPascalCase } from "js-convert-case";
const target_locreq = _locreq(process.cwd());
export async function generateStimulusControllers(
controller_dirs: string[]
): Promise<void> {
const output_file = "src/front/controllers.ts";
const stimulus_search_dirs = [
target_locreq.resolve("src/"),
...controller_dirs,
];
const controller_names = (
await Promise.all(stimulus_search_dirs.map((dir) => recursive(dir)))
)
.flat()
.filter((c) => c.endsWith(".stimulus.ts"))
.sort();
const controller_data = controller_names
.map(
(p) =>
"./" +
path
.relative(target_locreq.resolve(dirname(output_file)), p)
.replace(/\.[tj]sx?$/, ".js")
)
.map((relative_path) => {
const name = relative_path.match(/\/([^/]+).stimulus/)?.[1];
return { name, relative_path };
});
const content = `// DO NOT EDIT! This file is generated automaticaly with 'npm run generate-stimulus'
import * as Turbo from "@hotwired/turbo";
import { Application } from "stimulus";
const application = Application.start();
${controller_data
.map(
(c) => `
import { default as ${toPascalCase(c.name)} } from "${c.relative_path}";
application.register("${toKebabCase(c.name)}", ${toPascalCase(c.name)});`
)
.join("\n")}
export { Turbo };
`;
await fs.writeFile(
target_locreq.resolve(output_file),
prettier.format(content, await getPrettierConfig())
);
// eslint-disable-next-line no-console
console.log(`Successfuly generated new ${output_file} file`);
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (20 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557112
Default Alt Text
generate-stimulus.ts (1 KB)

Event Timeline