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 2185b1a2f8
commit ac88c15113
3 changed files with 15 additions and 9 deletions

View File

@ -21,8 +21,8 @@ enum LogLevel {
export class MiaoScriptConsole implements Console {
Console: NodeJS.ConsoleConstructor;
private sourceMaps: { [key: string]: SourceMapBuilder } = {};
private sourceFileMaps: { [key: string]: string } = {};
private static sourceMaps: { [key: string]: SourceMapBuilder } = {};
private static sourceFileMaps: { [key: string]: string } = {};
private _name: string = '';
private _level: LogLevel = LogLevel.INFO;
@ -93,8 +93,8 @@ export class MiaoScriptConsole implements Console {
readSourceMap(fileName: string, lineNumber: number) {
try {
if (fileName.endsWith('js')) {
if (this.sourceMaps[fileName] === undefined) {
this.sourceMaps[fileName] = null
if (MiaoScriptConsole.sourceMaps[fileName] === undefined) {
MiaoScriptConsole.sourceMaps[fileName] = null
let sourceLine = base.read(fileName).split('\n');
let lastLine = sourceLine[sourceLine.length - 1]
if (lastLine.startsWith('//# sourceMappingURL=')) {
@ -109,14 +109,14 @@ export class MiaoScriptConsole implements Console {
if (file.exists()) { sourceContent = base.read(file) }
}
if (sourceContent) {
this.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent))
this.sourceFileMaps[fileName] = Paths.get(fileName, '..', this.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath();
MiaoScriptConsole.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent))
MiaoScriptConsole.sourceFileMaps[fileName] = Paths.get(fileName, '..', MiaoScriptConsole.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath();
}
}
}
if (this.sourceMaps[fileName]) {
let sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 25, true, true);
fileName = this.sourceFileMaps[fileName]
if (MiaoScriptConsole.sourceMaps[fileName]) {
let sourceMapping = MiaoScriptConsole.sourceMaps[fileName].getSource(lineNumber, 25, true, true);
fileName = MiaoScriptConsole.sourceFileMaps[fileName]
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'
console.i18n("ms.core.ioc.completed", { scope: global.scope, time: (Date.now() - containerStartTime) / 1000 })
import http from '@ccms/common/dist/http'
import { EventEmitter } from 'events'
@provideSingleton(MiaoScriptCore)
class MiaoScriptCore {
@ -25,6 +26,7 @@ class MiaoScriptCore {
}
loadServerConsole() {
global.setGlobal('eventCenter', new EventEmitter(), { writable: false, configurable: false });
//@ts-ignore
global.setGlobal('console', new this.Console(), { writable: false, configurable: false })
}

View File

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