@veducio/keja 0.6.0 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veducio/keja",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "packageManager": "bun@1.4.4",
5
5
  "description": "Veducio Keja - RESTFUL Web Server, Based on Lightweight Hono Framework, Written in TypeScript, Powered by Bun",
6
6
  "license": "MIT",
@@ -1,3 +1,5 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
1
3
  import path from "node:path";
2
4
  import { describe, expect, it } from "vitest";
3
5
  import { freshImportUrl, resolveProjectImport } from "./reload-graph.ts";
@@ -26,6 +28,32 @@ describe("project reload imports", () => {
26
28
  });
27
29
  });
28
30
 
31
+ it("adds a TypeScript extension to an extensionless relative import", () => {
32
+ const project = mkdtempSync(path.join(tmpdir(), "keja-reload-"));
33
+ const repositories = path.join(project, "src", "db", "repositories.ts");
34
+ const importer = path.join(
35
+ project,
36
+ "src",
37
+ "public",
38
+ "config",
39
+ "tenants.ts",
40
+ );
41
+ mkdirSync(path.dirname(repositories), { recursive: true });
42
+ mkdirSync(path.dirname(importer), { recursive: true });
43
+ writeFileSync(repositories, "export {}\n");
44
+ try {
45
+ const resolved = resolveProjectImport({
46
+ importer: `keja:${importer}`,
47
+ specifier: "../../db/repositories",
48
+ projectRoot: project,
49
+ generation: "4",
50
+ });
51
+ expect(resolved?.file).toBe(repositories);
52
+ } finally {
53
+ rmSync(project, { recursive: true, force: true });
54
+ }
55
+ });
56
+
29
57
  it("follows the next file imported by that dependency", () => {
30
58
  const data = path.join(root, "src", "data.ts");
31
59
  const resolved = resolveProjectImport({
@@ -1,4 +1,4 @@
1
- import { readFileSync } from "node:fs";
1
+ import { existsSync, readFileSync, statSync } from "node:fs";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
 
@@ -35,7 +35,9 @@ export function resolveProjectImport(input: {
35
35
  if (!inProject && !inGraph) {
36
36
  return null;
37
37
  }
38
- const resolved = path.resolve(path.dirname(importerFile), specifier);
38
+ const resolved = resolveRelativeFile(
39
+ path.resolve(path.dirname(importerFile), specifier),
40
+ );
39
41
  if (isNodeModules(resolved)) {
40
42
  return null;
41
43
  }
@@ -148,6 +150,46 @@ function isRelativeSpecifier(specifier: string) {
148
150
  return specifier.startsWith("./") || specifier.startsWith("../");
149
151
  }
150
152
 
153
+ const sourceExtensions = [
154
+ ".ts",
155
+ ".tsx",
156
+ ".js",
157
+ ".jsx",
158
+ ".mjs",
159
+ ".cjs",
160
+ ".json",
161
+ ] as const;
162
+
163
+ function resolveRelativeFile(resolved: string) {
164
+ if (fileExists(resolved)) {
165
+ return resolved;
166
+ }
167
+ if (path.extname(resolved) !== "") {
168
+ return resolved;
169
+ }
170
+ for (const extension of sourceExtensions) {
171
+ const candidate = `${resolved}${extension}`;
172
+ if (fileExists(candidate)) {
173
+ return candidate;
174
+ }
175
+ }
176
+ for (const extension of sourceExtensions) {
177
+ const candidate = path.join(resolved, `index${extension}`);
178
+ if (fileExists(candidate)) {
179
+ return candidate;
180
+ }
181
+ }
182
+ return resolved;
183
+ }
184
+
185
+ function fileExists(file: string) {
186
+ try {
187
+ return existsSync(file) && statSync(file).isFile();
188
+ } catch {
189
+ return false;
190
+ }
191
+ }
192
+
151
193
  function filePath(importer: string) {
152
194
  const withoutQuery = importer.split("?")[0] ?? importer;
153
195
  const clean = withoutQuery.startsWith(`${namespace}:`)