File size: 1,499 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import path from "node:path";

export const DEFAULT_CLI_NAME = "openclaw";
export const ALT_CLI_NAME = "openskynet";

const KNOWN_CLI_NAMES = new Set([DEFAULT_CLI_NAME, ALT_CLI_NAME]);
const CLI_PREFIX_RE = /^(?:((?:pnpm|npm|bunx|npx)\s+))?(openclaw|openskynet)\b/;

export function resolveCliName(argv: string[] = process.argv): string {
  if (process.env.OPENSKYNET_MODE === "1" || process.env.OPENSKYNET_STATE_DIR) {
    return ALT_CLI_NAME;
  }
  
  const argv1 = argv[1];
  if (!argv1) {
    try {
      if (process.cwd().includes("openskynet")) return ALT_CLI_NAME;
    } catch {}
    return DEFAULT_CLI_NAME;
  }
  
  const base = path.basename(argv1).trim();
  const name = base.replace(/\.(?:mjs|[mc]?js)$/, "");
  if (KNOWN_CLI_NAMES.has(name)) {
    return name;
  }
  
  // Smart detection for development scripts (e.g. npx tsx test.ts)
  try {
    if (process.cwd().includes("openskynet") || argv1.includes("openskynet")) {
      return ALT_CLI_NAME;
    }
  } catch {}

  return DEFAULT_CLI_NAME;
}

export function replaceCliName(command: string, cliName = resolveCliName()): string {
  if (!command.trim()) {
    return command;
  }
  if (!CLI_PREFIX_RE.test(command)) {
    return command;
  }
  return command.replace(CLI_PREFIX_RE, (_match, runner: string | undefined) => {
    return `${runner ?? ""}${cliName}`;
  });
}

export function resolveCliDisplayName(argv: string[] = process.argv): string {
  return resolveCliName(argv) === ALT_CLI_NAME ? "OpenSkynet" : "OpenClaw";
}