@infinity-cli/ipublish 1.2.2 → 1.2.4

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,6 +1,11 @@
1
- import { Static } from '@sinclair/typebox';
2
1
  import { RuntimeConfig } from '@infinity-cli/shared';
2
+ import { Static } from '@sinclair/typebox';
3
3
  declare const TypeBox_PublishConfig: import("@sinclair/typebox").TObject<{
4
+ jumpIp: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
5
+ jumpUser: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
6
+ jumpLoginType: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"password">, import("@sinclair/typebox").TLiteral<"privateKey">]>>;
7
+ jumpPassword: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
8
+ jumpPrivateKey: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
4
9
  ip: import("@sinclair/typebox").TString;
5
10
  user: import("@sinclair/typebox").TString;
6
11
  loginType: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"password">, import("@sinclair/typebox").TLiteral<"privateKey">]>>;
@@ -13,9 +18,14 @@ declare const TypeBox_PublishConfig: import("@sinclair/typebox").TObject<{
13
18
  }>;
14
19
  export type Type_PublishConfig = Static<typeof TypeBox_PublishConfig>;
15
20
  export declare const runtimeConfig: RuntimeConfig<{
16
- loginType?: "password" | "privateKey" | undefined;
21
+ jumpIp?: string | undefined;
22
+ jumpUser?: string | undefined;
23
+ jumpLoginType?: "password" | "privateKey" | undefined;
17
24
  password?: string | undefined;
18
25
  privateKey?: string | undefined;
26
+ jumpPassword?: string | undefined;
27
+ jumpPrivateKey?: string | undefined;
28
+ loginType?: "password" | "privateKey" | undefined;
19
29
  cmd?: string | undefined;
20
30
  cwd?: string | undefined;
21
31
  ip: string;
@@ -4,10 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.runtimeConfig = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const typebox_1 = require("@sinclair/typebox");
9
7
  const shared_1 = require("@infinity-cli/shared");
8
+ const typebox_1 = require("@sinclair/typebox");
9
+ const path_1 = __importDefault(require("path"));
10
10
  const TypeBox_PublishConfig = typebox_1.Type.Object({
11
+ jumpIp: typebox_1.Type.Optional(typebox_1.Type.String()),
12
+ jumpUser: typebox_1.Type.Optional(typebox_1.Type.String()),
13
+ jumpLoginType: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.Literal('password'), typebox_1.Type.Literal('privateKey')])),
14
+ jumpPassword: typebox_1.Type.Optional(typebox_1.Type.String()),
15
+ jumpPrivateKey: typebox_1.Type.Optional(typebox_1.Type.String()),
11
16
  ip: typebox_1.Type.String(),
12
17
  user: typebox_1.Type.String(),
13
18
  loginType: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.Literal('password'), typebox_1.Type.Literal('privateKey')])),
@@ -13,48 +13,87 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.uploadRemoteAction = exports.uploadRemoteWithConfig = void 0;
16
- const config_1 = require("./config");
16
+ const shared_1 = require("@infinity-cli/shared");
17
17
  const node_ssh_1 = require("node-ssh");
18
+ const ora_1 = __importDefault(require("ora"));
18
19
  const path_1 = __importDefault(require("path"));
19
- const util_1 = require("../util");
20
- const shared_1 = require("@infinity-cli/shared");
21
20
  const rimraf_1 = __importDefault(require("rimraf"));
