Skip to content

Commit

Permalink
* Added type declaration for .eslintrc.mjs.
Browse files Browse the repository at this point in the history
* Isolated logging code into LogFactory.mts.
* Moved build scripts to "build" directory.
* Fixed unit-tests.
  • Loading branch information
cowwoc committed Dec 5, 2023
1 parent 25e96d6 commit 49fb5f9
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 260 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Linter} from "eslint";

declare const config: Linter.Config;
export default config;
49 changes: 49 additions & 0 deletions build/LogFactory.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
createLogger,
format,
Logger,
transports
} from "winston";

// https://stackoverflow.com/a/63486530/14731
const Reset = "\x1b[0m";
const FgWhite = "\x1b[37m";
const BgRed = "\x1b[41m";

class LogFactory
{
/**
* @param name the name of the logger
*/
public static getLogger(name: string): Logger
{
return createLogger({
transports: [new transports.Console()],
format: format.combine(
format(info =>
{
// https://github.com/winstonjs/winston/issues/1345#issuecomment-393853665
info.level = info.level.toUpperCase();
return info;
})(),
format.errors({stack: true}),
format.prettyPrint(),
format.colorize(),
format.timestamp({format: "YYYY-MM-DD HH:mm:ss.SSS"}),
format.printf(({
timestamp,
level,
message,
stack
}) =>
{
if (stack)
return `${timestamp} ${level} ${FgWhite + BgRed + name + Reset} - ${message}\n${stack}`;
return `${timestamp} ${level} ${FgWhite + BgRed + name + Reset} - ${message}`;
})
)
});
}
}

export {LogFactory};
71 changes: 8 additions & 63 deletions scripts/build.mts → build/Project.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as url from "url";
import url from "url";
import path from "path";
import {ESLint} from "eslint";
// @ts-ignore
import eslintConfig from "../.eslintrc.mjs";
import TypeDoc from "typedoc";
import fs from "fs";
import fs from "node:fs";
import rollupCommonjs from "@rollup/plugin-commonjs";
import {nodeResolve as rollupNodeResolve} from "@rollup/plugin-node-resolve";
import rollupTypescript from "@rollup/plugin-typescript";
Expand All @@ -17,15 +15,11 @@ import ts from "typescript";
import {glob} from "glob";
import {minify} from "terser";
import {spawn} from "child_process";
import {
createLogger,
format,
Logger,
transports
} from "winston";
import {mode} from "./mode.mjs";
import {LogFactory} from "./LogFactory.mjs";
import eslintConfig from "../.eslintrc.mjs";

class Build
class Project
{
private readonly mode: string;

Expand Down Expand Up @@ -86,7 +80,7 @@ class Build
// them.
config.include = config.include.filter((element: string) =>
{
return element !== "build.mts" && !element.startsWith("test/");
return element !== "build/Project.mts" && !element.startsWith("test/");
});
config.compilerOptions.outDir = "target/publish/node/";
config.compilerOptions.declaration = true;
Expand Down Expand Up @@ -283,62 +277,13 @@ class Build
}
}

// https://stackoverflow.com/a/63486530/14731
const Reset = "\x1b[0m";
const FgWhite = "\x1b[37m";
const BgRed = "\x1b[41m";

class LogFactory
{
/**
* @param name the name of the logger
* @param mode the operational mode ("DEBUG" or "RELEASE")
*/
public static getLogger(name: string, mode: string): Logger
{
let messageFormat;
if (mode === "DEBUG")
messageFormat = format.prettyPrint();
else
messageFormat = format.simple();
return createLogger({
transports: [new transports.Console()],
format: format.combine(
format(info =>
{
// https://github.com/winstonjs/winston/issues/1345#issuecomment-393853665
info.level = info.level.toUpperCase();
return info;
})(),
format.errors({stack: true}),
messageFormat,
format.colorize(),
format.timestamp({format: "YYYY-MM-DD HH:mm:ss.SSS"}),
format.printf(({
timestamp,
level,
message,
stack
}) =>
{
if (stack)
{
return `${timestamp} ${level} ${FgWhite + BgRed + name + Reset} - ${message}\n${stack}`;
}
return `${timestamp} ${level} ${FgWhite + BgRed + name + Reset} - ${message}`;
})
)
});
}
}

console.time("Time elapsed");

const __filename = path.basename(url.fileURLToPath(import.meta.url));
const log = LogFactory.getLogger(__filename, mode);
const log = LogFactory.getLogger(__filename);
log.info(mode + " mode detected");

const build = new Build(mode);
const build = new Project(mode);
await Promise.all([build.lint(), build.compileForNode(), build.compileForBrowser(),
build.generateDocumentation(), build.copyResources()]);
await build.test();
Expand Down
2 changes: 1 addition & 1 deletion scripts/TestCompiler.mts → build/TestCompiler.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestCompiler
private static readonly rootDir: string = path.resolve(".");
// We have to include at least one existing file to avoid CompilerHost complaining that it couldn't find any
// input files
private static readonly existingFileToSuppressError = "scripts/build.mts";
private static readonly existingFileToSuppressError = "build/Project.mts";
private static readonly snippetFilename = "test.mts";
private static readonly config = TestCompiler.createParsedCommandLine();
private static readonly defaultCompilerHost = ts.createCompilerHost(TestCompiler.config.options);
Expand Down
File renamed without changes.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"url": "https://github.com/cowwoc/requirements.js/"
},
"scripts": {
"build.debug": "node node_modules/tsx/dist/cli.mjs scripts/build.mts --mode=DEBUG",
"build.release": "node node_modules/tsx/dist/cli.mjs scripts/build.mts --mode=RELEASE"
"build.debug": "node node_modules/tsx/dist/cli.mjs build/Project.mts --mode=DEBUG",
"build.release": "node node_modules/tsx/dist/cli.mjs build/Project.mts --mode=RELEASE"
},
"browser": "browser/index.js",
"module": "node/index.mjs",
Expand All @@ -33,25 +33,25 @@
"license": "Apache-2.0",
"packageManager": "[email protected]",
"devDependencies": {
"@eslint/js": "^8.54.0",
"@eslint/js": "^8.55.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/chai": "^4.3.11",
"@types/diff": "^5.0.8",
"@types/eslint": "^8.44.7",
"@types/eslint": "^8.44.8",
"@types/lodash": "^4.14.202",
"@types/minimist": "^1.2.5",
"@types/mocha": "^10.0.6",
"@types/node": "^20.9.4",
"@types/node": "^20.10.3",
"@types/rollup-plugin-node-globals": "^1.4.4",
"@types/tmp": "^0.2.6",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"browser-sync": "^2.29.3",
"c8": "^8.0.1",
"chai": "^4.3.10",
"eslint": "^8.54.0",
"eslint": "^8.55.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-tsdoc": "^0.2.17",
Expand All @@ -60,19 +60,19 @@
"install": "^0.13.0",
"minimist": "^1.2.8",
"mocha": "^10.2.0",
"pnpm": "^8.10.5",
"pnpm": "^8.11.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rollup": "^4.5.1",
"rollup": "^4.6.1",
"rollup-plugin-node-globals": "^1.4.0",
"source-map-support": "^0.5.21",
"strip-ansi": "^7.1.0",
"taffydb": "^2.7.3",
"terser": "^5.24.0",
"terser": "^5.25.0",
"tmp": "^0.2.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"tsx": "^4.4.0",
"tsx": "^4.6.2",
"typedoc": "^0.25.4",
"typedoc-plugin-missing-exports": "^2.1.0",
"typescript": "^5.3.2",
Expand Down
Loading

0 comments on commit 49fb5f9

Please sign in to comment.