rol-websocket-channel 1.3.6 → 1.3.7

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.
@@ -1,12 +1,14 @@
1
+ import fs from 'node:fs';
1
2
  import path from 'node:path';
2
3
  import { fileURLToPath } from 'node:url';
3
4
  import { JsonRpcException, JSON_RPC_ERRORS } from '../jsonrpc.js';
4
5
  const __filename = fileURLToPath(import.meta.url);
5
6
  const __dirname = path.dirname(__filename);
6
7
  export function getProjectRoot() {
7
- // __dirname /path/to/rol-websocket-channel/src/admin/lib
8
- // 需要返回 /path/to/rol-websocket-channel
9
- const root = path.resolve(__dirname, '..', '..', '..');
8
+ let root = path.resolve(__dirname, '..', '..', '..');
9
+ if (path.basename(root) === 'dist') {
10
+ root = path.dirname(root);
11
+ }
10
12
  console.log('[paths] __dirname:', __dirname);
11
13
  console.log('[paths] getProjectRoot:', root);
12
14
  return root;
@@ -16,26 +18,58 @@ export function getOpenClawRoot() {
16
18
  console.log('[paths] getOpenClawRoot from env:', process.env.OPENCLAW_HOME);
17
19
  return path.resolve(process.env.OPENCLAW_HOME);
18
20
  }
19
- // 插件在 ~/.openclaw/extensions/rol-websocket-channel
20
- // 需要返回 ~/.openclaw
21
21
  const projectRoot = getProjectRoot();
22
+ const configuredRoot = findNearestOpenClawConfigRoot(projectRoot);
23
+ if (configuredRoot) {
24
+ console.log('[paths] getOpenClawRoot from discovered openclaw.json:', configuredRoot);
25
+ return configuredRoot;
26
+ }
27
+ const openclawDir = findAncestorNamed(projectRoot, '.openclaw');
28
+ if (openclawDir) {
29
+ console.log('[paths] getOpenClawRoot from .openclaw ancestor:', openclawDir);
30
+ return openclawDir;
31
+ }
22
32
  const parentDir = path.dirname(projectRoot);
23
33
  const parentName = path.basename(parentDir);
24
34
  console.log('[paths] projectRoot:', projectRoot);
25
35
  console.log('[paths] parentDir:', parentDir);
26
36
  console.log('[paths] parentName:', parentName);
27
37
  if (parentName === 'extensions') {
28
- // parentDir 是 ~/.openclaw/extensions
29
- // 返回 ~/.openclaw
30
38
  const openclawRoot = path.dirname(parentDir);
31
39
  console.log('[paths] getOpenClawRoot (extensions):', openclawRoot);
32
40
  return openclawRoot;
33
41
  }
34
- // 兜底:假设插件在 openclaw 根目录的子目录
35
42
  const fallbackRoot = path.resolve(projectRoot, '..');
36
43
  console.log('[paths] getOpenClawRoot (fallback):', fallbackRoot);
37
44
  return fallbackRoot;
38
45
  }
