Skip to content

Commit

Permalink
chore: 统一日志输出格式
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongzhi107 committed Apr 18, 2018
1 parent d86cd3c commit 2def616
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/bin/packing-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ webpack(webpackConfig, (err, stats) => {
if (err) {
console.log(err);
} else if (stats.hasErrors()) {
console.log(red('❌ ERROR in ', stats.compilation.errors));
console.log(red('💔 webpack: bundle is now INVALID.'));
const message = red(`[错误]: 💔 Webpack 打包失败。\n${stats.compilation.errors}`);
console.log(message);
// 让 jenkins 终止编译
process.exit(1);
} else if (stats.hasWarnings()) {
console.log(yellow('⚠️ webpack: ', stats.compilation.warnings));
const message = yellow(`[警告]: ⚠️ Webpack 打包成功,请关注以下信息:\n${stats.compilation.warnings}`);
console.log(yellow(message));
} else {
console.log(stats.toString(stats));
console.log('💚 webpack: bundle is now VALID.');
console.log('[成功]:💚 Webpack 打包成功。');
}
});
26 changes: 17 additions & 9 deletions src/lib/dotenv.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
import dotenv from 'dotenv';
import chalk from 'chalk';
import { getContext } from '..';
import getExistsFilePath from './getExistsFilePath';

const context = getContext();

Expand All @@ -13,16 +14,23 @@ dotenv.config({
// 当加载 .env 失败时,终止程序
const { NODE_ENV } = process.env;
if (!NODE_ENV) {
console.log([
'[error]: The .env file is not found. ',
'Please run the following command to create the file in the root of the project:',
'echo NODE_ENV=local > .env'
].join('\n'));
const message = `
[错误]: 未能在工程根目录下找到 \`.env\` 文件。
解决方法:
方法1:运行 echo NODE_ENV=local > .env 创建文件。
方法2:启动时传递环境变量 NODE_ENV=<local|beta|production>
`;
console.log(chalk.red(message));
process.exit(1);
}

const envInProject = resolve(getContext(), `profiles/${NODE_ENV}.env`);
const envInLib = resolve(__dirname, `../../profiles/${NODE_ENV}.env`);
const dotenvConfig = existsSync(envInProject) ? envInProject : envInLib;
dotenv.config({ path: dotenvConfig });
console.log(`[dotenv]\`${dotenvConfig}\` loaded.`);
const dotenvConfig = getExistsFilePath(envInProject, envInLib);
try {
dotenv.config({ path: dotenvConfig });
console.log(`[成功]: 配置文件加载成功,文件位置:${dotenvConfig}`);
} catch (e) {
const message = `[错误]: 配置文件加载失败,文件位置:${dotenvConfig}`;
console.log(chalk.red(message));
}

0 comments on commit 2def616

Please sign in to comment.