@leftium/gg 0.0.18 → 0.0.20
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/dist/gg.js +37 -34
- package/package.json +1 -1
package/dist/gg.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import debugFactory from './debug.js';
|
|
2
2
|
import ErrorStackParser from 'error-stack-parser';
|
|
3
|
-
import { BROWSER } from 'esm-env';
|
|
3
|
+
import { BROWSER, DEV } from 'esm-env';
|
|
4
4
|
// Helper to detect if we're running in CloudFlare Workers
|
|
5
5
|
const isCloudflareWorker = () => {
|
|
6
6
|
// Check for CloudFlare Workers-specific global
|
|
@@ -82,6 +82,13 @@ const srcRootRegex = new RegExp(ggConfig.srcRootPattern, 'i');
|
|
|
82
82
|
// - Cache and reuse the same log function for a given callpoint.
|
|
83
83
|
const namespaceToLogFunction = new Map();
|
|
84
84
|
let maxCallpointLength = 0;
|
|
85
|
+
/**
|
|
86
|
+
* Reset the namespace width tracking.
|
|
87
|
+
* Useful after configuration checks that may have long callpoint paths.
|
|
88
|
+
*/
|
|
89
|
+
function resetNamespaceWidth() {
|
|
90
|
+
maxCallpointLength = 0;
|
|
91
|
+
}
|
|
85
92
|
function openInEditorUrl(fileName) {
|
|
86
93
|
return ggConfig.openInEditorUrlTemplate.replace('$FILENAME', encodeURIComponent(fileName).replaceAll('%2F', '/'));
|
|
87
94
|
}
|
|
@@ -89,45 +96,38 @@ export function gg(...args) {
|
|
|
89
96
|
if (!ggConfig.enabled || isCloudflareWorker()) {
|
|
90
97
|
return args.length ? args[0] : { url: '', stack: [] };
|
|
91
98
|
}
|
|
92
|
-
//
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const { functionName } = stack[0];
|
|
118
|
-
//console.log({ filename, fileNameToOpen: filenameToOpen, fileNameToDisplay: filenameToDisplay });
|
|
119
|
-
// A callpoint is uniquely identified by the filename plus function name
|
|
120
|
-
const callpoint = `${filenameToDisplay}${functionName ? `@${functionName}` : ''}`;
|
|
121
|
-
if (callpoint.length < 80 && callpoint.length > maxCallpointLength) {
|
|
122
|
-
maxCallpointLength = callpoint.length;
|
|
99
|
+
// Initialize return values
|
|
100
|
+
let fileName = '';
|
|
101
|
+
let functionName = '';
|
|
102
|
+
let url = '';
|
|
103
|
+
let stack = [];
|
|
104
|
+
let namespace = 'gg:';
|
|
105
|
+
// In development: calculate detailed callpoint information
|
|
106
|
+
// In production: skip expensive stack parsing and use simple namespace
|
|
107
|
+
if (DEV) {
|
|
108
|
+
// Ignore first stack frame, which is always the call to gg() itself.
|
|
109
|
+
stack = ErrorStackParser.parse(new Error()).splice(1);
|
|
110
|
+
// Example: http://localhost:5173/src/routes/+page.svelte
|
|
111
|
+
const filename = stack[0].fileName?.replace(timestampRegex, '') || '';
|
|
112
|
+
// Example: src/routes/+page.svelte
|
|
113
|
+
const filenameToOpen = filename.replace(srcRootRegex, '$<folderName>/');
|
|
114
|
+
url = openInEditorUrl(filenameToOpen);
|
|
115
|
+
// Example: routes/+page.svelte
|
|
116
|
+
fileName = filename.replace(srcRootRegex, '');
|
|
117
|
+
functionName = stack[0].functionName || '';
|
|
118
|
+
// A callpoint is uniquely identified by the filename plus function name
|
|
119
|
+
const callpoint = `${fileName}${functionName ? `@${functionName}` : ''}`;
|
|
120
|
+
if (callpoint.length < 80 && callpoint.length > maxCallpointLength) {
|
|
121
|
+
maxCallpointLength = callpoint.length;
|
|
122
|
+
}
|
|
123
|
+
namespace = `gg:${callpoint.padEnd(maxCallpointLength, ' ')}${ggConfig.editorLink ? url : ''}`;
|
|
123
124
|
}
|
|
124
|
-
const namespace = `gg:${callpoint.padEnd(maxCallpointLength, ' ')}${ggConfig.editorLink ? url : ''}`;
|
|
125
125
|
const ggLogFunction = namespaceToLogFunction.get(namespace) ||
|
|
126
126
|
namespaceToLogFunction.set(namespace, debugFactory(namespace)).get(namespace);
|
|
127
127
|
if (!args.length) {
|
|
128
128
|
ggLogFunction(` 📝📝 ${url} 👀👀`);
|
|
129
129
|
return {
|
|
130
|
-
fileName
|
|
130
|
+
fileName,
|
|
131
131
|
functionName,
|
|
132
132
|
url,
|
|
133
133
|
stack
|
|
@@ -179,4 +179,7 @@ if (ggConfig.showHints && !isCloudflareWorker()) {
|
|
|
179
179
|
message(`${checkbox(ggLogTest.enabled)} DEBUG env variable: ${process?.env?.DEBUG}${hint}`);
|
|
180
180
|
}
|
|
181
181
|
console.log(ggMessage);
|
|
182
|
+
// Reset namespace width after configuration check
|
|
183
|
+
// This prevents the long callpoint from the config check from affecting subsequent logs
|
|
184
|
+
resetNamespaceWidth();
|
|
182
185
|
}
|