@moku-labs/web 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -17,14 +17,16 @@ import remarkRehype from "remark-rehype";
17
17
  import { visit } from "unist-util-visit";
18
18
  import { defaultSchema } from "hast-util-sanitize";
19
19
  import readingTime from "reading-time";
20
- import { existsSync, readFileSync, readdirSync } from "node:fs";
20
+ import { existsSync, readFileSync, readdirSync, statSync, watch } from "node:fs";
21
21
  import { createHash, randomUUID } from "node:crypto";
22
22
  import { Feed } from "feed";
23
23
  import { h } from "preact";
24
24
  import { renderToString } from "preact-render-to-string";
25
+ import { createInterface } from "node:readline";
26
+ import { networkInterfaces } from "node:os";
25
27
  //#region src/plugins/env/api.ts
26
28
  /** Error prefix for all env API failures. */
27
- const ERROR_PREFIX$14 = "[web]";
29
+ const ERROR_PREFIX$15 = "[web]";
28
30
  /**
29
31
  * Creates the env plugin API surface mounted at `ctx.env`. Closes over
30
32
  * `ctx.state` ({@link EnvState}) and reads the frozen `resolved` / `publicMap`
@@ -68,7 +70,7 @@ function createEnvApi(ctx) {
68
70
  */
69
71
  require(key) {
70
72
  const value = resolved.get(key);
71
- if (value === void 0) throw new Error(`${ERROR_PREFIX$14} env: required variable "${key}" is not defined.`);
73
+ if (value === void 0) throw new Error(`${ERROR_PREFIX$15} env: required variable "${key}" is not defined.`);
72
74
  return value;
73
75
  },
74
76
  /**
@@ -135,7 +137,7 @@ function createEnvState() {
135
137
  /** Error message thrown by every frozen-map mutator. */
136
138
  const FROZEN_MESSAGE = "env: map is frozen and cannot be mutated";
137
139
  /** Error prefix for all resolution-pipeline failures. */
138
- const ERROR_PREFIX$13 = "[web]";
140
+ const ERROR_PREFIX$14 = "[web]";
139
141
  /**
140
142
  * Throws the canonical frozen-map error; installed as a map's `set`/`clear`/`delete`.
141
143
  *
@@ -186,8 +188,8 @@ function crossCheckPublicPrefix(config) {
186
188
  const { schema, publicPrefix } = config;
187
189
  for (const [key, spec] of Object.entries(schema)) {
188
190
  const hasPrefix = key.startsWith(publicPrefix);
189
- if (spec.public === true && !hasPrefix) throw new Error(`${ERROR_PREFIX$13} env: "${key}" is marked public but does not start with "${publicPrefix}".`);
190
- if (hasPrefix && spec.public !== true) throw new Error(`${ERROR_PREFIX$13} env: "${key}" starts with "${publicPrefix}" but is not marked public:true.`);
191
+ if (spec.public === true && !hasPrefix) throw new Error(`${ERROR_PREFIX$14} env: "${key}" is marked public but does not start with "${publicPrefix}".`);
192
+ if (hasPrefix && spec.public !== true) throw new Error(`${ERROR_PREFIX$14} env: "${key}" starts with "${publicPrefix}" but is not marked public:true.`);
191
193
  }
192
194
  }
193
195
  /**
@@ -238,7 +240,7 @@ function validateSchema(ctx) {
238
240
  crossCheckPublicPrefix(config);
239
241
  for (const [key, spec] of Object.entries(schema)) {
240
242
  if (merged[key] === void 0 && spec.default !== void 0) merged[key] = spec.default;
241
- if (merged[key] === void 0 && spec.required === true) throw new Error(`${ERROR_PREFIX$13} env: required variable "${key}" is not defined by any provider or default.`);
243
+ if (merged[key] === void 0 && spec.required === true) throw new Error(`${ERROR_PREFIX$14} env: required variable "${key}" is not defined by any provider or default.`);
242
244
  }
243
245
  for (const [key, spec] of Object.entries(schema)) {
244
246
  const value = merged[key];
@@ -780,7 +782,7 @@ const createCore = coreConfig.createCore;
780
782
  //#endregion
781
783
  //#region src/plugins/i18n/api.ts
782
784
  /** Error prefix for all i18n lifecycle failures. */
783
- const ERROR_PREFIX$12 = "[web]";
785
+ const ERROR_PREFIX$13 = "[web]";
784
786
  /**
785
787
  * Validates the resolved i18n config (fail-fast at `createApp`). Throws when
786
788
  * `locales` is empty or when `defaultLocale` is not a member of `locales`.
@@ -796,8 +798,8 @@ const ERROR_PREFIX$12 = "[web]";
796
798
  */
797
799
  function validateI18nConfig(ctx) {
798
800
  const { locales, defaultLocale } = ctx.config;
799
- if (locales.length === 0) throw new Error(`${ERROR_PREFIX$12} i18n.locales must contain at least one locale.\n Set pluginConfigs.i18n.locales to a non-empty array, e.g. ["en"].`);
800
- if (!locales.includes(defaultLocale)) throw new Error(`${ERROR_PREFIX$12} i18n.defaultLocale "${defaultLocale}" is not in i18n.locales [${locales.join(", ")}].\n Set pluginConfigs.i18n.defaultLocale to one of the configured locales, or add "${defaultLocale}" to i18n.locales.`);
801
+ if (locales.length === 0) throw new Error(`${ERROR_PREFIX$13} i18n.locales must contain at least one locale.\n Set pluginConfigs.i18n.locales to a non-empty array, e.g. ["en"].`);
802
+ if (!locales.includes(defaultLocale)) throw new Error(`${ERROR_PREFIX$13} i18n.defaultLocale "${defaultLocale}" is not in i18n.locales [${locales.join(", ")}].\n Set pluginConfigs.i18n.defaultLocale to one of the configured locales, or add "${defaultLocale}" to i18n.locales.`);
801
803
  }
802
804
  /**
803
805
  * Creates the i18n plugin API surface — locale registry accessors plus the
@@ -1801,7 +1803,7 @@ const contentPlugin = createPlugin$1("content", {
1801
1803
  //#endregion
1802
1804
  //#region src/plugins/site/api.ts
1803
1805
  /** Error prefix for all site lifecycle/validation failures. */
1804
- const ERROR_PREFIX$11 = "[web]";
1806
+ const ERROR_PREFIX$12 = "[web]";
1805
1807
  /**
1806
1808
  * Joins a relative path against an absolute base URL, normalizing the slash
1807
1809
  * boundary to exactly one "/". Returns the base unchanged for an empty or
@@ -1869,8 +1871,8 @@ function isAbsoluteUrl(value) {
1869
1871
  * ```
1870
1872
  */
1871
1873
  function validateSiteConfig(ctx) {
1872
- if (!isNonEmpty(ctx.config.name)) throw new Error(`${ERROR_PREFIX$11} site.name is required.\n Provide a non-empty site name in pluginConfigs.site.name.`);
1873
- if (!isAbsoluteUrl(ctx.config.url)) throw new Error(`${ERROR_PREFIX$11} site.url must be a valid absolute URL (http/https), received ${JSON.stringify(ctx.config.url)}.\n Provide an absolute URL in pluginConfigs.site.url, e.g. "https://blog.dev".`);
1874
+ if (!isNonEmpty(ctx.config.name)) throw new Error(`${ERROR_PREFIX$12} site.name is required.\n Provide a non-empty site name in pluginConfigs.site.name.`);
1875
+ if (!isAbsoluteUrl(ctx.config.url)) throw new Error(`${ERROR_PREFIX$12} site.url must be a valid absolute URL (http/https), received ${JSON.stringify(ctx.config.url)}.\n Provide an absolute URL in pluginConfigs.site.url, e.g. "https://blog.dev".`);
1874
1876
  }
1875
1877
  /**
1876
1878
  * Creates the site plugin API surface — read-only accessors over frozen config
@@ -2060,7 +2062,7 @@ function matchRoute(compiled, pathname) {
2060
2062
  * `manifest`. Returns values/copies, never the raw `ctx.state` reference (spec/11 §2.4).
2061
2063
  */
2062
2064
  /** Error prefix for router API failures. */
2063
- const ERROR_PREFIX$10 = "[web] router";
2065
+ const ERROR_PREFIX$11 = "[web] router";
2064
2066
  /**
2065
2067
  * Read the compiled matcher table, throwing if `onInit` has not run yet. This
2066
2068
  * `null` cannot occur in practice post-`onInit`; the guard documents the invariant.
@@ -2074,7 +2076,7 @@ const ERROR_PREFIX$10 = "[web] router";
2074
2076
  * ```
2075
2077
  */
2076
2078
  function readTable(state) {
2077
- if (state.table === null) throw new Error(`${ERROR_PREFIX$10}: matcher table accessed before onInit compiled it.`);
2079
+ if (state.table === null) throw new Error(`${ERROR_PREFIX$11}: matcher table accessed before onInit compiled it.`);
2078
2080
  return state.table;
2079
2081
  }
2080
2082
  /**
@@ -2128,7 +2130,7 @@ function toClientRoute(entry) {
2128
2130
  * api.match("/en/hello/");
2129
2131
  * ```
2130
2132
  */
