Skip to content

Commit

Permalink
- refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Nefjodov committed Dec 18, 2022
1 parent 202ba58 commit cbac3a3
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONFIG=../../../../../dev.json
CONFIG=dev.json
31 changes: 0 additions & 31 deletions browser/build.js

This file was deleted.

47 changes: 47 additions & 0 deletions browser/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {build} from "esbuild";
import {transformSync} from "esbuild";
import NodeModulesPolyfills from '@esbuild-plugins/node-modules-polyfill';
import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill';
import fs from "fs";
import path from "path";


class Build
{
private readonly distName = "../dist_client";
private readonly distPath = path.resolve(__dirname, this.distName);

public constructor()
{
fs.rmSync(this.distPath, {recursive: true, force: true});
fs.mkdirSync(this.distPath);

this.copy("./test.html", "/test.html");
this.copy("../dev.json", "/dev.json");

transformSync("isBrowser", {define: {isBrowser: "true"}});
build({
plugins: [NodeModulesPolyfills(), GlobalsPolyfills({process: true, buffer: true})],
entryPoints: [
'./test/index.ts',
],
outfile: "./dist_client" + "/test.js",
bundle: true,
loader: {".ts": "ts"},
define: {
"global": 'window'
}
}
).then(() => console.log("⚡ Done")).catch(() => process.exit(1));
}

private copy(from: string, to: string): void
{
fs.copyFileSync(
path.resolve(__dirname, from),
path.resolve(__dirname, this.distName + to)
);
}
}

new Build();
16 changes: 16 additions & 0 deletions browser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"declaration": true,
"noImplicitAny": true,
"module": "commonjs",
"sourceMap": true,
"target": "ES2015",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"noImplicitOverride": true,
"moduleResolution": "Node",
"outDir": "./dist",
"strict": true
}
}
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "domwires",
"version": "0.9.118",
"version": "0.9.120",
"description": "Flexible and extensible MVC framework",
"repository": {
"type": "git",
Expand All @@ -16,7 +16,8 @@
"@types/node": "18.7.12",
"inversify": "6.0.1",
"inversify-inject-decorators": "3.1.0",
"reflect-metadata": "0.1.13"
"reflect-metadata": "0.1.13",
"browser-or-node": "2.1.1"
},
"devDependencies": {
"esbuild": "0.15.18",
Expand All @@ -33,16 +34,18 @@
"rimraf": "3.0.2",
"ts-node": "10.9.1",
"typescript": "4.7.4",
"http-server": "14.1.1"
"http-server": "14.1.1",
"@esbuild-plugins/node-modules-polyfill": "0.1.4",
"@esbuild-plugins/node-globals-polyfill": "0.1.1"
},
"engines": {
"node": ">=16.17.0"
},
"scripts": {
"test-node": "mocha -r ts-node/register test/*.ts --timeout=2000 --exclude test/ClientAppTest.ts --exclude test/index.ts",
"test-node": "mocha -r ts-node/register test/*.ts --timeout=2000 --exclude test/index.ts",
"eslint-setup": "npm install eslint --save-dev",
"eslint-run": "eslint . --ext .ts",
"test-browser": "node browser/build.js && http-server ./dist_client -a localhost -p 1234 -o /test.html",
"test-browser": "ts-node --project browser/tsconfig.json browser/build.ts && http-server ./dist_client -a localhost -p 1234 -o /test.html",
"clean": "rimraf dist/ && rimraf dist_client/",
"copy-files": "copyfiles -u 1 dev.json dist/"
}
Expand Down
48 changes: 48 additions & 0 deletions src/com/domwires/core/app/AbstractApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {MessageDispatcher} from "../mvc/message/IMessageDispatcher";
import fs from "fs";
import * as dotenv from "dotenv";
import {isNode} from "browser-or-node";

dotenv.config();

export abstract class AbstractApp<TAppConfig = unknown> extends MessageDispatcher
{
protected _appConfigJson!: TAppConfig;

public get appConfigJson(): TAppConfig
{
return this._appConfigJson;
}

public loadConfig(configLoaded?: (success: boolean) => void): void
{
const configPath = process.env.CONFIG || "./dev" + ".json";

try
{
this.warn("Loading app config:", configPath);

if (isNode)
{
this._appConfigJson = JSON.parse(fs.readFileSync(configPath, "utf-8"));

if (configLoaded) configLoaded(true);
}
else
{
fetch(configPath).then(() =>
{
if (configLoaded) configLoaded(true);
}).catch(() =>
{
if (configLoaded) configLoaded(false);
});
}
} catch (e)
{
this.warn("Failed to load app config:", configPath);

if (configLoaded) configLoaded(false);
}
}
}
24 changes: 0 additions & 24 deletions src/com/domwires/core/app/AbstractServerApp.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/com/domwires/core/app/IApp.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/com/domwires/logger/ILogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Logger extends AbstractDisposable implements ILogger
if (this.loglevel.level >= LogLevel.ERROR.level)
{
console.error(Logger.paintPrefix(this.caller(args), this.t) + " " +
Logger.paintArgs(Color.TP_ANSI_BG_RED, ...args));
Logger.paintArgs(Color.TP_ANSI_BG_CYAN, ...args));
}

return this;
Expand Down
25 changes: 25 additions & 0 deletions test/AppTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "reflect-metadata";
import {Suite} from "mocha";
import {expect} from "chai";
import {Factory, Logger, LogLevel} from "../src";
import {AbstractApp} from "../src/com/domwires/core/app/AbstractApp";

describe('AppTest', function (this: Suite)
{
class MockApp extends AbstractApp<{ name: string; age: number }>
{
}

it('testAppConfig', async () =>
{
const f = new Factory(new Logger(LogLevel.VERBOSE));
const app = f.getInstance<MockApp>(MockApp);

app.loadConfig(success =>
{
expect(success).true;
expect(app.appConfigJson.name).equals("Anton");
expect(app.appConfigJson.age).equals(36);
});
});
});
27 changes: 0 additions & 27 deletions test/ClientAppTest.ts

This file was deleted.

27 changes: 0 additions & 27 deletions test/ServerAppTest.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./AppTest";
export * from "./ArrayUtilsTest";
export * from "./BubbleMessageTest";
export * from "./ClientAppTest";
export * from "./CommandMapperTest";
export * from "./ContextTest";
export * from "./EnumTest";
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"outDir": "./dist",
"noImplicitOverride": true,
"strict": true,
"baseUrl": "."
"baseUrl": ".",
"esModuleInterop": true
},
"include": [
"src/**/*"
Expand Down

0 comments on commit cbac3a3

Please sign in to comment.