web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

build.ts (2674B)


      1 #!/usr/bin/env node
      2 
      3 import fs from "node:fs";
      4 import path from "node:path";
      5 import yargs from "yargs";
      6 import { hideBin } from "yargs/helpers";
      7 import { z } from "zod";
      8 import type { ThemeKey } from "../core/types.js";
      9 import { build_layout_css_from_presets } from "../generator/layout_css.js";
     10 import { build_styles_css_from_presets } from "../generator/styles_css.js";
     11 import { build_themes_css_by_preset } from "../generator/themes_css.js";
     12 
     13 const args_schema = z.object({
     14     presets: z.array(z.string()).optional()
     15 });
     16 
     17 type BuildArgs = z.infer<typeof args_schema>;
     18 
     19 
     20 const resolve_script_dir = (): string => {
     21     const script_path = process.argv[1];
     22 
     23     if (script_path === undefined) {
     24         throw new Error("script path unavailable");
     25     }
     26 
     27     return path.dirname(path.resolve(script_path));
     28 };
     29 
     30 const package_root = path.resolve(resolve_script_dir(), "..", "..");
     31 const css_output_dir = path.resolve(package_root, "css");
     32 const styles_output_path = path.resolve(css_output_dir, "styles.css");
     33 const layout_output_path = path.resolve(css_output_dir, "layout.css");
     34 
     35 const write_css = (file_path: string, css: string): void => {
     36     const directory = path.dirname(file_path);
     37 
     38     if (!fs.existsSync(directory)) {
     39         fs.mkdirSync(directory, { recursive: true })
     40     }
     41 
     42     fs.writeFileSync(file_path, css, { encoding: "utf8" });
     43 };
     44 
     45 const parse_cli_args = (): BuildArgs => {
     46     const argv = yargs(hideBin(process.argv))
     47         .option("presets", {
     48             type: "string",
     49             array: true,
     50             demandOption: false
     51         })
     52         .strict()
     53         .help()
     54         .parseSync();
     55 
     56     return args_schema.parse(argv);
     57 };
     58 
     59 const main = (): void => {
     60     try {
     61         const args = parse_cli_args();
     62         const presets = args.presets;
     63 
     64         const styles_css = build_styles_css_from_presets(presets);
     65         write_css(styles_output_path, styles_css);
     66 
     67         const layout_css = build_layout_css_from_presets(undefined);
     68         write_css(layout_output_path, layout_css);
     69 
     70         const themes_css_by_preset = build_themes_css_by_preset(presets);
     71         const theme_keys = Object.keys(themes_css_by_preset) as ThemeKey[];
     72 
     73         for (const theme_key of theme_keys) {
     74             const css = themes_css_by_preset[theme_key];
     75             const theme_output_path = path.resolve(
     76                 css_output_dir,
     77                 `theme_${theme_key}.css`
     78             );
     79             write_css(theme_output_path, css);
     80         }
     81     } catch (error) {
     82         const message =
     83             error instanceof Error ? error.message : "unexpected error";
     84         console.error(`Error: ${message}`);
     85         process.exit(1);
     86     }
     87 };
     88 
     89 main();