UNPKG

t-comm

Version:

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

120 lines (117 loc) 3.8 kB
import { a as __spreadArray, b as __awaiter, c as __generator } from '../tslib.es6-096fffdd.js'; import { readFileSync } from '../fs/fs.mjs'; import { execCommand } from '../node/node-command.mjs'; import { getPublishBashPath } from '../publish/helper.mjs'; import 'fs'; import '@babel/runtime/helpers/typeof'; import 'path'; import '../env-variable/env-variable.mjs'; 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, _d, files, flag; return __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 = 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]; } }); }); } export { build, buildAndUpload, upload };