22
- const ora_1 = __importDefault(require("ora"));
21
+ const ssh2_1 = require("ssh2");
22
+ const util_1 = require("../util");
23
+ const config_1 = require("./config");
23
24
  const uploadRemoteWithConfig = (config) => __awaiter(void 0, void 0, void 0, function* () {
24
25
  let spinner;
25
- const { ip, user, loginType, password, privateKey, from, to, cmd, cwd } = config;
26
- if (to === '/') {
27
- throw new Error('to:不允许在服务器根目录操作');
26
+ const { jumpIp, jumpUser, jumpLoginType, jumpPassword, jumpPrivateKey, ip, user, loginType, password, privateKey, from, to, cmd, cwd, } = config;
27
+ if (['', '/'].includes(to.trim())) {
28
+ console.error((0, shared_1.red)('to:不允许在服务器根目录操作'));
29
+ process.exit(1);
28
30
  }
29
- spinner = (0, ora_1.default)(`打包文件`).start();
31
+ spinner = (0, ora_1.default)().start();
30
32
  const { zipPath, zipName, dir } = yield (0, util_1.zip)(from);
31
- spinner.succeed();
32
- const ssh = new node_ssh_1.NodeSSH();
33
- const connectConfig = {
34
- host: ip,
35
- username: user,
36
- password,
37
- privateKey,
38
- };
39
- if (password && privateKey) {
40
- if (loginType === 'password') {
41
- delete connectConfig.privateKey;
33
+ const targetSSH = new node_ssh_1.NodeSSH();
34
+ try {
35
+ const targetConfig = {
36
+ host: ip,
37
+ username: user,
38
+ password,
39
+ privateKey,
40
+ };
41
+ if (password && privateKey) {
42
+ if (loginType === 'password') {
43
+ delete targetConfig.privateKey;
44
+ }
45
+ else {
46
+ delete targetConfig.password;
47
+ }
48
+ }
49
+ console.log(`开始连接并上传:${ip} ${to}`);
50
+ if (jumpIp) {
51
+ spinner.text = '使用跳板机连接';
52
+ const jumpConfig = {
53
+ host: jumpIp,
54
+ username: jumpUser,
55
+ password: jumpPassword,
56
+ privateKey: jumpPrivateKey,
57
+ };
58
+ if (jumpPassword && jumpPrivateKey) {
59
+ if (jumpLoginType === 'password') {
60
+ delete jumpConfig.privateKey;
61
+ }
62
+ else {
63
+ delete jumpConfig.password;
64
+ }
65
+ }
66
+ const jumpClient = new ssh2_1.Client();
67
+ const stream = yield new Promise((resolve, reject) => {
68
+ jumpClient
69
+ .on('ready', () => {
70
+ spinner.text = '跳板机连接成功,尝试端口转发';
71
+ jumpClient.forwardOut('127.0.0.1', 0, ip, 22, (err, stream) => {
72
+ if (err) {
73
+ return reject(new Error(`端口转发失败: ${err.message}`));
74
+ }
75
+ resolve(stream);
76
+ });
77
+ })
78
+ .on('error', (err) => {
79
+ reject(new Error(`跳板机连接错误: ${err.message}`));
80
+ })
81
+ .connect(jumpConfig);
82
+ });
83
+ yield targetSSH.connect(Object.assign(Object.assign({}, targetConfig), { sock: stream }));
42
84
  }
43
85
  else {
44
- delete connectConfig.password;
86
+ spinner.text = '直接连接';
87
+ yield targetSSH.connect(targetConfig);
45
88
  }
46
- }
47
- spinner = (0, ora_1.default)(`连接并上传:${ip} ${to}`).start();
48
- try {
49
- yield ssh.connect(connectConfig);
50
- yield ssh.putFile(zipPath, path_1.default.join(to, zipName));
89
+ yield targetSSH.putFile(zipPath, path_1.default.join(to, zipName));
51
90
  (0, rimraf_1.default)(zipPath, () => { });
52
- spinner.succeed();
91
+ spinner.succeed('文件上传成功');
53
92
  }
54
93
  catch (error) {
55
94
  (0, rimraf_1.default)(zipPath, () => { });
56
- spinner.fail();
57
- throw error;
95
+ spinner.fail('文件上传失败');
96
+ process.exit(1);
58
97
  }
59
98
  function execCmd() {
60
99
  return __awaiter(this, void 0, void 0, function* () {
@@ -79,25 +118,26 @@ const uploadRemoteWithConfig = (config) => __awaiter(void 0, void 0, void 0, fun
79
118
  }
80
119
  }
81
120
  for (const cmd of cmds) {
82
- console.log(' ' + cmd);
83
- const result = yield ssh.execCommand(cmd, {
121
+ console.log(`执行远程命令:${cmd}`);
122
+ const result = yield targetSSH.execCommand(cmd, {
84
123
  cwd: cwd ? cwd : to,
85
124
  });
86
125
  result.stdout && console.log('STDOUT: ' + result.stdout);
87
- if (result.stderr) {
88
- console.error((0, shared_1.red)('STDERR: ' + result.stderr));
126
+ result.stderr && console.log('STDERR: ' + result.stderr);
127
+ if (result.code) {
128
+ console.error((0, shared_1.red)('Command failed with exit code: ' + result.code));
129
+ process.exit(1);
89
130
  }
90
131
  }
91
132
  });
92
133
  }
93
- spinner = (0, ora_1.default)(`执行远程命令`).start();
94
134
  try {
95
135
  yield execCmd();
96
- spinner.succeed();
136
+ spinner.succeed('执行命令成功');
97
137
  process.exit(0);
98
138
  }
99
139
  catch (error) {
100
- spinner.fail();
140
+ spinner.fail('执行命令失败');
101
141
  process.exit(1);
102
142
  }
103
143
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infinity-cli/ipublish",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "license": "ISC",
5
5
  "description": "上传服务器文件",
6
6
  "main": "dist/lib/index.js",
@@ -35,5 +35,5 @@
35
35
  "ora": "5",
36
36
  "rimraf": "^3.0.2"
37
37
  },
38
- "gitHead": "f44cb88aa9af453d3d61cce95576ef5790648972"
38
+ "gitHead": "49da7789d397ac020d2b9f2c91d96df9384336fe"
39
39
  }
package/readme.md CHANGED
@@ -18,6 +18,11 @@
18
18
 
19
19
  ```javascript
20
20
  {
21
+ jumpIp: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 跳板机的ip
22
+ jumpUser: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 跳板机的账户
23
+ jumpLoginType: Type.Optional(Type.Union([Type.Literal('password'), Type.Literal('privateKey')])), // [选填] privateKey和password都有值才有意义,否则哪个值设置了就用哪个
24
+ jumpPassword: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 跳板机的密码,与privateKey填一个就可以了
25
+ jumpPrivateKey: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 跳板机的privateKey,与password填一个就可以了
21
26
  ip: Type.String(), // [选填, 默认从全局配置读取] 服务器的ip
22
27
  user: Type.String(), // [选填, 默认从全局配置读取] 登录服务器的账户
23
28
  loginType: Type.Optional(Type.Union([Type.Literal('password'), Type.Literal('privateKey')])), // [选填] privateKey和password都有值才有意义,否则哪个值设置了就用哪个