Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
refactor: 优化 notify 的显示
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Jun 15, 2020
1 parent 1a9ff35 commit 46e4f7a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
3 changes: 0 additions & 3 deletions packages/remax-cli/src/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ describe('output', () => {
it('notify', () => {
output('test', 'red');
expect(notifier.notify).not.toBeCalled();

output('test', 'red', true);
expect(notifier.notify).toBeCalledTimes(1);
});
});
15 changes: 11 additions & 4 deletions packages/remax-cli/src/build/mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ export default function buildMini(api: API, options: Options): webpack.Compiler
output.message('🚀 启动 watch\n', 'blue');
const watcher = compiler.watch({}, (error, stats) => {
if (error) {
output.error(error.message, notify);
output.error(error.message);
if (notify) {
output.notice(error.message);
}
throw error;
}

const info = stats.toJson();

if (stats.hasErrors()) {
output.error(info.errors.join('\n'), notify);
const message = info.errors.join('\n');
output.error(message);
if (notify) {
output.notice(message);
}
}

if (stats.hasWarnings()) {
Expand All @@ -41,15 +48,15 @@ export default function buildMini(api: API, options: Options): webpack.Compiler
output.message('🚀 启动 build\n', 'blue');
compiler.run((error, stats) => {
if (error) {
output.error(error.message, notify);
output.error(error.message);

throw error;
}

const info = stats.toJson();

if (stats.hasErrors()) {
output.error(info.errors.join('\n'), notify);
output.error(info.errors.join('\n'));

process.exit(1);
}
Expand Down
24 changes: 6 additions & 18 deletions packages/remax-cli/src/build/utils/output.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import notifier from 'node-notifier';

const COLORS = {
Expand All @@ -8,39 +9,26 @@ const COLORS = {
};
const RESET = '\x1b[0m';

export const output = (content: string | string[], color: 'red' | 'green' | 'blue' | 'yellow', notify = false) => {
export const output = (content: string | string[], color: 'red' | 'green' | 'blue' | 'yellow') => {
const message = Array.isArray(content) ? content : [content];
console.log(`${COLORS[color]}%s${RESET}`, ...message);

if (notify) {
notifier.notify({
title: 'Remax',
message: message.join(' '),
});
}
};

function log(type: 'error' | 'warn', message: string, notify?: boolean) {
function log(type: 'error' | 'warn', message: string) {
console[type](message);

if (notify) {
notifier.notify({
title: 'Remax',
message,
});
}
}

function notice(message: string) {
notifier.notify({
title: 'Remax',
title: 'Remax build error',
message,
icon: path.join(__dirname, '../../../error.png'),
});
}

export default {
message: output,
error: (message: string, notify?: boolean) => log('error', message, notify),
error: (message: string) => log('error', message),
warn: (message: string) => log('warn', message),
notice,
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { Options } from '@remax/types';

export function walk(api: API, filePath: string, templatePaths: Set<string>, options: Options) {
if (!fs.existsSync(filePath)) {
output.error(`文件 ${filePath} 不存在`, options.notify);
output.error(`文件 ${filePath} 不存在`);
if (options.notify) {
output.notice(`文件 ${filePath} 不存在`);
}
return;
}

Expand Down

0 comments on commit 46e4f7a

Please sign in to comment.