Page MenuHomeSealhub

No OneTemporary

diff --git a/src/add-route.ts b/src/add-route.ts
index 0e99c8b..84a2cd8 100644
--- a/src/add-route.ts
+++ b/src/add-route.ts
@@ -1,85 +1,86 @@
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> {
- prompts.override(require("yargs").argv);
+export async function addRoute(
+ params: Partial<{ [key in "action" | "url" | "mode"]: string }>,
+ app_directory: string = process.cwd()
+): Promise<void> {
+ const target_locreq = _locreq(app_directory);
+ prompts.override(params);
const response = await prompts([
{
type: "text",
name: "action",
message: 'What\'s the name of the action (e.g. "AddUser"): ',
validate: (s: string) =>
s.length > 3 ? true : "Should be at least 3 characters long",
},
{
type: "text",
name: "url",
message:
"Enter a full absolute path for the new route (e.g. /admin/users/add): ",
validate: (s: string) =>
s.trim()[0] == "/" ? true : "Should start with a '/'",
},
{
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 { action, url, mode } = response as Record<string, 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();
}
diff --git a/src/build.ts b/src/build.ts
index cc7a859..568ffd5 100644
--- a/src/build.ts
+++ b/src/build.ts
@@ -1,91 +1,93 @@
import { build, BuildResult } from "esbuild";
import { sassPlugin } from "esbuild-sass-plugin";
import * as chokidar from "chokidar";
import { default as glob } from "tiny-glob";
import { generateScssIncludes } from "./find-scss-includes";
import { sleep } from "./utils/sleep";
import { generateCollections } from "./generate-collections";
import { generateRoutes } from "./generate-routes";
async function build_ts() {
await Promise.all([generateCollections(), generateRoutes()]);
const entryPoints = await glob("./src/back/**/*.ts");
try {
await build({
entryPoints,
sourcemap: true,
outdir: "./dist/back",
logLevel: "info",
platform: "node",
target: "es2022",
format: "cjs",
});
} catch (e) {
console.error(e.message);
}
}
async function complex_build(watch: boolean): Promise<void> {
let scss_build: BuildResult | null = null;
if (watch) {
const watcher = chokidar.watch("src", { ignoreInitial: true });
watcher.on("all", async (_, path) => {
if (!scss_build) return;
if (path.endsWith(".scss") && !path.endsWith("/includes.scss")) {
// refresh the list of all scss files in includes.scss
await generateScssIncludes();
try {
await scss_build?.rebuild?.();
console.log(`Built main.scss [on ${path}]`);
} catch (e) {
console.error(e.message);
await sleep(200);
scss_build
?.rebuild?.()
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.catch((e) => console.error(e?.message));
}
}
if (
path.endsWith(".ts") &&
!path.endsWith("src/back/collections/collections.ts") &&
!path.endsWith("src/back/routes/routes.ts") &&
!path.endsWith("src/back/routes/urls.ts")
) {
await build_ts();
console.log(`Finished TS build [on ${path}]`);
}
});
}
await generateScssIncludes();
scss_build = await build({
entryPoints: ["./src/main.scss"],
sourcemap: true,
outfile: "./public/dist/style.css",
logLevel: "info",
incremental: watch,
plugins: [sassPlugin()],
});
await build_ts();
watch && scss_build?.rebuild?.();
}
-export async function buildProject(...args: string[]): Promise<void> {
- const watch = args.includes("--watch");
-
+export async function buildProject({
+ watch,
+}: {
+ watch: boolean;
+}): Promise<void> {
try {
await build({
entryPoints: ["./src/front/index.ts"],
sourcemap: true,
outfile: "./public/dist/bundle.js",
logLevel: "info",
bundle: true,
watch,
});
await complex_build(watch);
} catch (e) {
console.log("CAUGHT!");
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
console.error(e.message);
}
}
diff --git a/src/cli.ts b/src/cli.ts
index 58b70d8..cf78dda 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -1,36 +1,36 @@
#!/usr/bin/env node
import { addCollection } from "./add-collection";
import { addRoute } from "./add-route";
import { buildProject } from "./build";
import { generateScssIncludes } from "./find-scss-includes";
import { generateCollections } from "./generate-collections";
import { generateRoutes } from "./generate-routes";
import { makeEnv } from "./make-env";
const actions: Record<
string,
- (...args: string[]) => Promise<void> | undefined
+ (args: Record<string, string | boolean>) => Promise<void> | undefined
> = {
"add-collection": addCollection,
"add-route": addRoute,
"generate-collections": generateCollections,
"generate-routes": generateRoutes,
"generate-scss-includes": generateScssIncludes,
build: buildProject,
default: async function () {
console.log("Usage: `sealscript <action>`");
console.log(
`Available actions: ${Object.keys(actions)
.filter((e) => e != "default")
.join(", ")}`
);
},
"make-env": makeEnv,
};
void (async function () {
const action = process.argv.at(2);
actions;
const fn = actions[action || "default"] || actions.default;
- await fn(...process.argv.slice(3));
+ await fn(require("yargs").argv);
})();

File Metadata

Mime Type
text/x-diff
Expires
Tue, Dec 24, 14:05 (52 m, 57 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557332
Default Alt Text
(7 KB)

Event Timeline