46
+ function findNearestOpenClawConfigRoot(startDir) {
47
+ for (const dir of walkAncestors(startDir)) {
48
+ if (fs.existsSync(path.join(dir, 'openclaw.json'))) {
49
+ return dir;
50
+ }
51
+ }
52
+ return null;
53
+ }
54
+ function findAncestorNamed(startDir, name) {
55
+ for (const dir of walkAncestors(startDir)) {
56
+ if (path.basename(dir) === name) {
57
+ return dir;
58
+ }
59
+ }
60
+ return null;
61
+ }
62
+ function* walkAncestors(startDir) {
63
+ let current = path.resolve(startDir);
64
+ while (true) {
65
+ yield current;
66
+ const parent = path.dirname(current);
67
+ if (parent === current) {
68
+ return;
69
+ }
70
+ current = parent;
71
+ }
72
+ }
39
73
  export function ensureInside(parentDir, targetPath) {
40
74
  const parent = path.resolve(parentDir);
41
75
  const target = path.resolve(targetPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rol-websocket-channel",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "description": "Unified OpenClaw plugin: MQTT Channel + Admin Bridge for remote management",
5
5
  "license": "MIT",
6
6
  "author": "nixgnehc",
@@ -1,3 +1,4 @@
1
+ import fs from 'node:fs';
1
2
  import path from 'node:path';
2
3
  import { fileURLToPath } from 'node:url';
3
4
 
@@ -7,9 +8,11 @@ const __filename = fileURLToPath(import.meta.url);
7
8
  const __dirname = path.dirname(__filename);
8
9
 
9
10
  export function getProjectRoot(): string {
10
- // __dirname /path/to/rol-websocket-channel/src/admin/lib
11
- // 需要返回 /path/to/rol-websocket-channel
12
- const root = path.resolve(__dirname, '..', '..', '..');
11
+ let root = path.resolve(__dirname, '..', '..', '..');
12
+ if (path.basename(root) === 'dist') {
13
+ root = path.dirname(root);
14
+ }
15
+
13
16
  console.log('[paths] __dirname:', __dirname);
14
17
  console.log('[paths] getProjectRoot:', root);
15
18
  return root;
@@ -21,9 +24,19 @@ export function getOpenClawRoot(): string {
21
24
  return path.resolve(process.env.OPENCLAW_HOME);
22
25
  }
23
26
 
24
- // 插件在 ~/.openclaw/extensions/rol-websocket-channel
25
- // 需要返回 ~/.openclaw
26
27
  const projectRoot = getProjectRoot();
28
+ const configuredRoot = findNearestOpenClawConfigRoot(projectRoot);
29
+ if (configuredRoot) {
30
+ console.log('[paths] getOpenClawRoot from discovered openclaw.json:', configuredRoot);
31
+ return configuredRoot;
32
+ }
33
+
34
+ const openclawDir = findAncestorNamed(projectRoot, '.openclaw');
35
+ if (openclawDir) {
36
+ console.log('[paths] getOpenClawRoot from .openclaw ancestor:', openclawDir);
37
+ return openclawDir;
38
+ }
39
+
27
40
  const parentDir = path.dirname(projectRoot);
28
41
  const parentName = path.basename(parentDir);
29
42
 
@@ -32,19 +45,48 @@ export function getOpenClawRoot(): string {
32
45
  console.log('[paths] parentName:', parentName);
33
46
 
34
47
  if (parentName === 'extensions') {
35
- // parentDir 是 ~/.openclaw/extensions
36
- // 返回 ~/.openclaw
37
48
  const openclawRoot = path.dirname(parentDir);
38
49
  console.log('[paths] getOpenClawRoot (extensions):', openclawRoot);
39
50
  return openclawRoot;
40
51
  }
41
52
 
42
- // 兜底:假设插件在 openclaw 根目录的子目录
43
53
  const fallbackRoot = path.resolve(projectRoot, '..');
44
54
  console.log('[paths] getOpenClawRoot (fallback):', fallbackRoot);
45
55
  return fallbackRoot;
46
56
  }
47
57
 
58
+ function findNearestOpenClawConfigRoot(startDir: string): string | null {
59
+ for (const dir of walkAncestors(startDir)) {
60
+ if (fs.existsSync(path.join(dir, 'openclaw.json'))) {
61
+ return dir;
62
+ }
63
+ }
64
+
65
+ return null;
66
+ }
67
+
68
+ function findAncestorNamed(startDir: string, name: string): string | null {
69
+ for (const dir of walkAncestors(startDir)) {
70
+ if (path.basename(dir) === name) {
71
+ return dir;
72
+ }
73
+ }
74
+
75
+ return null;
76
+ }
77
+
78
+ function* walkAncestors(startDir: string): Generator<string> {
79
+ let current = path.resolve(startDir);
80
+ while (true) {
81
+ yield current;
82
+ const parent = path.dirname(current);
83
+ if (parent === current) {
84
+ return;
85
+ }
86
+ current = parent;
87
+ }
88
+ }
89
+
48
90
  export function ensureInside(parentDir: string, targetPath: string): string {
49
91
  const parent = path.resolve(parentDir);
50
92
  const target = path.resolve(targetPath);