t-comm
Version:
专业、稳定、纯粹的工具库
181 lines (178 loc) • 6.41 kB
JavaScript
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { readFileSync } from '../fs/fs.mjs';
import { execCommand } from '../node/node-command.mjs';
import { getFs } from '../nodejs/fs.mjs';
import { getPath } from '../nodejs/path.mjs';
import { getPublishBashPath } from '../publish/helper.mjs';
import { getFsExtra } from '../third-party/fs-extra.mjs';
import { getTar } from '../third-party/tar.mjs';
import '../nodejs/crypto.mjs';
import '../nodejs/os.mjs';
import '@babel/runtime/helpers/typeof';
import '../nodejs/child_process.mjs';
import '../env-variable/env-variable.mjs';
var DEFAULT_HOST_TARGET_DIR = '/root/ft_local';
/**
* 从 package.json 中解析需要打包的文件列表
* @param {object} options 配置
* @param {string} [options.root] 项目根目录,默认为 process.cwd()
* @returns {Array<string>} 文件列表
* @example
* ```ts
* // 使用当前工作目录的 package.json
* const files = resolveFiles();
* // ['dist', 'README.md', '.env.local']
*
* // 指定项目根目录
* const files2 = resolveFiles({ root: '/path/to/project' });
* ```
*/
function resolveFiles() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$root = _ref.root,
root = _ref$root === void 0 ? process.cwd() : _ref$root;
if (!root) {
root = process.cwd();
}
var pkgPath = getPath().resolve(root, 'package.json');
var fs = getFs();
if (!fs.existsSync(root)) {
throw new Error('ERROR: root 不存在');
}
if (!fs.existsSync(pkgPath)) {
throw new Error('ERROR: package.json 不存在');
}
var _readFileSync = readFileSync(pkgPath, true),
_readFileSync$files = _readFileSync.files,
files = _readFileSync$files === void 0 ? [] : _readFileSync$files;
if (files.indexOf('.env.local') < 0 && fs.existsSync('.env.local')) {
files.push('.env.local');
}
return files;
}
function build(_ref2) {
var files = _ref2.files,
root = _ref2.root,
bundleName = _ref2.bundleName,
_ref2$outputDir = _ref2.outputDir,
outputDir = _ref2$outputDir === void 0 ? 'dist' : _ref2$outputDir;
var resolvedFiles = !files || files.length === 0 ? resolveFiles({
root: root
}) : files;
return new Promise(function (resolve, reject) {
console.log('[build] 开始打包...');
console.log('[build] Files: ', resolvedFiles);
// 创建输出目录
var fse = getFsExtra();
fse.ensureDirSync("./".concat(outputDir));
var tar = getTar();
// 打包
tar.c({
gzip: true,
file: "".concat(outputDir, "/").concat(bundleName, ".tar.gz"),
filter: function filter() {
return true;
}
}, _toConsumableArray(resolvedFiles)).then(function () {
console.log('[build] 打包完成');
resolve(true);
})["catch"](function (err) {
console.log('[build] 打包失败');
reject(err);
});
});
}
function upload(_ref3) {
var root = _ref3.root,
bundleName = _ref3.bundleName,
hostName = _ref3.hostName,
hostPwd = _ref3.hostPwd,
_ref3$hostTargetDir = _ref3.hostTargetDir,
hostTargetDir = _ref3$hostTargetDir === void 0 ? DEFAULT_HOST_TARGET_DIR : _ref3$hostTargetDir,
_ref3$wrapHostPwd = _ref3.wrapHostPwd,
wrapHostPwd = _ref3$wrapHostPwd === void 0 ? true : _ref3$wrapHostPwd,
_ref3$outputDir = _ref3.outputDir,
outputDir = _ref3$outputDir === void 0 ? 'dist' : _ref3$outputDir;
if (!hostName || !hostName || !bundleName) {
throw new Error('参数不全');
}
console.log('[upload] 开始上传...');
var publishBash = getPublishBashPath();
var wrappedHostPwd = wrapHostPwd ? "\"".concat(hostPwd, "\"") : hostPwd;
execCommand("sh ".concat(publishBash, " ./").concat(outputDir, "/").concat(bundleName, ".tar.gz ").concat(hostTargetDir, " ").concat(hostName, " ").concat(wrappedHostPwd), root, 'inherit');
console.log('[upload] 上传完成');
}
/**
* 打包并上传到服务器
* @param {object} options 配置
* @param {string} options.hostName 服务器名称
* @param {string} options.hostPwd 服务器密码
* @param {string} [options.root] 项目根目录
* @param {string} [options.bundleName] 打包文件名称
* @param {boolean} [options.wrapHostPwd=true] 是否用双引号包裹密码,默认为 true
* @param {string} [options.outputDir='dist'] 打包输出目录,默认为 'dist'
* @example
*
* await buildAndUpload({
* hostName: '9.9.9.9',
* hostPwd: 'xxxx',
* bundleName: 'cron-job-svr',
* });
*
*/
function buildAndUpload(_ref4) {
var _ref4$root = _ref4.root,
root = _ref4$root === void 0 ? process.cwd() : _ref4$root,
_ref4$bundleName = _ref4.bundleName,
bundleName = _ref4$bundleName === void 0 ? 'bundle' : _ref4$bundleName,
hostName = _ref4.hostName,
hostPwd = _ref4.hostPwd,
hostTargetDir = _ref4.hostTargetDir,
_ref4$wrapHostPwd = _ref4.wrapHostPwd,
wrapHostPwd = _ref4$wrapHostPwd === void 0 ? true : _ref4$wrapHostPwd,
_ref4$outputDir = _ref4.outputDir,
outputDir = _ref4$outputDir === void 0 ? 'dist' : _ref4$outputDir;
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var files, flag;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
files = resolveFiles({
root: root
});
_context.next = 1;
return build({
files: files,
bundleName: bundleName,
outputDir: outputDir
});
case 1:
flag = _context.sent;
if (flag) {
_context.next = 2;
break;
}
return _context.abrupt("return", files);
case 2:
_context.next = 3;
return upload({
root: root,
bundleName: bundleName,
hostName: hostName,
hostPwd: hostPwd,
hostTargetDir: hostTargetDir,
wrapHostPwd: wrapHostPwd,
outputDir: outputDir
});
case 3:
return _context.abrupt("return", files);
case 4:
case "end":
return _context.stop();
}
}, _callee);
}));
}
export { build, buildAndUpload, resolveFiles, upload };