@boon4681/giri 0.0.2-alpha-8 → 0.0.3

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
@@ -410,9 +410,9 @@ var init_schema = __esm({
410
410
 
411
411
  // src/cli.ts
412
412
  var import_node_child_process = require("child_process");
413
- var import_node_fs10 = require("fs");
413
+ var import_node_fs11 = require("fs");
414
414
  var import_promises4 = require("fs/promises");
415
- var import_node_path15 = require("path");
415
+ var import_node_path16 = require("path");
416
416
  var prompts = __toESM(require("@clack/prompts"));
417
417
 
418
418
  // src/app.ts
@@ -1951,43 +1951,121 @@ async function syncProject(config, options = {}) {
1951
1951
  }
1952
1952
 
1953
1953
  // src/generator/watch.ts
1954
- var import_node_fs8 = require("fs");
1955
- var import_node_path13 = require("path");
1954
+ var import_node_fs9 = require("fs");
1955
+ var import_node_path14 = require("path");
1956
1956
 
1957
- // src/loader/module-loader.ts
1957
+ // src/loader/import-graph.ts
1958
+ var import_node_fs8 = require("fs");
1958
1959
  var import_node_path12 = require("path");
1960
+ var import_typescript5 = __toESM(require("typescript"));
1961
+ var import_tinyglobby2 = require("tinyglobby");
1962
+ var RESOLVE_EXTS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
1963
+ var JS_EXT = /\.(?:c|m)?jsx?$/;
1959
1964
  var toSlash = (path) => path.split(import_node_path12.sep).join("/");
1960
- var isProjectModule = (id, root) => {
1961
- return id.startsWith(root) && !id.includes(`${import_node_path12.sep}node_modules${import_node_path12.sep}`) && !id.includes(`${import_node_path12.sep}.giri${import_node_path12.sep}`);
1962
- };
1963
- var buildModuleGraph = (cwd) => {
1964
- const root = (0, import_node_path12.resolve)(cwd) + import_node_path12.sep;
1965
+ function probeFile(base) {
1966
+ if ((0, import_node_fs8.existsSync)(base) && (0, import_node_fs8.statSync)(base).isFile()) {
1967
+ return base;
1968
+ }
1969
+ for (const ext of RESOLVE_EXTS) {
1970
+ const candidate = base + ext;
1971
+ if ((0, import_node_fs8.existsSync)(candidate)) {
1972
+ return candidate;
1973
+ }
1974
+ }
1975
+ for (const ext of RESOLVE_EXTS) {
1976
+ const candidate = (0, import_node_path12.join)(base, `index${ext}`);
1977
+ if ((0, import_node_fs8.existsSync)(candidate)) {
1978
+ return candidate;
1979
+ }
1980
+ }
1981
+ return void 0;
1982
+ }
1983
+ function resolveSpecifier(specifier, fromFile, alias, cwd) {
1984
+ let target;
1985
+ if (specifier.startsWith(".")) {
1986
+ target = (0, import_node_path12.resolve)((0, import_node_path12.dirname)(fromFile), specifier);
1987
+ } else {
1988
+ const aliased = resolveAliasRequest(specifier, alias, cwd);
1989
+ if (aliased === void 0) {
1990
+ return void 0;
1991
+ }
1992
+ target = aliased;
1993
+ }
1994
+ const resolved = probeFile(target);
1995
+ if (resolved) {
1996
+ return resolved;
1997
+ }
1998
+ if (JS_EXT.test(target)) {
1999
+ return probeFile(target.replace(JS_EXT, ""));
2000
+ }
2001
+ return void 0;
2002
+ }
2003
+ function importSpecifiers(file) {
2004
+ let source;
2005
+ try {
2006
+ source = import_typescript5.default.createSourceFile(file, (0, import_node_fs8.readFileSync)(file, "utf8"), import_typescript5.default.ScriptTarget.Latest, false);
2007
+ } catch {
2008
+ return [];
2009
+ }
2010
+ const specifiers = [];
2011
+ const visit = (node) => {
2012
+ if ((import_typescript5.default.isImportDeclaration(node) || import_typescript5.default.isExportDeclaration(node)) && node.moduleSpecifier && import_typescript5.default.isStringLiteral(node.moduleSpecifier)) {
2013
+ specifiers.push(node.moduleSpecifier.text);
2014
+ } else if (import_typescript5.default.isImportEqualsDeclaration(node) && import_typescript5.default.isExternalModuleReference(node.moduleReference) && import_typescript5.default.isStringLiteralLike(node.moduleReference.expression)) {
2015
+ specifiers.push(node.moduleReference.expression.text);
2016
+ } else if (import_typescript5.default.isCallExpression(node)) {
2017
+ const isRequire = import_typescript5.default.isIdentifier(node.expression) && node.expression.text === "require";
2018
+ const isDynamicImport = node.expression.kind === import_typescript5.default.SyntaxKind.ImportKeyword;
2019
+ const [first] = node.arguments;
2020
+ if ((isRequire || isDynamicImport) && first && import_typescript5.default.isStringLiteralLike(first)) {
2021
+ specifiers.push(first.text);
2022
+ }
2023
+ }
2024
+ import_typescript5.default.forEachChild(node, visit);
2025
+ };
2026
+ visit(source);
2027
+ return specifiers;
2028
+ }
2029
+ async function buildImportGraph(config, cwd) {
2030
+ const root = (0, import_node_path12.resolve)(cwd);
2031
+ const outDir = (0, import_node_path12.resolve)(root, config.outDir ?? ".giri") + import_node_path12.sep;
2032
+ const files = await (0, import_tinyglobby2.glob)("**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}", {
2033
+ cwd: root,
2034
+ absolute: true,
2035
+ onlyFiles: true,
2036
+ ignore: ["**/node_modules/**"]
2037
+ });
1965
2038
  const importers = /* @__PURE__ */ new Map();
1966
2039
  const nodes = /* @__PURE__ */ new Set();
1967
- for (const id of Object.keys(require.cache)) {
1968
- if (!isProjectModule(id, root)) {
1969
- continue;
1970
- }
1971
- const mod = require.cache[id];
1972
- if (!mod) {
2040
+ for (const file of files) {
2041
+ if (file.startsWith(outDir)) {
1973
2042
  continue;
1974
2043
  }
1975
- nodes.add(toSlash(id));
1976
- for (const child of mod.children) {
1977
- if (!isProjectModule(child.id, root)) {
2044
+ for (const specifier of importSpecifiers(file)) {
2045
+ const dep = resolveSpecifier(specifier, file, config.alias, root);
2046
+ if (!dep || dep.startsWith(outDir)) {
1978
2047
  continue;
1979
2048
  }
1980
- nodes.add(toSlash(child.id));
1981
- const dep = toSlash(child.id);
1982
- let set = importers.get(dep);
2049
+ const from = toSlash(file);
2050
+ const to = toSlash(dep);
2051
+ nodes.add(from);
2052
+ nodes.add(to);
2053
+ let set = importers.get(to);
1983
2054
  if (!set) {
1984
2055
  set = /* @__PURE__ */ new Set();
1985
- importers.set(dep, set);
2056
+ importers.set(to, set);
1986
2057
  }
1987
- set.add(toSlash(id));
2058
+ set.add(from);
1988
2059
  }
1989
2060
  }
1990
2061
  return { importers, nodes };
2062
+ }
2063
+
2064
+ // src/loader/module-loader.ts
2065
+ var import_node_path13 = require("path");
2066
+ var toSlash2 = (path) => path.split(import_node_path13.sep).join("/");
2067
+ var isProjectModule = (id, root) => {
2068
+ return id.startsWith(root) && !id.includes(`${import_node_path13.sep}node_modules${import_node_path13.sep}`) && !id.includes(`${import_node_path13.sep}.giri${import_node_path13.sep}`);
1991
2069
  };
1992
2070
  var collectDependents = (graph, start) => {
1993
2071
  const out = /* @__PURE__ */ new Set([start]);
@@ -2005,13 +2083,13 @@ var collectDependents = (graph, start) => {
2005
2083
  };
2006
2084
  var purgeModules = (files) => {
2007
2085
  for (const id of Object.keys(require.cache)) {
2008
- if (files.has(toSlash(id))) {
2086
+ if (files.has(toSlash2(id))) {
2009
2087
  delete require.cache[id];
2010
2088
  }
2011
2089
  }
2012
2090
  };
2013
2091
  var purgeProjectModules = (cwd) => {
2014
- const root = (0, import_node_path12.resolve)(cwd) + import_node_path12.sep;
2092
+ const root = (0, import_node_path13.resolve)(cwd) + import_node_path13.sep;
2015
2093
  for (const id of Object.keys(require.cache)) {
2016
2094
  if (isProjectModule(id, root)) {
2017
2095
  delete require.cache[id];
@@ -2019,9 +2097,9 @@ var purgeProjectModules = (cwd) => {
2019
2097
  }
2020
2098
  };
2021
2099
  var purgeGeneratedModules = (outDir) => {
2022
- const root = (0, import_node_path12.resolve)(outDir) + import_node_path12.sep;
2100
+ const root = (0, import_node_path13.resolve)(outDir) + import_node_path13.sep;
2023
2101
  for (const id of Object.keys(require.cache)) {
2024
- if ((0, import_node_path12.resolve)(id).startsWith(root)) {
2102
+ if ((0, import_node_path13.resolve)(id).startsWith(root)) {
2025
2103
  delete require.cache[id];
2026
2104
  }
2027
2105
  }
@@ -2048,8 +2126,8 @@ function createWatchUpdater(config, initial) {
2048
2126
  const key = route.file;
2049
2127
  try {
2050
2128
  const { createSchemaProgram: createSchemaProgram2, extractRouteResponses: extractRouteResponses2 } = await Promise.resolve().then(() => (init_schema(), schema_exports));
2051
- const appTypes = (0, import_node_path13.join)(paths.outDir, "types", "app.d.ts");
2052
- const program = createSchemaProgram2(paths, (0, import_node_fs8.existsSync)(appTypes) ? [key, appTypes] : [key]);
2129
+ const appTypes = (0, import_node_path14.join)(paths.outDir, "types", "app.d.ts");
2130
+ const program = createSchemaProgram2(paths, (0, import_node_fs9.existsSync)(appTypes) ? [key, appTypes] : [key]);
2053
2131
  data.responsesByFile.set(key, extractRouteResponses2(program, key));
2054
2132
  } catch {
2055
2133
  }
@@ -2080,15 +2158,15 @@ function createWatchUpdater(config, initial) {
2080
2158
  if (!filename) {
2081
2159
  return fullResync();
2082
2160
  }
2083
- const abs = (0, import_node_path13.resolve)((0, import_node_path13.dirname)(paths.routesDir), filename);
2161
+ const abs = (0, import_node_path14.resolve)((0, import_node_path14.dirname)(paths.routesDir), filename);
2084
2162
  const file = slash(abs);
2085
- if (!(0, import_node_fs8.existsSync)(abs)) {
2163
+ if (!(0, import_node_fs9.existsSync)(abs)) {
2086
2164
  return fullResync();
2087
2165
  }
2088
- if ((0, import_node_fs8.statSync)(abs).isDirectory()) {
2166
+ if ((0, import_node_fs9.statSync)(abs).isDirectory()) {
2089
2167
  return "skip";
2090
2168
  }
2091
- const graph = buildModuleGraph(paths.cwd);
2169
+ const graph = await buildImportGraph(config, paths.cwd);
2092
2170
  const isRoute = routes.some((candidate) => slash(candidate.file) === file);
2093
2171
  if (!graph.nodes.has(file) && !isRoute) {
2094
2172
  return fullResync();
@@ -2110,20 +2188,20 @@ function createWatchUpdater(config, initial) {
2110
2188
  }
2111
2189
 
2112
2190
  // src/lifecycle.ts
2113
- var import_node_fs9 = require("fs");
2114
- var import_node_path14 = require("path");
2191
+ var import_node_fs10 = require("fs");
2192
+ var import_node_path15 = require("path");
2115
2193
  var MAIN_EXTENSIONS2 = ["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs"];
2116
2194
  function resolveMainFile(cwd) {
2117
2195
  for (const ext of MAIN_EXTENSIONS2) {
2118
- const file = (0, import_node_path14.join)(cwd, "src", `main.${ext}`);
2119
- if ((0, import_node_fs9.existsSync)(file)) {
2196
+ const file = (0, import_node_path15.join)(cwd, "src", `main.${ext}`);
2197
+ if ((0, import_node_fs10.existsSync)(file)) {
2120
2198
  return file;
2121
2199
  }
2122
2200
  }
2123
2201
  return void 0;
2124
2202
  }
2125
2203
  async function loadLifecycle(cwd = process.cwd()) {
2126
- const file = resolveMainFile((0, import_node_path14.resolve)(cwd));
2204
+ const file = resolveMainFile((0, import_node_path15.resolve)(cwd));
2127
2205
  if (!file) {
2128
2206
  return {};
2129
2207
  }
@@ -2281,9 +2359,9 @@ function parseFlags(args) {
2281
2359
  return flags;
2282
2360
  }
2283
2361
  async function ensureGitignore(cwd) {
2284
- const file = (0, import_node_path15.join)(cwd, ".gitignore");
2362
+ const file = (0, import_node_path16.join)(cwd, ".gitignore");
2285
2363
  const entry = ".giri";
2286
- if (!(0, import_node_fs10.existsSync)(file)) {
2364
+ if (!(0, import_node_fs11.existsSync)(file)) {
2287
2365
  await (0, import_promises4.writeFile)(file, `${entry}
2288
2366
  `);
2289
2367
  return;
@@ -2295,8 +2373,8 @@ async function ensureGitignore(cwd) {
2295
2373
  }
2296
2374
  }
2297
2375
  async function ensureTsConfig(cwd) {
2298
- const file = (0, import_node_path15.join)(cwd, "tsconfig.json");
2299
- if ((0, import_node_fs10.existsSync)(file)) {
2376
+ const file = (0, import_node_path16.join)(cwd, "tsconfig.json");
2377
+ if ((0, import_node_fs11.existsSync)(file)) {
2300
2378
  return;
2301
2379
  }
2302
2380
  await (0, import_promises4.writeFile)(
@@ -2325,7 +2403,7 @@ async function ensureTsConfig(cwd) {
2325
2403
  async function missingDeps(cwd, candidates) {
2326
2404
  let pkg = {};
2327
2405
  try {
2328
- pkg = JSON.parse(await (0, import_promises4.readFile)((0, import_node_path15.join)(cwd, "package.json"), "utf8"));
2406
+ pkg = JSON.parse(await (0, import_promises4.readFile)((0, import_node_path16.join)(cwd, "package.json"), "utf8"));
2329
2407
  } catch {
2330
2408
  }
2331
2409
  const present = /* @__PURE__ */ new Set();
@@ -2394,7 +2472,7 @@ async function selectAdapter(interactive) {
2394
2472
  return ADAPTERS.find((adapter) => adapter.value === picked) ?? null;
2395
2473
  }
2396
2474
  async function initProject(cwd, flags) {
2397
- if (!(0, import_node_fs10.existsSync)((0, import_node_path15.join)(cwd, "package.json"))) {
2475
+ if (!(0, import_node_fs11.existsSync)((0, import_node_path16.join)(cwd, "package.json"))) {
2398
2476
  throw new Error(
2399
2477
  "No package.json found. Run `giri init` inside an existing project - set one up first (e.g. `npm init -y` and install typescript), then re-run."
2400
2478
  );
@@ -2419,13 +2497,13 @@ async function initProject(cwd, flags) {
2419
2497
  prompts.cancel(`The ${adapter.label} adapter isn't available yet - only Hono ships today.`);
2420
2498
  return;
2421
2499
  }
2422
- const configPath = (0, import_node_path15.join)(cwd, "giri.config.ts");
2423
- if (!(0, import_node_fs10.existsSync)(configPath)) {
2500
+ const configPath = (0, import_node_path16.join)(cwd, "giri.config.ts");
2501
+ if (!(0, import_node_fs11.existsSync)(configPath)) {
2424
2502
  await (0, import_promises4.writeFile)(configPath, configSource(adapter));
2425
2503
  }
2426
- const routePath = (0, import_node_path15.join)(cwd, "src", "routes", "+get.ts");
2427
- if (!(0, import_node_fs10.existsSync)(routePath)) {
2428
- await (0, import_promises4.mkdir)((0, import_node_path15.join)(cwd, "src", "routes"), { recursive: true });
2504
+ const routePath = (0, import_node_path16.join)(cwd, "src", "routes", "+get.ts");
2505
+ if (!(0, import_node_fs11.existsSync)(routePath)) {
2506
+ await (0, import_promises4.mkdir)((0, import_node_path16.join)(cwd, "src", "routes"), { recursive: true });
2429
2507
  await (0, import_promises4.writeFile)(
2430
2508
  routePath,
2431
2509
  [
@@ -2510,8 +2588,8 @@ async function serveProject(config, flags) {
2510
2588
  const port = flags.port ?? cfg.server?.port ?? 3e3;
2511
2589
  const hostname = flags.hostname ?? cfg.server?.hostname;
2512
2590
  if (flags.watch) {
2513
- const srcDir = (0, import_node_path15.resolve)(current.paths.routesDir, "..");
2514
- if ((0, import_node_fs10.existsSync)(srcDir)) {
2591
+ const srcDir = (0, import_node_path16.resolve)(current.paths.routesDir, "..");
2592
+ if ((0, import_node_fs11.existsSync)(srcDir)) {
2515
2593
  let timer;
2516
2594
  let syncing = false;
2517
2595
  const changed = /* @__PURE__ */ new Set();
@@ -2541,7 +2619,7 @@ async function serveProject(config, flags) {
2541
2619
  }
2542
2620
  rebuild = true;
2543
2621
  if (name) {
2544
- dirtySet.add((0, import_node_path15.resolve)(srcDir, name));
2622
+ dirtySet.add((0, import_node_path16.resolve)(srcDir, name));
2545
2623
  }
2546
2624
  const rel = name ? `src/${name.replace(/\\/g, "/")}` : "src";
2547
2625
  log2.change(outcome === "full" ? "sync" : "update", rel, bump(rel));
@@ -2599,11 +2677,11 @@ async function serveProject(config, flags) {
2599
2677
  };
2600
2678
  };
2601
2679
  await boot(config);
2602
- const configPath = flags.watch ? findConfigPath((0, import_node_path15.resolve)(process.cwd())) : void 0;
2680
+ const configPath = flags.watch ? findConfigPath((0, import_node_path16.resolve)(process.cwd())) : void 0;
2603
2681
  if (configPath) {
2604
2682
  let timer;
2605
2683
  let restarting = false;
2606
- const configName = (0, import_node_path15.basename)(configPath);
2684
+ const configName = (0, import_node_path16.basename)(configPath);
2607
2685
  const restart = async () => {
2608
2686
  if (restarting) {
2609
2687
  return;
@@ -2622,9 +2700,9 @@ async function serveProject(config, flags) {
2622
2700
  restarting = false;
2623
2701
  }
2624
2702
  };
2625
- const configWatcher = watch((0, import_node_path15.dirname)(configPath), { persistent: true });
2703
+ const configWatcher = watch((0, import_node_path16.dirname)(configPath), { persistent: true });
2626
2704
  configWatcher.on("all", (_event, filename) => {
2627
- if (filename && (0, import_node_path15.basename)(filename.toString()) === configName) {
2705
+ if (filename && (0, import_node_path16.basename)(filename.toString()) === configName) {
2628
2706
  clearTimeout(timer);
2629
2707
  timer = setTimeout(() => void restart(), 150);
2630
2708
  }
@@ -2653,7 +2731,7 @@ function registerShutdown(cleanup) {
2653
2731
  }
2654
2732
  async function main() {
2655
2733
  const [command = "help", ...args] = process.argv.slice(2);
2656
- const cwd = (0, import_node_path15.resolve)(process.cwd());
2734
+ const cwd = (0, import_node_path16.resolve)(process.cwd());
2657
2735
  if (command === "help" || command === "--help" || command === "-h") {
2658
2736
  help();
2659
2737
  return;