@rpcbase/cli 0.79.0 → 0.80.0

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": "@rpcbase/cli",
3
- "version": "0.79.0",
3
+ "version": "0.80.0",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
package/src/cmd-deploy.js CHANGED
@@ -1,4 +1,4 @@
1
- import { execFileSync, execSync } from "child_process";
1
+ import { execSync } from "child_process";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
4
  import os from "os";
package/src/cmd-ssh.js ADDED
@@ -0,0 +1,91 @@
1
+ import { execSync } from "child_process";
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import os from "os";
5
+ import validator from "validator";
6
+
7
+ export const ssh = async (argv) => {
8
+ const log = (message) => {
9
+ if (argv.verbose) console.log(message);
10
+ }
11
+
12
+ const {
13
+ RB_DEPLOY_HOST,
14
+ RB_DEPLOY_USER,
15
+ RB_DEPLOY_KEY,
16
+ } = argv.env || {};
17
+
18
+ if (
19
+ !RB_DEPLOY_HOST ||
20
+ !validator.isURL(RB_DEPLOY_HOST, { require_tld: false })
21
+ ) {
22
+ throw new Error(
23
+ "Missing or invalid required environment variable: RB_DEPLOY_HOST",
24
+ );
25
+ }
26
+
27
+ if (!RB_DEPLOY_USER || !validator.isLength(RB_DEPLOY_USER, { min: 1 })) {
28
+ throw new Error(
29
+ "Missing or invalid required environment variable: RB_DEPLOY_USER",
30
+ );
31
+ }
32
+
33
+ if (!RB_DEPLOY_KEY || !validator.isLength(RB_DEPLOY_KEY, { min: 1 })) {
34
+ throw new Error(
35
+ "Missing or invalid required environment variable: RB_DEPLOY_KEY",
36
+ );
37
+ }
38
+
39
+ const host = RB_DEPLOY_HOST;
40
+ const user = RB_DEPLOY_USER;
41
+ let keyPath = RB_DEPLOY_KEY;
42
+
43
+ let tempKeyPath = null;
44
+
45
+ const execSsh = async (command, stdioInherit = true) => {
46
+ const sshCommand = `ssh -i "${keyPath}" -o StrictHostKeyChecking=no ${user}@${host} "${command}"`;
47
+ log(`${command}`);
48
+ const out = execSync(sshCommand, {
49
+ stdio: (argv.verbose || stdioInherit) ? "inherit" : "pipe",
50
+ })
51
+ const res = out?.toString()?.trim() || ""
52
+ return res
53
+ };
54
+
55
+ try {
56
+ if (keyPath.startsWith("~")) keyPath = keyPath.replace("~", os.homedir());
57
+
58
+ if (!fs.existsSync(keyPath)) {
59
+ // try to treat RB_DEPLOY_KEY as base64-encoded key content
60
+ const keyContent = Buffer.from(RB_DEPLOY_KEY, "base64").toString("utf-8");
61
+ if (!keyContent.includes("-----BEGIN")) {
62
+ throw new Error(
63
+ "Decoded content does not appear to be a valid SSH key",
64
+ );
65
+ }
66
+ tempKeyPath = path.join(os.tmpdir(), `deploy-key-${Date.now()}`);
67
+ fs.writeFileSync(tempKeyPath, keyContent);
68
+ fs.chmodSync(tempKeyPath, "400");
69
+ keyPath = tempKeyPath;
70
+ }
71
+
72
+ await execSsh("echo connection-ok", false);
73
+
74
+ await execSsh(argv.command, true)
75
+
76
+ } catch (error) {
77
+ console.error(
78
+ `Command failed: ${error instanceof Error ? error.message : error}`,
79
+ );
80
+ throw error;
81
+ } finally {
82
+ if (tempKeyPath) {
83
+ try {
84
+ fs.unlinkSync(tempKeyPath);
85
+ log(`Removed temporary key file: ${tempKeyPath}`);
86
+ } catch (cleanupError) {
87
+ console.warn(`Failed to remove temporary key file: ${cleanupError}`);
88
+ }
89
+ }
90
+ }
91
+ };
package/src/index.js CHANGED
@@ -5,12 +5,14 @@ import { hideBin } from 'yargs/helpers'
5
5
  import dotenv from 'dotenv'
6
6
  import fs from 'fs'
7
7
  import {deploy} from "./cmd-deploy.js"
8
+ import {ssh} from "./cmd-ssh.js"
8
9
  import {waitFor} from "./cmd-wait-for/index.js"
9
10
 
10
11
  yargs(hideBin(process.argv))
11
12
  .option("env", {
12
13
  describe: "Path to environment file",
13
14
  type: "array",
15
+ nargs: 1,
14
16
  global: true,
15
17
  default: ["process.env"],
16
18
  coerce: (envFiles) => {
@@ -69,6 +71,25 @@ yargs(hideBin(process.argv))
69
71
  await deploy(argv)
70
72
  },
71
73
  )
74
+ .command(
75
+ "ssh <command>",
76
+ "Run ssh command",
77
+ (yargs) => {
78
+ return yargs
79
+ .positional("command", {
80
+ describe: "Command to run on the remote server",
81
+ type: "string",
82
+ })
83
+ .option("verbose", {
84
+ alias: "v",
85
+ describe: "Run with verbose logging",
86
+ type: "boolean",
87
+ })
88
+ },
89
+ async(argv) => {
90
+ await ssh(argv)
91
+ },
92
+ )
72
93
  .command("wait-for", "Waits for the specified service to become available", () => {}, (argv) => {
73
94
  waitFor()
74
95
  })