@boon4681/giri 0.0.2-alpha-1 → 0.0.2-alpha-2

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/cli.js CHANGED
@@ -461,29 +461,44 @@ var safeRegister = async () => {
461
461
  await assertES5(res.unregister);
462
462
  return res;
463
463
  };
464
- var load = async () => {
465
- const defaultTsConfigExists = (0, import_node_fs.existsSync)((0, import_node_path.resolve)("giri.config.ts"));
466
- const defaultJsConfigExists = (0, import_node_fs.existsSync)((0, import_node_path.resolve)("giri.config.js"));
467
- const defaultConfigPath = defaultTsConfigExists ? "giri.config.ts" : defaultJsConfigExists ? "giri.config.js" : void 0;
468
- if (!defaultConfigPath) {
469
- import_prompts.log.error("Config file not found.");
470
- (0, import_node_process.exit)(1);
464
+ var findConfigPath = (cwd = (0, import_node_path.resolve)()) => {
465
+ for (const name of ["giri.config.ts", "giri.config.js"]) {
466
+ const path = (0, import_node_path.resolve)(cwd, name);
467
+ if ((0, import_node_fs.existsSync)(path)) {
468
+ return path;
469
+ }
471
470
  }
472
- const path = (0, import_node_path.resolve)(defaultConfigPath);
473
- if (!(0, import_node_fs.existsSync)(path)) {
474
- import_prompts.log.error(`${path} file does not exist`);
471
+ return void 0;
472
+ };
473
+ var load = async (opts = {}) => {
474
+ const fail = (message) => {
475
+ if (opts.throwOnError) {
476
+ throw new Error(message);
477
+ }
478
+ import_prompts.log.error(message);
475
479
  (0, import_node_process.exit)(1);
480
+ };
481
+ const path = findConfigPath();
482
+ if (!path) {
483
+ fail("Config file not found.");
476
484
  }
477
485
  const { unregister } = await safeRegister();
478
- const required = require(`${path}`);
479
- const content = required.default ?? required;
486
+ let content;
487
+ try {
488
+ const required = require(`${path}`);
489
+ content = required.default ?? required;
490
+ } finally {
491
+ }
480
492
  unregister();
481
493
  const res = import_value.Value.Check(configSchema, content);
482
494
  if (!res) {
483
- for (const error of [...import_value.Value.Errors(configSchema, content)]) {
484
- import_prompts.log.error(error.message);
495
+ const messages = [...import_value.Value.Errors(configSchema, content)].map((error) => error.message);
496
+ if (!opts.throwOnError) {
497
+ for (const message of messages) {
498
+ import_prompts.log.error(message);
499
+ }
485
500
  }
486
- (0, import_node_process.exit)(1);
501
+ fail(messages.join("\n"));
487
502
  }
488
503
  return content;
489
504
  };
@@ -2027,69 +2042,120 @@ function displayHost(address) {
2027
2042
  return address.includes(":") ? `[${address}]` : address;
2028
2043
  }
2029
2044
  async function serveProject(config, flags) {
2030
- const initial = await syncProject(config);
2031
- log2.success(
2032
- `synced ${initial.routes.length} route${initial.routes.length === 1 ? "" : "s"} ${muted(`at ${initial.paths.outDir}`)}`,
2033
- "sync"
2034
- );
2035
- const lifecycle = await loadLifecycle();
2036
- const services = await runInit(lifecycle);
2037
- let current = await buildGiriApp(config, { services });
2038
- const port = flags.port ?? config.server?.port ?? 3e3;
2039
- const hostname = flags.hostname ?? config.server?.hostname;
2040
- if (flags.watch) {
2041
- const srcDir = (0, import_node_path15.resolve)(current.paths.routesDir, "..");
2042
- if ((0, import_node_fs9.existsSync)(srcDir)) {
2043
- let timer;
2044
- let syncing = false;
2045
- const changed = /* @__PURE__ */ new Set();
2046
- const { watch } = await import("fs");
2047
- const updater = createWatchUpdater(config, initial);
2048
- const hmrCount = /* @__PURE__ */ new Map();
2049
- const bump = (key) => {
2050
- const next = (hmrCount.get(key) ?? 0) + 1;
2051
- hmrCount.set(key, next);
2052
- return next;
2053
- };
2054
- const flush = async () => {
2055
- if (syncing) {
2056
- return;
2057
- }
2058
- syncing = true;
2059
- try {
2060
- while (changed.size > 0) {
2061
- const batch = [...changed];
2062
- changed.clear();
2063
- for (const name of batch) {
2064
- const outcome = await updater.apply(name || null);
2065
- const rel = name ? `src/${name.replace(/\\/g, "/")}` : "src";
2066
- log2.change(outcome === "full" ? "sync" : "update", rel, bump(rel));
2045
+ const { watch } = await import("fs");
2046
+ let stop;
2047
+ const boot = async (cfg) => {
2048
+ const initial = await syncProject(cfg);
2049
+ log2.success(
2050
+ `synced ${initial.routes.length} route${initial.routes.length === 1 ? "" : "s"} ${muted(`at ${initial.paths.outDir}`)}`,
2051
+ "sync"
2052
+ );
2053
+ const lifecycle = await loadLifecycle();
2054
+ const services = await runInit(lifecycle);
2055
+ let current = await buildGiriApp(cfg, { services });
2056
+ const port = flags.port ?? cfg.server?.port ?? 3e3;
2057
+ const hostname = flags.hostname ?? cfg.server?.hostname;
2058
+ const closers = [];
2059
+ if (flags.watch) {
2060
+ const srcDir = (0, import_node_path15.resolve)(current.paths.routesDir, "..");
2061
+ if ((0, import_node_fs9.existsSync)(srcDir)) {
2062
+ let timer;
2063
+ let syncing = false;
2064
+ const changed = /* @__PURE__ */ new Set();
2065
+ const updater = createWatchUpdater(cfg, initial);
2066
+ const hmrCount = /* @__PURE__ */ new Map();
2067
+ const bump = (key) => {
2068
+ const next = (hmrCount.get(key) ?? 0) + 1;
2069
+ hmrCount.set(key, next);
2070
+ return next;
2071
+ };
2072
+ const flush = async () => {
2073
+ if (syncing) {
2074
+ return;
2075
+ }
2076
+ syncing = true;
2077
+ try {
2078
+ while (changed.size > 0) {
2079
+ const batch = [...changed];
2080
+ changed.clear();
2081
+ for (const name of batch) {
2082
+ const outcome = await updater.apply(name || null);
2083
+ const rel = name ? `src/${name.replace(/\\/g, "/")}` : "src";
2084
+ log2.change(outcome === "full" ? "sync" : "update", rel, bump(rel));
2085
+ }
2086
+ current = await buildGiriApp(cfg, { services });
2067
2087
  }
2068
- current = await buildGiriApp(config, { services });
2088
+ } catch (error) {
2089
+ log2.error(error instanceof Error ? error.message : String(error), "watch");
2090
+ } finally {
2091
+ syncing = false;
2069
2092
  }
2070
- } catch (error) {
2071
- log2.error(error instanceof Error ? error.message : String(error), "watch");
2072
- } finally {
2073
- syncing = false;
2074
- }
2075
- if (changed.size > 0) {
2076
- void flush();
2077
- }
2078
- };
2079
- watch(srcDir, { recursive: true }, (_event, filename) => {
2080
- changed.add(filename ? filename.toString() : "");
2081
- clearTimeout(timer);
2082
- timer = setTimeout(() => void flush(), 150);
2083
- });
2093
+ if (changed.size > 0) {
2094
+ void flush();
2095
+ }
2096
+ };
2097
+ const watcher = watch(srcDir, { recursive: true }, (_event, filename) => {
2098
+ changed.add(filename ? filename.toString() : "");
2099
+ clearTimeout(timer);
2100
+ timer = setTimeout(() => void flush(), 150);
2101
+ });
2102
+ closers.push(() => {
2103
+ clearTimeout(timer);
2104
+ watcher.close();
2105
+ });
2106
+ }
2084
2107
  }
2108
+ const handler = (request) => cfg.adapter.fetch(current.app, request);
2109
+ const server = cfg.adapter.serve(handler, { port, hostname }, (info) => {
2110
+ log2.ready(`http://${displayHost(info.address)}:${info.port}`);
2111
+ });
2112
+ stop = async () => {
2113
+ for (const close of closers) {
2114
+ await close();
2115
+ }
2116
+ try {
2117
+ await server.close();
2118
+ } catch {
2119
+ }
2120
+ if (lifecycle.teardown) {
2121
+ await lifecycle.teardown(services);
2122
+ }
2123
+ };
2124
+ };
2125
+ await boot(config);
2126
+ const configPath = flags.watch ? findConfigPath((0, import_node_path15.resolve)(process.cwd())) : void 0;
2127
+ if (configPath) {
2128
+ let timer;
2129
+ let restarting = false;
2130
+ const configName = (0, import_node_path15.basename)(configPath);
2131
+ const restart = async () => {
2132
+ if (restarting) {
2133
+ return;
2134
+ }
2135
+ restarting = true;
2136
+ try {
2137
+ log2.info(`${color.green("restart")} ${configName} changed`, "config");
2138
+ delete require.cache[configPath];
2139
+ const next = await load({ throwOnError: true });
2140
+ await stop?.();
2141
+ await boot(next);
2142
+ } catch (error) {
2143
+ log2.error(error instanceof Error ? error.message : String(error), "config");
2144
+ log2.info("kept the previous server running \u2014 fix the config and save again", "config");
2145
+ } finally {
2146
+ restarting = false;
2147
+ }
2148
+ };
2149
+ watch((0, import_node_path15.dirname)(configPath), { recursive: false }, (_event, filename) => {
2150
+ if (filename && (0, import_node_path15.basename)(filename.toString()) === configName) {
2151
+ clearTimeout(timer);
2152
+ timer = setTimeout(() => void restart(), 150);
2153
+ }
2154
+ });
2085
2155
  }
2086
- const handler = (request) => config.adapter.fetch(current.app, request);
2087
- const server = config.adapter.serve(handler, { port, hostname }, (info) => {
2088
- log2.ready(`http://${displayHost(info.address)}:${info.port}`);
2089
- });
2090
- registerShutdown(server, lifecycle, services);
2156
+ registerShutdown(() => stop?.());
2091
2157
  }
2092
- function registerShutdown(server, lifecycle, services) {
2158
+ function registerShutdown(cleanup) {
2093
2159
  let shuttingDown = false;
2094
2160
  const shutdown = async () => {
2095
2161
  if (shuttingDown) {
@@ -2097,10 +2163,7 @@ function registerShutdown(server, lifecycle, services) {
2097
2163
  }
2098
2164
  shuttingDown = true;
2099
2165
  try {
2100
- await server.close();
2101
- if (lifecycle.teardown) {
2102
- await lifecycle.teardown(services);
2103
- }
2166
+ await cleanup();
2104
2167
  } catch (error) {
2105
2168
  log2.error(error instanceof Error ? error.message : String(error));
2106
2169
  process.exitCode = 1;