From a41536515956be98c8586029407b9700718daea7 Mon Sep 17 00:00:00 2001 From: pengfeizhou Date: Sun, 7 Feb 2021 16:33:31 +0800 Subject: [PATCH] feat:Optimize console log --- doric-cli/src/actions.ts | 13 ++++++++----- doric-cli/src/dev.ts | 5 ++++- doric-cli/src/server.ts | 20 ++++++++++++++------ doric-demo/src/Counter.ts | 3 +++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/doric-cli/src/actions.ts b/doric-cli/src/actions.ts index 3f17af66..9df3ac12 100644 --- a/doric-cli/src/actions.ts +++ b/doric-cli/src/actions.ts @@ -29,9 +29,12 @@ async function doMerge(jsFile: string) { export async function mergeMap(mapFile: string) { - const mergedMap = createMergedSourceMapFromFiles([ - mapFile.replace(/bundle\//, 'build/'), - mapFile, - ], true); - await fs.promises.writeFile(mapFile, mergedMap); + const buildMap = mapFile.replace(/bundle\//, 'build/') + if (fs.existsSync(buildMap)) { + const mergedMap = createMergedSourceMapFromFiles([ + buildMap, + mapFile, + ], true); + await fs.promises.writeFile(mapFile, mergedMap); + } } \ No newline at end of file diff --git a/doric-cli/src/dev.ts b/doric-cli/src/dev.ts index 8385c65f..371db129 100644 --- a/doric-cli/src/dev.ts +++ b/doric-cli/src/dev.ts @@ -83,7 +83,10 @@ export default async function dev() { return; } try { - const sourceMap = mergeMap(`${jsFile}.map`); + const sourceMap = `${jsFile}.map` + if (fs.existsSync(sourceMap)) { + mergeMap(sourceMap); + } server.connections.forEach((e: any) => { e.sendText( JSON.stringify({ diff --git a/doric-cli/src/server.ts b/doric-cli/src/server.ts index 83616d42..fe768c73 100644 --- a/doric-cli/src/server.ts +++ b/doric-cli/src/server.ts @@ -8,19 +8,20 @@ export async function createServer() { let contextId: string = "0" let clientConnection: any = null let debuggerConnection: any = null + let deviceId = 0 const server = (ws as any).createServer((connection: any) => { + let thisDeviceId = deviceId++ console.log('Connected', connection.headers.host) if (connection.headers.host.startsWith("localhost")) { - console.log(`Debugger ${connection.key} attached to dev kit`.green) + console.log(`Debugger ${thisDeviceId} attached to dev kit`.green) debuggerConnection = connection clientConnection.sendText(JSON.stringify({ cmd: 'SWITCH_TO_DEBUG', contextId: contextId }), () => { }) } else { - console.log(`Client ${connection.key} attached to dev kit`.green) + console.log(`Client ${thisDeviceId} attached to dev kit`.green) } - connection.on('text', function (result: string) { let resultObject = JSON.parse(result) switch (resultObject.cmd) { @@ -49,12 +50,19 @@ export async function createServer() { console.log(resultObject.data.exception.red); break; case 'LOG': + const date = new Date + const format = function (num: number) { + return (Array(2).join("0") + num).slice(-2); + }; + const timeStr = `${format(date.getHours())}:${format(date.getMinutes())}:${format(date.getSeconds())}.${(Array(3).join("0") + date.getMilliseconds()).slice(-3)}` + let logContent = resultObject.data.message as string + if (resultObject.data.type == 'DEFAULT') { - console.log(`>>>>>>${(resultObject.data.message as string).green}>>>>>>`); + console.log(`${timeStr} Device ${thisDeviceId} ${"[I]".green} ${logContent.green}`.bgBlue); } else if (resultObject.data.type == 'ERROR') { - console.log(`>>>>>>${(resultObject.data.message as string).red}>>>>>>`); + console.log(`${timeStr} Device ${thisDeviceId} ${"[E]".green} ${logContent.green}`.bgRed); } else if (resultObject.data.type == 'WARN') { - console.log(`>>>>>>${(resultObject.data.message as string).yellow}>>>>>>`); + console.log(`${timeStr.black} ${("Device " + thisDeviceId).black} ${"[W]".green} ${logContent.green}`.bgYellow); } break } diff --git a/doric-demo/src/Counter.ts b/doric-demo/src/Counter.ts index 7bcb2361..33ea75f1 100644 --- a/doric-demo/src/Counter.ts +++ b/doric-demo/src/Counter.ts @@ -41,6 +41,9 @@ class CounterVM extends ViewModel { } onBind(s: CountModel, vh: CounterView) { vh.number.text = `${s.count}` + log("onBind\nseee") + logw("onBind") + loge("onBind") } }