UNPKG

t-comm

Version:

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

187 lines (184 loc) 7.39 kB
import { b as __awaiter, c as __generator } from '../tslib.es6-096fffdd.js'; import { spawnSync } from 'child_process'; import * as path from 'path'; import { getIPAddressStr, getIPAddress } from '../ip/ip.mjs'; import { ENV_MAP, PUBLISH_ENV_MAP, PUBLISH_HOST_ENV } from './config.mjs'; import { getPublishRootDir, getPublishBashPath, getPublishEnvValue, getPublishModuleName } from './helper.mjs'; import { postFile } from './post-file.mjs'; import 'os'; import 'fs'; import '../env-variable/env-variable.mjs'; import 'http'; var rootDir = getPublishRootDir(); var PUBLISH_BASH_FILE = getPublishBashPath(); function getDevPwd() { var devHostName = getPublishEnvValue(ENV_MAP.DEV_HOST_NAME); var devHostPwd = getPublishEnvValue(ENV_MAP.DEV_HOST_PWD); return { devHostName: devHostName, devHostPwd: devHostPwd }; } function validate(_a) { var dir = _a.dir, publishPathProd = _a.publishPathProd, publishPathTest = _a.publishPathTest, publishEnv = _a.publishEnv; if (!dir || typeof dir !== 'string') { console.log('[publish] 模块名称不正确,请检查.env.local'); return 0; } if (publishEnv === PUBLISH_ENV_MAP.PROD && (!publishPathProd || typeof publishPathProd !== 'string')) { console.log('[publish] 正式环境发布路径不正确,请检查.env'); return 0; } if (publishEnv === PUBLISH_ENV_MAP.TEST && (!publishPathTest || typeof publishPathTest !== 'string')) { console.log('[publish] 测试环境发布路径不正确,请检查.env'); return 0; } // 禁止本机发布 if ((getIPAddress().indexOf('10.45') === 0 || getIPAddress().indexOf('10.20') === 0) && publishEnv === PUBLISH_ENV_MAP.PROD) { console.log('[publish] 禁止在本机发布!'); return 0; } return true; } function localPublish(options) { return __awaiter(this, void 0, void 0, function () { var dir, author, publishPathProd, publishPathTest, _a, publishEnv, moduleName, staticDir, tarName, fileSrc, fileAll; return __generator(this, function (_b) { switch (_b.label) { case 0: dir = getPublishEnvValue(ENV_MAP.VUE_APP_DIR); author = getPublishEnvValue(ENV_MAP.VUE_APP_AUTHOR); publishPathProd = getPublishEnvValue(ENV_MAP.VUE_APP_PATH_PROD); publishPathTest = getPublishEnvValue(ENV_MAP.VUE_APP_PATH_TEST); _a = options.publishEnv, publishEnv = _a === void 0 ? PUBLISH_ENV_MAP.TEST : _a; if (!validate({ dir: dir, publishPathProd: publishPathProd, publishPathTest: publishPathTest, publishEnv: publishEnv })) { return [2 /*return*/]; } console.log('[publish] 准备发布模块:', dir); moduleName = getPublishModuleName(dir); staticDir = 'static'; tarName = "static_".concat(moduleName, "_").concat(getIPAddressStr()); fileSrc = options.fileTar || path.join(rootDir, 'dist', dir, staticDir, "".concat(tarName, ".tar")); fileAll = options.fileDir || path.join(rootDir, 'dist', dir, staticDir); console.log('[publish] moduleName: ', moduleName); return [4 /*yield*/, realPublish({ publishEnv: publishEnv, publishPathProd: publishPathProd, publishPathTest: publishPathTest, moduleName: moduleName, fileAll: fileAll, fileSrc: fileSrc, author: author, dir: dir, options: options })]; case 1: _b.sent(); return [2 /*return*/]; } }); }); } function realPublish(_a) { var publishEnv = _a.publishEnv, publishPathProd = _a.publishPathProd, publishPathTest = _a.publishPathTest, moduleName = _a.moduleName, fileAll = _a.fileAll, fileSrc = _a.fileSrc, author = _a.author, dir = _a.dir, options = _a.options; return __awaiter(this, void 0, void 0, function () { var desc, env, shell, _b, devHostName, devHostPwd, fileDataInfo, files; return __generator(this, function (_c) { switch (_c.label) { case 0: desc = ''; env = PUBLISH_HOST_ENV.TEST; shell = {}; shell.runSync = function (cmd, args, options) { var shellResult = spawnSync(cmd, args, options); if (shellResult.status !== 0) { console.log('[publish] failed: ', shellResult.stderr); } return shellResult; }; if (publishEnv === PUBLISH_ENV_MAP.PROD) { if (!publishPathProd || typeof publishPathProd !== 'string') { console.log('[publish] 正式环境发布路径不正确,请检查.env'); return [2 /*return*/]; } desc = "".concat(publishPathProd, "/").concat(moduleName); env = PUBLISH_HOST_ENV.PROD; } else if (publishEnv === PUBLISH_ENV_MAP.DEV_CLOUD) { _b = getDevPwd(), devHostName = _b.devHostName, devHostPwd = _b.devHostPwd; if (!devHostName || !devHostPwd) { console.log('[publish] failed: ', "\u6CA1\u6709\u627E\u5230\u6709\u6548\u7684 ".concat(ENV_MAP.DEV_HOST_NAME, " \u6216 ").concat(ENV_MAP.DEV_HOST_PWD)); return [2 /*return*/]; } if (options === null || options === void 0 ? void 0 : options.publishTargetDir) { shell.runSync('sh', [PUBLISH_BASH_FILE, fileAll, options.publishTargetDir, devHostName, devHostPwd], { stdio: 'inherit' }); } else if (moduleName === 'mobile-official-web') { shell.runSync('sh', [PUBLISH_BASH_FILE, fileAll, '/root/html/', devHostName, devHostPwd]); } else { shell.runSync('sh', [PUBLISH_BASH_FILE, fileAll, "/root/html/".concat(moduleName, "/"), devHostName, devHostPwd], { stdio: 'inherit' }); } return [2 /*return*/]; } else { if (!publishPathTest || typeof publishPathTest !== 'string') { console.log('[publish] 测试环境发布路径不正确,请检查.env'); return [2 /*return*/]; } desc = "".concat(publishPathTest, "/").concat(moduleName); env = PUBLISH_HOST_ENV.TEST; } fileDataInfo = [{ urlKey: 'src', urlValue: fileSrc }, { urlKey: 'des', urlValue: desc }, { urlKey: 'env', urlValue: env }, { urlKey: 'author', urlValue: author || '' }]; console.log('[publish] ', { fileDataInfo: fileDataInfo }); files = [{ urlKey: 'src', urlValue: fileSrc }]; return [4 /*yield*/, postFile(fileDataInfo, files, options).then(function (data) { if (data.r == 0) { console.log("[\u53D1\u5E03\u6210\u529F]........publish ".concat(dir, " to ").concat(env, " success........\n")); } else { console.log('[发布失败]', data); } })["catch"](function (err) { console.log('[发布失败]', err); })]; case 1: _c.sent(); return [2 /*return*/]; } }); }); } export { localPublish };