UNPKG

gxd-vue-library

Version:

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

224 lines (191 loc) 7.85 kB
'use strict'; const path = require('path'); const clog = require('./../clog'); const basePath = require('./../path'); const fileHelper = require('./../fileHepler'); const packageDirectory = basePath.packageDirectory; const htmlConfig = require('./../lib/html.config'); const os = require('os'); const utils = require('./../lib/utils') let fromLibraryPath = path.resolve('./node_modules/gxd-vue-library/src/components'); let toLibraryPath = path.resolve('./src/components'); let fromDomePath = path.resolve('./node_modules/gxd-vue-library/src/dome'); let toDomePath = path.resolve('./src/dome'); let fromUtilsPath = path.resolve('./node_modules/gxd-vue-library/src/common'); let toUtilsPath = path.resolve('./src/utils'); let fromCommonScssPath = path.resolve('./node_modules/gxd-vue-library/src/common.scss'); let toCommonScssPath = path.resolve('./src/common.scss'); let fromMixinsPath = path.resolve('./node_modules/gxd-vue-library/src/mixins'); let toMixinsPath = path.resolve('./src/mixins'); let fromTestDataPath = path.resolve('./node_modules/gxd-vue-library/src/cityData.js'); let toTestDataPath = path.resolve('./src/cityData.js'); let fromViewsPackPath = path.resolve('./node_modules/gxd-vue-library/src/viewsPack'); let toViewsPackPath = path.resolve('./src/viewsPack'); let fromGulpfileJsPath = path.resolve('./node_modules/gxd-vue-library/gulpfile.js'); let toGulpfileJsPath = path.resolve('./gulpfile.js'); /** * @description 初始化风格 */ const initStyle = () => { let logs = []; const getStyle = (key) => { if (process.env.status === 'test' && fileHelper.existFileSync(getPackage) ) { const {getPackagePath} = require(getPackage); return { fromPath: fileHelper.getPlatformSurePath(getPackagePath(`/src/${key}`)), toPath: path.resolve(`./src/${key}`) } } return { fromPath: path.resolve(`./node_modules/gxd-vue-library/src/${key}`), toPath: path.resolve(`./src/${key}`) } }; ['stylesBlue', 'stylesGreen'].map(item => { let {fromPath, toPath} = getStyle(item); if (fileHelper.existFileSync(fromPath)) { fileHelper.copySync(fromPath, toPath); logs.push(fileHelper.getPlatformSurePath(toPath)); } }); clog('拷贝风格文件,目录:', 'green'); clog(`${JSON.stringify(logs, null, 2)}`, 'cyan'); }; //本地获取拷贝框架资源 const getPackage = `${basePath.rootDir}/get.package.path.js`; if (process.env.status === 'test' && fileHelper.existFileSync(getPackage) ) { const {getPackagePath} = require(getPackage); fromLibraryPath = fileHelper.getPlatformSurePath(getPackagePath('/src/components')); fromDomePath = fileHelper.getPlatformSurePath(getPackagePath('/src/dome')); fromUtilsPath = fileHelper.getPlatformSurePath(getPackagePath('/src/common')); fromCommonScssPath = fileHelper.getPlatformSurePath(getPackagePath('/src/common.scss')); fromMixinsPath = fileHelper.getPlatformSurePath(getPackagePath('/src/mixins')); fromTestDataPath = fileHelper.getPlatformSurePath(getPackagePath('/src/cityData.js')); fromViewsPackPath = fileHelper.getPlatformSurePath(getPackagePath('/src/viewsPack')); fromGulpfileJsPath = fileHelper.getPlatformSurePath(getPackagePath('/gulpfile.js')); clog(`从本地获取拷贝框架资源,目录:${os.EOL}${JSON.stringify({ fromLibraryPath, fromDomePath, fromUtilsPath, fromCommonScssPath, fromMixinsPath, fromTestDataPath, fromViewsPackPath, fromGulpfileJsPath }, null, 2)}`, 'cyan'); clog(`${os.EOL}`); } /** * @description 执行命令 * @param callback {Function|null} 执行完成回调 * @param isServer {Boolean} 是否为自定义打包命令 */ const init = (callback = null , isServer=false)=>{ //copy components if(!isServer || (isServer && !fileHelper.existFileSync(toLibraryPath))) { fileHelper.copySync(fromLibraryPath, toLibraryPath); clog(`复制插件库成功,目录:${toLibraryPath}`, 'green'); } //copy dome if (!isServer || (isServer && !fileHelper.existFileSync(toDomePath))) { fileHelper.copySync(fromDomePath,toDomePath); clog(`复制DOME文件成功,目录:${toDomePath}`, 'green'); } //copy common if (!isServer || (isServer && !fileHelper.existFileSync(toUtilsPath))) { fileHelper.copySync(fromUtilsPath, toUtilsPath); clog(`复制工具函数文件成功,目录:${toUtilsPath}`, 'green'); } //copy common.scss if (!isServer || (isServer && !fileHelper.existFileSync(toCommonScssPath))) { fileHelper.copySync(fromCommonScssPath,toCommonScssPath); clog(`复制common.scss成功,目录:${toCommonScssPath}`, 'green'); } //copy mixins if (!isServer || (isServer && !fileHelper.existFileSync(toMixinsPath))) { fileHelper.copySync(fromMixinsPath, toMixinsPath); clog(`复制mixins成功,目录:${toMixinsPath}`, 'green'); } //copy TestData if (!isServer || (isServer && !fileHelper.existFileSync(toTestDataPath))) { fileHelper.copySync(fromTestDataPath, toTestDataPath); clog(`复制TestData成功,目录:${toTestDataPath}`, 'green'); } //copy gulp if (!isServer || (isServer && !fileHelper.existFileSync(toGulpfileJsPath))) { fileHelper.copySync(fromGulpfileJsPath, toGulpfileJsPath); clog(`复制模块名字成功,目录:${toGulpfileJsPath}`, 'green'); } //copy ViewsPack下的子目录(只拷贝一次,再次拷贝需要删除子目录在执行更新任务) let fromViewsPackArray = fileHelper.readdirSync(fromViewsPackPath); if (!fileHelper.existFileSync(toViewsPackPath)) { fileHelper.mkdirSync(path.resolve('./src'), 'viewsPack'); } let copyArr = []; let existArr = []; fromViewsPackArray.map(item=>{ if(item.type === 'dir') { let fromPath = fromViewsPackPath + `/${item.name}`; let toPath = toViewsPackPath + `/${item.name}`; if(!fileHelper.existFileSync(toPath)) { fileHelper.copySync(fromPath, toPath); copyArr.push({ form: fileHelper.getPlatformSurePath(fromPath), to: fileHelper.getPlatformSurePath(toPath) }) } else{ existArr.push({ form: fileHelper.getPlatformSurePath(fromPath), to: fileHelper.getPlatformSurePath(toPath) }) } } }); if (copyArr.length > 0) { clog(`复制ViewsPack子目录成功,${os.EOL}目录:${JSON.stringify(copyArr, null, 2)}`, 'green'); } else { clog(`复制ViewsPack子目录数量(0)个`, "green"); } if (existArr.length > 0) { clog(`ViewsPack已存在子目录数量(${existArr.length}),${os.EOL}目录:${JSON.stringify(existArr, null, 2)}`, 'red'); } //初始化风格 initStyle(); //检查并更新package.json if(!isServer) { let res = fileHelper.readFileSync(packageDirectory); let pack = JSON.parse(res); let newDevDependencies = []; let newDependencies = []; Object.keys(htmlConfig.devDependencies).map(key => { if (!pack.devDependencies[key]) { pack.devDependencies[key] = htmlConfig.devDependencies[key]; newDevDependencies.push(key); } }); Object.keys(htmlConfig.dependencies).map(key => { if (!pack.dependencies[key]) { pack.dependencies[key] = htmlConfig.dependencies[key]; newDependencies.push(key); } }); let packStr = JSON.stringify(pack, null, 2); fileHelper.writeFileSync(packageDirectory, packStr); let arr = newDevDependencies.concat(newDependencies); if (arr.length > 0) { clog(`更新完成【Package.json】`, 'green'); clog(`请执行【npm install】命令`, 'red'); clog(arr, 'green'); process.exit(0); } else { clog(`无需更新插件包【Package.json】`, 'green') } } if(typeof callback === 'function') callback(); }; module.exports = init;