UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

117 lines (114 loc) 3.61 kB
import { b as __awaiter, c as __generator, a as __spreadArray } from '../tslib.es6-48fa7a9b.js'; import { execCommand } from '../node/node-command.js'; import { getPublishBashPath } from '../publish/helper.js'; import '@babel/runtime/helpers/typeof'; import 'path'; import 'fs'; import '../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; } }, __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 = getPublishBashPath(); 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 __awaiter(this, void 0, void 0, function () { var path, pkgPath, fs, files, flag; return __generator(this, function (_d) { switch (_d.label) { case 0: if (!root) { root = process.cwd(); } path = require('path'); pkgPath = path.resolve(root, 'package.json'); fs = require('fs'); if (!fs.existsSync(root)) { return [2 /*return*/]; } if (!fs.existsSync(pkgPath)) { throw new Error('ERROR: package.json 不存在'); } files = require(pkgPath).files || []; 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 = _d.sent(); if (!flag) return [2 /*return*/]; upload({ root: root, bundleName: bundleName, hostName: hostName, hostPwd: hostPwd, hostTargetDir: hostTargetDir }); return [2 /*return*/]; } }); }); } export { buildAndUpload };