UNPKG

custom-app

Version:

ITIMS��Ʒ�鿪��ר��React���,�Dz��ý��ּ�dhcc-app���������

300 lines (267 loc) 10.4 kB
#! /usr/bin/env node let exec = require('child_process').exec; const webpack = require('webpack') const fs = require('fs-extra') const path = require('path') const chalk = require('chalk') const util = require('../util/util') const spinner = require('../util/spinner'); const os = require('os') function build (projectName, options) { projectName = projectName === "all" ? "." : projectName; const cwd = options.cwd || process.cwd() const reactAppPath = util.getReactAppPath(cwd); const inCurrent = projectName === '.' const name = inCurrent ? path.relative('../', cwd) : projectName const targetDir = path.resolve(cwd, projectName || '.') const webappPath = path.join(path.dirname(reactAppPath), "webapp") if (!fs.existsSync(webappPath)) { fs.mkdirSync(webappPath); } const reactappPath = path.join(webappPath, "reactapp") if (!fs.existsSync(reactappPath)) { fs.mkdirSync(reactappPath); } const moduleappPath = path.join(reactappPath, require(path.join(reactAppPath, "package.json")).name) if (!fs.existsSync(moduleappPath)) { fs.mkdirSync(moduleappPath); } if (inCurrent) { //顶级目录全部打包 let list = []; fs.readdir(path.join(reactAppPath, "src"), function (err, files) { files.forEach(function (v, i, array) { // webpackSPA(reactAppPath, v) let promise = function () { return new Promise(function (resolve, reject) { delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'webpack.base.conf.js'))] delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'webpack.prod.conf.js'))] delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'project.js'))] delete require.cache[require.resolve(path.join(reactAppPath, 'config', 'rootpath.js'))] if (util.isPlugin(reactAppPath, v)) { webpackPlugin2(reactAppPath, v, function () { resolve(); }) } else { webpackSPA(reactAppPath, v, function () { resolve(); }) } }) } list.push(promise) }) queue(list) .then(data => { }) }) } else { if (util.isPlugin(reactAppPath, projectName)) { webpackPlugin2(reactAppPath, projectName) } else { webpackSPA(reactAppPath, projectName) } } } async function queue (arr) { let res = null for (let promise of arr) { res = await promise(res) } return await res } function webpackSPA (reactAppPath, projectName, callback) { fs.writeFileSync(path.join(reactAppPath, 'config', 'rootpath.js'), `exports.path = '${reactAppPath.split("\\")}'`) fs.writeFileSync(path.join(reactAppPath, 'config', 'project.js'), `exports.name = '${projectName}'`) spinner.logWithSpinner('', chalk.cyan(`开始打包${projectName}\n`)); webpack(require(path.join(reactAppPath, 'config', 'webpack.prod.conf.js')), function (err, stats) { process.stdout.write(stats.toString({ colors: true, modules: false, children: false, chunks: false, chunkModules: false })) if (!err) { spinner.logWithSpinner(chalk.cyan(`打包${projectName}成功`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); callback && callback.call(); } else { spinner.logWithSpinner(chalk.cyan(`打包${projectName}失败`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); } }) // exec(`webpack --config ${path.join(reactAppPath, 'config', 'webpack.prod.conf.js')}`, { cwd: reactAppPath }, function (err, stdout, stderr) { // if (err) { // console.error(chalk.red(`打包${projectName}失败:` + stderr)); // console.log(stdout); // spinner.stopSpinner(); // process.exit(1) // } else { // spinner.logWithSpinner('', stdout) // spinner.logWithSpinner(chalk.cyan(`打包${projectName}成功`)) // spinner.logWithSpinner(' ', ' '); // spinner.stopSpinner(); // callback && callback.call(); // } // }); } function webpackPlugin2 (reactAppPath, projectName, callback) { var npmLogin = require('npm-cli-login'); var conf = require('./plugin/userinfo.js') if (conf.userinfo) { var userinfo = JSON.parse(conf.userinfo) if (userinfo.username && userinfo.password && userinfo.email) { //var registry = 'http://172.24.24.236:8081/repository/host236' var registry = 'https://registry.npmjs.org/' npmLogin(userinfo.username, userinfo.password, userinfo.email, registry) } else { throw Error(`用户信息无效,${JSON.stringify(userinfo)}`) } } else { throw Error("缺少用户信息,请先通过dhcc login命令录入用户信息") } fs.writeFileSync(path.join(reactAppPath, 'config', 'rootpath.js'), `exports.path = '${reactAppPath.split("\\")}'`) fs.writeFileSync(path.join(reactAppPath, 'config', 'project.js'), `exports.name = '${projectName}'`) spinner.logWithSpinner('', chalk.cyan(`开始打包${projectName} prod\n`)); let list = [] let promise = function () { return new Promise(function (resolve, reject) { webpack(require(path.join(reactAppPath, 'config', 'webpack.prod.conf.js')), function (err, stats) { process.stdout.write(stats.toString({ colors: true, modules: false, children: false, chunks: false, chunkModules: false })) if (!err) { spinner.logWithSpinner(chalk.cyan(`打包${projectName} prod成功`)) resolve(); } else { spinner.logWithSpinner(chalk.cyan(`打包${projectName}失败`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); process.exit(1) } }) }) } list.push(promise); promise = function () { return new Promise(function (resolve, reject) { spinner.logWithSpinner('', chalk.cyan(`开始打包${projectName} dev\n`)); webpack(require(path.join(reactAppPath, 'config', 'webpack.dev.conf.js')), function (err, stats) { process.stdout.write(stats.toString({ colors: true, modules: false, children: false, chunks: false, chunkModules: false })) if (!err) { spinner.logWithSpinner(chalk.cyan(`打包${projectName} dev成功`)) resolve(); } else { spinner.logWithSpinner(chalk.cyan(`打包${projectName} dev失败`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); process.exit(1) } }) }) } list.push(promise); promise = function () { return new Promise(function (resolve, reject) { spinner.logWithSpinner('', chalk.cyan(`开始上传${projectName} \n`)); var packageJSON = require(path.join(reactAppPath, "src", projectName, 'package.json')) /** package.json文件的version参数 */ var version = packageJSON.version var newversion = handleType(version) packageJSON.version = newversion; fs.writeFileSync(path.join(reactAppPath, "src", projectName, 'package.json'), JSON.stringify(packageJSON, null, 2) + os.EOL); exec(`npm publish`, { cwd: path.join(reactAppPath, "src", projectName) }, function (err, stdout, stderr) { if (err) { console.error(chalk.red(`publish ${projectName}失败:` + stderr)); console.log(stdout); spinner.stopSpinner(); process.exit(1) } else { spinner.logWithSpinner('', stdout) spinner.logWithSpinner(chalk.cyan(`publish ${projectName}成功`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); callback && callback.call(); } }) }) } list.push(promise); queue(list) .then(data => { }) } function webpackPlugin (reactAppPath, projectName, callback) { var npmLogin = require('npm-cli-login'); var conf = require('./plugin/userinfo.js') if (conf.userinfo) { var userinfo = JSON.parse(conf.userinfo) if (userinfo.username && userinfo.password && userinfo.email) { var registry = 'https://registry.npmjs.org/' npmLogin(userinfo.username, userinfo.password, userinfo.email, registry) } else { throw Error(`用户信息无效,${JSON.stringify(userinfo)}`) } } else { throw Error("缺少用户信息,请先通过dhcc login命令录入用户信息") } var packageJSON = require(path.join(reactAppPath, "src", projectName, 'package.json')) /** package.json文件的version参数 */ var version = packageJSON.version var newversion = handleType(version) packageJSON.version = newversion; fs.writeFileSync(path.join(reactAppPath, "src", projectName, 'package.json'), JSON.stringify(packageJSON, null, 2) + os.EOL); exec(`npm publish`, { cwd: path.join(reactAppPath, "src", projectName) }, function (err, stdout, stderr) { if (err) { console.error(chalk.red(`publish ${projectName}失败:` + stderr)); console.log(stdout); spinner.stopSpinner(); process.exit(1) } else { spinner.logWithSpinner('', stdout) spinner.logWithSpinner(chalk.cyan(`publish ${projectName}成功`)) spinner.logWithSpinner(' ', ' '); spinner.stopSpinner(); callback && callback.call(); } }) } function handleType (oldVersion, type) { var oldVersionArr = oldVersion.split("."); //版本号第一位 如:1.2.3 则为 1 var firstNum = +oldVersionArr[0]; //版本号第二位 如:1.2.3 则为 2 var secondNum = +oldVersionArr[1]; //版本号第三位 如:1.2.3 则为 3 var thirdNum = +oldVersionArr[2]; switch (type) { case "release": //release分支的处理逻辑 ++secondNum; break; case "hotfix": //hotfix分支的处理逻辑 ++thirdNum; break; default: ++thirdNum; break; } return firstNum + "." + secondNum + "." + thirdNum; } module.exports = (...args) => { build(...args) }