@infinity-cli/ipublish 1.0.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/dist/bin.d.ts +2 -0
- package/dist/bin.js +33 -0
- package/dist/command/config.d.ts +26 -0
- package/dist/command/config.js +66 -0
- package/dist/command/upload.d.ts +7 -0
- package/dist/command/upload.js +114 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +6 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +51 -0
- package/package.json +39 -0
- package/readme.md +43 -0
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
const commander_1 = require("commander");
|
|
14
|
+
const config_1 = require("./command/config");
|
|
15
|
+
const upload_1 = require("./command/upload");
|
|
16
|
+
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const pkg = require('../package.json');
|
|
18
|
+
const program = new commander_1.Command();
|
|
19
|
+
program.version(pkg.version);
|
|
20
|
+
const configCommand = config_1.runtimeConfig.makeConfigCommand();
|
|
21
|
+
if (configCommand) {
|
|
22
|
+
program.addCommand(configCommand);
|
|
23
|
+
}
|
|
24
|
+
program
|
|
25
|
+
.command('upload')
|
|
26
|
+
.description('上传到服务器')
|
|
27
|
+
.argument('[LABEL_ENV]', '全局配置标签')
|
|
28
|
+
.option('-c, --config <config>', '本地配置文件')
|
|
29
|
+
.allowUnknownOption()
|
|
30
|
+
.action(upload_1.uploadRemoteAction);
|
|
31
|
+
program.parse(process.argv);
|
|
32
|
+
});
|
|
33
|
+
main();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Static } from '@sinclair/typebox';
|
|
2
|
+
import { RuntimeConfig } from '@infinity-cli/shared';
|
|
3
|
+
declare const TypeBox_PublishConfig: import("@sinclair/typebox").TObject<{
|
|
4
|
+
ip: import("@sinclair/typebox").TString;
|
|
5
|
+
user: import("@sinclair/typebox").TString;
|
|
6
|
+
loginType: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TLiteral<"password">, import("@sinclair/typebox").TLiteral<"privateKey">]>>;
|
|
7
|
+
password: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
8
|
+
privateKey: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
9
|
+
from: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TString>;
|
|
10
|
+
to: import("@sinclair/typebox").TString;
|
|
11
|
+
cmd: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
12
|
+
cwd: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
13
|
+
}>;
|
|
14
|
+
export declare type Type_PublishConfig = Static<typeof TypeBox_PublishConfig>;
|
|
15
|
+
export declare const runtimeConfig: RuntimeConfig<{
|
|
16
|
+
loginType?: "password" | "privateKey" | undefined;
|
|
17
|
+
password?: string | undefined;
|
|
18
|
+
privateKey?: string | undefined;
|
|
19
|
+
cmd?: string | undefined;
|
|
20
|
+
cwd?: string | undefined;
|
|
21
|
+
ip: string;
|
|
22
|
+
user: string;
|
|
23
|
+
from: import("@sinclair/typebox").StaticArray<import("@sinclair/typebox").TString>;
|
|
24
|
+
to: string;
|
|
25
|
+
}>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.runtimeConfig = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const typebox_1 = require("@sinclair/typebox");
|
|
9
|
+
const shared_1 = require("@infinity-cli/shared");
|
|
10
|
+
const TypeBox_PublishConfig = typebox_1.Type.Object({
|
|
11
|
+
ip: typebox_1.Type.String(),
|
|
12
|
+
user: typebox_1.Type.String(),
|
|
13
|
+
loginType: typebox_1.Type.Optional(typebox_1.Type.Union([typebox_1.Type.Literal('password'), typebox_1.Type.Literal('privateKey')])),
|
|
14
|
+
password: typebox_1.Type.Optional(typebox_1.Type.String()),
|
|
15
|
+
privateKey: typebox_1.Type.Optional(typebox_1.Type.String()),
|
|
16
|
+
from: typebox_1.Type.Array(typebox_1.Type.String()),
|
|
17
|
+
to: typebox_1.Type.String(),
|
|
18
|
+
cmd: typebox_1.Type.Optional(typebox_1.Type.String()),
|
|
19
|
+
cwd: typebox_1.Type.Optional(typebox_1.Type.String()),
|
|
20
|
+
});
|
|
21
|
+
const validate = (val) => !!val || '请输入值';
|
|
22
|
+
const publishConfigQuestions = [
|
|
23
|
+
{
|
|
24
|
+
type: 'input',
|
|
25
|
+
name: 'ip',
|
|
26
|
+
message: '请输入ip',
|
|
27
|
+
validate,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: 'input',
|
|
31
|
+
name: 'user',
|
|
32
|
+
message: '请输入用户名',
|
|
33
|
+
validate,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: 'list',
|
|
37
|
+
name: 'loginType',
|
|
38
|
+
message: '请选择验证类型',
|
|
39
|
+
choices: [
|
|
40
|
+
{ name: '密码', value: 'password' },
|
|
41
|
+
{ name: 'private key', value: 'privateKey' },
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: 'input',
|
|
46
|
+
message: '请输入密码',
|
|
47
|
+
name: 'password',
|
|
48
|
+
validate,
|
|
49
|
+
when: (value) => {
|
|
50
|
+
return value.loginType === 'password';
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: 'input',
|
|
55
|
+
message: '请输入privateKey路径',
|
|
56
|
+
name: 'privateKey',
|
|
57
|
+
validate,
|
|
58
|
+
when: (value) => {
|
|
59
|
+
return value.loginType === 'privateKey';
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
exports.runtimeConfig = new shared_1.RuntimeConfig((0, shared_1.getBinName)(path_1.default.join(__dirname, '../../package.json')), {
|
|
64
|
+
configBox: TypeBox_PublishConfig,
|
|
65
|
+
globalConfigQuestions: publishConfigQuestions,
|
|
66
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Type_PublishConfig } from './config';
|
|
2
|
+
export declare const uploadRemoteWithConfig: (config: Type_PublishConfig) => Promise<never>;
|
|
3
|
+
export declare const uploadRemoteAction: (LABEL_ENV: any, { config: configName }?: {
|
|
4
|
+
config: undefined;
|
|
5
|
+
}, { args }?: {
|
|
6
|
+
args: undefined;
|
|
7
|
+
}) => Promise<void>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.uploadRemoteAction = exports.uploadRemoteWithConfig = void 0;
|
|
16
|
+
const config_1 = require("./config");
|
|
17
|
+
const node_ssh_1 = require("node-ssh");
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
const util_1 = require("../util");
|
|
20
|
+
const shared_1 = require("@infinity-cli/shared");
|
|
21
|
+
const rimraf_1 = __importDefault(require("rimraf"));
|
|
22
|
+
const ora_1 = __importDefault(require("ora"));
|
|
23
|
+
const uploadRemoteWithConfig = (config) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
let spinner;
|
|
25
|
+
const { ip, user, loginType, password, privateKey, from, to, cmd, cwd } = config;
|
|
26
|
+
if (to === '/') {
|
|
27
|
+
throw new Error('to:不允许在服务器根目录操作');
|
|
28
|
+
}
|
|
29
|
+
spinner = (0, ora_1.default)(`打包文件`).start();
|
|
30
|
+
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;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
delete connectConfig.password;
|
|
45
|
+
}
|
|
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));
|
|
51
|
+
(0, rimraf_1.default)(zipPath, () => { });
|
|
52
|
+
spinner.succeed();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
(0, rimraf_1.default)(zipPath, () => { });
|
|
56
|
+
spinner.fail();
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
function execCmd() {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
const cmds = [];
|
|
62
|
+
const unzipCmd = [];
|
|
63
|
+
if (dir) {
|
|
64
|
+
dir.forEach((file) => {
|
|
65
|
+
if (file.startsWith('/')) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
unzipCmd.push(`rm ${file} -rf`);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
unzipCmd.push(`tar xzf ${zipName}`);
|
|
72
|
+
cmds.push(unzipCmd.join(' && '));
|
|
73
|
+
if (cmd) {
|
|
74
|
+
if (/rm[\s]+-rf[\s]+\//.test(cmd)) {
|
|
75
|
+
console.log((0, shared_1.red)(`包含危险操作:${cmd}`));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
cmds.push(cmd);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const cmd of cmds) {
|
|
82
|
+
console.log(' ' + cmd);
|
|
83
|
+
const result = yield ssh.execCommand(cmd, {
|
|
84
|
+
cwd: cwd ? cwd : to,
|
|
85
|
+
});
|
|
86
|
+
result.stdout && console.log('STDOUT: ' + result.stdout);
|
|
87
|
+
if (result.stderr) {
|
|
88
|
+
console.error('STDERR: ' + result.stderr);
|
|
89
|
+
throw result.stderr;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
spinner = (0, ora_1.default)(`执行远程命令`).start();
|
|
95
|
+
try {
|
|
96
|
+
yield execCmd();
|
|
97
|
+
spinner.succeed();
|
|
98
|
+
process.exit(0);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
spinner.fail();
|
|
102
|
+
process.exit(1);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
exports.uploadRemoteWithConfig = uploadRemoteWithConfig;
|
|
106
|
+
const uploadRemoteAction = (LABEL_ENV, { config: configName } = { config: undefined }, { args } = { args: undefined }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
|
+
const config = yield config_1.runtimeConfig.getConfig(args, configName, LABEL_ENV);
|
|
108
|
+
if (!config.password && !config.privateKey) {
|
|
109
|
+
console.error('password和privateKey至少设置一项');
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
(0, exports.uploadRemoteWithConfig)(config);
|
|
113
|
+
});
|
|
114
|
+
exports.uploadRemoteAction = uploadRemoteAction;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { uploadRemoteAction, uploadRemoteWithConfig } from '../command/upload';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uploadRemoteWithConfig = exports.uploadRemoteAction = void 0;
|
|
4
|
+
var upload_1 = require("../command/upload");
|
|
5
|
+
Object.defineProperty(exports, "uploadRemoteAction", { enumerable: true, get: function () { return upload_1.uploadRemoteAction; } });
|
|
6
|
+
Object.defineProperty(exports, "uploadRemoteWithConfig", { enumerable: true, get: function () { return upload_1.uploadRemoteWithConfig; } });
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zip = void 0;
|
|
4
|
+
const zip = (ZIP_FILES) => {
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const archiver = require('archiver');
|
|
8
|
+
const ZIP_NAME = 'ipublish.tar.gz';
|
|
9
|
+
const dir = [];
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const outputPath = path.join(process.cwd(), ZIP_NAME);
|
|
12
|
+
const output = fs.createWriteStream(outputPath);
|
|
13
|
+
const archive = archiver('tar', {
|
|
14
|
+
zlib: { level: 9 },
|
|
15
|
+
gzip: true,
|
|
16
|
+
});
|
|
17
|
+
output.on('close', function () {
|
|
18
|
+
console.log(`打包完成:${outputPath}`);
|
|
19
|
+
console.log((archive.pointer() / 1024 / 1024).toFixed(2) + 'M total bytes');
|
|
20
|
+
resolve({ zipPath: outputPath, zipName: ZIP_NAME, dir });
|
|
21
|
+
});
|
|
22
|
+
output.on('end', function () {
|
|
23
|
+
console.log('end: Data has been drained');
|
|
24
|
+
});
|
|
25
|
+
archive.on('warning', function (err) {
|
|
26
|
+
console.warn('warning: ', err);
|
|
27
|
+
});
|
|
28
|
+
archive.on('error', function (err) {
|
|
29
|
+
console.error('error: ', err);
|
|
30
|
+
reject(err);
|
|
31
|
+
});
|
|
32
|
+
archive.pipe(output);
|
|
33
|
+
ZIP_FILES.forEach((file) => {
|
|
34
|
+
try {
|
|
35
|
+
const stat = fs.statSync(path.join(process.cwd(), file));
|
|
36
|
+
if (stat.isFile()) {
|
|
37
|
+
archive.file(file);
|
|
38
|
+
}
|
|
39
|
+
else if (stat.isDirectory()) {
|
|
40
|
+
archive.directory(file);
|
|
41
|
+
dir.push(file);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error('ZIP_FILES.forEach ~ error', error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
archive.finalize();
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
exports.zip = zip;
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@infinity-cli/ipublish",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "ISC",
|
|
5
|
+
"description": "上传服务器文件",
|
|
6
|
+
"main": "dist/lib/index.js",
|
|
7
|
+
"module": "dist/lib/index.js",
|
|
8
|
+
"types": "dist/lib/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"ipublish": "dist/bin.js"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public",
|
|
14
|
+
"registry": "https://registry.npmjs.org/"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rimraf dist && tsc -b"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ipublish",
|
|
24
|
+
"cli",
|
|
25
|
+
"infinity",
|
|
26
|
+
"infinity cli",
|
|
27
|
+
"infinity-cli"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@infinity-cli/shared": "^1.0.0",
|
|
31
|
+
"@sinclair/typebox": "^0.23.1",
|
|
32
|
+
"archiver": "^5.3.0",
|
|
33
|
+
"commander": "^8.2.0",
|
|
34
|
+
"node-ssh": "^12.0.3",
|
|
35
|
+
"ora": "5",
|
|
36
|
+
"rimraf": "^3.0.2"
|
|
37
|
+
},
|
|
38
|
+
"gitHead": "a99e49cbb0b80dd40fcd5cbce9f020440eee402f"
|
|
39
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# ipublish
|
|
2
|
+
|
|
3
|
+
## 功能
|
|
4
|
+
|
|
5
|
+
- 根据配置上传资源到服务器并且执行相应 command
|
|
6
|
+
|
|
7
|
+
## 使用方式
|
|
8
|
+
|
|
9
|
+
1. 安装: `npm install -g @infinity-cli/ipublish`
|
|
10
|
+
2. 配置: 一般项目根目录配置 infinity.config.js 或 infinity.config.json
|
|
11
|
+
3. 使用说明: 查看 `ipublish -h` `ipublish config -h`
|
|
12
|
+
|
|
13
|
+
## 配置说明
|
|
14
|
+
|
|
15
|
+
参考 https://www.npmjs.com/package/@infinity-cli/shared
|
|
16
|
+
|
|
17
|
+
### 可配置属性
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
{
|
|
21
|
+
ip: Type.String(), // [选填, 默认从全局配置读取] 服务器的ip
|
|
22
|
+
user: Type.String(), // [选填, 默认从全局配置读取] 登录服务器的账户
|
|
23
|
+
loginType: Type.Optional(Type.Union([Type.Literal('password'), Type.Literal('privateKey')])), // [选填] privateKey和password都有值才有意义,否则哪个值设置了就用哪个
|
|
24
|
+
password: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 登录服务器的密码,与privateKey填一个就可以了
|
|
25
|
+
privateKey: Type.Optional(Type.String()), // [选填, 默认从全局配置读取] 登录服务器的privateKey,与password填一个就可以了
|
|
26
|
+
from: Type.Array(Type.String()), //[必填] 相对项目根目录的本地文件
|
|
27
|
+
to: Type.String(), //[必填] 服务器存放文件的目录
|
|
28
|
+
cmd: Type.Optional(Type.String()), //[选填,默认为空] 在服务器执行的command
|
|
29
|
+
cwd: Type.Optional(Type.String()), //[选填,默认为${this.to}] 在服务器执行的command的目录
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 示例 infinity.config.json
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"ipublish": {
|
|
38
|
+
"from": ["package.json", "dist", "script", "pm2.json"],
|
|
39
|
+
"to": "/opt/project/test",
|
|
40
|
+
"cmd": "ls && pm2 restart"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|