mentie 0.1.9 → 0.1.11
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/modules/environment.js +7 -0
- package/modules/logging.js +36 -8
- package/package.json +1 -1
package/modules/environment.js
CHANGED
|
@@ -7,6 +7,13 @@
|
|
|
7
7
|
* @returns {boolean} Returns true if the code is running in a web environment, otherwise returns false.
|
|
8
8
|
*/
|
|
9
9
|
export const is_web = typeof window !== 'undefined'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the code is running within a Cypress environment.
|
|
13
|
+
* @returns {boolean} Returns true if the code is running in a Cypress environment, otherwise returns false.
|
|
14
|
+
*/
|
|
15
|
+
export const is_cypress = is_web && typeof window.Cypress !== 'undefined'
|
|
16
|
+
|
|
10
17
|
/**
|
|
11
18
|
* Checks if the code is running in a Node environment.
|
|
12
19
|
* @returns {boolean} Returns true if the code is running in a Node environment, otherwise returns false.
|
package/modules/logging.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Import environment data
|
|
2
|
-
import { dev, is_emulator, loglevel } from "./environment.js"
|
|
2
|
+
import { dev, is_cypress, is_emulator, loglevel } from "./environment.js"
|
|
3
3
|
|
|
4
4
|
const should_log = levels => {
|
|
5
5
|
|
|
@@ -13,6 +13,13 @@ const should_log = levels => {
|
|
|
13
13
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Adds a stack trace IN PLACE to the provided messages array if executed in a browser context
|
|
18
|
+
* and if the URL query string contains `trace=true`.
|
|
19
|
+
*
|
|
20
|
+
* @param {Array} messages - The array of messages to potentially add a trace to.
|
|
21
|
+
* @returns {Array} - The modified array of messages, including a stack trace if conditions were met.
|
|
22
|
+
*/
|
|
16
23
|
const add_trace = messages => {
|
|
17
24
|
|
|
18
25
|
// Try to add stack to messages if needed
|
|
@@ -31,7 +38,7 @@ const add_trace = messages => {
|
|
|
31
38
|
// Remove the first line of the stack trace
|
|
32
39
|
stack = stack.split( '\n' ).slice( 1 ).join( '\n' )
|
|
33
40
|
|
|
34
|
-
//
|
|
41
|
+
// Annotate the provided messages
|
|
35
42
|
messages.push( { stack } )
|
|
36
43
|
|
|
37
44
|
return messages
|
|
@@ -45,6 +52,27 @@ const add_trace = messages => {
|
|
|
45
52
|
|
|
46
53
|
}
|
|
47
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Modifies the input messages by potentially stringifying them if in a Cypress environment,
|
|
57
|
+
* and by appending a stack trace if the conditions specified in `add_trace` are met.
|
|
58
|
+
*
|
|
59
|
+
* @param {Array} messages - The array of messages to be annotated.
|
|
60
|
+
* @returns {void} - This function modifies the input array directly and does not return a value.
|
|
61
|
+
*/
|
|
62
|
+
const annotate_messages = messages => {
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// If we are running in cypress, stringify the messages because they become unavailable in the console
|
|
66
|
+
if( is_cypress ) messages = messages.map( message => JSON.stringify( message, null, 2 ) )
|
|
67
|
+
|
|
68
|
+
// Annotate the provided messages
|
|
69
|
+
messages = add_trace( messages )
|
|
70
|
+
|
|
71
|
+
// Return the annotated messages
|
|
72
|
+
return messages
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
|
|
48
76
|
/**
|
|
49
77
|
* Logs the provided messages to the console.
|
|
50
78
|
* Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn', 'info'.
|
|
@@ -64,8 +92,8 @@ export function log( ...messages ) {
|
|
|
64
92
|
// Log the messages if the loglevel matches
|
|
65
93
|
if( dev || should_log( levels ) ) {
|
|
66
94
|
|
|
67
|
-
//
|
|
68
|
-
|
|
95
|
+
// Annotate the provided messages
|
|
96
|
+
annotate_messages( messages )
|
|
69
97
|
|
|
70
98
|
// Log the messages
|
|
71
99
|
console.log( ...messages )
|
|
@@ -89,8 +117,8 @@ log.info = function( ...messages ) {
|
|
|
89
117
|
// Log the messages if the loglevel matches
|
|
90
118
|
if( is_emulator || should_log( levels ) ) {
|
|
91
119
|
|
|
92
|
-
//
|
|
93
|
-
|
|
120
|
+
// Annotate the provided messages
|
|
121
|
+
annotate_messages( messages )
|
|
94
122
|
|
|
95
123
|
// Log the messages
|
|
96
124
|
console.info( ...messages )
|
|
@@ -114,8 +142,8 @@ log.warn = function( ...messages ) {
|
|
|
114
142
|
// Log the messages if the loglevel matches
|
|
115
143
|
if( dev || should_log( levels ) ) {
|
|
116
144
|
|
|
117
|
-
//
|
|
118
|
-
|
|
145
|
+
// Annotate the provided messages
|
|
146
|
+
annotate_messages( messages )
|
|
119
147
|
|
|
120
148
|
// Log the messages
|
|
121
149
|
console.warn( ...messages )
|