@ts-for-gir/lib 4.0.0-beta.30 → 4.0.0-beta.32

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/constants.ts +26 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-for-gir/lib",
3
- "version": "4.0.0-beta.30",
3
+ "version": "4.0.0-beta.32",
4
4
  "description": "Typescript .d.ts generator from GIR for gjs",
5
5
  "main": "src/index.ts",
6
6
  "module": "src/index.ts",
@@ -48,9 +48,9 @@
48
48
  "typescript": "^5.9.2"
49
49
  },
50
50
  "dependencies": {
51
- "@gi.ts/parser": "^4.0.0-beta.30",
52
- "@ts-for-gir/reporter": "^4.0.0-beta.30",
53
- "@ts-for-gir/templates": "^4.0.0-beta.30",
51
+ "@gi.ts/parser": "^4.0.0-beta.32",
52
+ "@ts-for-gir/reporter": "^4.0.0-beta.32",
53
+ "@ts-for-gir/templates": "^4.0.0-beta.32",
54
54
  "colorette": "^2.0.20",
55
55
  "ejs": "^3.1.10",
56
56
  "glob": "^11.0.3",
package/src/constants.ts CHANGED
@@ -1,18 +1,17 @@
1
- import { createRequire } from "node:module";
1
+ import { readFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
3
4
  import type { LibraryVersion } from "./library-version.ts";
4
5
 
5
- const require = createRequire(import.meta.url);
6
-
7
6
  export const COMMENT_REG_EXP = /\/\*.*\*\//g;
8
7
  export const PARAM_REG_EXP = /[0-9a-zA-Z_]*:/g;
9
8
  export const OPT_PARAM_REG_EXP = /[0-9a-zA-Z_]*\?:/g;
10
9
  export const NEW_LINE_REG_EXP = /[\n\r]+/g;
11
10
 
12
11
  /**
13
- * Package information interface for workspace root package.json
12
+ * Package information interface for package.json
14
13
  */
15
- interface WorkspacePackage {
14
+ interface Package {
16
15
  name: string;
17
16
  version: string;
18
17
  description: string;
@@ -22,44 +21,43 @@ interface WorkspacePackage {
22
21
  }
23
22
 
24
23
  /**
25
- * Resolves the workspace root package.json path
26
- * Uses require.resolve to find the correct path regardless of execution context
24
+ * Resolves the current package's package.json path
25
+ * Uses import.meta.url for ES Module compatibility
26
+ * Works both in workspace and after publishing
27
27
  */
28
- function resolveWorkspacePackageJson(): string {
28
+ function resolvePackageJson(): string {
29
29
  try {
30
- // Try to resolve from the workspace root by going up from this package
31
- // @ts-for-gir/lib -> ts-for-gir root
32
- return require.resolve("../../../package.json");
33
- } catch {
34
- // Fallback: try to resolve from current package's package.json location
35
- try {
36
- const currentPackageJson = require.resolve("@ts-for-gir/lib/package.json");
37
- return join(dirname(dirname(currentPackageJson)), "package.json");
38
- } catch {
39
- throw new Error("Unable to resolve workspace package.json path");
40
- }
30
+ // Get the directory of the current module
31
+ const currentModulePath = fileURLToPath(import.meta.url);
32
+ const currentDir = dirname(currentModulePath);
33
+
34
+ // Go up to the package root (src/ -> package root)
35
+ const packageRoot = join(currentDir, "..");
36
+ const packageJsonPath = join(packageRoot, "package.json");
37
+
38
+ return packageJsonPath;
39
+ } catch (error) {
40
+ throw new Error(`Unable to resolve package.json path: ${error instanceof Error ? error.message : "Unknown error"}`);
41
41
  }
42
42
  }
43
43
 
44
44
  /**
45
- * Reads and parses the workspace package.json file synchronously
46
- * Contains version and metadata shared across all workspace packages
45
+ * Reads and parses the current package's package.json file synchronously
46
+ * Contains version and metadata for this specific package
47
47
  */
48
- function readWorkspacePackageSync(): WorkspacePackage {
48
+ function readPackageSync(): Package {
49
49
  try {
50
- const packagePath = resolveWorkspacePackageJson();
51
- // Use dynamic import to avoid top-level await in constants
52
- const { readFileSync } = require("node:fs");
50
+ const packagePath = resolvePackageJson();
53
51
  const content = readFileSync(packagePath, "utf-8");
54
- return JSON.parse(content) as WorkspacePackage;
52
+ return JSON.parse(content) as Package;
55
53
  } catch (error) {
56
54
  const message = error instanceof Error ? error.message : "Unknown error";
57
- throw new Error(`Failed to read workspace package.json: ${message}`);
55
+ throw new Error(`Failed to read package.json: ${message}`);
58
56
  }
59
57
  }
60
58
 
61
59
  // Read package information once at module load
62
- export const PACKAGE = readWorkspacePackageSync();
60
+ export const PACKAGE = readPackageSync();
63
61
 
64
62
  export const APP_NAME = "ts-for-gir";
65
63
  export const APP_USAGE = "TypeScript type definition generator for GObject introspection GIR files";