UNPKG

gxd-vue-library

Version:

依赖与element Ui插件库,聚福宝福利PC端插件库

213 lines (185 loc) 6.85 kB
'use strict'; const path = require('path'); const clog = require('./build/clog'); const fileHelper = require('./build/fileHepler'); const basePath = require('./build/path'); const toCommands = path.resolve('./build'); const lib = require('./build/lib/utils'); const handleJsonPackage = require('./build/version/Json'); const execSync = require('child_process').execSync; const utils = require('./build/lib/utils'); /** * @description 复制基础库下的指定文件目录到根目录下指定文件目录 * @param isLocal * @param dir */ const copyBaseLibDir = (isLocal, dir)=>{ lib.logsLine(clog, '开始拷贝命令行资源'); let fromCommandsPath = path.resolve(`./node_modules/gxd-vue-library/${dir}`); //本地获取拷贝命令行资源 if (isLocal) { const getPackage = `${basePath.rootDir}/get.package.path.js`; if (fileHelper.existFileSync(getPackage)) { const {getPackagePath} = require(getPackage); fromCommandsPath = fileHelper.getPlatformSurePath(getPackagePath(`/${dir}`)); clog(`从本地获取拷贝命令行相关资源,目录:${fromCommandsPath}`, 'cyan'); } } fileHelper.copySync(fromCommandsPath, toCommands); clog(`拷贝命令行操作成功,目录:${toCommands}`, 'green'); lib.logsLine(clog); }; function getVersion(argv){ let temp = {}; argv.map(item=>{ if(item.indexOf('=') !== -1) { let arr = item.split('='); //基础库 if(arr[0] === 'lib') { temp['B'] = arr[1] } //业务库 if(arr[0] === 'bus') { temp['A'] = arr[1] } } }) return temp; } /** * @description 获取发布资源 * @param current 当前项目package.json文件内容 * @param publishHistory 获取远端库发版本日志 * @param nodeModulePath 获取当项目node_modules下的库package.json * @param libName 需要更新的业务库名称 * @param customVersion 自定版本 默认 * @returns {*} */ function checkVersion(current, publishHistory, nodeModulePath, libName, customVersion = null){ let isUpdate = false; let lastVersion = publishHistory.version; //项目node_modules if (fileHelper.existFileSync(nodeModulePath)) { let temp = require(nodeModulePath); if (lastVersion !== temp.version) { current['dependencies'][libName] = lastVersion; isUpdate = true; } } //当前项目package.json文件内容 if (lastVersion !== current['dependencies'][libName]) { current['dependencies'][libName] = lastVersion; isUpdate = true; } //判断指定版本是否不再发布版本列表中 if(customVersion) { let hasVersion = false Object.keys(publishHistory.history).map(version=>{ if(customVersion === version) hasVersion = true; }); if(hasVersion){ current['dependencies'][libName] = customVersion; isUpdate = true; } else { clog(`当前指定发布版本不存在:${customVersion}`, 'red'); clog(`请当前已发布版本,查看:https://image.jufubao.cn/jfb_version/${libName}.json`, 'red'); process.exit(0) } } clog(`请当前已发布版本,查看:https://image.jufubao.cn/jfb_version/${libName}.json`, 'green'); return {isUpdate,current} } (async ()=>{ let argv = utils.getArgv(); if (argv.length === 0) { clog(`请传入参数, 例如: node ./commands.update.js true`, 'red'); process.exit(-1); } let isLocal = argv[0] === 'true'; let customVersion = {}; //当前项目package,json文件路径 const currentPackage = require(fileHelper.getPlatformSurePath(`${basePath.rootDir}/package.json`)); let flow = false; //指定版本业务库版本 if(argv.length > 1) { if(argv[1] !== 'flow') customVersion = getVersion(argv.slice(1)) else { flow = true; customVersion = { //业务库 A:currentPackage.dependencies['jufubao-admin-library'], //基础库 B:currentPackage.dependencies['gxd-vue-library'] } } } //本地文件拷贝基础框架 if (isLocal) { //拷贝命令行资源 copyBaseLibDir(isLocal, 'build'); //复制插件库文件 clog(`从本地拷贝框架资源`, 'cyanBG'); utils.logsLine(clog); execSync(`cross-env status=test node ./build/commands/copy.init.js`, {stdio: 'inherit'}); clog(`完成更新`, 'cyan'); utils.logsLine(clog); process.exit(0) } //获取远端基础库发版本日志与获取当项目node_modules下的基础库package.json const baseLibrary = await handleJsonPackage.getByCodeJson('gxd-vue-library'); const nodeModulesPath = fileHelper.getPlatformSurePath(basePath.rootDir + '/node_modules/gxd-vue-library/package.json'); //获取远端基础业务逻辑发版本日志与获取当项目node_modules下的基础业务库package.json const adminLibrary = await handleJsonPackage.getByCodeJson('jufubao-admin-library'); const adminNodeModulesPath = fileHelper.getPlatformSurePath(basePath.rootDir + '/node_modules/jufubao-admin-library/package.json'); //获取基础版本信息 const base = checkVersion(currentPackage, baseLibrary, nodeModulesPath, 'gxd-vue-library',customVersion.B); //获取业务版本信息 let admin = {}; if(currentPackage.name !== 'jufubao-admin-library') { admin = checkVersion( currentPackage, adminLibrary, currentPackage.name === 'jufubao-admin-library'? `${basePath.rootDir}/package.json`: adminNodeModulesPath, 'jufubao-admin-library', customVersion.A ); } //当任何一个库有更新均重新生产package.json if(admin.isUpdate || base.isUpdate) { fileHelper.writeFileSync( fileHelper.getPlatformSurePath(fileHelper.getPlatformSurePath(basePath.rootDir + '/package.json')), JSON.stringify(currentPackage, null, 2) ) } //静默模式 let oss = ''; if(flow) oss = 'cross-env oss=true ' //执行命令行 if (admin.isUpdate || base.isUpdate) { //是否需要升级基础插件 let cmdStr = `npm i --registry=https://registry.npmmirror.com`; execSync(cmdStr, {stdio: 'inherit'}); //拷贝命令行资源 copyBaseLibDir(isLocal, 'build'); //复制插件库文件 cmdStr = `${oss} node ./build/commands/copy.init.js`; clog(`完成更新框架资源`, 'cyanBG'); utils.logsLine(clog, 'start'); clog(`执行命令行:${cmdStr}`, 'cyan'); utils.logsLine(clog); execSync(cmdStr, {stdio: 'inherit'}); clog(`完成更新`, 'cyan'); utils.logsLine(clog); } else { //复制插件库文件 let cmdStr = `${oss} node ./build/commands/copy.init.js`; clog(`完成更新框架资源`, 'cyanBG'); utils.logsLine(clog, 'start'); clog(`执行命令行:${cmdStr}`, 'cyan'); utils.logsLine(clog); execSync(cmdStr, {stdio: 'inherit'}); clog(`完成更新`, 'cyan'); } })();