@lazycatcloud/lzc-cli 1.2.26 → 1.2.28

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,59 +1,63 @@
1
- import { spawn, spawnSync } from "child_process";
2
- import fs from "node:fs";
3
- import shellApi from "../shellapi.js";
4
- import inquirer from "inquirer";
5
- import { isDebugMode } from "../utils.js";
1
+ import { spawn, spawnSync } from "child_process"
2
+ import fs from "node:fs"
3
+ import shellApi from "../shellapi.js"
4
+ import inquirer from "inquirer"
5
+ import { isDebugMode, resolveDomain } from "../utils.js"
6
6
 
7
7
  export class DebugBridge {
8
8
  constructor() {
9
- this.uid = shellApi.uid;
10
- this.boxname = shellApi.boxname;
11
- this.sshCmd = `ssh ${isDebugMode() ? "-vv" : ""} -p 22222 box@dev.${
12
- this.boxname
13
- }.heiyu.space`;
9
+ this.uid = shellApi.uid
10
+ this.boxname = shellApi.boxname
11
+ this.domain = `dev.${this.boxname}.heiyu.space`
12
+ this.sshCmd = `ssh ${isDebugMode() ? "-vv" : ""} -p 22222 box@${this.domain}`
14
13
  }
15
14
 
16
- common(cmd, args) {
17
- const ssh = spawnSync(cmd, args, {
15
+ async common(cmd, args) {
16
+ const resolvedIp = await resolveDomain(this.domain)
17
+ const sshCmd = cmd.replace(this.domain, resolvedIp)
18
+ const ssh = spawnSync(sshCmd, args, {
18
19
  shell: true,
19
20
  encoding: "utf-8",
20
- stdio: ["pipe", "pipe", "inherit"],
21
- });
21
+ stdio: ["pipe", "pipe", "inherit"]
22
+ })
22
23
  return new Promise((resolve, reject) => {
23
24
  ssh.status == 0
24
25
  ? resolve(ssh.stdout)
25
- : reject(`执行命令 ${cmd} ${args.join(" ")} 出错\n${ssh.stderr ?? ""}`);
26
- });
26
+ : reject(
27
+ `执行命令 ${sshCmd} ${args.join(" ")} 出错\n${ssh.stderr ?? ""}`
28
+ )
29
+ })
27
30
  }
28
31
 
29
32
  async install(lpkPath, pkgId) {
30
33
  if (!(await this.canPublicKey())) {
31
- await this.sshCopyId();
34
+ await this.sshCopyId()
32
35
  }
33
36
 
34
- const stream = fs.createReadStream(lpkPath);
37
+ const stream = fs.createReadStream(lpkPath)
38
+ const resolvedIp = await resolveDomain(this.domain)
35
39
  const ssh = spawn(
36
- this.sshCmd,
40
+ `ssh -p 22222 box@${resolvedIp}`,
37
41
  [`install --uid ${this.uid} --pkgId ${pkgId}`],
38
42
  {
39
43
  shell: true,
40
- stdio: ["pipe", "inherit", "inherit"],
44
+ stdio: ["pipe", "inherit", "inherit"]
41
45
  }
42
- );
43
- stream.pipe(ssh.stdin);
46
+ )
47
+ stream.pipe(ssh.stdin)
44
48
  return new Promise((resolve, reject) => {
45
49
  ssh.on("close", (code) => {
46
- code == 0 ? resolve() : reject("install 失败");
47
- });
48
- });
50
+ code == 0 ? resolve() : reject("install 失败")
51
+ })
52
+ })
49
53
  }
50
54
 
51
55
  async canPublicKey() {
52
56
  try {
53
- await this.common(this.sshCmd, [`-o PasswordAuthentication=no`]);
54
- return true;
57
+ await this.common(this.sshCmd, [`-o PasswordAuthentication=no`])
58
+ return true
55
59
  } catch {
56
- return false;
60
+ return false
57
61
  }
58
62
  }
59
63
 
