@lwrjs/diagnostics 0.11.0-alpha.8 → 0.11.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.
@@ -21,4 +21,5 @@ var __toModule = (module2) => {
21
21
  __markAsModule(exports);
22
22
  __exportStar(exports, __toModule(require("./descriptions/index.cjs")));
23
23
  __exportStar(exports, __toModule(require("./errors.cjs")));
24
+ __exportStar(exports, __toModule(require("./logger.cjs")));
24
25
  __exportStar(exports, __toModule(require("./types.cjs")));
@@ -0,0 +1,146 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, {get: all[name], enumerable: true});
11
+ };
12
+ var __exportStar = (target, module2, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && key !== "default")
16
+ __defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
17
+ }
18
+ return target;
19
+ };
20
+ var __toModule = (module2) => {
21
+ return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
22
+ };
23
+
24
+ // packages/@lwrjs/diagnostics/src/logger.ts
25
+ __markAsModule(exports);
26
+ __export(exports, {
27
+ DEBUG: () => DEBUG,
28
+ ERROR: () => ERROR,
29
+ INFO: () => INFO,
30
+ VERBOSE: () => VERBOSE,
31
+ WARN: () => WARN,
32
+ logger: () => logger,
33
+ stringifyError: () => stringifyError
34
+ });
35
+ var import_errors = __toModule(require("./errors.cjs"));
36
+ var VERBOSE = "verbose";
37
+ var DEBUG = "debug";
38
+ var INFO = "info";
39
+ var WARN = "warn";
40
+ var ERROR = "error";
41
+ var options = {};
42
+ var DUPES = new Set();
43
+ var currentLevel = process.env.LOG_LEVEL || INFO;
44
+ function log(level, message, additionalInfo) {
45
+ const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
46
+ if (currentLevel !== LOG_LEVEL) {
47
+ currentLevel = LOG_LEVEL;
48
+ console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
49
+ }
50
+ let shouldLog = false;
51
+ switch (level) {
52
+ case VERBOSE:
53
+ shouldLog = LOG_LEVEL == VERBOSE;
54
+ break;
55
+ case DEBUG:
56
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
57
+ break;
58
+ case INFO:
59
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
60
+ break;
61
+ case WARN:
62
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
63
+ break;
64
+ case ERROR:
65
+ shouldLog = true;
66
+ break;
67
+ }
68
+ if (shouldLog && options.dedupe && options.dedupe.has(level)) {
69
+ const key = `[${level}] : ${message}`;
70
+ if (DUPES.has(key)) {
71
+ shouldLog = false;
72
+ } else {
73
+ DUPES.add(key);
74
+ }
75
+ }
76
+ if (shouldLog) {
77
+ const logMessage = `[${level}]${gap(message)}${message}`;
78
+ const additionalMessage = additionalInfo ? `Additional Info: ${JSON.stringify(additionalInfo)}` : void 0;
79
+ if (level == ERROR) {
80
+ console.error("%s", logMessage);
81
+ if (additionalInfo) {
82
+ console.error("\n%s", additionalMessage);
83
+ }
84
+ } else if (level == WARN) {
85
+ console.warn("%s", logMessage);
86
+ if (additionalInfo) {
87
+ console.warn("\n%s", additionalMessage);
88
+ }
89
+ } else if (level == DEBUG || level == VERBOSE) {
90
+ console.log("%s", logMessage);
91
+ if (additionalInfo) {
92
+ console.log("\n%s", additionalMessage);
93
+ }
94
+ } else {
95
+ console.log(logMessage);
96
+ if (additionalInfo) {
97
+ console.log(`
98
+ ${additionalMessage}`);
99
+ }
100
+ }
101
+ }
102
+ }
103
+ var stringifyError = (error) => {
104
+ if (error instanceof import_errors.DiagnosticsError) {
105
+ return JSON.stringify({
106
+ message: error.message,
107
+ diagnostics: error.diagnostics,
108
+ stack: error.stack
109
+ });
110
+ } else if (typeof error === "string" || error instanceof String) {
111
+ return error;
112
+ } else {
113
+ const propertyNames = Object.getOwnPropertyNames(error);
114
+ const retObj = {};
115
+ for (let property, i = 0, len = propertyNames.length; i < len; ++i) {
116
+ property = propertyNames[i];
117
+ const descriptor = Object.getOwnPropertyDescriptor(error, property);
118
+ retObj[property] = descriptor?.value;
119
+ }
120
+ return JSON.stringify(retObj);
121
+ }
122
+ };
123
+ function gap(message) {
124
+ return String(message)?.indexOf("[") === 0 ? "" : " ";
125
+ }
126
+ function logError(err, additionalInfo) {
127
+ if (err instanceof import_errors.DiagnosticsError || !err || !err.message) {
128
+ log(ERROR, stringifyError(err), additionalInfo);
129
+ }
130
+ if (err && err.message) {
131
+ console.error("%s", `[${ERROR}]${gap(err.message)}${err.message}`);
132
+ console.error("%s", err);
133
+ }
134
+ }
135
+ var logger = {
136
+ verbose: (message, additionalInfo) => log(VERBOSE, message, additionalInfo),
137
+ debug: (message, additionalInfo) => log(DEBUG, message, additionalInfo),
138
+ info: (message, additionalInfo) => log(INFO, message, additionalInfo),
139
+ warn: (message, additionalInfo) => log(WARN, message, additionalInfo),
140
+ error: (error, additionalInfo) => logError(error, additionalInfo),
141
+ log,
142
+ setOptions: (opts) => {
143
+ options = opts;
144
+ },
145
+ currentLevel
146
+ };
@@ -1,4 +1,5 @@
1
1
  export * from './descriptions/index.js';
