zalo-agent-cli 1.0.3 → 1.0.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zalo-agent-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "CLI tool for Zalo automation — multi-account, proxy support, bank transfers, QR payments",
5
5
  "type": "module",
6
6
  "bin": {
@@ -17,7 +17,8 @@
17
17
  "lint": "eslint src/",
18
18
  "lint:fix": "eslint src/ --fix",
19
19
  "format": "prettier --write src/",
20
- "format:check": "prettier --check src/"
20
+ "format:check": "prettier --check src/",
21
+ "release": "npm version patch -m 'release: v%s' && git push origin main --tags"
21
22
  },
22
23
  "keywords": [
23
24
  "zalo",
package/src/index.js CHANGED
@@ -5,21 +5,29 @@
5
5
  * Entry point: registers all command groups via Commander.js.
6
6
  */
7
7
 
8
+ import { readFileSync } from "node:fs";
9
+ import { fileURLToPath } from "node:url";
10
+ import { dirname, join } from "node:path";
8
11
  import { Command } from "commander";
9
12
  import { registerLoginCommands } from "./commands/login.js";
13
+
14
+ const __dirname = dirname(fileURLToPath(import.meta.url));
15
+ const pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf8"));
10
16
  import { registerMsgCommands } from "./commands/msg.js";
11
17
  import { registerFriendCommands } from "./commands/friend.js";
12
18
  import { registerGroupCommands } from "./commands/group.js";
13
19
  import { registerConvCommands } from "./commands/conv.js";
14
20
  import { registerAccountCommands } from "./commands/account.js";
15
21
  import { autoLogin } from "./core/zalo-client.js";
22
+ import { checkForUpdates, selfUpdate } from "./utils/update-check.js";
23
+ import { success, error } from "./utils/output.js";
16
24
 
17
25
  const program = new Command();
18
26
 
19
27
  program
20
28
  .name("zalo-agent")
21
29
  .description("CLI tool for Zalo automation — multi-account, proxy, bank transfers, QR payments")
22
- .version("1.0.0")
30
+ .version(pkg.version)
23
31
  .option("--json", "Output results as JSON (machine-readable)")
24
32
  .hook("preAction", async (thisCommand) => {
25
33
  // Suppress zca-js internal logs in JSON mode to keep stdout clean for piping
@@ -28,10 +36,24 @@ program
28
36
  }
29
37
  // Auto-login before any command that needs it (skip for login/account commands)
30
38
  const cmdName = thisCommand.args?.[0] || thisCommand.name();
31
- const skipAutoLogin = ["login", "account", "help", "version"].includes(cmdName);
39
+ const skipAutoLogin = ["login", "account", "help", "version", "update"].includes(cmdName);
32
40
  if (!skipAutoLogin) {
33
41
  await autoLogin(program.opts().json);
34
42
  }
43
+ // Non-blocking update check (skip for update command itself)
44
+ if (cmdName !== "update") {
45
+ checkForUpdates(pkg.version, program.opts().json);
46
+ }
47
+ });
48
+
49
+ // Self-update command
50
+ program
51
+ .command("update")
52
+ .description("Update zalo-agent-cli to the latest version")
53
+ .action(() => {
54
+ const ok = selfUpdate();
55
+ if (ok) success(`Updated to latest version`);
56
+ else error("Update failed. Try manually: npm install -g zalo-agent-cli@latest");
35
57
  });
36
58
 
37
59
  // Register all command groups
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Non-blocking update check — warns user if a newer version is available on npm.
3
+ * Runs silently in background; never blocks CLI execution.
4
+ */
5
+
6
+ import { execSync } from "node:child_process";
7
+ import { warning } from "./output.js";
8
+
9
+ /**
10
+ * Check npm registry for latest version, warn if outdated.
11
+ * @param {string} currentVersion - Current package version
12
+ * @param {boolean} jsonMode - Suppress output in JSON mode
13
+ */
14
+ export function checkForUpdates(currentVersion, jsonMode) {
15
+ if (jsonMode) return;
16
+
17
+ try {
18
+ const latest = execSync("npm view zalo-agent-cli version", {
19
+ encoding: "utf8",
20
+ timeout: 5000,
21
+ stdio: ["pipe", "pipe", "pipe"],
22
+ }).trim();
23
+
24
+ if (latest && latest !== currentVersion) {
25
+ warning(`Update available: ${currentVersion} → ${latest}. Run: zalo-agent update`);
26
+ }
27
+ } catch {
28
+ // Silent failure — network issues shouldn't block CLI usage
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Self-update by running npm install -g.
34
+ * @returns {boolean} success
35
+ */
36
+ export function selfUpdate() {
37
+ try {
38
+ execSync("npm install -g zalo-agent-cli@latest", {
39
+ encoding: "utf8",
40
+ stdio: "inherit",
41
+ timeout: 60000,
42
+ });
43
+ return true;
44
+ } catch {
45
+ return false;
46
+ }
47
+ }