@@ -64,79 +68,84 @@ export class DebugBridge {
64
68
  type: "input",
65
69
  default: "y",
66
70
  message:
67
- "检测到你目前使用的密码帐号登录,是否使用 ssh-copy-id 上传密钥 (y/n): ",
68
- },
69
- ];
70
- const answers = await inquirer.prompt(questions);
71
+ "检测到你目前使用的密码帐号登录,是否使用 ssh-copy-id 上传密钥 (y/n): "
72
+ }
73
+ ]
74
+ const answers = await inquirer.prompt(questions)
71
75
  if (answers.upload.toLowerCase() == "y") {
72
- return this.common(`ssh-copy-id`, [
73
- `-f -p 22222 box@dev.${this.boxname}.heiyu.space`,
74
- ]);
76
+ const resolvedIp = await resolveDomain(this.domain)
77
+ return this.common(`ssh-copy-id`, [`-f -p 22222 box@${resolvedIp}`])
75
78
  }
76
79
  }
77
80
 
78
81
  async status(appId) {
79
- return this.common(this.sshCmd, [`status --uid ${this.uid}`, appId]);
82
+ return this.common(this.sshCmd, [`status --uid ${this.uid}`, appId])
80
83
  }
81
84
 
82
85
  async isDevshell(appId) {
83
86
  const stdout = await this.common(this.sshCmd, [
84
87
  `isDevshell --uid ${this.uid}`,
85
- appId,
86
- ]);
87
- return stdout == "true";
88
+ appId
89
+ ])
90
+ return stdout == "true"
88
91
  }
89
92
 
90
93
  async resume(appId) {
91
- return this.common(this.sshCmd, [`resume --uid ${this.uid}`, appId]);
94
+ return this.common(this.sshCmd, [`resume --uid ${this.uid}`, appId])
92
95
  }
93
96
 
94
97
  async uninstall(appId) {
95
- return this.common(this.sshCmd, [`uninstall --uid ${this.uid}`, appId]);
98
+ return this.common(this.sshCmd, [`uninstall --uid ${this.uid}`, appId])
96
99
  }
97
100
 
98
101
  async devshell(appId, isUserApp, onconnect = null) {
102
+ const resolvedIp = await resolveDomain(this.domain)
99
103
  const stream = spawn(
100
- this.sshCmd,
104
+ `ssh -p 22222 box@${resolvedIp}`,
101
105
  [
102
106
  "-t",
103
107
  "devshell",
104
108
  `--uid ${this.uid}`,
105
109
  isUserApp ? "--userapp" : "",
106
110
  appId,
107
- "/lzcapp/pkg/content/devshell/exec.sh",
111
+ "/lzcapp/pkg/content/devshell/exec.sh"
108
112
  ],
109
113
  {
110
114
  shell: true,
111
- stdio: "inherit",
115
+ stdio: "inherit"
112
116
  }
113
- );
117
+ )
114
118
  return new Promise((resolve, reject) => {
115
119
  stream.on("close", (code) => {
116
- code == 0 ? resolve() : reject();
117
- });
120
+ code == 0 ? resolve() : reject()
121
+ })
118
122
  if (onconnect) {
119
- stream.on("spawn", onconnect);
123
+ stream.on("spawn", onconnect)
120
124
  }
121
- });
125
+ })
122
126
  }
123
127
 
124
128
  async buildImage(label, contextTar) {
125
- const tag = `debug.bridge/${label}`;
126
- const stream = fs.createReadStream(contextTar);
127
- const ssh = spawn(this.sshCmd, [`build --tag ${tag}`], {
128
- shell: true,
129
- stdio: ["pipe", "inherit", "inherit"],
130
- });
131
- stream.pipe(ssh.stdin);
129
+ const tag = `debug.bridge/${label}`
130
+ const resolvedIp = await resolveDomain(this.domain)
131
+ const stream = fs.createReadStream(contextTar)
132
+ const ssh = spawn(
133
+ `ssh -p 22222 box@${resolvedIp}`,
134
+ [`build --tag ${tag}`],
135
+ {
136
+ shell: true,
137
+ stdio: ["pipe", "inherit", "inherit"]
138
+ }
139
+ )
140
+ stream.pipe(ssh.stdin)
132
141
  return new Promise((resolve, reject) => {
133
142
  ssh.on("close", (code) => {
134
143
  code == 0
135
144
  ? resolve(`dev.${this.boxname}.heiyu.space/${tag}`)
136
- : reject("在盒子中构建 image 失败");
137
- });
145
+ : reject("在盒子中构建 image 失败")
146
+ })
138
147
  }).finally(() => {
139
- fs.rmSync(contextTar);
140
- });
148
+ fs.rmSync(contextTar)
149
+ })
141
150
  }
142
151
  }