Skip to content

Commit

Permalink
fix: display of double import warning
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Oct 4, 2023
1 parent 7117c6b commit c55f2d8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
13 changes: 13 additions & 0 deletions packages/core/src/core.spec.ts
Expand Up @@ -5,6 +5,7 @@ import * as sinon from "sinon";
import { stub } from "sinon";
import { Core, OriginFilter, WebsiteOriginFilter } from "./core";
import {
Application,
Authentication,
Bean,
ConsoleLoggerService,
Expand Down Expand Up @@ -600,6 +601,18 @@ class CoreTest extends WebdaTest {
// @ts-ignore
assert.strictEqual(this.webda._contextProviders[0], provider);
assert.throws(() => this.webda.registerOperation("__proto__", undefined), Error);

assert.ok(Core['getSingletonInfo'](this.webda).match(/- file:\/\/.*packages\/core\/src\/core.ts \/ \d+.\d+.\d+/));
}

@test
async testSingleton() {
new Core(this.webda.getApplication());
// @ts-ignore
process.webda = this.webda;
// This should generate a warning
Core.get();
// Could check things here
}

@test
Expand Down
26 changes: 24 additions & 2 deletions packages/core/src/core.ts
Expand Up @@ -427,16 +427,38 @@ export class Core<E extends CoreEvents = CoreEvents> extends events.EventEmitter
this.setGlobalContext(new GlobalContext(this));
}

/**
* Get the current script location
* @returns
*/
private getScriptUrl() {
return import.meta.url;
}

/**
* Return information on the singleton import and version
* @param singleton
* @returns
*/
private static getSingletonInfo(singleton: Core) : string {
let res = `- `
if (singleton.getScriptUrl) {
res += singleton.getScriptUrl();
}
res += " / " + singleton.getVersion();
return res;
}

/**
* Get the singleton of Webda Core
* @returns
*/
static get(): Core {
// @ts-ignore
let singleton: Core = process.webda;
if (Core.singleton !== singleton && !singleton._dualImportWarn) {
if (Core.singleton !== singleton && !singleton._dualImportWarn && Core.singleton !== undefined && singleton !== undefined) {
singleton._dualImportWarn = true;
singleton.log("WARN", "Several import version of WebdaCore has been identified");
singleton.log("ERROR", `Several import version of WebdaCore has been identified.\n\tIt can impact your models.\n\t- ${this.getSingletonInfo(singleton)}\n\t- ${this.getSingletonInfo(Core.singleton)}`);
}
// Store WebdaCore in process to avoid conflict with import
return singleton;
Expand Down
16 changes: 8 additions & 8 deletions sample-app/webda.module.json
Expand Up @@ -601,7 +601,7 @@
"type": "string"
},
"interfaceParam": {
"$ref": "#/definitions/Partial%3Cinterface-1076421816-246-294-1076421816-0-1238%3E",
"$ref": "#/definitions/Partial%3Cinterface-829200435-246-294-829200435-0-1238%3E",
"description": "The Partial will generate a new $ref"
},
"openapi": {
Expand All @@ -616,7 +616,7 @@
],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Partial<interface-1076421816-246-294-1076421816-0-1238>": {
"Partial<interface-829200435-246-294-829200435-0-1238>": {
"type": "object",
"properties": {
"test": {
Expand Down Expand Up @@ -1074,7 +1074,7 @@
"description": "URL on which to serve the content"
},
"introspection": {
"$ref": "#/definitions/Partial%3Cclass-178760925-2914-3398-178760925-0-9208976353190%3E"
"$ref": "#/definitions/Partial%3Cclass-1131383939-2914-3398-1131383939-0-9208976353190%3E"
},
"openapi": {
"type": "object",
Expand All @@ -1087,7 +1087,7 @@
],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Partial<class-178760925-2914-3398-178760925-0-9208976353190>": {
"Partial<class-1131383939-2914-3398-1131383939-0-9208976353190>": {
"type": "object",
"properties": {
"type": {
Expand Down Expand Up @@ -1118,7 +1118,7 @@
"type": "string"
},
"interfaceParam": {
"$ref": "#/definitions/Partial%3Cinterface-1076421816-246-294-1076421816-0-1238%3E",
"$ref": "#/definitions/Partial%3Cinterface-829200435-246-294-829200435-0-1238%3E",
"description": "The Partial will generate a new $ref"
},
"fourthParameter": {
Expand All @@ -1139,7 +1139,7 @@
],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Partial<interface-1076421816-246-294-1076421816-0-1238>": {
"Partial<interface-829200435-246-294-829200435-0-1238>": {
"type": "object",
"properties": {
"test": {
Expand Down Expand Up @@ -1341,7 +1341,7 @@
"type": "string"
},
"interfaceParam": {
"$ref": "#/definitions/Partial%3Cinterface-1076421816-246-294-1076421816-0-1238%3E",
"$ref": "#/definitions/Partial%3Cinterface-829200435-246-294-829200435-0-1238%3E",
"description": "The Partial will generate a new $ref"
},
"openapi": {
Expand All @@ -1356,7 +1356,7 @@
],
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Partial<interface-1076421816-246-294-1076421816-0-1238>": {
"Partial<interface-829200435-246-294-829200435-0-1238>": {
"type": "object",
"properties": {
"test": {
Expand Down

0 comments on commit c55f2d8

Please sign in to comment.