Skip to content

Commit

Permalink
⚡ 增加自定义主进程日志
Browse files Browse the repository at this point in the history
  • Loading branch information
BySlin committed Sep 26, 2021
1 parent ea679b3 commit d35fe4e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 62 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ export default defineConfig({
// if (type === 'main') {}
// if (type === 'preload') {}
},
//2.1.8新增 开启自定义主进程日志时,你需要自己过滤electron无用日志
//2.1.9新增 开启自定义主进程日志时
logProcess(log: string, type: LogType) {
// if (type === 'normal') {
// logProcess('Main', log, chalk.blue);
// } else if (type === 'error') {
// logProcess('Main', log, chalk.red);
// }
if (type === 'normal') {
console.log(log);
} else if (type === 'error') {
console.error(log);
}
},
builderOptions: {
//配置参考 https://www.electron.build/configuration/configuration
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"prettier": "^2.3.2",
"react": "17.x",
"react-dom": "17.x",
"umi-plugin-electron-builder": "^2.1.8",
"umi-plugin-electron-builder": "^2.1.9",
"yorkie": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion examples/main+renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"prettier": "^2.3.2",
"react": "17.x",
"react-dom": "17.x",
"umi-plugin-electron-builder": "^2.1.8",
"umi-plugin-electron-builder": "^2.1.9",
"yorkie": "^2.0.0"
}
}
14 changes: 11 additions & 3 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,19 @@ var runDev = function runDev(api) {
}