2131
- function createApi$4(ctx) {
2133
+ function createApi$5(ctx) {
2132
2134
  const { state } = ctx;
2133
2135
  return {
2134
2136
  /**
@@ -2158,7 +2160,7 @@ function createApi$4(ctx) {
2158
2160
  */
2159
2161
  toUrl(routeName, params) {
2160
2162
  const entry = readTable(state).byName.get(routeName);
2161
- if (!entry) throw new Error(`${ERROR_PREFIX$10}: unknown route name "${routeName}".`);
2163
+ if (!entry) throw new Error(`${ERROR_PREFIX$11}: unknown route name "${routeName}".`);
2162
2164
  return entry.toUrl(params);
2163
2165
  },
2164
2166
  /**
@@ -2293,7 +2295,7 @@ function bySpecificity(a, b) {
2293
2295
  * only (`CompileInput`) — never the plugin ctx.
2294
2296
  */
2295
2297
  /** Shared `[web]` error prefix for router validation failures. */
2296
- const ERROR_PREFIX$9 = "[web] router";
2298
+ const ERROR_PREFIX$10 = "[web] router";
2297
2299
  /**
2298
2300
  * Validate the route map (fail-fast in `onInit`). Throws with the `[web]` prefix
2299
2301
  * naming the offending route/pattern on any failure: empty map, a pattern not
@@ -2308,12 +2310,12 @@ const ERROR_PREFIX$9 = "[web] router";
2308
2310
  */
2309
2311
  function validateRoutes(routes) {
2310
2312
  const names = Object.keys(routes);
2311
- if (names.length === 0) throw new Error(`${ERROR_PREFIX$9}: route map is empty — provide at least one route via pluginConfigs.router.routes.`);
2313
+ if (names.length === 0) throw new Error(`${ERROR_PREFIX$10}: route map is empty — provide at least one route via pluginConfigs.router.routes.`);
2312
2314
  for (const name of names) {
2313
2315
  const pattern = routes[name]?.pattern ?? "";
2314
- if (!pattern.startsWith("/")) throw new Error(`${ERROR_PREFIX$9}: route "${name}" pattern must start with "/" (got "${pattern}").`);
2315
- if ((pattern.match(/\{/g) ?? []).length !== (pattern.match(/\}/g) ?? []).length) throw new Error(`${ERROR_PREFIX$9}: route "${name}" pattern has unbalanced braces ("${pattern}").`);
2316
- if ((pattern.match(/\{lang:\?\}/g) ?? []).length > 1) throw new Error(`${ERROR_PREFIX$9}: route "${name}" pattern has more than one {lang:?} segment ("${pattern}").`);
2316
+ if (!pattern.startsWith("/")) throw new Error(`${ERROR_PREFIX$10}: route "${name}" pattern must start with "/" (got "${pattern}").`);
2317
+ if ((pattern.match(/\{/g) ?? []).length !== (pattern.match(/\}/g) ?? []).length) throw new Error(`${ERROR_PREFIX$10}: route "${name}" pattern has unbalanced braces ("${pattern}").`);
2318
+ if ((pattern.match(/\{lang:\?\}/g) ?? []).length > 1) throw new Error(`${ERROR_PREFIX$10}: route "${name}" pattern has more than one {lang:?} segment ("${pattern}").`);
2317
2319
  }
2318
2320
  }
2319
2321
  /**
@@ -2718,7 +2720,7 @@ function defineRoutes(routes) {
2718
2720
  * const state = createState({ global: {}, config: { routes: {} } });
2719
2721
  * ```
2720
2722
  */
2721
- function createState$4(_ctx) {
2723
+ function createState$5(_ctx) {
2722
2724
  return {
2723
2725
  table: null,
2724
2726
  mode: _ctx.config.mode ?? "hybrid"
@@ -2754,8 +2756,8 @@ const routerPlugin = createPlugin$1("router", {
2754
2756
  routes: {},
2755
2757
  mode: "hybrid"
2756
2758
  },
2757
- createState: createState$4,
2758
- api: createApi$4,
2759
+ createState: createState$5,
2760
+ api: createApi$5,
2759
2761
  onInit(ctx) {
2760
2762
  const i18n = ctx.require(i18nPlugin);
2761
2763
  const baseUrl = ctx.require(sitePlugin).url();
@@ -3095,7 +3097,7 @@ function serializeHead(elements) {
3095
3097
  * it to a string. It holds no resource and caches no subscription.
3096
3098
  */
3097
3099
  /** Error prefix for head API invariant failures. */
3098
- const ERROR_PREFIX$8 = "[head]";
3100
+ const ERROR_PREFIX$9 = "[head]";
3099
3101
  /**
3100
3102
  * Read the normalized defaults, asserting the post-`onInit` invariant (the slot is
3101
3103
  * `null` only before `onInit` assigns it, which cannot occur at render time).
@@ -3109,7 +3111,7 @@ const ERROR_PREFIX$8 = "[head]";
3109
3111
  * ```
3110
3112
  */
3111
3113
  function readDefaults(state) {
3112
- if (state.defaults === null) throw new Error(`${ERROR_PREFIX$8}: defaults accessed before onInit normalized them.`);
3114
+ if (state.defaults === null) throw new Error(`${ERROR_PREFIX$9}: defaults accessed before onInit normalized them.`);
3113
3115
  return state.defaults;
3114
3116
  }
3115
3117
  /**
@@ -3125,7 +3127,7 @@ function readDefaults(state) {
3125
3127
  * api.render(route, data);
3126
3128
  * ```
3127
3129
  */
3128
- function createApi$3(ctx) {
3130
+ function createApi$4(ctx) {
3129
3131
  return {
3130
3132
  /**
3131
3133
  * Compose the final `<head>` inner HTML for a route (pulled by `build`).
@@ -3152,7 +3154,7 @@ render(route, data) {
3152
3154
  //#endregion
3153
3155
  //#region src/plugins/head/config.ts
3154
3156
  /** Error prefix for all head config-validation failures. */
3155
- const ERROR_PREFIX$7 = "[head] config:";
3157
+ const ERROR_PREFIX$8 = "[head] config:";
3156
3158
  /** The allowed `twitterCard` literals (also the runtime guard set). */
3157
3159
  const VALID_TWITTER_CARDS = ["summary", "summary_large_image"];
3158
3160
  /**
@@ -3165,7 +3167,7 @@ const VALID_TWITTER_CARDS = ["summary", "summary_large_image"];
3165
3167
  * createPlugin("head", { config: defaultConfig });
3166
3168
  * ```
3167
3169
  */
3168
- const defaultConfig$2 = { twitterCard: "summary_large_image" };
3170
+ const defaultConfig$3 = { twitterCard: "summary_large_image" };
3169
3171
  /**
3170
3172
  * Structurally validate the resolved head config (no I/O). Throws a standard
3171
3173
  * `[head] config: …` error when `titleTemplate` is provided without the `%s`
@@ -3179,8 +3181,8 @@ const defaultConfig$2 = { twitterCard: "summary_large_image" };
3179
3181
  * ```
3180
3182
  */
3181
3183
  function validateHeadConfig(config) {
3182
- if (config.titleTemplate !== void 0 && !config.titleTemplate.includes("%s")) throw new Error(`${ERROR_PREFIX$7} titleTemplate must contain the "%s" token (replaced by the route title), received ${JSON.stringify(config.titleTemplate)}.`);
3183
- if (config.twitterCard !== void 0 && !VALID_TWITTER_CARDS.includes(config.twitterCard)) throw new Error(`${ERROR_PREFIX$7} twitterCard must be one of [${VALID_TWITTER_CARDS.join(", ")}], received ${JSON.stringify(config.twitterCard)}.`);
3184
+ if (config.titleTemplate !== void 0 && !config.titleTemplate.includes("%s")) throw new Error(`${ERROR_PREFIX$8} titleTemplate must contain the "%s" token (replaced by the route title), received ${JSON.stringify(config.titleTemplate)}.`);
3185
+ if (config.twitterCard !== void 0 && !VALID_TWITTER_CARDS.includes(config.twitterCard)) throw new Error(`${ERROR_PREFIX$8} twitterCard must be one of [${VALID_TWITTER_CARDS.join(", ")}], received ${JSON.stringify(config.twitterCard)}.`);
3184
3186
  }
3185
3187
  /**
3186
3188
  * Validate then build the frozen, normalized {@link HeadDefaults} snapshot read by
@@ -3248,7 +3250,7 @@ const headHelpers = {
3248
3250
  * const state = createState({ global: {}, config: {} });
3249
3251
  * ```
3250
3252
  */
3251
- function createState$3(_ctx) {
3253
+ function createState$4(_ctx) {
3252
3254
  return { defaults: null };
3253
3255
  }
3254
3256
  //#endregion
@@ -3283,9 +3285,9 @@ const headPlugin = createPlugin$1("head", {
3283
3285
  routerPlugin
3284
3286
  ],
3285
3287
  helpers: headHelpers,
3286
- config: defaultConfig$2,
3287
- createState: createState$3,
3288
- api: createApi$3,
3288
+ config: defaultConfig$3,
3289
+ createState: createState$4,
3290
+ api: createApi$4,
3289
3291
  onInit(ctx) {
3290
3292
  ctx.state.defaults = normalizeHeadConfig(ctx.config);
3291
3293
  }
@@ -4878,7 +4880,7 @@ async function runPipeline(ctx, options) {
4878
4880
  * @file build plugin — API factory (run + phases), cross-plugin wiring, and onInit config validation.
4879
4881
  */
4880
4882
  /** Error prefix for build config/validation failures (spec/11 Part-3). */
4881
- const ERROR_PREFIX$6 = "[web] build";
4883
+ const ERROR_PREFIX$7 = "[web] build";
4882
4884
  /** Recognized font file extensions for OG-image validation. */
4883
4885
  const FONT_EXTENSIONS = [
4884
4886
  ".ttf",
@@ -4886,7 +4888,7 @@ const FONT_EXTENSIONS = [
4886
4888
  ".woff"
4887
4889
  ];
4888
4890
  /** Typed default `build` config (R6: no inline `as`). `ogImage: false` disables OG generation. */
4889
- const defaultConfig$1 = {
4891
+ const defaultConfig$2 = {
4890
4892
  outDir: "./dist",
4891
4893
  minify: true,
4892
4894
  feeds: true,
@@ -4908,7 +4910,7 @@ const defaultConfig$1 = {
4908
4910
  * await api.run({ outDir: "./preview" });
4909
4911
  * ```
4910
4912
  */
4911
- function createApi$2(ctx) {
4913
+ function createApi$3(ctx) {
4912
4914
  return {
4913
4915
  /**
4914
4916
  * Run the full SSG pipeline and write the site to disk.
@@ -4949,8 +4951,8 @@ function createApi$2(ctx) {
4949
4951
  * ```
4950
4952
  */
4951
4953
  function validateFonts(og) {
4952
- if (typeof og.fontDir !== "string" || og.fontDir.length === 0 || !existsSync(og.fontDir)) throw new Error(`${ERROR_PREFIX$6}.ogImage: fontDir "${og.fontDir}" does not exist — provide a directory with at least one font.`);
4953
- if (!readdirSync(og.fontDir).some((name) => FONT_EXTENSIONS.some((extension) => name.endsWith(extension)))) throw new Error(`${ERROR_PREFIX$6}.ogImage: fontDir "${og.fontDir}" contains no .ttf/.otf/.woff font files.`);
4954
+ if (typeof og.fontDir !== "string" || og.fontDir.length === 0 || !existsSync(og.fontDir)) throw new Error(`${ERROR_PREFIX$7}.ogImage: fontDir "${og.fontDir}" does not exist — provide a directory with at least one font.`);
4955
+ if (!readdirSync(og.fontDir).some((name) => FONT_EXTENSIONS.some((extension) => name.endsWith(extension)))) throw new Error(`${ERROR_PREFIX$7}.ogImage: fontDir "${og.fontDir}" contains no .ttf/.otf/.woff font files.`);
4954
4956
  }
4955
4957
  /**
4956
4958
  * Validates `build` config synchronously in `onInit` (return value discarded).
@@ -4963,11 +4965,11 @@ function validateFonts(og) {
4963
4965
  * validateConfig(ctx.config);
4964
4966
  * ```
4965
4967
  */
4966
- function validateConfig$1(config) {
4967
- if (typeof config.outDir !== "string" || config.outDir.trim().length === 0) throw new Error(`${ERROR_PREFIX$6}.outDir: must be a non-empty string.`);
4968
- if (config.publicDir !== void 0 && typeof config.publicDir !== "string") throw new Error(`${ERROR_PREFIX$6}.publicDir: must be a string when set.`);
4969
- if (config.template !== void 0 && typeof config.template !== "string") throw new Error(`${ERROR_PREFIX$6}.template: must be a string path when set.`);
4970
- if (config.clientEntry !== void 0 && typeof config.clientEntry !== "string") throw new Error(`${ERROR_PREFIX$6}.clientEntry: must be a string path when set.`);
4968
+ function validateConfig$2(config) {
4969
+ if (typeof config.outDir !== "string" || config.outDir.trim().length === 0) throw new Error(`${ERROR_PREFIX$7}.outDir: must be a non-empty string.`);
4970
+ if (config.publicDir !== void 0 && typeof config.publicDir !== "string") throw new Error(`${ERROR_PREFIX$7}.publicDir: must be a string when set.`);
4971
+ if (config.template !== void 0 && typeof config.template !== "string") throw new Error(`${ERROR_PREFIX$7}.template: must be a string path when set.`);
4972
+ if (config.clientEntry !== void 0 && typeof config.clientEntry !== "string") throw new Error(`${ERROR_PREFIX$7}.clientEntry: must be a string path when set.`);
4971
4973
  if (config.ogImage) validateFonts(config.ogImage);
4972
4974
  }
4973
4975
  //#endregion
@@ -5006,7 +5008,7 @@ function createEvents(register) {
5006
5008
  * const state = createState({ global: {}, config });
5007
5009
  * ```
5008
5010
  */
5009
- function createState$2(ctx) {
5011
+ function createState$3(ctx) {
5010
5012
  return {
5011
5013
  config: ctx.config,
5012
5014
  manifest: null,
@@ -5050,11 +5052,11 @@ const buildPlugin = createPlugin$1("build", {
5050
5052
  routerPlugin,
5051
5053
  headPlugin
5052
5054
  ],
5053
- config: defaultConfig$1,
5054
- createState: createState$2,
5055
+ config: defaultConfig$2,
5056
+ createState: createState$3,
5055
5057
  events: createEvents,
5056
- api: createApi$2,
5057
- onInit: (ctx) => validateConfig$1(ctx.config)
5058
+ api: createApi$3,
5059
+ onInit: (ctx) => validateConfig$2(ctx.config)
5058
5060
  });
5059
5061
  //#endregion
5060
5062
  //#region src/plugins/deploy/wrangler.ts
@@ -5069,7 +5071,7 @@ const buildPlugin = createPlugin$1("build", {
5069
5071
  */
5070
5072
  const MOKU_WRANGLER_VERSION = "4.34.0";
5071
5073
  /** Error prefix for deploy runtime failures (spec/11 Part-3). */
5072
- const ERROR_PREFIX$5 = "[web] deploy";
5074
+ const ERROR_PREFIX$6 = "[web] deploy";
5073
5075
  /** Mask substituted for a detected secret-like token. */
5074
5076
  const MASK = "***";
5075
5077
  /** Minimum token length eligible for entropy-gated scrubbing. */
@@ -5147,7 +5149,7 @@ function scrubSecrets(text, allowlist) {
5147
5149
  * guardBranch("preview/landing"); // "preview/landing"
5148
5150
  */
5149
5151
  function guardBranch(branch) {
5150
- if (!BRANCH_REGEX.test(branch) || branch.startsWith("-")) throw deployError("ERR_DEPLOY_INVALID_BRANCH", `${ERROR_PREFIX$5}: branch ${JSON.stringify(branch)} is invalid.\n Branches must match /^[a-zA-Z0-9/_.-]+$/ so they cannot inject wrangler flags.`);
5152
+ if (!BRANCH_REGEX.test(branch) || branch.startsWith("-")) throw deployError("ERR_DEPLOY_INVALID_BRANCH", `${ERROR_PREFIX$6}: branch ${JSON.stringify(branch)} is invalid.\n Branches must match /^[a-zA-Z0-9/_.-]+$/ so they cannot inject wrangler flags.`);
5151
5153
  return branch;
5152
5154
  }
5153
5155
  /**
@@ -5164,7 +5166,7 @@ function guardBranch(branch) {
5164
5166
  function assertWithinRoot(outDir, root) {
5165
5167
  const resolved = path.isAbsolute(outDir) ? path.resolve(outDir) : path.resolve(root, outDir);
5166
5168
  const rootResolved = path.resolve(root);
5167
- if (resolved !== rootResolved && !resolved.startsWith(rootResolved + path.sep)) throw deployError("ERR_DEPLOY_PATH_TRAVERSAL", `${ERROR_PREFIX$5}: outDir ${JSON.stringify(outDir)} resolves outside the project root.\n Point outDir at a directory inside ${JSON.stringify(rootResolved)}.`);
5169
+ if (resolved !== rootResolved && !resolved.startsWith(rootResolved + path.sep)) throw deployError("ERR_DEPLOY_PATH_TRAVERSAL", `${ERROR_PREFIX$6}: outDir ${JSON.stringify(outDir)} resolves outside the project root.\n Point outDir at a directory inside ${JSON.stringify(rootResolved)}.`);
5168
5170
  return resolved;
5169
5171
  }
5170
5172
  /**
@@ -5249,11 +5251,11 @@ function classifyWranglerError(exitCode, scrubbedStderr) {
5249
5251
  const haystack = scrubbedStderr.toLowerCase();
5250
5252
  for (const signature of ERROR_SIGNATURES) if (signature.match.some((needle) => haystack.includes(needle))) return {
5251
5253
  code: signature.kind,
5252
- message: `${ERROR_PREFIX$5}: wrangler failed (exit ${exitCode}).\n ${signature.advice}`
5254
+ message: `${ERROR_PREFIX$6}: wrangler failed (exit ${exitCode}).\n ${signature.advice}`
5253
5255
  };
5254
5256
  return {
5255
5257
  code: "ERR_DEPLOY_WRANGLER_FAILED",
5256
- message: `${ERROR_PREFIX$5}: wrangler failed (exit ${exitCode}).\n ${scrubbedStderr.trim().slice(-500)}`
5258
+ message: `${ERROR_PREFIX$6}: wrangler failed (exit ${exitCode}).\n ${scrubbedStderr.trim().slice(-500)}`
5257
5259
  };
5258
5260
  }
5259
5261
  /**
@@ -5519,7 +5521,7 @@ async function reconcile(input) {
5519
5521
  * and short-circuiting on the first failure.
5520
5522
  */
5521
5523
  /** Error prefix for deploy preflight failures (spec/11 Part-3). */
5522
- const ERROR_PREFIX$4 = "[web] deploy";
5524
+ const ERROR_PREFIX$5 = "[web] deploy";
5523
5525
  /** Cloudflare Pages free-tier file-count limit. */
5524
5526
  const FREE_TIER_FILE_LIMIT = 2e4;
5525
5527
  /** Cloudflare Pages paid-tier file-count limit (env override target). */
@@ -5595,15 +5597,15 @@ async function runPreflight(config, root, env = process.env) {
5595
5597
  try {
5596
5598
  await stat(wranglerPath);
5597
5599
  } catch {
5598
- throw deployError("ERR_DEPLOY_NO_WRANGLER_CONFIG", `${ERROR_PREFIX$4}: wrangler.jsonc not found.\n Run \`app.deploy.init()\` to scaffold it, then retry.`);
5600
+ throw deployError("ERR_DEPLOY_NO_WRANGLER_CONFIG", `${ERROR_PREFIX$5}: wrangler.jsonc not found.\n Run \`app.deploy.init()\` to scaffold it, then retry.`);
5599
5601
  }
5600
5602
  const stats = await inspectOutdir(path.isAbsolute(config.outDir) ? path.resolve(config.outDir) : path.resolve(root, config.outDir)).catch(() => {
5601
- throw deployError("ERR_DEPLOY_EMPTY_OUTDIR", `${ERROR_PREFIX$4}: outDir ${JSON.stringify(config.outDir)} is missing.\n Run your build first, then retry.`);
5603
+ throw deployError("ERR_DEPLOY_EMPTY_OUTDIR", `${ERROR_PREFIX$5}: outDir ${JSON.stringify(config.outDir)} is missing.\n Run your build first, then retry.`);
5602
5604
  });
5603
- if (stats.fileCount === 0) throw deployError("ERR_DEPLOY_EMPTY_OUTDIR", `${ERROR_PREFIX$4}: outDir ${JSON.stringify(config.outDir)} is empty — nothing to deploy.`);
5605
+ if (stats.fileCount === 0) throw deployError("ERR_DEPLOY_EMPTY_OUTDIR", `${ERROR_PREFIX$5}: outDir ${JSON.stringify(config.outDir)} is empty — nothing to deploy.`);
5604
5606
  const limit = resolveFileLimit(env);
5605
- if (stats.fileCount > limit) throw deployError("ERR_DEPLOY_TOO_MANY_FILES", `${ERROR_PREFIX$4}: outDir contains ${stats.fileCount} files; the limit is ${limit}.\n Raise it with ${MAX_FILES_ENV} (paid tier) or reduce the output.`);
5606
- if (stats.oversizePath !== null) throw deployError("ERR_DEPLOY_FILE_TOO_LARGE", `${ERROR_PREFIX$4}: file ${JSON.stringify(stats.oversizePath)} exceeds the 25 MiB per-file limit.`);
5607
+ if (stats.fileCount > limit) throw deployError("ERR_DEPLOY_TOO_MANY_FILES", `${ERROR_PREFIX$5}: outDir contains ${stats.fileCount} files; the limit is ${limit}.\n Raise it with ${MAX_FILES_ENV} (paid tier) or reduce the output.`);
5608
+ if (stats.oversizePath !== null) throw deployError("ERR_DEPLOY_FILE_TOO_LARGE", `${ERROR_PREFIX$5}: file ${JSON.stringify(stats.oversizePath)} exceeds the 25 MiB per-file limit.`);
5607
5609
  }
5608
5610
  //#endregion
5609
5611
  //#region src/plugins/deploy/slug.ts
@@ -5648,7 +5650,7 @@ function toSlug(name) {
5648
5650
  //#endregion
5649
5651
  //#region src/plugins/deploy/api.ts
5650
5652
  /** Error prefix for deploy config/validation failures (spec/11 Part-3). */
5651
- const ERROR_PREFIX$3 = "[web] deploy";
5653
+ const ERROR_PREFIX$4 = "[web] deploy";
5652
5654
  /** `YYYY-MM-DD` validator for the compatibility date config field. */
5653
5655
  const COMPAT_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
5654
5656
  /**
@@ -5661,12 +5663,12 @@ const COMPAT_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
5661
5663
  * @example
5662
5664
  * createPlugin("deploy", { onInit: validateConfig });
5663
5665
  */
5664
- function validateConfig(ctx) {
5666
+ function validateConfig$1(ctx) {
5665
5667
  const { config } = ctx;
5666
- if (config.target !== "cloudflare-pages") throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$3}: target ${JSON.stringify(config.target)} is unsupported.\n Only "cloudflare-pages" is supported in this version.`);
5667
- if (typeof config.outDir !== "string" || config.outDir.length === 0) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$3}: outDir must be a non-empty string.\n Set pluginConfigs.deploy.outDir to your build output directory (e.g. "dist").`);
5668
- if (!Array.isArray(config.scrubAllowlist) || !config.scrubAllowlist.every((item) => typeof item === "string")) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$3}: scrubAllowlist must be an array of strings.`);
5669
- if (config.compatibilityDate !== void 0 && !COMPAT_DATE_REGEX.test(config.compatibilityDate)) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$3}: compatibilityDate ${JSON.stringify(config.compatibilityDate)} must be in YYYY-MM-DD form.`);
5668
+ if (config.target !== "cloudflare-pages") throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$4}: target ${JSON.stringify(config.target)} is unsupported.\n Only "cloudflare-pages" is supported in this version.`);
5669
+ if (typeof config.outDir !== "string" || config.outDir.length === 0) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$4}: outDir must be a non-empty string.\n Set pluginConfigs.deploy.outDir to your build output directory (e.g. "dist").`);
5670
+ if (!Array.isArray(config.scrubAllowlist) || !config.scrubAllowlist.every((item) => typeof item === "string")) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$4}: scrubAllowlist must be an array of strings.`);
5671
+ if (config.compatibilityDate !== void 0 && !COMPAT_DATE_REGEX.test(config.compatibilityDate)) throw deployError("ERR_DEPLOY_CONFIG", `${ERROR_PREFIX$4}: compatibilityDate ${JSON.stringify(config.compatibilityDate)} must be in YYYY-MM-DD form.`);
5670
5672
  ctx.require(sitePlugin);
5671
5673
  }
5672
5674
  /**
@@ -5682,7 +5684,7 @@ function validateConfig(ctx) {
5682
5684
  * const api = createApi(ctx);
5683
5685
  * await api.run({ branch: "preview/landing" });
5684
5686
  */
5685
- function createApi$1(ctx) {
5687
+ function createApi$2(ctx) {
5686
5688
  return {
5687
5689
  /**
5688
5690
  * Deploy the built outDir to Cloudflare Pages via the wrangler subprocess.
@@ -5773,7 +5775,7 @@ function createApi$1(ctx) {
5773
5775
  * createPlugin("deploy", { config: defaultConfig });
5774
5776
  * ```
5775
5777
  */
5776
- const defaultConfig = {
5778
+ const defaultConfig$1 = {
5777
5779
  target: "cloudflare-pages",
5778
5780
  outDir: "dist",
5779
5781
  productionBranch: "main",
@@ -5826,7 +5828,7 @@ const defaultSpawn = (cmd, options) => {
5826
5828
  * @example
5827
5829
  * const state = createState({ global: {}, config });
5828
5830
  */
5829
- function createState$1(_ctx) {
5831
+ function createState$2(_ctx) {
5830
5832
  return {
5831
5833
  lastDeployment: null,
5832
5834
  spawn: defaultSpawn
@@ -5860,11 +5862,1172 @@ function createState$1(_ctx) {
5860
5862
  * ```
5861
5863
  */
5862
5864
  const deployPlugin = createPlugin$1("deploy", {
5863
- config: defaultConfig,
5865
+ config: defaultConfig$1,
5864
5866
  depends: [sitePlugin],
5865
- createState: createState$1,
5867
+ createState: createState$2,
5866
5868
  events: deployEvents,
5867
- onInit: validateConfig,
5869
+ onInit: validateConfig$1,
5870
+ api: createApi$2
5871
+ });
5872
+ //#endregion
5873
+ //#region src/plugins/cli/errors.ts
5874
+ /** Error prefix for cli config/validation/runtime failures (spec/11 Part-3). */
5875
+ const ERROR_PREFIX$3 = "[web] cli";
5876
+ /**
5877
+ * Construct a cli `Error` carrying a taxonomy `code` property. Centralizes the
5878
+ * `Object.assign(new Error(message), { code })` pattern so the `code` is always
5879
+ * preserved on the thrown value (the message is expected to already be prefixed).
5880
+ *
5881
+ * @param code - The cli error `code` from the taxonomy.
5882
+ * @param message - The actionable error message.
5883
+ * @returns An `Error` whose `code` property is set.
5884
+ * @example
5885
+ * throw cliError("ERR_CLI_CONFIG", "[web] cli: port must be 1–65535.");
5886
+ */
5887
+ function cliError(code, message) {
5888
+ return Object.assign(new Error(message), { code });
5889
+ }
5890
+ /** The live-reload client snippet injected before `</body>` in served HTML. */
5891
+ const RELOAD_CLIENT = `<script>(()=>{try{const s=new EventSource("/__moku_reload");s.addEventListener("reload",()=>location.reload());}catch{}})();<\/script>`;
5892
+ /**
5893
+ * Inject the live-reload SSE client immediately before the closing `</body>` (or
5894
+ * append it when there is no `</body>`). Pure — unit-testable without a server.
5895
+ *
5896
+ * @param html - The page HTML to augment.
5897
+ * @returns The HTML with the reload client injected.
5898
+ * @example
5899
+ * injectReloadClient("<body>hi</body>"); // "<body>hi<script>…<\/script></body>"
5900
+ */
5901
+ function injectReloadClient(html) {
5902
+ const index = html.lastIndexOf("</body>");
5903
+ return index === -1 ? html + RELOAD_CLIENT : html.slice(0, index) + RELOAD_CLIENT + html.slice(index);
5904
+ }
5905
+ /**
5906
+ * Run one rebuild and report the result. Skips re-entrancy via the shared `building`
5907
+ * flag and routes success to `onReloaded`, failure to `onError`.
5908
+ *
5909
+ * @param input - The rebuild dependencies + the changed file.
5910
+ * @param input.runBuild - Runs one build and resolves with its summary.
5911
+ * @param input.onReloaded - Called with the changed file + summary after a rebuild.
5912
+ * @param input.onError - Called when a rebuild throws.
5913
+ * @param input.file - The changed file to report alongside the summary.
5914
+ * @returns Resolves once the rebuild settles (always — errors are routed, not thrown).
5915
+ * @example
5916
+ * await runOneRebuild({ runBuild, onReloaded, onError, file: "a.md" });
5917
+ */
5918
+ async function runOneRebuild(input) {
5919
+ try {
5920
+ const summary = await input.runBuild();
5921
+ input.onReloaded({
5922
+ file: input.file,
5923
+ pageCount: summary.pageCount,
5924
+ durationMs: summary.durationMs
5925
+ });
5926
+ } catch (error) {
5927
+ input.onError(error);
5928
+ }
5929
+ }
5930
+ /**
5931
+ * Create a {@link Rebuilder}. The latest changed file within the debounce window
5932
+ * wins; while a rebuild is in flight further notifications are dropped (the watcher
5933
+ * keeps firing, but only one build runs at a time, matching the blog `dev.ts`).
5934
+ *
5935
+ * @param input - The rebuild dependencies.
5936
+ * @param input.debounceMs - Debounce window in milliseconds.
5937
+ * @param input.runBuild - Runs one build and resolves with its summary.
5938
+ * @param input.onReloaded - Called with the changed file + summary after a rebuild.
5939
+ * @param input.onError - Called when a rebuild throws.
5940
+ * @returns The debounced rebuild driver.
5941
+ * @example
5942
+ * createRebuilder({ debounceMs: 150, runBuild, onReloaded, onError });
5943
+ */
5944
+ function createRebuilder(input) {
5945
+ let timer;
5946
+ let pendingFile = "";
5947
+ let building = false;
5948
+ /**
5949
+ * Run the queued rebuild once (skips when one is already in flight), resetting the
5950
+ * in-flight flag when it settles.
5951
+ *
5952
+ * @returns Resolves once the rebuild settles (errors are routed, never thrown).
5953
+ * @example
5954
+ * await fire();
5955
+ */
5956
+ const fire = async () => {
5957
+ timer = void 0;
5958
+ if (building) return;
5959
+ building = true;
5960
+ await runOneRebuild({
5961
+ runBuild: input.runBuild,
5962
+ onReloaded: input.onReloaded,
5963
+ onError: input.onError,
5964
+ file: pendingFile
5965
+ });
5966
+ building = false;
5967
+ };
5968
+ return {
5969
+ /**
5970
+ * Queue a rebuild for the changed file (debounced + coalesced).
5971
+ *
5972
+ * @param file - The changed path that triggered the rebuild.
5973
+ * @example
5974
+ * rebuilder.schedule("a.md");
5975
+ */
5976
+ schedule(file) {
5977
+ pendingFile = file;
5978
+ if (timer) clearTimeout(timer);
5979
+ timer = setTimeout(fire, input.debounceMs);
5980
+ },
5981
+ /**
5982
+ * Cancel any pending (not-yet-fired) rebuild timer.
5983
+ *
5984
+ * @example
5985
+ * rebuilder.cancel();
5986
+ */
5987
+ cancel() {
5988
+ if (timer) clearTimeout(timer);
5989
+ timer = void 0;
5990
+ }
5991
+ };
5992
+ }
5993
+ /**
5994
+ * Install SIGINT/SIGTERM handlers that run `teardown()` and resolve the returned
5995
+ * promise, so a long-running command (`serve`/`preview`) unblocks its `await` on
5996
+ * Ctrl-C / termination and detaches its own listeners. Used by both servers.
5997
+ *
5998
+ * @param teardown - Cleanup to run on the first termination signal.
5999
+ * @returns A promise that resolves once a termination signal has been handled.
6000
+ * @example
6001
+ * await installSignalTeardown(() => server.stop());
6002
+ */
6003
+ function installSignalTeardown(teardown) {
6004
+ return new Promise((resolve) => {
6005
+ /**
6006
+ * Detach both signal listeners, run teardown, and resolve the wait (once).
6007
+ *
6008
+ * @example
6009
+ * onSignal();
6010
+ */
6011
+ const onSignal = () => {
6012
+ process.off("SIGINT", onSignal);
6013
+ process.off("SIGTERM", onSignal);
6014
+ teardown();
6015
+ resolve();
6016
+ };
6017
+ process.on("SIGINT", onSignal);
6018
+ process.on("SIGTERM", onSignal);
6019
+ });
6020
+ }
6021
+ /** The SSE comment line sent on connect to open the stream. */
6022
+ const SSE_OPEN = ": connected\n\n";
6023
+ /** The SSE frame pushed to reload a connected browser. */
6024
+ const SSE_RELOAD = "event: reload\ndata: 1\n\n";
6025
+ /**
6026
+ * Create a {@link ReloadHub} backed by `ReadableStream` controllers. Each `connect()`
6027
+ * enqueues into a new stream; `reloadAll()` writes the reload frame to every live
6028
+ * controller (dropping any that have closed).
6029
+ *
6030
+ * @returns The reload hub.
6031
+ * @example
6032
+ * const hub = createReloadHub();
6033
+ */
6034
+ function createReloadHub() {
6035
+ const encoder = new TextEncoder();
6036
+ const clients = /* @__PURE__ */ new Set();
6037
+ return {
6038
+ /**
6039
+ * Open one SSE connection, register its controller, and return the streaming
6040
+ * `Response` (a connect comment is sent immediately to open the stream).
6041
+ *
6042
+ * @returns A `text/event-stream` response wired to this hub.
6043
+ * @example
6044
+ * return hub.connect();
6045
+ */
6046
+ connect() {
6047
+ let owned;
6048
+ const stream = new ReadableStream({
6049
+ /**
6050
+ * Register the stream's controller and send the opening comment.
6051
+ *
6052
+ * @param controller - The new stream's controller.
6053
+ * @example
6054
+ * start(controller);
6055
+ */
6056
+ start(controller) {
6057
+ owned = controller;
6058
+ clients.add(controller);
6059
+ controller.enqueue(encoder.encode(SSE_OPEN));
6060
+ },
6061
+ /**
6062
+ * Drop this client's controller when the browser disconnects.
6063
+ *
6064
+ * @example
6065
+ * cancel();
6066
+ */
6067
+ cancel() {
6068
+ if (owned) clients.delete(owned);
6069
+ }
6070
+ });
6071
+ return new Response(stream, { headers: {
6072
+ "content-type": "text/event-stream",
6073
+ "cache-control": "no-cache",
6074
+ connection: "keep-alive"
6075
+ } });
6076
+ },
6077
+ /**
6078
+ * Push a `reload` frame to every connected client, dropping any closed ones.
6079
+ *
6080
+ * @example
6081
+ * hub.reloadAll();
6082
+ */
6083
+ reloadAll() {
6084
+ for (const controller of clients) try {
6085
+ controller.enqueue(encoder.encode(SSE_RELOAD));
6086
+ } catch {
6087
+ clients.delete(controller);
6088
+ }
6089
+ },
6090
+ /**
6091
+ * The number of currently-connected clients.
6092
+ *
6093
+ * @returns The live client count.
6094
+ * @example
6095
+ * hub.size();
6096
+ */
6097
+ size() {
6098
+ return clients.size;
6099
+ }
6100
+ };
6101
+ }
6102
+ /**
6103
+ * Build the live-reload-aware request handler for the dev server: serves the SSE
6104
+ * stream at {@link RELOAD_PATH}, injects the reload client into HTML responses (when
6105
+ * `liveReload`), and falls through to the {@link resolveCleanUrl} static resolver for
6106
+ * everything else.
6107
+ *
6108
+ * @param ctx - The cli plugin context (provides `config` + `state.fileResponse`).
6109
+ * @param hub - The reload hub the SSE endpoint connects to.
6110
+ * @returns The `fetch` handler passed to the static server.
6111
+ * @example
6112
+ * const handler = createDevHandler(ctx, hub);
6113
+ */
6114
+ function createDevHandler(ctx, hub) {
6115
+ return async (request) => {
6116
+ const pathname = decodeURIComponent(new URL(request.url).pathname);
6117
+ if (pathname === "/__moku_reload") return hub.connect();
6118
+ const resolved = resolveCleanUrl(ctx.config.outDir, pathname);
6119
+ if (resolved.file === null) return new Response("Not Found", { status: 404 });
6120
+ const response = ctx.state.fileResponse(resolved.file, resolved.status);
6121
+ if (ctx.config.liveReload && resolved.file.endsWith(".html")) {
6122
+ const html = injectReloadClient(await response.text());
6123
+ return new Response(html, {
6124
+ status: resolved.status,
6125
+ headers: { "content-type": "text/html; charset=utf-8" }
6126
+ });
6127
+ }
6128
+ return response;
6129
+ };
6130
+ }
6131
+ /**
6132
+ * Run the dev loop: an initial build, an in-process static server that injects the
6133
+ * live-reload client, a recursive watcher over `config.watchDirs`, and a debounced
6134
+ * rebuild that re-renders and pushes a browser reload. Resolves on SIGINT/SIGTERM,
6135
+ * which stops the server, closes the watchers, and cancels any pending rebuild.
6136
+ *
6137
+ * @param ctx - The cli plugin context (config, state seams, `require`).
6138
+ * @param port - The port to bind the dev server to.
6139
+ * @returns Resolves once the server has been torn down by a termination signal.
6140
+ * @example
6141
+ * await runDevServer(ctx, 4173);
6142
+ */
6143
+ async function runDevServer(ctx, port) {
6144
+ await ctx.require(buildPlugin).run();
6145
+ const hub = createReloadHub();
6146
+ const server = ctx.state.serveStatic({
6147
+ port,
6148
+ fetch: createDevHandler(ctx, hub)
6149
+ });
6150
+ const rebuilder = createRebuilder({
6151
+ debounceMs: ctx.config.debounceMs,
6152
+ /**
6153
+ * Re-run the SSG build for a rebuild.
6154
+ *
6155
+ * @returns The rebuild summary.
6156
+ * @example
6157
+ * await runBuild();
6158
+ */
6159
+ runBuild() {
6160
+ return ctx.require(buildPlugin).run();
6161
+ },
6162
+ /**
6163
+ * Render the reload line and push a browser reload after a rebuild.
6164
+ *
6165
+ * @param info - The changed file plus the rebuild's page count and duration.
6166
+ * @example
6167
+ * onReloaded({ file: "a.md", pageCount: 1, durationMs: 10 });
6168
+ */
6169
+ onReloaded(info) {
6170
+ ctx.state.render.reload(info);
6171
+ hub.reloadAll();
6172
+ },
6173
+ /**
6174
+ * Render a rebuild failure (the dev loop keeps running).
6175
+ *
6176
+ * @param error - The thrown rebuild error.
6177
+ * @example
6178
+ * onError(new Error("boom"));
6179
+ */
6180
+ onError(error) {
6181
+ ctx.state.render.error("rebuild failed", error);
6182
+ }
6183
+ });
6184
+ const watchers = ctx.config.watchDirs.map((dir) => ctx.state.watch(dir, () => rebuilder.schedule(dir)));
6185
+ ctx.state.render.serverReady({
6186
+ local: `http://localhost:${port}`,
6187
+ network: ctx.state.networkUrl(port),
6188
+ watching: ctx.config.watchDirs
6189
+ });
6190
+ return installSignalTeardown(() => {
6191
+ rebuilder.cancel();
6192
+ for (const watcher of watchers) watcher.close();
6193
+ server.stop();
6194
+ });
6195
+ }
6196
+ //#endregion
6197
+ //#region src/plugins/cli/preview.ts
6198
+ /**
6199
+ * @file cli plugin — static preview server for the built `dist/`. Exposes a PURE,
6200
+ * server-agnostic clean-URL resolver (`resolveCleanUrl`) — unit-tested without a
6201
+ * socket — plus `runPreviewServer`, which serves the resolved files via `Bun.serve`
6202
+ * the way Cloudflare Pages does (trailing slash → `index.html`, extensionless →
6203
+ * `<path>/index.html`, a miss → the nearest `404.html`). No reload injection.
6204
+ */
6205
+ /**
6206
+ * Strip leading `../` segments (after `normalize`) so a request can never escape the
6207
+ * served root via path traversal.
6208
+ *
6209
+ * @param pathname - The decoded request pathname.
6210
+ * @returns The traversal-safe relative path.
6211
+ * @example
6212
+ * safePath("../../etc/passwd"); // "etc/passwd"
6213
+ */
6214
+ function safePath(pathname) {
6215
+ return path.normalize(pathname).replace(/^(\.\.(?:[/\\]|$))+/, "");
6216
+ }
6217
+ /**
6218
+ * The default {@link FileProbe} backed by `node:fs.statSync` — a single stat (no
6219
+ * exists+stat race) that swallows the ENOENT thrown for a missing path.
6220
+ *
6221
+ * @param filePath - Candidate on-disk path.
6222
+ * @returns Whether it resolves to a regular file.
6223
+ * @example
6224
+ * statIsFile("/dist/index.html");
6225
+ */
6226
+ function statIsFile(filePath) {
6227
+ try {
6228
+ return statSync(filePath).isFile();
6229
+ } catch {
6230
+ return false;
6231
+ }
6232
+ }
6233
+ /**
6234
+ * Resolve a request pathname to a real file under `rootDir`, mirroring Cloudflare
6235
+ * Pages clean URLs. Pure and server-agnostic: it touches the filesystem only through
6236
+ * the injected {@link FileProbe}, so it is unit-tested without a server. A trailing
6237
+ * slash maps to `index.html`; an extensionless path tries the file then
6238
+ * `<path>/index.html`; a miss climbs toward the root for the nearest `404.html`
6239
+ * (served with status `404`).
6240
+ *
6241
+ * @param rootDir - The absolute (or cwd-relative) build output directory.
6242
+ * @param pathname - The decoded request pathname (always starts with `/`).
6243
+ * @param isFile - File-existence probe (defaults to {@link statIsFile}).
6244
+ * @returns The resolved file + status (file `null` when not even a `404.html` exists).
6245
+ * @example
6246
+ * resolveCleanUrl("dist", "/about/", path => set.has(path));
6247
+ */
6248
+ function resolveCleanUrl(rootDir, pathname, isFile = statIsFile) {
6249
+ const relative = safePath(pathname);
6250
+ const base = path.join(rootDir, relative);
6251
+ const candidates = pathname.endsWith("/") ? [path.join(base, "index.html")] : [base, path.join(base, "index.html")];
6252
+ for (const candidate of candidates) if (isFile(candidate)) return {
6253
+ file: candidate,
6254
+ status: 200
6255
+ };
6256
+ const segments = path.join(rootDir, relative).split(path.sep).filter(Boolean);
6257
+ for (let depth = segments.length; depth >= 1; depth--) {
6258
+ const candidate = path.join(segments.slice(0, depth).join(path.sep), "404.html");
6259
+ if (isFile(candidate)) return {
6260
+ file: candidate,
6261
+ status: 404
6262
+ };
6263
+ }
6264
+ const root = path.join(rootDir, "404.html");
6265
+ return isFile(root) ? {
6266
+ file: root,
6267
+ status: 404
6268
+ } : {
6269
+ file: null,
6270
+ status: 404
6271
+ };
6272
+ }
6273
+ /**
6274
+ * Build the request handler for the preview server: resolves each request via
6275
+ * {@link resolveCleanUrl} and serves the file (no reload injection, mirroring prod).
6276
+ *
6277
+ * @param ctx - The cli plugin context (provides `config` + `state.fileResponse`).
6278
+ * @returns The `fetch` handler passed to the static server.
6279
+ * @example
6280
+ * const handler = createPreviewHandler(ctx);
6281
+ */
6282
+ function createPreviewHandler(ctx) {
6283
+ return (request) => {
6284
+ const pathname = decodeURIComponent(new URL(request.url).pathname);
6285
+ const resolved = resolveCleanUrl(ctx.config.outDir, pathname);
6286
+ if (resolved.file === null) return new Response("Not Found", { status: 404 });
6287
+ return ctx.state.fileResponse(resolved.file, resolved.status);
6288
+ };
6289
+ }
6290
+ /**
6291
+ * Run the static preview server for the built `dist/`. Serves files resolved by
6292
+ * {@link resolveCleanUrl} via the injectable static-server seam — with no live-reload
6293
+ * injection, mirroring production. Renders the server-ready panel and resolves on
6294
+ * SIGINT/SIGTERM.
6295
+ *
6296
+ * @param ctx - The cli plugin context (provides `config` + `state` seams).
6297
+ * @param port - The port to bind to.
6298
+ * @returns Resolves once the server has been torn down by a termination signal.
6299
+ * @example
6300
+ * await runPreviewServer(ctx, 4173);
6301
+ */
6302
+ function runPreviewServer(ctx, port) {
6303
+ const server = ctx.state.serveStatic({
6304
+ port,
6305
+ fetch: createPreviewHandler(ctx)
6306
+ });
6307
+ ctx.state.render.serverReady({
6308
+ local: `http://localhost:${port}`,
6309
+ network: ctx.state.networkUrl(port)
6310
+ });
6311
+ return installSignalTeardown(() => server.stop());
6312
+ }
6313
+ //#endregion
6314
+ //#region src/plugins/cli/api.ts
6315
+ /**
6316
+ * @file cli plugin — API factory (build · serve · preview · deploy), the cli plugin
6317
+ * context type, and config-only `validateConfig`. The four closures are wiring-thin:
6318
+ * each renders the Panel header, then delegates to `build`/`deploy` (via `require`)
6319
+ * or to the server modules. Live build/deploy progress arrives through hooks (in
6320
+ * `index.ts`), so the methods' return values come from the awaited `run()` results.
6321
+ */
6322
+ /** Lowest valid TCP port. */
6323
+ const MIN_PORT = 1;
6324
+ /** Highest valid TCP port. */
6325
+ const MAX_PORT = 65535;
6326
+ /**
6327
+ * Validate the resolved cli config during `onInit` (config-only — no resource
6328
+ * allocation, per spec/06 §2). Throws `ERR_CLI_CONFIG` (`[web] cli: …`) when `port`
6329
+ * is not an integer in 1–65535, `outDir`/`notFoundFile` are not non-empty strings,
6330
+ * `watchDirs` is not a non-empty string array, or `debounceMs` is negative.
6331
+ *
6332
+ * @param config - The resolved cli configuration to validate.
6333
+ * @throws {Error} `ERR_CLI_CONFIG` when any field is invalid.
6334
+ * @example
6335
+ * validateConfig({ outDir: "dist", port: 4173, watchDirs: ["content"], debounceMs: 150, notFoundFile: "404.html", liveReload: true });
6336
+ */
6337
+ function validateConfig(config) {
6338
+ if (!Number.isInteger(config.port) || config.port < MIN_PORT || config.port > MAX_PORT) throw cliError("ERR_CLI_CONFIG", `${ERROR_PREFIX$3}: port must be an integer in ${MIN_PORT}–${MAX_PORT}.\n Set pluginConfigs.cli.port to a valid TCP port (e.g. 4173).`);
6339
+ if (typeof config.outDir !== "string" || config.outDir.length === 0) throw cliError("ERR_CLI_CONFIG", `${ERROR_PREFIX$3}: outDir must be a non-empty string.\n Set pluginConfigs.cli.outDir to your build output directory (e.g. "dist").`);
6340
+ if (typeof config.notFoundFile !== "string" || config.notFoundFile.length === 0) throw cliError("ERR_CLI_CONFIG", `${ERROR_PREFIX$3}: notFoundFile must be a non-empty string.\n Set pluginConfigs.cli.notFoundFile to the not-found page filename (e.g. "404.html").`);
6341
+ if (!Array.isArray(config.watchDirs) || config.watchDirs.length === 0 || !config.watchDirs.every((dir) => typeof dir === "string" && dir.length > 0)) throw cliError("ERR_CLI_CONFIG", `${ERROR_PREFIX$3}: watchDirs must be a non-empty array of non-empty strings.\n Set pluginConfigs.cli.watchDirs to the directories serve() should watch (e.g. ["content", "src"]).`);
6342
+ if (typeof config.debounceMs !== "number" || config.debounceMs < 0) throw cliError("ERR_CLI_CONFIG", `${ERROR_PREFIX$3}: debounceMs must be a number >= 0.\n Set pluginConfigs.cli.debounceMs to the rebuild debounce window in milliseconds (e.g. 150).`);
6343
+ }
6344
+ /**
6345
+ * Create the cli plugin API surface — exactly `build`, `serve`, `preview`, `deploy`.
6346
+ * Each method renders `state.render.header(<command>)` first, then does its work;
6347
+ * live progress is rendered by the hooks wired in `index.ts`, so each method's
6348
+ * return value comes from the awaited `build.run()` / `deploy.run()` result.
6349
+ *
6350
+ * @param ctx - Plugin context (provides `require`, `state`, `config`).
6351
+ * @returns The {@link Api} surface mounted at `app.cli`.
6352
+ * @example
6353
+ * const api = createApi(ctx);
6354
+ * await api.build();
6355
+ */
6356
+ function createApi$1(ctx) {
6357
+ return {
6358
+ /**
6359
+ * Run the SSG build and (by default) assert the not-found page exists.
6360
+ *
6361
+ * @param options - Optional `assertNotFound` toggle (default `true`).
6362
+ * @returns The build summary (`outDir`, `pageCount`, `durationMs`).
6363
+ * @throws {Error} `ERR_CLI_NOT_FOUND` when the not-found page is missing and asserted.
6364
+ * @example
6365
+ * await api.build();
6366
+ */
6367
+ async build(options = {}) {
6368
+ const { assertNotFound = true } = options;
6369
+ ctx.state.render.header("build");
6370
+ const result = await ctx.require(buildPlugin).run();
6371
+ const page = path.join(ctx.config.outDir, ctx.config.notFoundFile);
6372
+ if (assertNotFound && !existsSync(page)) {
6373
+ ctx.state.render.error(`${page} missing — set build.notFound (CF Pages would flip to SPA mode)`);
6374
+ throw cliError("ERR_CLI_NOT_FOUND", `${ERROR_PREFIX$3}: ${page} missing after build.\n Set build.notFound so the SSG emits it (CF Pages flips to SPA mode without a top-level 404), or pass { assertNotFound: false } to skip this check.`);
6375
+ }
6376
+ return result;
6377
+ },
6378
+ /**
6379
+ * Dev loop: build once, serve `dist/` in-process (live-reload injected), watch
6380
+ * `watchDirs`, debounced rebuild + reload. Resolves on SIGINT/SIGTERM.
6381
+ *
6382
+ * @param options - Optional port override (defaults to `config.port`).
6383
+ * @returns Resolves once the server has been torn down.
6384
+ * @example
6385
+ * await api.serve({ port: 3000 });
6386
+ */
6387
+ serve(options = {}) {
6388
+ const { port = ctx.config.port } = options;
6389
+ ctx.state.render.header("serve");
6390
+ return runDevServer(ctx, port);
6391
+ },
6392
+ /**
6393
+ * Static preview of the built `dist/` with CF-Pages clean-URL resolution.
6394
+ *
6395
+ * @param options - Optional port override (defaults to `config.port`).
6396
+ * @returns Resolves once the server has been torn down.
6397
+ * @example
6398
+ * await api.preview();
6399
+ */
6400
+ preview(options = {}) {
6401
+ const { port = ctx.config.port } = options;
6402
+ ctx.state.render.header("preview");
6403
+ return runPreviewServer(ctx, port);
6404
+ },
6405
+ /**
6406
+ * Scaffold, then deploy. A y/N confirm is shown only when a human is present (an
6407
+ * interactive TTY, with `CI` unset). Non-interactive runs (CI, or any non-TTY)
6408
+ * skip the prompt and deploy, so the consumer scripts never hang a pipeline.
6409
+ * `options.yes` forces the skip anywhere. An interactive "no" returns
6410
+ * `{ deployed: false, reason: "declined" }`.
6411
+ *
6412
+ * @param options - Optional branch override and `yes` flag.
6413
+ * @returns The deploy outcome (completed details, or `declined` if a TTY user says no).
6414
+ * @example
6415
+ * await api.deploy({ branch: "preview/x", yes: true });
6416
+ */
6417
+ async deploy(options = {}) {
6418
+ const { branch, yes = false } = options;
6419
+ ctx.state.render.header("deploy");
6420
+ await ctx.require(deployPlugin).init({ ci: true });
6421
+ if (process.stdout.isTTY === true && process.env.CI === void 0 && !yes) {
6422
+ if (!await ctx.state.confirm(`Deploy ${ctx.config.outDir}/ to cloudflare-pages?`)) {
6423
+ ctx.state.render.warn("deploy skipped");
6424
+ return {
6425
+ deployed: false,
6426
+ reason: "declined"
6427
+ };
6428
+ }
6429
+ } else if (!yes) ctx.state.render.info("non-interactive — skipping deploy confirmation");
6430
+ return {
6431
+ deployed: true,
6432
+ ...await ctx.require(deployPlugin).run(branch === void 0 ? {} : { branch })
6433
+ };
6434
+ }
6435
+ };
6436
+ }
6437
+ //#endregion
6438
+ //#region src/plugins/cli/defaults.ts
6439
+ /**
6440
+ * Default cli configuration. Consumers override individual fields via
6441
+ * `pluginConfigs.cli`. Declared as a typed const (no inline `as` assertion).
6442
+ *
6443
+ * @example
6444
+ * ```ts
6445
+ * createPlugin("cli", { config: defaultConfig });
6446
+ * ```
6447
+ */
6448
+ const defaultConfig = {
6449
+ outDir: "dist",
6450
+ port: 4173,
6451
+ watchDirs: ["content", "src"],
6452
+ debounceMs: 150,
6453
+ notFoundFile: "404.html",
6454
+ liveReload: true
6455
+ };
6456
+ //#endregion
6457
+ //#region src/plugins/cli/network.ts
6458
+ /**
6459
+ * @file cli plugin — LAN network-URL derivation. Picks the first non-internal IPv4
6460
+ * from `node:os` `networkInterfaces()` to render the "Network" URL in the
6461
+ * server-ready panel. The interface source is injectable so it is unit-testable.
6462
+ */
6463
+ /**
6464
+ * Whether an interface entry is a usable, non-internal IPv4 address.
6465
+ *
6466
+ * @param entry - One interface address entry.
6467
+ * @returns `true` when it is an external IPv4 address.
6468
+ * @example
6469
+ * isExternalIPv4({ address: "10.0.0.2", family: "IPv4", internal: false }); // true
6470
+ */
6471
+ function isExternalIPv4(entry) {
6472
+ return !entry.internal && (entry.family === "IPv4" || entry.family === 4);
6473
+ }
6474
+ /**
6475
+ * Pick the first non-internal IPv4 address from the interface source, or `null` when
6476
+ * none exists (offline / loopback-only).
6477
+ *
6478
+ * @param source - Interface source (defaults to `node:os` `networkInterfaces`).
6479
+ * @returns The first external IPv4 address string, or `null`.
6480
+ * @example
6481
+ * const ip = lanAddress();
6482
+ */
6483
+ function lanAddress(source = networkInterfaces) {
6484
+ for (const entries of Object.values(source())) for (const entry of entries ?? []) if (isExternalIPv4(entry)) return entry.address;
6485
+ return null;
6486
+ }
6487
+ /**
6488
+ * Build the LAN URL (`http://<ip>:<port>`) for the server-ready panel, or `null`
6489
+ * when no non-internal IPv4 is available.
6490
+ *
6491
+ * @param port - The port the server is bound to.
6492
+ * @param source - Interface source (defaults to `node:os` `networkInterfaces`).
6493
+ * @returns The `http://<ip>:<port>` URL, or `null` when offline.
6494
+ * @example
6495
+ * networkUrl(4173); // "http://192.168.1.10:4173" or null
6496
+ */
6497
+ function networkUrl(port, source = networkInterfaces) {
6498
+ const ip = lanAddress(source);
6499
+ return ip === null ? null : `http://${ip}:${port}`;
6500
+ }
6501
+ //#endregion
6502
+ //#region src/plugins/cli/render/ansi.ts
6503
+ /**
6504
+ * @file cli plugin — TTY/NO_COLOR-aware ANSI color + box-drawing helpers shared by
6505
+ * the Panel renderer. Modeled on the legacy `scripts/_log.ts`: color and box glyphs
6506
+ * are emitted only on a real TTY with `NO_COLOR` unset; otherwise plain ASCII so
6507
+ * CI logs and pipes stay readable.
6508
+ */
6509
+ /** The ANSI escape byte (ESC, `0x1b`), built so no literal control char is in source. */
6510
+ const ESC = String.fromCodePoint(27);
6511
+ /** ANSI SGR codes used by the Panel renderer (each prefixed with the ESC byte). */
6512
+ const ANSI = {
6513
+ reset: `${ESC}[0m`,
6514
+ bold: `${ESC}[1m`,
6515
+ dim: `${ESC}[2m`,
6516
+ red: `${ESC}[31m`,
6517
+ green: `${ESC}[32m`,
6518
+ yellow: `${ESC}[33m`,
6519
+ blue: `${ESC}[34m`,
6520
+ magenta: `${ESC}[35m`,
6521
+ cyan: `${ESC}[36m`,
6522
+ gray: `${ESC}[90m`
6523
+ };
6524
+ /** Unicode rounded box glyphs used when output is a color-capable TTY. */
6525
+ const UNICODE_BOX = {
6526
+ topLeft: "╭",
6527
+ topRight: "╮",
6528
+ bottomLeft: "╰",
6529
+ bottomRight: "╯",
6530
+ horizontal: "─",
6531
+ vertical: "│"
6532
+ };
6533
+ /** ASCII box glyphs used when output is piped/CI (plain mode). */
6534
+ const ASCII_BOX = {
6535
+ topLeft: "+",
6536
+ topRight: "+",
6537
+ bottomLeft: "+",
6538
+ bottomRight: "+",
6539
+ horizontal: "-",
6540
+ vertical: "|"
6541
+ };
6542
+ /**
6543
+ * Matches every ANSI SGR escape sequence (used to measure visible width). Built from
6544
+ * the {@link ESC} byte so no literal control character appears in the source regex.
6545
+ */
6546
+ const ANSI_PATTERN = new RegExp(String.raw`${ESC}\[[0-9;]*m`, "g");
6547
+ /**
6548
+ * Whether ANSI color/box glyphs should be emitted: a TTY stream with `NO_COLOR`
6549
+ * unset. Reads `process.stdout.isTTY` and `process.env.NO_COLOR` by default so the
6550
+ * renderer auto-degrades in CI and pipes, exactly like the legacy logger.
6551
+ *
6552
+ * @param stream - Stream to probe for `isTTY` (defaults to `process.stdout`).
6553
+ * @param noColor - The `NO_COLOR` value (defaults to `process.env.NO_COLOR`).
6554
+ * @returns `true` when color should be used.
6555
+ * @example
6556
+ * supportsColor(); // true in an interactive terminal
6557
+ */
6558
+ function supportsColor(stream = process.stdout, noColor = process.env.NO_COLOR) {
6559
+ return stream.isTTY === true && noColor === void 0;
6560
+ }
6561
+ /**
6562
+ * Select the box glyph set for the given color mode (Unicode on a TTY, ASCII off it).
6563
+ *
6564
+ * @param color - Whether color/Unicode output is enabled.
6565
+ * @returns The matching {@link BoxGlyphs} set.
6566
+ * @example
6567
+ * const glyphs = boxGlyphs(supportsColor());
6568
+ */
6569
+ function boxGlyphs(color) {
6570
+ return color ? UNICODE_BOX : ASCII_BOX;
6571
+ }
6572
+ /**
6573
+ * The visible width of a string, ignoring any ANSI escape sequences it contains.
6574
+ *
6575
+ * @param text - The (possibly colorized) text to measure.
6576
+ * @returns The number of visible characters.
6577
+ * @example
6578
+ * visibleWidth(`${ANSI.red}hi${ANSI.reset}`); // 2
6579
+ */
6580
+ function visibleWidth(text) {
6581
+ return text.replaceAll(ANSI_PATTERN, "").length;
6582
+ }
6583
+ /**
6584
+ * Build a {@link Palette} bound to a fixed color mode. When `color` is `false` every
6585
+ * helper returns its input unchanged, so the same render code path produces plain
6586
+ * output in CI/pipes.
6587
+ *
6588
+ * @param color - Whether color is enabled (typically `supportsColor()`).
6589
+ * @returns The bound color palette.
6590
+ * @example
6591
+ * const palette = makePalette(supportsColor());
6592
+ * const line = palette.green("done");
6593
+ */
6594
+ function makePalette(color) {
6595
+ return {
6596
+ enabled: color,
6597
+ /**
6598
+ * Wrap text in the given ANSI code (returns it unchanged when color is off).
6599
+ *
6600
+ * @param code - The ANSI SGR code to apply.
6601
+ * @param text - The text to colorize.
6602
+ * @returns The colorized (or unchanged) text.
6603
+ * @example
6604
+ * palette.paint(ANSI.green, "ok");
6605
+ */
6606
+ paint(code, text) {
6607
+ return color ? `${code}${text}${ANSI.reset}` : text;
6608
+ },
6609
+ /**
6610
+ * Bold the given text (no-op in plain mode).
6611
+ *
6612
+ * @param text - The text to embolden.
6613
+ * @returns The bold (or unchanged) text.
6614
+ * @example
6615
+ * palette.bold("title");
6616
+ */
6617
+ bold(text) {
6618
+ return this.paint(ANSI.bold, text);
6619
+ },
6620
+ /**
6621
+ * Dim the given text (no-op in plain mode).
6622
+ *
6623
+ * @param text - The text to dim.
6624
+ * @returns The dim (or unchanged) text.
6625
+ * @example
6626
+ * palette.dim("· 84ms");
6627
+ */
6628
+ dim(text) {
6629
+ return this.paint(ANSI.dim, text);
6630
+ },
6631
+ /**
6632
+ * Color the given text green (no-op in plain mode).
6633
+ *
6634
+ * @param text - The text to colorize.
6635
+ * @returns The green (or unchanged) text.
6636
+ * @example
6637
+ * palette.green("✓");
6638
+ */
6639
+ green(text) {
6640
+ return this.paint(ANSI.green, text);
6641
+ },
6642
+ /**
6643
+ * Color the given text yellow (no-op in plain mode).
6644
+ *
6645
+ * @param text - The text to colorize.
6646
+ * @returns The yellow (or unchanged) text.
6647
+ * @example
6648
+ * palette.yellow("~");
6649
+ */
6650
+ yellow(text) {
6651
+ return this.paint(ANSI.yellow, text);
6652
+ },
6653
+ /**
6654
+ * Color the given text red (no-op in plain mode).
6655
+ *
6656
+ * @param text - The text to colorize.
6657
+ * @returns The red (or unchanged) text.
6658
+ * @example
6659
+ * palette.red("✗");
6660
+ */
6661
+ red(text) {
6662
+ return this.paint(ANSI.red, text);
6663
+ },
6664
+ /**
6665
+ * Color the given text cyan (no-op in plain mode).
6666
+ *
6667
+ * @param text - The text to colorize.
6668
+ * @returns The cyan (or unchanged) text.
6669
+ * @example
6670
+ * palette.cyan("http://localhost:4173");
6671
+ */
6672
+ cyan(text) {
6673
+ return this.paint(ANSI.cyan, text);
6674
+ }
6675
+ };
6676
+ }
6677
+ /**
6678
+ * Frame a list of already-rendered content lines in a box, padding each line to the
6679
+ * width of the widest visible line. Uses Unicode borders when `color` is enabled and
6680
+ * ASCII otherwise. Visible width ignores embedded ANSI so colored lines align.
6681
+ *
6682
+ * @param lines - The content lines (may contain ANSI color codes).
6683
+ * @param color - Whether to use Unicode borders (and assume color-capable output).
6684
+ * @returns The boxed lines (top border, content rows, bottom border).
6685
+ * @example
6686
+ * box(["Local: http://localhost:4173"], true);
6687
+ */
6688
+ function box(lines, color) {
6689
+ const glyphs = boxGlyphs(color);
6690
+ const inner = Math.max(0, ...lines.map((line) => visibleWidth(line)));
6691
+ const horizontal = glyphs.horizontal.repeat(inner + 2);
6692
+ const top = `${glyphs.topLeft}${horizontal}${glyphs.topRight}`;
6693
+ const bottom = `${glyphs.bottomLeft}${horizontal}${glyphs.bottomRight}`;
6694
+ return [
6695
+ top,
6696
+ ...lines.map((line) => {
6697
+ const pad = " ".repeat(inner - visibleWidth(line));
6698
+ return `${glyphs.vertical} ${line}${pad} ${glyphs.vertical}`;
6699
+ }),
6700
+ bottom
6701
+ ];
6702
+ }
6703
+ //#endregion
6704
+ //#region src/plugins/cli/render/panel.ts
6705
+ /** Per-command label shown in the header badge beside the logo. */
6706
+ const COMMAND_LABEL = {
6707
+ build: "build",
6708
+ serve: "serve · dev",
6709
+ preview: "preview",
6710
+ deploy: "deploy"
6711
+ };
6712
+ /**
6713
+ * Render one human-readable duration suffix (e.g. `· 84ms`).
6714
+ *
6715
+ * @param palette - The active color palette.
6716
+ * @param durationMs - Duration in milliseconds (omitted → empty string).
6717
+ * @returns The dim `· Nms` suffix, or `""` when no duration is given.
6718
+ * @example
6719
+ * durationSuffix(palette, 84); // " · 84ms" (dim)
6720
+ */
6721
+ function durationSuffix(palette, durationMs) {
6722
+ if (durationMs === void 0) return "";
6723
+ return ` ${palette.dim(`· ${durationMs}ms`)}`;
6724
+ }
6725
+ /**
6726
+ * Create the Panel {@link CliRenderer}. Output is written through the injected sink
6727
+ * (default `console.log`/`console.error`) and colorized only when color is enabled,
6728
+ * so the identical render path yields box-drawn color panels on a TTY and plain
6729
+ * ASCII lines in CI/pipes.
6730
+ *
6731
+ * @param options - Optional sinks + a color override (see {@link PanelOptions}).
6732
+ * @returns The renderer mounted on `state.render` and driven by the API + hooks.
6733
+ * @example
6734
+ * const render = createPanelRenderer();
6735
+ * render.header("build");
6736
+ */
6737
+ function createPanelRenderer(options = {}) {
6738
+ const write = options.write ?? ((line) => console.log(line));
6739
+ const writeError = options.writeError ?? ((line) => console.error(line));
6740
+ const color = options.color ?? supportsColor();
6741
+ const palette = makePalette(color);
6742
+ /**
6743
+ * Write each line of a multi-line block through the stdout sink.
6744
+ *
6745
+ * @param lines - The rendered lines to write in order.
6746
+ * @example
6747
+ * writeBlock(["a", "b"]);
6748
+ */
6749
+ const writeBlock = (lines) => {
6750
+ for (const line of lines) write(line);
6751
+ };
6752
+ return {
6753
+ /**
6754
+ * Render the boxed `MOKU WEB` logo + command label.
6755
+ *
6756
+ * @param command - The command being run, shown beside the logo.
6757
+ * @example
6758
+ * render.header("serve");
6759
+ */
6760
+ header(command) {
6761
+ writeBlock(box([`${palette.bold(palette.cyan("MOKU WEB"))} ${palette.dim(COMMAND_LABEL[command])}`], color));
6762
+ },
6763
+ /**
6764
+ * Render a live per-phase row from a `build:phase` event.
6765
+ *
6766
+ * @param phase - The `build:phase` payload.
6767
+ * @example
6768
+ * render.phase({ phase: "pages", status: "done", durationMs: 12 });
6769
+ */
6770
+ phase(phase) {
6771
+ const done = phase.status === "done";
6772
+ write(` ${done ? palette.green("✓") : palette.dim("•")} ${done ? phase.phase : palette.dim(phase.phase)}${durationSuffix(palette, phase.durationMs)}`);
6773
+ },
6774
+ /**
6775
+ * Render the BUILD summary block from a `build:complete` event.
6776
+ *
6777
+ * @param summary - The `build:complete` payload.
6778
+ * @example
6779
+ * render.built({ outDir: "dist", pageCount: 12, durationMs: 840 });
6780
+ */
6781
+ built(summary) {
6782
+ const pages = palette.bold(String(summary.pageCount));
6783
+ writeBlock(box([
6784
+ `${palette.green("✓")} ${palette.bold("BUILD")} complete`,
6785
+ `${palette.dim("pages")} ${pages}`,
6786
+ `${palette.dim("time")} ${summary.durationMs}ms`,
6787
+ `${palette.dim("out")} ${summary.outDir}/`
6788
+ ], color));
6789
+ },
6790
+ /**
6791
+ * Render the bordered server-ready panel (Local / Network URLs + watched dirs).
6792
+ *
6793
+ * @param info - Local/Network URLs and optionally the watched directories.
6794
+ * @example
6795
+ * render.serverReady({ local: "http://localhost:4173", network: null });
6796
+ */
6797
+ serverReady(info) {
6798
+ const lines = [`${palette.green("➜")} ${palette.bold("Local")} ${palette.cyan(info.local)}`, `${palette.green("➜")} ${palette.bold("Network")} ${info.network ? palette.cyan(info.network) : palette.dim("unavailable")}`];
6799
+ if (info.watching && info.watching.length > 0) lines.push(`${palette.dim("watching")} ${palette.dim(info.watching.join(", "))}`);
6800
+ writeBlock(box(lines, color));
6801
+ },
6802
+ /**
6803
+ * Render the post-rebuild line ("~ file" + "✓ rebuilt N pages · Xms · reloaded").
6804
+ *
6805
+ * @param info - The changed file plus the rebuild's page count and duration.
6806
+ * @example
6807
+ * render.reload({ file: "content/a.md", pageCount: 12, durationMs: 84 });
6808
+ */
6809
+ reload(info) {
6810
+ write(` ${palette.yellow("~")} ${info.file}`);
6811
+ write(` ${palette.green("✓")} rebuilt ${palette.bold(String(info.pageCount))} pages ${palette.dim(`· ${info.durationMs}ms · browser reloaded`)}`);
6812
+ },
6813
+ /**
6814
+ * Render the deploy result panel from a `deploy:complete` event.
6815
+ *
6816
+ * @param result - The `deploy:complete` payload.
6817
+ * @example
6818
+ * render.deployed({ url: "https://x.pages.dev", deploymentId: "id", branch: "main", durationMs: 1200 });
6819
+ */
6820
+ deployed(result) {
6821
+ writeBlock(box([
6822
+ `${palette.green("✓")} ${palette.bold("DEPLOYED")}`,
6823
+ `${palette.dim("url")} ${palette.cyan(result.url)}`,
6824
+ `${palette.dim("branch")} ${result.branch}`,
6825
+ `${palette.dim("id")} ${result.deploymentId}`,
6826
+ `${palette.dim("time")} ${result.durationMs}ms`
6827
+ ], color));
6828
+ },
6829
+ /**
6830
+ * Render a neutral informational line.
6831
+ *
6832
+ * @param message - The line to print.
6833
+ * @example
6834
+ * render.info("watching for changes…");
6835
+ */
6836
+ info(message) {
6837
+ write(` ${palette.cyan("›")} ${message}`);
6838
+ },
6839
+ /**
6840
+ * Render a warning line (to stderr).
6841
+ *
6842
+ * @param message - The warning to print.
6843
+ * @example
6844
+ * render.warn("deploy skipped");
6845
+ */
6846
+ warn(message) {
6847
+ writeError(` ${palette.yellow("⚠")} ${message}`);
6848
+ },
6849
+ /**
6850
+ * Render an error line (to stderr), optionally with a cause.
6851
+ *
6852
+ * @param message - The error summary to print.
6853
+ * @param cause - Optional underlying error/value to print beneath the summary.
6854
+ * @example
6855
+ * render.error("build failed", err);
6856
+ */
6857
+ error(message, cause) {
6858
+ writeError(` ${palette.red("✗")} ${message}`);
6859
+ if (cause !== void 0) writeError(String(cause));
6860
+ }
6861
+ };
6862
+ }
6863
+ //#endregion
6864
+ //#region src/plugins/cli/state.ts
6865
+ /**
6866
+ * @file cli plugin — state factory. Wires the default injectable seams: the Panel
6867
+ * renderer, a stdin y/N `confirm`, the `Date.now` clock, a recursive `node:fs.watch`
6868
+ * wrapper, and the `Bun.serve`/`Bun.file` static-server seams (resolved lazily, like
6869
+ * deploy's `defaultSpawn`, so a non-Bun runtime fails coded rather than as a raw
6870
+ * `TypeError` and tests can inject fakes). Unit tests swap any of these.
6871
+ */
6872
+ /**
6873
+ * Resolve the `Bun` runtime global, or `undefined` when not running under Bun.
6874
+ *
6875
+ * @returns The Bun runtime, or `undefined`.
6876
+ * @example
6877
+ * const bun = bunRuntime();
6878
+ */
6879
+ function bunRuntime() {
6880
+ return globalThis.Bun;
6881
+ }
6882
+ /**
6883
+ * Default static-server factory — resolves `Bun.serve` lazily at call time so the
6884
+ * server is only required when a long-running command actually starts one.
6885
+ *
6886
+ * @param options - Port + `fetch` handler (see {@link ServeStaticFunction}).
6887
+ * @returns The running server handle.
6888
+ * @throws {Error} When no Bun runtime is available to serve.
6889
+ * @example
6890
+ * defaultServeStatic({ port: 4173, fetch: () => new Response("ok") });
6891
+ */
6892
+ const defaultServeStatic = (options) => {
6893
+ const runtime = bunRuntime();
6894
+ if (runtime === void 0) throw new Error("[web] cli: no Bun runtime available to start the server.\n Run serve()/preview() under Bun, or inject state.serveStatic in tests.");
6895
+ return runtime.serve(options);
6896
+ };
6897
+ /**
6898
+ * Default file-response factory — `new Response(Bun.file(path), { status })`. Resolves
6899
+ * `Bun.file` lazily so it is only required when a request is actually served.
6900
+ *
6901
+ * @param path - Absolute on-disk path to stream.
6902
+ * @param status - HTTP status for the response.
6903
+ * @returns The file `Response`.
6904
+ * @throws {Error} When no Bun runtime is available to read the file.
6905
+ * @example
6906
+ * defaultFileResponse("/dist/index.html", 200);
6907
+ */
6908
+ const defaultFileResponse = (path, status) => {
6909
+ const runtime = bunRuntime();
6910
+ if (runtime === void 0) throw new Error("[web] cli: no Bun runtime available to read files.\n Run serve()/preview() under Bun, or inject state.fileResponse in tests.");
6911
+ return new Response(runtime.file(path), { status });
6912
+ };
6913
+ /**
6914
+ * Default stdin y/N prompt. Reads a single line from `process.stdin` via
6915
+ * `node:readline` and resolves `true` only on an explicit `y`/`yes` (default `No`).
6916
+ * Tests inject a canned answer so no real TTY interaction occurs.
6917
+ *
6918
+ * @param question - The yes/no question to display.
6919
+ * @returns Resolves `true` when the user answered yes.
6920
+ * @example
6921
+ * await defaultConfirm("Deploy dist/?");
6922
+ */
6923
+ function defaultConfirm(question) {
6924
+ return new Promise((resolve) => {
6925
+ const readline = createInterface({
6926
+ input: process.stdin,
6927
+ output: process.stdout
6928
+ });
6929
+ readline.question(`${question} [y/N] `, (answer) => {
6930
+ readline.close();
6931
+ resolve(/^y(es)?$/i.test(answer.trim()));
6932
+ });
6933
+ });
6934
+ }
6935
+ /**
6936
+ * Default recursive directory watcher — wraps `node:fs.watch` with `{ recursive: true }`
6937
+ * and adapts its handle to {@link WatchHandle}. Tests inject a fake emitter so no real
6938
+ * FS watch is registered.
6939
+ *
6940
+ * @param dir - The directory to watch recursively.
6941
+ * @param onChange - Invoked on any change beneath `dir`.
6942
+ * @returns A handle whose `close()` detaches the watcher.
6943
+ * @example
6944
+ * const handle = defaultWatch("content", () => rebuild());
6945
+ */
6946
+ function defaultWatch(dir, onChange) {
6947
+ const watcher = watch(dir, { recursive: true }, () => onChange());
6948
+ return {
6949
+ /**
6950
+ * Detach the underlying `node:fs.watch` listener.
6951
+ *
6952
+ * @example
6953
+ * handle.close();
6954
+ */
6955
+ close() {
6956
+ watcher.close();
6957
+ } };
6958
+ }
6959
+ /**
6960
+ * Default LAN network-URL deriver — wraps {@link networkUrl} so the production seam
6961
+ * reads `node:os` interfaces while tests can inject a deterministic value.
6962
+ *
6963
+ * @param port - The port the server is bound to.
6964
+ * @returns The `http://<ip>:<port>` URL, or `null` when offline.
6965
+ * @example
6966
+ * defaultNetworkUrl(4173);
6967
+ */
6968
+ function defaultNetworkUrl(port) {
6969
+ return networkUrl(port);
6970
+ }
6971
+ /**
6972
+ * Create the initial cli plugin state with the production seams wired. Every field is
6973
+ * an injectable seam (`render`, `confirm`, `clock`, `watch`, the server factories,
6974
+ * and `networkUrl`) so commands run under unit tests without real sockets/FS/TTY.
6975
+ *
6976
+ * @param _ctx - Minimal context with global + config (unused — state is static).
6977
+ * @param _ctx.global - Global plugin registry.
6978
+ * @param _ctx.config - Resolved plugin configuration.
6979
+ * @returns The initial cli state.
6980
+ * @example
6981
+ * const state = createState({ global: {}, config });
6982
+ */
6983
+ function createState$1(_ctx) {
6984
+ return {
6985
+ render: createPanelRenderer(),
6986
+ confirm: defaultConfirm,
6987
+ clock: Date.now,
6988
+ watch: defaultWatch,
6989
+ serveStatic: defaultServeStatic,
6990
+ fileResponse: defaultFileResponse,
6991
+ networkUrl: defaultNetworkUrl
6992
+ };
6993
+ }
6994
+ //#endregion
6995
+ //#region src/plugins/cli/index.ts
6996
+ /**
6997
+ * @file cli — Complex plugin (wiring harness only). Developer CLI:
6998
+ * build · serve · preview · deploy, with the boxed Panel renderer.
6999
+ * Depends: build, deploy. Listens: build:phase, build:complete, deploy:complete.
7000
+ * @see README.md
7001
+ */
7002
+ /**
7003
+ * cli plugin — the node-only developer CLI for `@moku-labs/web`. Mounts exactly four
7004
+ * methods at `app.cli` (`build`/`serve`/`preview`/`deploy`), each rendering through
7005
+ * the boxed Panel UI. Live build/deploy progress rides on hooks over the `build` and
7006
+ * `deploy` plugins' events; there is no argv parser and no `run()` dispatcher — the
7007
+ * consumer drives it from one thin script per command.
7008
+ *
7009
+ * @example Compose the CLI in a consumer app (node-only)
7010
+ * ```ts
7011
+ * import { buildPlugin, cliPlugin, createApp, deployPlugin } from "@moku-labs/web";
7012
+ *
7013
+ * const app = createApp({
7014
+ * plugins: [buildPlugin, deployPlugin, cliPlugin],
7015
+ * pluginConfigs: { cli: { outDir: "dist", port: 4173, watchDirs: ["content", "src"] } }
7016
+ * });
7017
+ * await app.start();
7018
+ * await app.cli.build();
7019
+ * ```
7020
+ */
7021
+ const cliPlugin = createPlugin$1("cli", {
7022
+ config: defaultConfig,
7023
+ depends: [buildPlugin, deployPlugin],
7024
+ createState: createState$1,
7025
+ onInit: (ctx) => validateConfig(ctx.config),
7026
+ hooks: (ctx) => ({
7027
+ "build:phase": (p) => ctx.state.render.phase(p),
7028
+ "build:complete": (p) => ctx.state.render.built(p),
7029
+ "deploy:complete": (p) => ctx.state.render.deployed(p)
7030
+ }),
5868
7031
  api: createApi$1
5869
7032
  });
5870
7033
  //#endregion
@@ -5935,7 +7098,7 @@ function spaEvents(register) {
5935
7098
  }
5936
7099
  //#endregion
5937
7100
  //#region src/plugins/spa/types.ts
5938
- var types_exports$8 = /* @__PURE__ */ __exportAll({ COMPONENT_HOOK_NAMES: () => COMPONENT_HOOK_NAMES });
7101
+ var types_exports$9 = /* @__PURE__ */ __exportAll({ COMPONENT_HOOK_NAMES: () => COMPONENT_HOOK_NAMES });
5939
7102
  /** Allowed hook names — single source of truth for fail-fast validation. */
5940
7103
  const COMPONENT_HOOK_NAMES = [
5941
7104
  "onCreate",
@@ -7001,27 +8164,30 @@ const spaPlugin = createPlugin$1("spa", {
7001
8164
  //#region src/plugins/build/types.ts
7002
8165
  var types_exports = /* @__PURE__ */ __exportAll({});
7003
8166
  //#endregion
7004
- //#region src/plugins/content/types.ts
8167
+ //#region src/plugins/cli/types.ts
7005
8168
  var types_exports$1 = /* @__PURE__ */ __exportAll({});
7006
8169
  //#endregion
7007
- //#region src/plugins/data/types.ts
8170
+ //#region src/plugins/content/types.ts
7008
8171
  var types_exports$2 = /* @__PURE__ */ __exportAll({});
7009
8172
  //#endregion
7010
- //#region src/plugins/deploy/types.ts
8173
+ //#region src/plugins/data/types.ts
7011
8174
  var types_exports$3 = /* @__PURE__ */ __exportAll({});
7012
8175
  //#endregion
7013
- //#region src/plugins/env/types.ts
8176
+ //#region src/plugins/deploy/types.ts
7014
8177
  var types_exports$4 = /* @__PURE__ */ __exportAll({});
7015
8178
  //#endregion
7016
- //#region src/plugins/head/types.ts
8179
+ //#region src/plugins/env/types.ts
7017
8180
  var types_exports$5 = /* @__PURE__ */ __exportAll({});
7018
8181
  //#endregion
7019
- //#region src/plugins/log/types.ts
8182
+ //#region src/plugins/head/types.ts
7020
8183
  var types_exports$6 = /* @__PURE__ */ __exportAll({});
7021
8184
  //#endregion
7022
- //#region src/plugins/router/types.ts
8185
+ //#region src/plugins/log/types.ts
7023
8186
  var types_exports$7 = /* @__PURE__ */ __exportAll({});
7024
8187
  //#endregion
8188
+ //#region src/plugins/router/types.ts
8189
+ var types_exports$8 = /* @__PURE__ */ __exportAll({});
8190
+ //#endregion
7025
8191
  //#region src/plugins/env/providers.ts
7026
8192
  /**
7027
8193
  * @file env plugin — built-in providers: dotenv, processEnv, cloudflareBindings.
@@ -7238,4 +8404,4 @@ const createApp = core.createApp;
7238
8404
  */
7239
8405
  const createPlugin = core.createPlugin;
7240
8406
  //#endregion
7241
- export { types_exports as Build, types_exports$1 as Content, types_exports$2 as Data, types_exports$3 as Deploy, types_exports$4 as Env, types_exports$5 as Head, types_exports$6 as Log, types_exports$7 as Router, types_exports$8 as Spa, browserEnv, buildArticleHead, buildPlugin, canonical, cloudflareBindings, contentPlugin, createApp, createComponent, createPlugin, dataPlugin, defineRoutes, deployPlugin, dotenv, envPlugin, feedLink, headPlugin, hreflang, i18nPlugin, jsonLd, logPlugin, meta, og, processEnv, route, routerPlugin, sitePlugin, spaPlugin, twitter };
8407
+ export { types_exports as Build, types_exports$1 as Cli, types_exports$2 as Content, types_exports$3 as Data, types_exports$4 as Deploy, types_exports$5 as Env, types_exports$6 as Head, types_exports$7 as Log, types_exports$8 as Router, types_exports$9 as Spa, browserEnv, buildArticleHead, buildPlugin, canonical, cliPlugin, cloudflareBindings, contentPlugin, createApp, createComponent, createPlugin, dataPlugin, defineRoutes, deployPlugin, dotenv, envPlugin, feedLink, headPlugin, hreflang, i18nPlugin, jsonLd, logPlugin, meta, og, processEnv, route, routerPlugin, sitePlugin, spaPlugin, twitter };