@cley_faye/boilerplate 3.3.1 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -10
- package/grunt.js +1 -1
- package/lib/express/logger.js +3 -35
- package/lib/express/pipelinebuilder.d.ts +6 -2
- package/lib/express/pipelinebuilder.js +44 -24
- package/lib/express.d.ts +5 -2
- package/lib/express.js +3 -0
- package/lib/grunt/reactapp/copy.d.ts +1 -1
- package/lib/grunt/reactapp/image.d.ts +1 -1
- package/lib/grunt/reactapp/pug.d.ts +1 -1
- package/lib/grunt/reactapp/sass.d.ts +1 -1
- package/lib/grunt/reactapp/webpack.d.ts +1 -1
- package/lib/grunt/reactapp/webpack.js +3 -3
- package/lib/repl.d.ts +27 -0
- package/lib/repl.js +68 -0
- package/lib/winston.d.ts +14 -0
- package/lib/winston.js +52 -2
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ example):
|
|
|
79
79
|
const {
|
|
80
80
|
reactApp,
|
|
81
81
|
reactAppDynamicTasks,
|
|
82
|
-
} = require("@cley_faye/boilerplate/grunt");
|
|
82
|
+
} = require("@cley_faye/boilerplate/grunt.js");
|
|
83
83
|
|
|
84
84
|
module.exports = grunt => {
|
|
85
85
|
const baseGruntConfig = {};
|
|
@@ -181,7 +181,7 @@ The example from before can become:
|
|
|
181
181
|
const {
|
|
182
182
|
reactApp,
|
|
183
183
|
reactAppOptionsHelper,
|
|
184
|
-
} = require("@cley_faye/boilerplate/grunt");
|
|
184
|
+
} = require("@cley_faye/boilerplate/grunt.js");
|
|
185
185
|
|
|
186
186
|
module.exports = grunt => {
|
|
187
187
|
const baseGruntConfig = {};
|
|
@@ -239,7 +239,7 @@ It provide basic type checking and inline help display for arguments passed to G
|
|
|
239
239
|
Usage:
|
|
240
240
|
|
|
241
241
|
```JavaScript
|
|
242
|
-
const {getOpts, OptType} = require("@cley_faye/boilerplate/grunt");
|
|
242
|
+
const {getOpts, OptType} = require("@cley_faye/boilerplate/grunt.js");
|
|
243
243
|
module.exports = grunt => {
|
|
244
244
|
const opts = getOpts(
|
|
245
245
|
grunt,
|
|
@@ -289,7 +289,7 @@ Quick sample:
|
|
|
289
289
|
|
|
290
290
|
```JavaScript
|
|
291
291
|
import express from "express";
|
|
292
|
-
import {createPipeline} from "@cley_faye/boilerplate/lib/express";
|
|
292
|
+
import {createPipeline} from "@cley_faye/boilerplate/lib/express.js";
|
|
293
293
|
|
|
294
294
|
const app = express();
|
|
295
295
|
app.use(createPipeline({
|
|
@@ -371,7 +371,7 @@ root of the service, or an object with the following properties:
|
|
|
371
371
|
#### Configuring the template engine
|
|
372
372
|
|
|
373
373
|
```JavaScript
|
|
374
|
-
import {setViewEngine} from "@cley_faye/boilerplate/lib/express";
|
|
374
|
+
import {setViewEngine} from "@cley_faye/boilerplate/lib/express.js";
|
|
375
375
|
|
|
376
376
|
const app = express();
|
|
377
377
|
setViewEngine(app, "pug", "webres/views", {});
|
|
@@ -385,9 +385,9 @@ Once the express application is defined, it need to be started to serve files an
|
|
|
385
385
|
A helper is provided to do so, using the `appStart()` function.
|
|
386
386
|
|
|
387
387
|
```JavaScript
|
|
388
|
-
import consoleLogger from "@cley_faye/boilerplate/lib/winston";
|
|
389
|
-
import app from "./app";
|
|
390
|
-
import {appStart} from "@cley_faye/boilerplate/lib/express";
|
|
388
|
+
import {consoleLogger} from "@cley_faye/boilerplate/lib/winston.js";
|
|
389
|
+
import app from "./app.js";
|
|
390
|
+
import {appStart} from "@cley_faye/boilerplate/lib/express.js";
|
|
391
391
|
|
|
392
392
|
appStart({
|
|
393
393
|
app,
|
|
@@ -410,7 +410,7 @@ The function returns a promise that resolve with the actual port used for listen
|
|
|
410
410
|
The server is automatically registered using the `autoclose` part to stop when a SIGINT signal is
|
|
411
411
|
received.
|
|
412
412
|
It is also possible to trigger a stop by calling the `closeServer()` function on
|
|
413
|
-
`@cley_faye/boilerplate/lib/express/autoclose`.
|
|
413
|
+
`@cley_faye/boilerplate/lib/express/autoclose.js`.
|
|
414
414
|
This feature only supports one server at a time.
|
|
415
415
|
|
|
416
416
|
Console logging
|
|
@@ -418,7 +418,7 @@ Console logging
|
|
|
418
418
|
For convenience, a winston logger using console as output is provided:
|
|
419
419
|
|
|
420
420
|
```JavaScript
|
|
421
|
-
import consoleLogger from "@cley_faye/boilerplate/lib/winston";
|
|
421
|
+
import {consoleLogger} from "@cley_faye/boilerplate/lib/winston.js";
|
|
422
422
|
|
|
423
423
|
consoleLogger.info("Log line");
|
|
424
424
|
```
|
|
@@ -436,3 +436,11 @@ configuration to `true`.
|
|
|
436
436
|
|
|
437
437
|
For Express app, there is no particular things to take care, except that if you use a facility like
|
|
438
438
|
`nodemon` you have to watch the TypeScript output instead of the actual source files.
|
|
439
|
+
|
|
440
|
+
REPL
|
|
441
|
+
----
|
|
442
|
+
This library provides a function to start a simple REPL instance (using node's built-in repl module)
|
|
443
|
+
to provide access to JavaScript features.
|
|
444
|
+
|
|
445
|
+
Basic usage includes predefined behavior for providing a database object and a list of services.
|
|
446
|
+
The export is in `lib/repl.js`.
|
package/grunt.js
CHANGED
package/lib/express/logger.js
CHANGED
|
@@ -33,38 +33,6 @@ exports.registerErrorLogger = exports.registerRouteLogger = void 0;
|
|
|
33
33
|
const winston_1 = __importDefault(require("winston"));
|
|
34
34
|
const express_winston_1 = __importDefault(require("express-winston"));
|
|
35
35
|
const winston_js_1 = require("../winston.js");
|
|
36
|
-
// High value because of colorize()
|
|
37
|
-
const LOG_LEVEL_PADDING_SIZE = 19;
|
|
38
|
-
/**
|
|
39
|
-
* Collapse all lines containing /node_modules/ into an ellipsis
|
|
40
|
-
*/
|
|
41
|
-
const filterNodeModules = function* (message) {
|
|
42
|
-
let inNodeModules = false;
|
|
43
|
-
const lines = message.split("\n");
|
|
44
|
-
for (const line of lines) {
|
|
45
|
-
if (/\/node_modules\//u.exec(line)) {
|
|
46
|
-
if (!inNodeModules) {
|
|
47
|
-
inNodeModules = true;
|
|
48
|
-
yield "[...]";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
inNodeModules = false;
|
|
53
|
-
yield line;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Prefix all lines with the level and the timestamp if needed
|
|
59
|
-
*/
|
|
60
|
-
const prefixOutput = (level, message, timestamp) => {
|
|
61
|
-
const timestampPrefix = timestamp
|
|
62
|
-
? `${new Date().toISOString()} `
|
|
63
|
-
: "";
|
|
64
|
-
const levelPrefix = `${level.toString().padStart(LOG_LEVEL_PADDING_SIZE)}: `;
|
|
65
|
-
return message.split("\n").map(line => `${timestampPrefix}${levelPrefix}${line}`)
|
|
66
|
-
.join("\n");
|
|
67
|
-
};
|
|
68
36
|
const customRouteFormat = (outputTimestamp) => winston_1.default.format.printf((info) => {
|
|
69
37
|
const meta = info.meta;
|
|
70
38
|
// Clean the meta object from stuff displayed in the regular message
|
|
@@ -86,7 +54,7 @@ const customRouteFormat = (outputTimestamp) => winston_1.default.format.printf((
|
|
|
86
54
|
}
|
|
87
55
|
const metaString = JSON.stringify(info.meta);
|
|
88
56
|
const finalMessage = `${info.message} ${metaString === "{}" ? "" : metaString}`;
|
|
89
|
-
return prefixOutput(info.level, finalMessage, outputTimestamp);
|
|
57
|
+
return (0, winston_js_1.prefixOutput)(info.level, finalMessage, outputTimestamp);
|
|
90
58
|
});
|
|
91
59
|
const registerRouteLogger = (app, logOptions) => {
|
|
92
60
|
const { route, logger, timestamp } = logOptions ?? {};
|
|
@@ -131,9 +99,9 @@ const customErrorFormat = (outputTimestamp, config) => {
|
|
|
131
99
|
return winston_1.default.format.printf((info) => {
|
|
132
100
|
const meta = info.meta;
|
|
133
101
|
const finalMessage = collapseNodeModules
|
|
134
|
-
? [...filterNodeModules(meta.message)].join("\n")
|
|
102
|
+
? [...(0, winston_js_1.filterNodeModules)(meta.message)].join("\n")
|
|
135
103
|
: meta.message;
|
|
136
|
-
return prefixOutput(info.level, finalMessage, outputTimestamp);
|
|
104
|
+
return (0, winston_js_1.prefixOutput)(info.level, finalMessage, outputTimestamp);
|
|
137
105
|
});
|
|
138
106
|
};
|
|
139
107
|
const registerErrorLogger = (app, logOptions) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import express from "express";
|
|
2
2
|
import { OptionsJson, OptionsUrlencoded, OptionsText, Options as OptionsRaw } from "body-parser";
|
|
3
|
-
import { LoggerOptions, ErrorLoggerOptions } from "./logger.js";
|
|
4
3
|
import winston from "winston";
|
|
4
|
+
import { LoggerOptions, ErrorLoggerOptions } from "./logger.js";
|
|
5
5
|
export declare type LogFunction = (...args: Array<string | number | Record<string, unknown>>) => void;
|
|
6
6
|
export interface MiddlewareOptions {
|
|
7
7
|
urlencoded?: boolean | OptionsUrlencoded;
|
|
@@ -30,6 +30,10 @@ export interface LogOptions {
|
|
|
30
30
|
logger?: winston.Logger;
|
|
31
31
|
timestamp?: boolean;
|
|
32
32
|
}
|
|
33
|
+
export interface DefaultErrorHandlerConfig {
|
|
34
|
+
/** Set to true to allow server errors without a code to be sent as-is in the response */
|
|
35
|
+
noErrorFilter?: boolean;
|
|
36
|
+
}
|
|
33
37
|
export interface PipelineSettings {
|
|
34
38
|
topLevels?: Array<RouterWithNullDef>;
|
|
35
39
|
routes?: Array<RouterWithNullDef>;
|
|
@@ -39,7 +43,7 @@ export interface PipelineSettings {
|
|
|
39
43
|
options?: {
|
|
40
44
|
log?: LogOptions;
|
|
41
45
|
middleware?: MiddlewareOptions;
|
|
42
|
-
defaultErrorHandler?: boolean;
|
|
46
|
+
defaultErrorHandler?: boolean | DefaultErrorHandlerConfig;
|
|
43
47
|
};
|
|
44
48
|
}
|
|
45
49
|
export default class PipelineBuilder {
|
|
@@ -30,31 +30,42 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
const express_1 = __importDefault(require("express"));
|
|
33
|
+
const winston_js_1 = require("../winston.js");
|
|
33
34
|
const logger_js_1 = require("./logger.js");
|
|
34
|
-
const defaultErrorHandler = (err, req, res, next) => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
const defaultErrorHandler = (errorHandlerConfig) => (err, req, res, next) => {
|
|
36
|
+
const noErrorFilter = typeof errorHandlerConfig === "boolean"
|
|
37
|
+
? false
|
|
38
|
+
: Boolean(errorHandlerConfig.noErrorFilter);
|
|
39
|
+
if (!res.headersSent) {
|
|
40
|
+
if (err.statusCode) {
|
|
41
|
+
const message = err.expose
|
|
42
|
+
? err.message
|
|
43
|
+
: undefined;
|
|
44
|
+
const accepted = req.accepts([
|
|
45
|
+
"text",
|
|
46
|
+
"html",
|
|
47
|
+
"json",
|
|
48
|
+
]);
|
|
49
|
+
if (accepted === "json") {
|
|
50
|
+
res.status(err.statusCode).send({
|
|
51
|
+
statusCode: err.statusCode,
|
|
52
|
+
message,
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (message) {
|
|
57
|
+
res.status(err.statusCode).send(message);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
res.sendStatus(err.statusCode);
|
|
61
|
+
}
|
|
49
62
|
return;
|
|
50
63
|
}
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
res.sendStatus(err.statusCode);
|
|
64
|
+
if (!noErrorFilter) {
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
66
|
+
res.status(500).send("An unexpected error occured");
|
|
67
|
+
return;
|
|
56
68
|
}
|
|
57
|
-
return;
|
|
58
69
|
}
|
|
59
70
|
next(err);
|
|
60
71
|
};
|
|
@@ -88,6 +99,15 @@ class PipelineBuilder {
|
|
|
88
99
|
createPipeline({ topLevels, routes, statics, postStatics, errorHandlers, options, }) {
|
|
89
100
|
const { middleware, log, defaultErrorHandler: useDefaultErrorHandler } = options ?? {};
|
|
90
101
|
this.internalLog("createPipeline()");
|
|
102
|
+
if (log) {
|
|
103
|
+
const errorConfig = typeof log.error === "boolean"
|
|
104
|
+
? {}
|
|
105
|
+
: log.error ?? {};
|
|
106
|
+
(0, winston_js_1.setConfig)({
|
|
107
|
+
timestamp: log.timestamp ?? false,
|
|
108
|
+
collapseNodeModules: errorConfig.collapseNodeModules ?? true,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
91
111
|
const router = express_1.default.Router();
|
|
92
112
|
this.internalLog("setGenericMiddlewares()");
|
|
93
113
|
this._setGenericMiddlewares(router, middleware);
|
|
@@ -247,10 +267,10 @@ class PipelineBuilder {
|
|
|
247
267
|
(0, logger_js_1.registerErrorLogger)(router, logOptions);
|
|
248
268
|
}
|
|
249
269
|
}
|
|
250
|
-
_setDefaultErrorHandler(router,
|
|
251
|
-
if (
|
|
270
|
+
_setDefaultErrorHandler(router, defaultErrorHandlerConfig) {
|
|
271
|
+
if (defaultErrorHandlerConfig) {
|
|
252
272
|
this.internalLog("Adding default error handler");
|
|
253
|
-
router.use(defaultErrorHandler);
|
|
273
|
+
router.use(defaultErrorHandler(defaultErrorHandlerConfig));
|
|
254
274
|
}
|
|
255
275
|
}
|
|
256
276
|
}
|
package/lib/express.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="express-serve-static-core" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import {
|
|
3
|
+
import { Server } from "net";
|
|
4
4
|
import express from "express";
|
|
5
5
|
import winston from "winston";
|
|
6
|
-
import {
|
|
6
|
+
import { PipelineSettings, LogFunction } from "./express/pipelinebuilder.js";
|
|
7
7
|
/** Create the full processing pipeline for an Express server
|
|
8
8
|
*
|
|
9
9
|
* This function creates a pipeline this way:
|
|
@@ -23,6 +23,9 @@ import { Server } from "net";
|
|
|
23
23
|
* If the request accepts json, a json object with the error is returned.
|
|
24
24
|
* Otherwise text is returned.
|
|
25
25
|
*
|
|
26
|
+
* For convenience, providing timestamp and collapseNodeModules here will automatically
|
|
27
|
+
* update the basic consoleLogger configuration.
|
|
28
|
+
*
|
|
26
29
|
* @param settings
|
|
27
30
|
*
|
|
28
31
|
* @param [settings.topLevels]
|
package/lib/express.js
CHANGED
|
@@ -51,6 +51,9 @@ const pipelinebuilder_js_1 = __importDefault(require("./express/pipelinebuilder.
|
|
|
51
51
|
* If the request accepts json, a json object with the error is returned.
|
|
52
52
|
* Otherwise text is returned.
|
|
53
53
|
*
|
|
54
|
+
* For convenience, providing timestamp and collapseNodeModules here will automatically
|
|
55
|
+
* update the basic consoleLogger configuration.
|
|
56
|
+
*
|
|
54
57
|
* @param settings
|
|
55
58
|
*
|
|
56
59
|
* @param [settings.topLevels]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GruntConfig } from "./util.js";
|
|
2
1
|
import { BaseOptions } from "../util.js";
|
|
3
2
|
import { HandlerFunctionResult } from "../reactapp.js";
|
|
3
|
+
import { GruntConfig } from "./util.js";
|
|
4
4
|
export interface CopyOptions extends BaseOptions {
|
|
5
5
|
options?: Record<string, unknown>;
|
|
6
6
|
excludedExtensions?: Array<string>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GruntConfig } from "./util.js";
|
|
2
1
|
import { BaseOptions } from "../util.js";
|
|
3
2
|
import { HandlerFunctionResult } from "../reactapp.js";
|
|
3
|
+
import { GruntConfig } from "./util.js";
|
|
4
4
|
export interface ImageOptions extends BaseOptions {
|
|
5
5
|
options?: Record<string, unknown>;
|
|
6
6
|
sourcePath?: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GruntConfig } from "./util.js";
|
|
2
1
|
import { BaseOptions } from "../util.js";
|
|
3
2
|
import { HandlerFunctionResult } from "../reactapp.js";
|
|
3
|
+
import { GruntConfig } from "./util.js";
|
|
4
4
|
export declare type DynamicDataFunc = () => Promise<Record<string, unknown>>;
|
|
5
5
|
export interface PugOptions extends BaseOptions {
|
|
6
6
|
options?: Record<string, unknown>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GruntConfig } from "./util.js";
|
|
2
1
|
import { BaseOptions } from "../util.js";
|
|
3
2
|
import { HandlerFunctionResult } from "../reactapp.js";
|
|
3
|
+
import { GruntConfig } from "./util.js";
|
|
4
4
|
export interface WebpackLoadersOptions {
|
|
5
5
|
development?: boolean;
|
|
6
6
|
defines?: Record<string, string>;
|
|
@@ -31,9 +31,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
exports.handle = exports.webpackLoadersDefault = void 0;
|
|
33
33
|
const path_1 = require("path");
|
|
34
|
-
const util_js_1 = require("./util.js");
|
|
35
34
|
const eslint_webpack_plugin_1 = __importDefault(require("eslint-webpack-plugin"));
|
|
36
35
|
const resolve_typescript_plugin_1 = __importDefault(require("resolve-typescript-plugin"));
|
|
36
|
+
const util_js_1 = require("./util.js");
|
|
37
37
|
const defaultCoreJS = 3;
|
|
38
38
|
const eslintPlugin = () => new eslint_webpack_plugin_1.default({ cache: true });
|
|
39
39
|
const defaultLoadersDefine = (options) => [
|
|
@@ -241,7 +241,7 @@ const handle = (gruntConfig, targetName, webpackOptions) => {
|
|
|
241
241
|
babel: { plugins: webpackOptions.babelPlugins },
|
|
242
242
|
typescript: webpackOptions.typescript,
|
|
243
243
|
});
|
|
244
|
-
const
|
|
244
|
+
const webpackResolve = computeWebpackResolve(webpackOptions);
|
|
245
245
|
const webpackConfig = {
|
|
246
246
|
mode: webpackOptions.mode,
|
|
247
247
|
devtool: webpackOptions.mode === "development" ? "eval-source-map" : false,
|
|
@@ -253,7 +253,7 @@ const handle = (gruntConfig, targetName, webpackOptions) => {
|
|
|
253
253
|
eslintPlugin(),
|
|
254
254
|
...webpackOptions.plugins ?? [],
|
|
255
255
|
],
|
|
256
|
-
resolve,
|
|
256
|
+
resolve: webpackResolve,
|
|
257
257
|
};
|
|
258
258
|
// Special case: I'm not sure I can move options in the task-specific part of
|
|
259
259
|
// the configuration, so I add it at the toplevel of the "webpack" task.
|
package/lib/repl.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Function called when the REPL is closed */
|
|
2
|
+
export declare type ReplCloseFunc = () => void;
|
|
3
|
+
/** Mimic the required elements from a db object */
|
|
4
|
+
interface DBLikeObject {
|
|
5
|
+
/** db object can have a reference to an initialized sequelize instance */
|
|
6
|
+
sequelize?: {
|
|
7
|
+
/** Sequelize instance should have a close() method */
|
|
8
|
+
close?: () => void;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
interface ReplArgs {
|
|
12
|
+
/** Display a service name in the prompt */
|
|
13
|
+
serviceName: string;
|
|
14
|
+
/**
|
|
15
|
+
* A DB object. If it exposes a db.sequelize.close() function, it will be called when the REPL is
|
|
16
|
+
* closed.
|
|
17
|
+
*/
|
|
18
|
+
db?: DBLikeObject;
|
|
19
|
+
srv?: Record<string, unknown>;
|
|
20
|
+
/** Extra properties to attach to the REPL context */
|
|
21
|
+
extraContext?: Record<string, unknown>;
|
|
22
|
+
onClose?: ReplCloseFunc;
|
|
23
|
+
historyFile?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Start the REPL prompt */
|
|
26
|
+
export declare const startRepl: ({ serviceName, db, srv, extraContext, onClose, historyFile, }: ReplArgs) => void;
|
|
27
|
+
export {};
|
package/lib/repl.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
* MIT License
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2021 Gabriel Paul "Cley Faye" Risterucci
|
|
7
|
+
*
|
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
* in the Software without restriction, including without limitation the rights
|
|
11
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
* furnished to do so, subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
* copies or substantial portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
* SOFTWARE.
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
"use strict";
|
|
28
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.startRepl = void 0;
|
|
33
|
+
const repl_1 = __importDefault(require("repl"));
|
|
34
|
+
const setupContext = (r, db, srv, extraContext) => {
|
|
35
|
+
const doContextSetup = () => {
|
|
36
|
+
Object.assign(r.context, {
|
|
37
|
+
...extraContext,
|
|
38
|
+
db,
|
|
39
|
+
srv,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
doContextSetup();
|
|
43
|
+
r.on("reset", doContextSetup);
|
|
44
|
+
};
|
|
45
|
+
/** Configure action when the REPL is closed */
|
|
46
|
+
const setupClose = (r, db, onClose) => {
|
|
47
|
+
if (!db && !onClose)
|
|
48
|
+
return;
|
|
49
|
+
r.on("exit", () => {
|
|
50
|
+
if (db?.sequelize?.close) {
|
|
51
|
+
db.sequelize.close();
|
|
52
|
+
}
|
|
53
|
+
if (onClose) {
|
|
54
|
+
onClose();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
/** Start the REPL prompt */
|
|
59
|
+
const startRepl = ({ serviceName = "", db, srv, extraContext, onClose, historyFile = ".node_repl_history", }) => {
|
|
60
|
+
const r = repl_1.default.start({
|
|
61
|
+
prompt: `${serviceName}> `,
|
|
62
|
+
breakEvalOnSigint: true,
|
|
63
|
+
});
|
|
64
|
+
setupContext(r, db, srv, extraContext);
|
|
65
|
+
setupClose(r, db, onClose);
|
|
66
|
+
r.setupHistory(historyFile, () => { });
|
|
67
|
+
};
|
|
68
|
+
exports.startRepl = startRepl;
|
package/lib/winston.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
import winston from "winston";
|
|
2
|
+
/**
|
|
3
|
+
* Collapse all lines containing /node_modules/ into an ellipsis
|
|
4
|
+
*/
|
|
5
|
+
export declare const filterNodeModules: (message: string) => Generator<string>;
|
|
6
|
+
/**
|
|
7
|
+
* Prefix all lines with the level and the timestamp if needed
|
|
8
|
+
*/
|
|
9
|
+
export declare const prefixOutput: (level: string, message: string, timestamp: boolean | undefined) => string;
|
|
10
|
+
export interface LogConfig {
|
|
11
|
+
timestamp: boolean;
|
|
12
|
+
collapseNodeModules: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Edit the configuration applied to consoleLogger */
|
|
15
|
+
export declare const setConfig: (config: LogConfig) => void;
|
|
2
16
|
export declare const transports: winston.transports.ConsoleTransportInstance[];
|
|
3
17
|
export declare const consoleLogger: winston.Logger;
|
package/lib/winston.js
CHANGED
|
@@ -29,11 +29,61 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
29
29
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
-
exports.consoleLogger = exports.transports = void 0;
|
|
32
|
+
exports.consoleLogger = exports.transports = exports.setConfig = exports.prefixOutput = exports.filterNodeModules = void 0;
|
|
33
33
|
const winston_1 = __importDefault(require("winston"));
|
|
34
|
+
// High value because of colorize()
|
|
35
|
+
const LOG_LEVEL_PADDING_SIZE = 19;
|
|
36
|
+
/**
|
|
37
|
+
* Collapse all lines containing /node_modules/ into an ellipsis
|
|
38
|
+
*/
|
|
39
|
+
const filterNodeModules = function* (message) {
|
|
40
|
+
let inNodeModules = false;
|
|
41
|
+
const lines = message.split("\n");
|
|
42
|
+
for (const line of lines) {
|
|
43
|
+
if (/\/node_modules\//u.exec(line)) {
|
|
44
|
+
if (!inNodeModules) {
|
|
45
|
+
inNodeModules = true;
|
|
46
|
+
yield "[...]";
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
inNodeModules = false;
|
|
51
|
+
yield line;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
exports.filterNodeModules = filterNodeModules;
|
|
56
|
+
/**
|
|
57
|
+
* Prefix all lines with the level and the timestamp if needed
|
|
58
|
+
*/
|
|
59
|
+
const prefixOutput = (level, message, timestamp) => {
|
|
60
|
+
const timestampPrefix = timestamp
|
|
61
|
+
? `${new Date().toISOString()} `
|
|
62
|
+
: "";
|
|
63
|
+
const levelPrefix = `${level.toString().padStart(LOG_LEVEL_PADDING_SIZE)}: `;
|
|
64
|
+
return message.split("\n").map(line => `${timestampPrefix}${levelPrefix}${line}`)
|
|
65
|
+
.join("\n");
|
|
66
|
+
};
|
|
67
|
+
exports.prefixOutput = prefixOutput;
|
|
68
|
+
const logConfig = {
|
|
69
|
+
timestamp: true,
|
|
70
|
+
collapseNodeModules: true,
|
|
71
|
+
};
|
|
72
|
+
const customFormat = winston_1.default.format.printf((info) => {
|
|
73
|
+
const message = ("stack" in info) ? info.stack : info.message;
|
|
74
|
+
const filteredMessage = logConfig.collapseNodeModules
|
|
75
|
+
? [...(0, exports.filterNodeModules)(message)].join("\n")
|
|
76
|
+
: message;
|
|
77
|
+
return (0, exports.prefixOutput)(info.level, filteredMessage, logConfig.timestamp);
|
|
78
|
+
});
|
|
79
|
+
/** Edit the configuration applied to consoleLogger */
|
|
80
|
+
const setConfig = (config) => {
|
|
81
|
+
Object.assign(logConfig, config);
|
|
82
|
+
};
|
|
83
|
+
exports.setConfig = setConfig;
|
|
34
84
|
exports.transports = [new winston_1.default.transports.Console()];
|
|
35
85
|
exports.consoleLogger = winston_1.default.createLogger({
|
|
36
86
|
level: "info",
|
|
37
|
-
format: winston_1.default.format.combine(winston_1.default.format.colorize(),
|
|
87
|
+
format: winston_1.default.format.combine(winston_1.default.format.colorize(), customFormat),
|
|
38
88
|
transports: exports.transports,
|
|
39
89
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cley_faye/boilerplate",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Boilerplate code generator for generic projects",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,15 +17,16 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/CleyFaye/boilerplate#readme",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@keeex/eslint-config": "^1.
|
|
20
|
+
"@keeex/eslint-config": "^1.7.1",
|
|
21
21
|
"@types/express": "^4.17.11",
|
|
22
22
|
"@types/grunt": "^0.4.25",
|
|
23
23
|
"@types/http-errors": "^1.8.0",
|
|
24
24
|
"@types/node": "^16.0.0",
|
|
25
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
26
|
-
"@typescript-eslint/parser": "^5.
|
|
27
|
-
"eslint": "^7.
|
|
28
|
-
"eslint-plugin-
|
|
25
|
+
"@typescript-eslint/eslint-plugin": "^5.12.0",
|
|
26
|
+
"@typescript-eslint/parser": "^5.12.0",
|
|
27
|
+
"eslint": "^8.7.0",
|
|
28
|
+
"eslint-plugin-import": "^2.25.4",
|
|
29
|
+
"eslint-plugin-promise": "^6.0.0",
|
|
29
30
|
"eslint-webpack-plugin": "^3.0.1",
|
|
30
31
|
"express-winston": "^4.0.5",
|
|
31
32
|
"grunt": "^1.4.1",
|
|
@@ -33,6 +34,7 @@
|
|
|
33
34
|
"grunt-contrib-clean": "^2.0.0",
|
|
34
35
|
"grunt-run": "^0.8.1",
|
|
35
36
|
"load-grunt-tasks": "^5.1.0",
|
|
37
|
+
"logform": "^2.3.2",
|
|
36
38
|
"typescript": "^4.1.5",
|
|
37
39
|
"winston": "^3.3.3"
|
|
38
40
|
},
|