spawnProcess = child_process_1.spawn(String(electronPath), ['--inspect=5858', path_1.default.join(utils_1.getDevBuildDir(api), 'main.js')]);
spawnProcess.stdout.on('data', function (d) {
return logProcess(d.toString(), 'normal');
spawnProcess.stdout.on('data', function (data) {
var log = utils_1.filterText(data.toString());

if (log) {
logProcess(log, 'normal');
}
});
spawnProcess.stderr.on('data', function (data) {
logProcess(data.toString(), 'error');
var log = utils_1.filterText(data.toString());

if (log) {
logProcess(log, 'error');
}
});
spawnProcess.on('close', function (code, signal) {
if (signal != 'SIGKILL') {
Expand Down
9 changes: 2 additions & 7 deletions lib/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/// <reference types="node" />
import { IApi } from 'umi';
import { ChildProcess } from 'child_process';
export declare function debounce(f: () => void, ms: number): () => void;
export declare function installRely(pkgName: string): void;
export declare function getRootPkg(): any;
Expand All @@ -10,9 +8,6 @@ export declare function getPreloadSrc(api: IApi): string;
export declare function getDevBuildDir(api: IApi): string;
export declare function getBuildDir(api: IApi): string;
export declare function getAbsOutputDir(api: IApi): string;
export interface LineFilter {
filter(line: string): boolean;
}
export declare function logProcessErrorOutput(label: 'Electron' | 'Renderer' | 'Main', childProcess: ChildProcess): void;
export declare function filterText(s: string): string | null;
export declare function logError(label: 'Electron' | 'Renderer' | 'Main', error: Error): void;
export declare function logProcess(label: 'Electron' | 'Renderer' | 'Main', data: string | Buffer, labelColor: any, lineFilter?: LineFilter | null): void;
export declare function logProcess(label: 'Electron' | 'Renderer' | 'Main', log: string, labelColor: any): void;
25 changes: 7 additions & 18 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.logProcess = exports.logError = exports.logProcessErrorOutput = exports.getAbsOutputDir = exports.getBuildDir = exports.getDevBuildDir = exports.getPreloadSrc = exports.getMainSrc = exports.getNodeModulesPath = exports.getRootPkg = exports.installRely = exports.debounce = void 0;
exports.logProcess = exports.logError = exports.filterText = exports.getAbsOutputDir = exports.getBuildDir = exports.getDevBuildDir = exports.getPreloadSrc = exports.getMainSrc = exports.getNodeModulesPath = exports.getRootPkg = exports.installRely = exports.debounce = void 0;

var path_1 = __importDefault(require("path"));

Expand Down Expand Up @@ -153,17 +153,17 @@ function getAbsOutputDir(api) {

exports.getAbsOutputDir = getAbsOutputDir;

function filterText(s, lineFilter) {
function filterText(s) {
var lines = s.trim().split(/\r?\n/).filter(function (it) {
if (lineFilter != null && !lineFilter.filter(it)) {
if (it.includes("Couldn't set selectedTextBackgroundColor from default ()")) {
return false;
}

if (it.includes("Couldn't set selectedTextBackgroundColor from default ()")) {
if (it.includes("Use NSWindow's -titlebarAppearsTransparent=YES instead.")) {
return false;
}

if (it.includes("Use NSWindow's -titlebarAppearsTransparent=YES instead.")) {
if (it.includes('Debugger listening on')) {
return false;
}

Expand All @@ -177,27 +177,16 @@ function filterText(s, lineFilter) {
return ' ' + lines.join("\n ") + '\n';
}

function logProcessErrorOutput(label, childProcess) {
childProcess.stderr.on('data', function (data) {
logProcess(label, data.toString(), chalk_1.default.red);
});
}

exports.logProcessErrorOutput = logProcessErrorOutput;
exports.filterText = filterText;

function logError(label, error) {
logProcess(label, error.stack || error.toString(), chalk_1.default.red);
}

exports.logError = logError;

function logProcess(label, data, labelColor, lineFilter) {
if (lineFilter === void 0) {
lineFilter = null;
}

function logProcess(label, log, labelColor) {
var LABEL_LENGTH = 28;
var log = filterText(data.toString(), lineFilter);

if (log == null || log.length === 0 || log.trim().length === 0) {
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "umi-plugin-electron-builder",
"version": "2.1.8",
"version": "2.1.9",
"description": "Umi plugin for electron-builder",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
14 changes: 11 additions & 3 deletions src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IApi, utils } from 'umi';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import {
debounce,
filterText,
getDevBuildDir,
getMainSrc,
getNodeModulesPath,
Expand Down Expand Up @@ -94,11 +95,18 @@ export const runDev = async (api: IApi) => {
'--inspect=5858',
path.join(getDevBuildDir(api), 'main.js'),
]);
spawnProcess.stdout.on('data', (d) => logProcess(d.toString(), 'normal'));
spawnProcess.stdout.on('data', (data) => {
const log = filterText(data.toString());
if (log) {
logProcess(log, 'normal');
}
});
spawnProcess.stderr.on('data', (data) => {
logProcess(data.toString(), 'error');
const log = filterText(data.toString());
if (log) {
logProcess(log, 'error');
}
});

spawnProcess.on('close', (code, signal) => {
if (signal != 'SIGKILL') {
process.exit(-1);
Expand Down
27 changes: 5 additions & 22 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from 'path';
import * as fse from 'fs-extra';
import { IApi, utils } from 'umi';
import chalk from 'chalk';
import { ChildProcess } from 'child_process';
import { ElectronBuilder } from '../types';

const { execa } = utils;
Expand Down Expand Up @@ -123,22 +122,14 @@ export function getAbsOutputDir(api: IApi) {
return path.join(process.cwd(), outputDir);
}

export interface LineFilter {
filter(line: string): boolean;
}

/**
* 过滤electron输出
*/
function filterText(s: string, lineFilter: LineFilter | null) {
export function filterText(s: string) {
const lines = s
.trim()
.split(/\r?\n/)
.filter((it) => {
if (lineFilter != null && !lineFilter.filter(it)) {
return false;
}

// https://github.com/electron/electron/issues/4420
// this warning can be safely ignored
if (
Expand All @@ -151,6 +142,9 @@ function filterText(s: string, lineFilter: LineFilter | null) {
) {
return false;
}
if (it.includes('Debugger listening on')) {
return false;
}
return (
!it.includes(
'Warning: This is an experimental feature and could change at any time.',
Expand All @@ -166,15 +160,6 @@ function filterText(s: string, lineFilter: LineFilter | null) {
return ' ' + lines.join(`\n `) + '\n';
}

export function logProcessErrorOutput(
label: 'Electron' | 'Renderer' | 'Main',
childProcess: ChildProcess,
) {
childProcess.stderr!!.on('data', (data) => {
logProcess(label, data.toString(), chalk.red);
});
}

export function logError(
label: 'Electron' | 'Renderer' | 'Main',
error: Error,
Expand All @@ -184,12 +169,10 @@ export function logError(

export function logProcess(
label: 'Electron' | 'Renderer' | 'Main',
data: string | Buffer,
log: string,
labelColor: any,
lineFilter: LineFilter | null = null,
) {
const LABEL_LENGTH = 28;
const log = filterText(data.toString(), lineFilter);
if (log == null || log.length === 0 || log.trim().length === 0) {
return;
}
Expand Down

0 comments on commit d35fe4e

Please sign in to comment.