Page MenuHomeSealhub

add-route.ts
No OneTemporary

add-route.ts

import { resolve } from "path";
import { promises as fs, existsSync } from "fs";
import { question } from "./utils/question";
import { pageTemplate } from "./templates/page";
import { formTemplate } from "./templates/form";
import { testTemplate } from "./templates/route-test";
import { escape_url_params } from "./utils/escape-url-params";
import _locreq from "locreq";
import { generateRoutes } from "./generate-routes";
import { listTemplate } from "./templates/list";
const target_locreq = _locreq(process.cwd());
export async function addRoute(): Promise<void> {
const action = await question(
'What\'s the name of the action (e.g. "AddUser"): ',
(s: string) => s.length > 3
);
const url = await question(
"Enter a full absolute path for the new route (e.g. /admin/users/add): ",
(s: string) => s.trim()[0] == "/"
);
const mode_answer = (await question(
"Is this a page (p), a form (f), or a list (l)? ",
(a: string) => ["p", "f", "l"].includes(a.toLowerCase())
)) as "p" | "f" | "l";
const mode = (<const>{ p: "page", f: "form", l: "list" })[mode_answer];
const file_path = target_locreq.resolve(
`src/back/routes/${escape_url_params(url)}.${mode}.ts`
);
const test_file_path = target_locreq.resolve(
`src/back/routes/${escape_url_params(url)}.test.ts`
);
const template = {
page: pageTemplate,
form: formTemplate,
list: listTemplate,
}[mode];
if (existsSync(file_path)) {
// eslint-disable-next-line no-console
console.error(`ERROR: File ${file_path} already exists.`);
return;
}
await fs.mkdir(resolve(file_path, "../"), { recursive: true });
await fs.writeFile(file_path, await template(action, file_path));
await fs.writeFile(test_file_path, testTemplate(action, file_path));
// eslint-disable-next-line no-console
console.log(`${file_path} created`);
// eslint-disable-next-line no-console
console.log(`${test_file_path} created`);
await generateRoutes();
}

File Metadata

Mime Type
text/x-java
Expires
Mon, May 19, 00:55 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
635114
Default Alt Text
add-route.ts (1 KB)

Event Timeline