2
2
  export * from './errors.js';
3
+ export * from './logger.js';
3
4
  export * from './types.js';
4
5
  //# sourceMappingURL=index.d.ts.map
package/build/es/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './descriptions/index.js';
2
2
  export * from './errors.js';
3
+ export * from './logger.js';
3
4
  export * from './types.js';
4
5
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,23 @@
1
+ type LEVEL = 'verbose' | 'debug' | 'info' | 'warn' | 'error';
2
+ export declare const VERBOSE: LEVEL;
3
+ export declare const DEBUG: LEVEL;
4
+ export declare const INFO: LEVEL;
5
+ export declare const WARN: LEVEL;
6
+ export declare const ERROR: LEVEL;
7
+ type LoggerOptions = {
8
+ dedupe?: Set<string>;
9
+ };
10
+ declare function log(level: string, message: string, additionalInfo?: any): void;
11
+ export declare const stringifyError: (error: any) => string;
12
+ export declare const logger: {
13
+ verbose: (message: string, additionalInfo?: any) => void;
14
+ debug: (message: string, additionalInfo?: any) => void;
15
+ info: (message: string, additionalInfo?: any) => void;
16
+ warn: (message: string, additionalInfo?: any) => void;
17
+ error: (error: any, additionalInfo?: any) => void;
18
+ log: typeof log;
19
+ setOptions: (opts: LoggerOptions) => void;
20
+ currentLevel: string;
21
+ };
22
+ export {};
23
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1,124 @@
1
+ import { DiagnosticsError } from './errors.js';
2
+ export const VERBOSE = 'verbose';
3
+ export const DEBUG = 'debug';
4
+ export const INFO = 'info';
5
+ export const WARN = 'warn';
6
+ export const ERROR = 'error';
7
+ let options = {};
8
+ const DUPES = new Set();
9
+ let currentLevel = process.env.LOG_LEVEL || INFO;
10
+ function log(level, message, additionalInfo) {
11
+ const LOG_LEVEL = process.env.LOG_LEVEL || INFO;
12
+ if (currentLevel !== LOG_LEVEL) {
13
+ currentLevel = LOG_LEVEL;
14
+ console.log(`LOG_LEVEL: ${LOG_LEVEL}`);
15
+ }
16
+ let shouldLog = false;
17
+ switch (level) {
18
+ case VERBOSE:
19
+ shouldLog = LOG_LEVEL == VERBOSE;
20
+ break;
21
+ case DEBUG:
22
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG;
23
+ break;
24
+ case INFO:
25
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO;
26
+ break;
27
+ case WARN:
28
+ shouldLog = LOG_LEVEL == VERBOSE || LOG_LEVEL == DEBUG || LOG_LEVEL == INFO || LOG_LEVEL == WARN;
29
+ break;
30
+ case ERROR:
31
+ shouldLog = true;
32
+ break;
33
+ }
34
+ // Check if we should suppress dupes and we have already logged this message
35
+ if (shouldLog && options.dedupe && options.dedupe.has(level)) {
36
+ const key = `[${level}] : ${message}`;
37
+ if (DUPES.has(key)) {
38
+ shouldLog = false;
39
+ }
40
+ else {
41
+ // add key to de-duplicate cache
42
+ DUPES.add(key);
43
+ }
44
+ }
45
+ if (shouldLog) {
46
+ const logMessage = `[${level}]${gap(message)}${message}`;
47
+ const additionalMessage = additionalInfo
48
+ ? `Additional Info: ${JSON.stringify(additionalInfo)}`
49
+ : undefined;
50
+ if (level == ERROR) {
51
+ console.error('\x1b[31m%s\x1b[0m', logMessage); // red
52
+ if (additionalInfo) {
53
+ console.error('\n\x1b[31m%s\x1b[0m', additionalMessage); // red
54
+ }
55
+ }
56
+ else if (level == WARN) {
57
+ console.warn('\x1b[33m%s\x1b[0m', logMessage); // yellow
58
+ if (additionalInfo) {
59
+ console.warn('\n\x1b[33m%s\x1b[0m', additionalMessage); // yellow
60
+ }
61
+ }
62
+ else if (level == DEBUG || level == VERBOSE) {
63
+ console.log('\x1b[2m%s\x1b[0m', logMessage); // dim
64
+ if (additionalInfo) {
65
+ console.log('\n\x1b[2m%s\x1b[0m', additionalMessage); // dim
66
+ }
67
+ }
68
+ else {
69
+ console.log(logMessage);
70
+ if (additionalInfo) {
71
+ console.log(`\n${additionalMessage}`);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ export const stringifyError = (error) => {
77
+ if (error instanceof DiagnosticsError) {
78
+ return JSON.stringify({
79
+ message: error.message,
80
+ diagnostics: error.diagnostics,
81
+ stack: error.stack,
82
+ });
83
+ }
84
+ else if (typeof error === 'string' || error instanceof String) {
85
+ return error;
86
+ }
87
+ else {
88
+ const propertyNames = Object.getOwnPropertyNames(error);
89
+ const retObj = {};
90
+ for (let property, i = 0, len = propertyNames.length; i < len; ++i) {
91
+ property = propertyNames[i];
92
+ const descriptor = Object.getOwnPropertyDescriptor(error, property);
93
+ retObj[property] = descriptor?.value;
94
+ }
95
+ return JSON.stringify(retObj);
96
+ }
97
+ };
98
+ // Return a space if there should be a space between the message and the level
99
+ function gap(message) {
100
+ return String(message)?.indexOf('[') === 0 ? '' : ' ';
101
+ }
102
+ function logError(err, additionalInfo) {
103
+ if (err instanceof DiagnosticsError || !err || !err.message) {
104
+ log(ERROR, stringifyError(err), additionalInfo);
105
+ }
106
+ // If this is an error with a message update the message header and log as is...
107
+ if (err && err.message) {
108
+ console.error('\x1b[31m%s\x1b[0m', `[${ERROR}]${gap(err.message)}${err.message}`);
109
+ console.error('\x1b[90m%s\x1b[0m', err);
110
+ }
111
+ }
112
+ export const logger = {
113
+ verbose: (message, additionalInfo) => log(VERBOSE, message, additionalInfo),
114
+ debug: (message, additionalInfo) => log(DEBUG, message, additionalInfo),
115
+ info: (message, additionalInfo) => log(INFO, message, additionalInfo),
116
+ warn: (message, additionalInfo) => log(WARN, message, additionalInfo),
117
+ error: (error, additionalInfo) => logError(error, additionalInfo),
118
+ log,
119
+ setOptions: (opts) => {
120
+ options = opts;
121
+ },
122
+ currentLevel,
123
+ };
124
+ //# sourceMappingURL=logger.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.11.0-alpha.8",
7
+ "version": "0.11.0",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -29,11 +29,19 @@
29
29
  "build/**/*.cjs",
30
30
  "build/**/*.d.ts"
31
31
  ],
32
+ "scripts": {
33
+ "build": "tsc -b",
34
+ "clean": "rm -rf build node_modules",
35
+ "test": "jest"
36
+ },
32
37
  "devDependencies": {
33
- "@lwrjs/types": "0.11.0-alpha.8"
38
+ "@lwrjs/types": "0.11.0",
39
+ "jest": "^26.6.3",
40
+ "ts-jest": "^26.5.6",
41
+ "typescript": "^4.9.5"
34
42
  },
35
43
  "engines": {
36
44
  "node": ">=16.0.0"
37
45
  },
38
- "gitHead": "bba3cfa996e84ee702f287e7e7b6361b807434db"
46
+ "gitHead": "fbc883ea90a12672ce6f1adc2201144fda8762bd"
39
47
  }