Page MenuHomeSealhub

add-route.ts
No OneTemporary

add-route.ts

import { existsSync, promises as fs } from "fs";
import _locreq from "locreq";
import { resolve } from "path";
import { generateRoutes } from "./generate-routes";
import { formTemplate } from "./templates/form";
import { listTemplate } from "./templates/list";
import { multiformTemplate } from "./templates/multiform";
import { pageTemplate } from "./templates/page";
import { redirectTemplate } from "./templates/redirect";
import { testTemplate } from "./templates/route-test";
import { escape_url_params } from "./utils/escape-url-params";
import { question } from "./utils/question";
import prompts = require("prompts");
import { postTemplate } from "./templates/post";
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 response = await prompts({
type: "select",
name: "mode",
message: "What kind of route is it?",
choices: [
{ title: "page", value: "page" },
{ title: "form", value: "form" },
{ title: "list", value: "list" },
{ title: "multiform", value: "multiform" },
{ title: "redirect", value: "redirect" },
{ title: "raw POST", value: "post" },
],
});
const mode = response.mode as string;
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,
multiform: multiformTemplate,
redirect: redirectTemplate,
post: postTemplate,
}[mode];
if (!template) {
throw new Error(`Could not get template ${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
Wed, May 7, 19:44 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
624898
Default Alt Text
add-route.ts (2 KB)

Event Timeline