feat: add global eventCenter & static console source map

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-13 18:41:05 +08:00
parent a5ca0c2346
commit 97db272041
3 changed files with 15 additions and 9 deletions

View File

@ -21,8 +21,8 @@ enum LogLevel {
export class MiaoScriptConsole implements Console { export class MiaoScriptConsole implements Console {
Console: NodeJS.ConsoleConstructor; Console: NodeJS.ConsoleConstructor;
private sourceMaps: { [key: string]: SourceMapBuilder } = {}; private static sourceMaps: { [key: string]: SourceMapBuilder } = {};
private sourceFileMaps: { [key: string]: string } = {}; private static sourceFileMaps: { [key: string]: string } = {};
private _name: string = ''; private _name: string = '';
private _level: LogLevel = LogLevel.INFO; private _level: LogLevel = LogLevel.INFO;
@ -93,8 +93,8 @@ export class MiaoScriptConsole implements Console {
readSourceMap(fileName: string, lineNumber: number) { readSourceMap(fileName: string, lineNumber: number) {
try { try {
if (fileName.endsWith('js')) { if (fileName.endsWith('js')) {
if (this.sourceMaps[fileName] === undefined) { if (MiaoScriptConsole.sourceMaps[fileName] === undefined) {
this.sourceMaps[fileName] = null MiaoScriptConsole.sourceMaps[fileName] = null
let sourceLine = base.read(fileName).split('\n'); let sourceLine = base.read(fileName).split('\n');
let lastLine = sourceLine[sourceLine.length - 1] let lastLine = sourceLine[sourceLine.length - 1]
if (lastLine.startsWith('//# sourceMappingURL=')) { if (lastLine.startsWith('//# sourceMappingURL=')) {
@ -109,14 +109,14 @@ export class MiaoScriptConsole implements Console {
if (file.exists()) { sourceContent = base.read(file) } if (file.exists()) { sourceContent = base.read(file) }
} }
if (sourceContent) { if (sourceContent) {
this.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent)) MiaoScriptConsole.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent))
this.sourceFileMaps[fileName] = Paths.get(fileName, '..', this.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath(); MiaoScriptConsole.sourceFileMaps[fileName] = Paths.get(fileName, '..', MiaoScriptConsole.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath();
} }
} }
} }
if (this.sourceMaps[fileName]) { if (MiaoScriptConsole.sourceMaps[fileName]) {
let sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 25, true, true); let sourceMapping = MiaoScriptConsole.sourceMaps[fileName].getSource(lineNumber, 25, true, true);
fileName = this.sourceFileMaps[fileName] fileName = MiaoScriptConsole.sourceFileMaps[fileName]
if (sourceMapping && lineNumber != sourceMapping.mapping.sourceLine) { lineNumber = sourceMapping.mapping.sourceLine; } if (sourceMapping && lineNumber != sourceMapping.mapping.sourceLine) { lineNumber = sourceMapping.mapping.sourceLine; }
} }
} }

View File

@ -4,6 +4,7 @@ import { plugin, server, task, constants } from '@ccms/api'
import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container' import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ccms/container'
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 }) console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
import http from '@ccms/common/dist/http' import http from '@ccms/common/dist/http'
import { EventEmitter } from 'events'
@provideSingleton(MiaoScriptCore) @provideSingleton(MiaoScriptCore)
class MiaoScriptCore { class MiaoScriptCore {
@ -25,6 +26,7 @@ class MiaoScriptCore {
} }
loadServerConsole() { loadServerConsole() {
global.setGlobal('eventCenter', new EventEmitter(), { writable: false, configurable: false });
//@ts-ignore //@ts-ignore
global.setGlobal('console', new this.Console(), { writable: false, configurable: false }) global.setGlobal('console', new this.Console(), { writable: false, configurable: false })
} }

View File

@ -45,6 +45,7 @@ declare global {
logger: any; logger: any;
debug: boolean; debug: boolean;
level: string; level: string;
eventCenter: EventEmitter;
NashornEngineStartTime: number; NashornEngineStartTime: number;
setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType<any>) => void; setGlobal: (key: string, value: any, config?: PropertyDescriptor & ThisType<any>) => void;
noop: () => void; noop: () => void;
@ -70,6 +71,9 @@ declare global {
console(...args: any): void; console(...args: any): void;
i18n(name: string, ...params: any[]): void; i18n(name: string, ...params: any[]): void;
} }
interface ProxyConstructor {
newProxy<T extends object>(target: T, handler: ProxyHandler<T>): T;
}
} }
export { }; export { };