t-comm
Version:
专业、稳定、纯粹的工具库
126 lines (121 loc) • 3.94 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib_es6 = require('../tslib.es6-01322ba9.js');
var fs_fs = require('../fs/fs.js');
var node_nodeCommand = require('../node/node-command.js');
var publish_helper = require('../publish/helper.js');
require('fs');
require('@babel/runtime/helpers/typeof');
require('path');
require('../env-variable/env-variable.js');
var DEFAULT_HOST_TARGET_DIR = '/root/ft_local';
function build(_a) {
var files = _a.files,
bundleName = _a.bundleName;
return new Promise(function (resolve, reject) {
console.log('[build] 开始打包...');
console.log('[build] Files: ', files);
// 创建dist目录
var fse = require('fs-extra');
fse.ensureDirSync('./dist');
var tar = require('tar');
// 打包
tar.c({
gzip: true,
file: "dist/".concat(bundleName, ".tar.gz"),
filter: function filter() {
return true;
}
}, tslib_es6.__spreadArray([], files, true)).then(function () {
console.log('[build] 打包完成');
resolve(true);
})["catch"](function (err) {
console.log('[build] 打包失败');
reject(err);
});
});
}
function upload(_a) {
var root = _a.root,
bundleName = _a.bundleName,
hostName = _a.hostName,
hostPwd = _a.hostPwd,
_b = _a.hostTargetDir,
hostTargetDir = _b === void 0 ? DEFAULT_HOST_TARGET_DIR : _b;
if (!hostName || !hostName || !bundleName) {
throw new Error('参数不全');
}
console.log('[upload] 开始上传...');
var publishBash = publish_helper.getPublishBashPath();
node_nodeCommand.execCommand("sh ".concat(publishBash, " ./dist/").concat(bundleName, ".tar.gz ").concat(hostTargetDir, " ").concat(hostName, " ").concat(hostPwd), root, 'inherit');
console.log('[upload] 上传完成');
}
/**
* 打包并上传到服务器
* @param {object} options 配置
* @param {string} options.hostName 服务器名称
* @param {string} options.hostPwd 服务器密码
* @param {string} [options.root] 项目根目录
* @param {string} [options.bundleName] 打包文件名称
* @example
*
* await buildAndUpload({
* hostName: '9.9.9.9',
* hostPwd: 'xxxx',
* bundleName: 'cron-job-svr',
* });
*
*/
function buildAndUpload(_a) {
var _b = _a.root,
root = _b === void 0 ? process.cwd() : _b,
_c = _a.bundleName,
bundleName = _c === void 0 ? 'bundle' : _c,
hostName = _a.hostName,
hostPwd = _a.hostPwd,
hostTargetDir = _a.hostTargetDir;
return tslib_es6.__awaiter(this, void 0, void 0, function () {
var path, pkgPath, fs, _d, files, flag;
return tslib_es6.__generator(this, function (_e) {
switch (_e.label) {
case 0:
if (!root) {
root = process.cwd();
}
path = require('path');
pkgPath = path.resolve(root, 'package.json');
fs = require('fs');
if (!fs.existsSync(root)) {
throw new Error('ERROR: root 不存在');
}
if (!fs.existsSync(pkgPath)) {
throw new Error('ERROR: package.json 不存在');
}
_d = fs_fs.readFileSync(pkgPath, true).files, files = _d === void 0 ? [] : _d;
if (files.indexOf('.env.local') < 0 && fs.existsSync('.env.local')) {
files.push('.env.local');
}
return [4 /*yield*/, build({
files: files,
bundleName: bundleName
})];
case 1:
flag = _e.sent();
if (!flag) return [2 /*return*/];
return [4 /*yield*/, upload({
root: root,
bundleName: bundleName,
hostName: hostName,
hostPwd: hostPwd,
hostTargetDir: hostTargetDir
})];
case 2:
_e.sent();
return [2 /*return*/, files];
}
});
});
}
exports.build = build;
exports.buildAndUpload = buildAndUpload;
exports.upload = upload;