@kadi.build/core 0.0.1-alpha.1 → 0.0.1-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.
Files changed (2) hide show
  1. package/index.js +47 -35
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -5,10 +5,6 @@ import { fileURLToPath } from 'url';
5
5
  import { exec, execSync, spawn } from 'child_process';
6
6
  import { Broker, BrokerMessageBuilder } from './broker.js';
7
7
  import { IPCManager, IPCMessageBuilder } from './ipc.js';
8
- import { createRequire } from 'module'; // Import createRequire to use require in ES Modules
9
-
10
- // Create a require function scoped to the current module
11
- const require = createRequire(import.meta.url);
12
8
 
13
9
  function resolveKadiExecPath() {
14
10
  const command = process.platform === 'win32' ? 'where kadi' : 'which kadi';
@@ -27,44 +23,56 @@ function resolveKadiExecPath() {
27
23
  }
28
24
 
29
25
  function resolveKadiInstallPath() {
26
+ /* 1 - production / normal install -------------------------------- */
30
27
  try {
31
- // Step 1: Get the path to the kadi binary
32
- const command = process.platform === 'win32' ? 'where kadi' : 'which kadi';
33
- const kadiBinaryPath = execSync(command, { encoding: 'utf8' }).trim();
28
+ if (typeof import.meta.resolve === 'function') {
29
+ const p = import.meta.resolve('@kadi.build/cli/package.json');
30
+ if (p) return path.dirname(new URL(p).pathname);
31
+ }
32
+ } catch {
33
+ /* ignore – fall through */
34
+ }
34
35
 
36
+ /* 2 - legacy strategy – follow the kadi executable --------------- */
37
+ try {
38
+ const cmd = process.platform === 'win32' ? 'where kadi' : 'which kadi';
39
+ const kadiBinaryPath = execSync(cmd, { encoding: 'utf8' }).trim();
35
40
  if (!kadiBinaryPath) {
36
41
  throw new Error('Kadi CLI tool not found in the system PATH');
37
42
  }
38
- // console.log('Resolved Kadi binary path:', kadiBinaryPath);
39
-
40
- // Step 2: Resolve the symlink to find the actual location of the binary
41
- const resolvedBinaryPath = fs.realpathSync(kadiBinaryPath);
42
- // console.log('Resolved binary real path:', resolvedBinaryPath);
43
+ const realKadiBinaryPath = fs.realpathSync(kadiBinaryPath); // resolve symlink
44
+ let kadiDir = path.dirname(realKadiBinaryPath); // start alongside the binary
43
45
 
44
- // Step 3: Correctly determine the root directory of the kadi package
45
- const kadiDir = path.dirname(resolvedBinaryPath); // Move up one level to get the kadi directory
46
- // console.log('Kadi directory:', kadiDir);
47
-
48
- // Step 4: Verify if this directory contains the kadi package.json
49
- const packageJsonPath = path.join(kadiDir, 'package.json');
50
- // console.log('Checking for package.json at:', packageJsonPath);
51
-
52
- if (fs.existsSync(packageJsonPath)) {
53
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
54
-
55
- if (packageJson.name === 'kadi') {
56
- // console.log('Kadi package.json found:', packageJsonPath);
46
+ const kadiPackageJsonPath = path.join(kadiDir, 'package.json');
47
+ if (fs.existsSync(kadiPackageJsonPath)) {
48
+ const kadiPackageJson = JSON.parse(
49
+ fs.readFileSync(kadiPackageJsonPath, 'utf8')
50
+ );
51
+ if (
52
+ kadiPackageJson.name === '@kadi.build/cli' ||
53
+ (kadiPackageJson.bin && kadiPackageJson.bin.kadi)
54
+ ) {
57
55
  return kadiDir;
58
56
  } else {
59
- throw new Error('Package.json found, but it is not for kadi.');
57
+ throw new Error(
58
+ 'Package.json found, but it is not for @kadi.build/cli'
59
+ );
60
60
  }
61
61
  } else {
62
- throw new Error('Kadi package.json not found in the resolved path.');
62
+ throw new Error(
63
+ '@kadi.build/cli package.json not found in the resolved path.'
64
+ );
63
65
  }
64
- } catch (error) {
65
- console.error('Error resolving Kadi CLI install path:', error);
66
- return null;
66
+ } catch {
67
+ /* ignore fall through */
67
68
  }
69
+
70
+ /* 3 - give up ----------------------------------------------------- */
71
+ throw new Error(
72
+ 'Could not locate @kadi.build/cli installation. ' +
73
+ 'Make sure the CLI is installed (`npm i -g @kadi.build/cli`) ' +
74
+ 'or run commands from inside the CLI repository during development.'
75
+ );
68
76
  }
69
77
 
70
78
  const __filename = fileURLToPath(import.meta.url);
@@ -194,16 +202,20 @@ export const GET_API_URL = kadijson.api + '/get';
194
202
 
195
203
  // Export all broker URLs
196
204
  const brokers = {};
197
- for (const [name, url] of Object.entries(kadijson.brokers)) {
198
- const brokerUrl = new URL(url);
199
- brokers[name] =
200
- `${brokerUrl.hostname}:${brokerUrl.port}${brokerUrl.pathname}`;
205
+ if (kadijson.brokers && typeof kadijson.brokers === 'object') {
206
+ for (const [name, url] of Object.entries(kadijson.brokers)) {
207
+ const brokerUrl = new URL(url);
208
+ brokers[name] =
209
+ `${brokerUrl.hostname}:${brokerUrl.port}${brokerUrl.pathname}`;
210
+ }
201
211
  }
202
212
 
203
213
  export const KADI_BROKERS = brokers;
204
214
 
205
215
  // Get default broker (first one defined)
206
- const defaultBrokerName = Object.keys(kadijson.brokers)[0];
216
+ const defaultBrokerName = kadijson.brokers
217
+ ? Object.keys(kadijson.brokers)[0]
218
+ : null;
207
219
  export const KADI_BROKER_URL = brokers[defaultBrokerName]; // Backward compatibility
208
220
 
209
221
  // Utility functions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kadi.build/core",
3
- "version": "0.0.1-alpha.1",
3
+ "version": "0.0.1-alpha.2",
4
4
  "description": "A module that is a comprehensive toolkit for developers integrating with the KADI infrastructure.",
5
5
  "main": "index.js",
6
6
  "type": "module",