chore(fes-cli): 规范cli输出的日志

This commit is contained in:
harrywan 2020-10-27 14:31:37 +08:00
parent bf07558130
commit a2da5d9821
11 changed files with 43 additions and 34 deletions

View File

@ -13,6 +13,7 @@ const HtmlPlugin = require('html-webpack-plugin');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const autoprefixer = require('autoprefixer');
const browsers = require('../helpers/browser');
const log = require('../helpers/log');
function handleGzipCompress(compress) {
@ -489,7 +490,7 @@ module.exports = function webpackConfig(configs, webpack, mode) {
let advancedConfig = {};
const projectWebpackConfigFile = path.resolve(configs.folders.PROJECT_DIR, 'webpack.config.js');
if (fs.existsSync(projectWebpackConfigFile)) {
console.log('[init] 加载项目个性webpack配置文件');
log.message('加载项目个性webpack配置');
// eslint-disable-next-line
advancedConfig = require(projectWebpackConfigFile)(mode, configs, webpack);
}

View File

@ -1,6 +1,5 @@
const path = require('path');
const fs = require('fs');
const log = require('./log');
function generateConfig(command, env) {
// cli目录
@ -51,12 +50,6 @@ function generateConfig(command, env) {
}
}
if (!config.needCDN) {
if (config.command === 'dev' || config.command === 'build') {
log.warn('项目没有配置cdn打包之后将不会请求cdn的地址请开发者注意');
}
}
return config;
}

View File

@ -16,6 +16,10 @@ module.exports = function createDevServer(port, defaultConfig) {
defaultConfig.plugins.push(new webpack.NamedModulesPlugin());
const app = express();
// 初始化Mock数据
initMock(app);
const compiler = webpack(defaultConfig);
// devServer 自带支持,添加自定义插件。
@ -40,9 +44,6 @@ module.exports = function createDevServer(port, defaultConfig) {
app.use('/static', express.static('src/static'));
// 初始化Mock数据
initMock(app);
opn(`http://localhost:${port}`);
http.createServer(app).listen(port);

View File

@ -1,13 +1,27 @@
const chalk = require('chalk');
const getTime = function () {
const now = new Date();
return now.toLocaleString();
};
const log = function (type, message) {
// const rn = /\n/.test(message) || /\r/.test(message);
// if (rn) {
// console.log(chalk[type](`${getTime()}[fes] `));
// return console.log(chalk[type](message));
// }
return console.log(`${getTime()} [fes] ${chalk[type](message)}`);
};
module.exports = {
error(msg) {
return console.log(chalk.red(msg));
return log('red', msg);
},
warn(msg) {
return console.log(chalk.yellow(msg));
return log('yellow', msg);
},
message(msg) {
return console.log(chalk.cyan(msg));
return log('cyan', msg);
}
};

View File

@ -59,22 +59,22 @@ const main = {
util.cleanCache(cgiMockFile);
try {
if (!fs.existsSync(cgiMockFile)) {
log.error('[WARN] 不存在mock.js文件');
log.error('mock.js文件不存在,请检查!');
} else {
// eslint-disable-next-line
const projectMock = require(cgiMockFile);
if (util.isFunction(projectMock)) {
global.router.stack = [];
projectMock(cgiMock, Mock, faker);
log.message('[SUCCESS] mock.js 加载成功');
log.message('mock.js 加载成功');
} else {
log.error(
`[ERROR] mock.js cannot be ${typeof projectMock}`
'mock.js 应该导出Function !'
);
}
}
} catch (e) {
log.error('[ERROR] mock.js 有误,请检查');
log.error('mock.js 加载失败,请检查:');
log.error(JSON.stringify(e));
}
};
@ -88,7 +88,7 @@ const main = {
});
util.watchFile(cgiMockFile, () => {
log.message('[INFO] mock.js 发生变化');
log.message('mock.js 发生变化重新加载Mock');
loadRouteConfig();
});
}

View File

@ -127,7 +127,6 @@ const fix = function (routes) {
routes.forEach((item) => {
const path = item.path;
let arr = path.split('/');
// console.log(arr);
if (arr[0] === '') {
arr = arr.slice(1);
}

View File

@ -12,13 +12,14 @@ function startBuild(config) {
const webpackConfig = createProdConfig(config, webpack, 'build');
webpack(webpackConfig, (err) => {
if (err) {
log.error(err);
log.error(JSON.stringify(err));
return;
}
console.log('[build] success');
log.message('执行build成功');
});
} catch (e) {
log.error(e);
log.message('执行build失败');
log.error(JSON.stringify(e));
}
}

View File

@ -52,11 +52,11 @@ function startDev(config) {
getPort(config.ports.server)
.then((port) => {
log.message(`------------ find port success. port: ${port}`);
log.message(`dev本地http服务端口: ${port}`);
createDevServer(port, webpackConfig);
}).catch((err) => {
log.message('------------ build error.');
log.error(err);
log.message('执行dev失败');
log.error(JSON.stringify(err));
});
}

View File

@ -7,15 +7,15 @@ const log = require('../helpers/log');
function createProject(config, projectName) {
log.message('正在初始化项目...');
log.message('正在初始化项目');
const projectDir = path.resolve(config.folders.PROJECT_DIR, projectName);
if (fs.pathExistsSync(projectDir)) {
log.error('该项目已存在,请重新输入!');
return Promise.reject();
}
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
const productDir = `${config.folders.PROJECT_DIR}/${projectName}`;
const stdout = execSync(`npm pack @webank/fes-template`, { encoding: 'utf8', stdio: [null]});
const stdout = execSync('npm pack @webank/fes-template', { encoding: 'utf8', stdio: [null] });
const filePath = path.resolve(config.folders.PROJECT_DIR, stdout.replace('\n', ''));
fs.mkdirSync(projectDir);
fs.createReadStream(filePath).pipe(

View File

@ -24,15 +24,15 @@ module.exports = function (config, rawArgv) {
stdio: 'inherit'
});
child.on('error', (e) => {
log.error('[ERROR] test命令执行失败');
log.error('执行test失败');
log.error(JSON.stringify(e));
});
child.on('exit', (code) => {
if (code !== 0) {
log.message(`[fes] mochapack进程退出code ${code}.`);
log.message(`mochapack进程退出code ${code}.`);
}
});
} else {
log.warn('[WRAN] 测试目录不存在请在项目根目录创建目录test');
log.warn('测试目录不存在请在项目根目录创建目录test');
}
};

View File

@ -5,10 +5,10 @@ function update(config) {
log.message('安装@webank/fes-core @webank/fes-ui...');
exec(`cd ${config.folders.PROJECT_DIR} && npm i @webank/fes-core @webank/fes-ui --save && npm i`, (err) => {
if (err) {
console.error(err);
log.error(err);
return;
}
log.message('升级完毕');
log.message('升级fes完毕');
});
}