@bamboocss/logger 1.11.1

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/LICENSE.md ADDED
@@ -0,0 +1,16 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Segun Adebayo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.js ADDED
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ colors: () => import_kleur2.default,
34
+ logger: () => logger,
35
+ quote: () => quote
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+ var import_kleur2 = __toESM(require("kleur"));
39
+
40
+ // src/create-logger.ts
41
+ var import_kleur = __toESM(require("kleur"));
42
+
43
+ // ../../node_modules/.pnpm/escape-string-regexp@5.0.0/node_modules/escape-string-regexp/index.js
44
+ function escapeStringRegexp(string) {
45
+ if (typeof string !== "string") {
46
+ throw new TypeError("Expected a string");
47
+ }
48
+ return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
49
+ }
50
+
51
+ // ../../node_modules/.pnpm/matcher@6.0.0/node_modules/matcher/index.js
52
+ var regexpCache = /* @__PURE__ */ new Map();
53
+ var sanitizeArray = (input, inputName) => {
54
+ if (!Array.isArray(input)) {
55
+ switch (typeof input) {
56
+ case "string": {
57
+ input = [input];
58
+ break;
59
+ }
60
+ case "undefined": {
61
+ input = [];
62
+ break;
63
+ }
64
+ default: {
65
+ throw new TypeError(`Expected '${inputName}' to be a string or an array, but got a type of '${typeof input}'`);
66
+ }
67
+ }
68
+ }
69
+ return input.filter((string) => {
70
+ if (typeof string !== "string") {
71
+ if (string === void 0) {
72
+ return false;
73
+ }
74
+ throw new TypeError(`Expected '${inputName}' to be an array of strings, but found a type of '${typeof string}' in the array`);
75
+ }
76
+ return true;
77
+ });
78
+ };
79
+ var makeRegexp = (pattern, options) => {
80
+ options = {
81
+ caseSensitive: false,
82
+ ...options
83
+ };
84
+ const flags = "s" + (options.caseSensitive ? "" : "i");
85
+ const cacheKey = pattern + "|" + flags;
86
+ if (regexpCache.has(cacheKey)) {
87
+ return regexpCache.get(cacheKey);
88
+ }
89
+ const negated = pattern[0] === "!";
90
+ if (negated) {
91
+ pattern = pattern.slice(1);
92
+ }
93
+ pattern = pattern.replaceAll(String.raw`\*`, "__ESCAPED_STAR__").replaceAll("\\\\", "__ESCAPED_BACKSLASH__").replaceAll(/\\(.)/g, "$1");
94
+ pattern = escapeStringRegexp(pattern).replaceAll(String.raw`\*`, ".*");
95
+ pattern = pattern.replaceAll("__ESCAPED_STAR__", String.raw`\*`).replaceAll("__ESCAPED_BACKSLASH__", "\\\\");
96
+ const regexp = new RegExp(`^${pattern}$`, flags);
97
+ regexp.negated = negated;
98
+ regexpCache.set(cacheKey, regexp);
99
+ return regexp;
100
+ };
101
+ var baseMatcher = (inputs, patterns, options, firstMatchOnly) => {
102
+ inputs = sanitizeArray(inputs, "inputs");
103
+ patterns = sanitizeArray(patterns, "patterns");
104
+ if (patterns.length === 0) {
105
+ return [];
106
+ }
107
+ patterns = patterns.map((pattern) => makeRegexp(pattern, options));
108
+ const negatedPatterns = patterns.filter((pattern) => pattern.negated);
109
+ const positivePatterns = patterns.filter((pattern) => !pattern.negated);
110
+ const { allPatterns } = options || {};
111
+ const result = [];
112
+ if (allPatterns && firstMatchOnly && negatedPatterns.length > 1 && positivePatterns.length === 0) {
113
+ for (const input of inputs) {
114
+ for (const pattern of negatedPatterns) {
115
+ if (pattern.test(input)) {
116
+ return [];
117
+ }
118
+ }
119
+ }
120
+ return inputs.slice(0, 1);
121
+ }
122
+ for (const input of inputs) {
123
+ let excludedByNegation = false;
124
+ for (const pattern of negatedPatterns) {
125
+ if (pattern.test(input)) {
126
+ excludedByNegation = true;
127
+ break;
128
+ }
129
+ }
130
+ if (excludedByNegation) {
131
+ continue;
132
+ }
133
+ if (positivePatterns.length === 0) {
134
+ result.push(input);
135
+ } else if (allPatterns) {
136
+ const matchedPositive = Array.from({ length: positivePatterns.length }, () => false);
137
+ for (const [index, pattern] of positivePatterns.entries()) {
138
+ if (pattern.test(input)) {
139
+ matchedPositive[index] = true;
140
+ }
141
+ }
142
+ if (matchedPositive.every(Boolean)) {
143
+ result.push(input);
144
+ }
145
+ } else {
146
+ let matchedAny = false;
147
+ for (const pattern of positivePatterns) {
148
+ if (pattern.test(input)) {
149
+ matchedAny = true;
150
+ break;
151
+ }
152
+ }
153
+ if (matchedAny) {
154
+ result.push(input);
155
+ }
156
+ }
157
+ if (firstMatchOnly && result.length > 0) {
158
+ break;
159
+ }
160
+ }
161
+ return result;
162
+ };
163
+ function isMatch(inputs, patterns, options) {
164
+ return baseMatcher(inputs, patterns, options, true).length > 0;
165
+ }
166
+
167
+ // src/create-logger.ts
168
+ var createLogger = (conf = {}) => {
169
+ let onLog = conf.onLog;
170
+ let level = conf.isDebug ? "debug" : conf.level ?? "info";
171
+ const filter = conf.filter !== "*" ? conf.filter?.split(/[\s,]+/) ?? [] : [];
172
+ const getLevel = () => filter.length ? "debug" : level;
173
+ const isValid = (level2, type) => {
174
+ const badLevel = logLevels[getLevel()].weight > logLevels[level2].weight;
175
+ const badType = filter.length > 0 && !matches(filter, type);
176
+ return !(badType || badLevel);
177
+ };
178
+ const stdout = (level2) => (type, data) => {
179
+ const entry = createEntry(level2, type, data);
180
+ if (level2 != null && isValid(level2, type)) {
181
+ const logEntry = formatEntry(entry) ?? {};
182
+ console.log(logEntry.label, logEntry.msg);
183
+ } else if (getLevel() !== "silent" && level2 == null) {
184
+ console.log(...[type, data].filter(Boolean));
185
+ }
186
+ onLog?.(entry);
187
+ };
188
+ const logFns = {
189
+ debug: stdout("debug"),
190
+ info: stdout("info"),
191
+ warn: stdout("warn"),
192
+ error: stdout("error")
193
+ };
194
+ const timing = (level2) => (msg) => {
195
+ const start = performance.now();
196
+ return (_msg = msg) => {
197
+ const end = performance.now();
198
+ const ms = end - start;
199
+ logFns[level2]("hrtime", `${_msg} ${import_kleur.default.gray(`(${ms.toFixed(2)}ms)`)}`);
200
+ };
201
+ };
202
+ const caughtError = (type, context, error) => {
203
+ const message = error instanceof Error ? error.message : String(error);
204
+ logFns.error(type, `${context}: ${message}`);
205
+ if (error instanceof Error && error.stack) {
206
+ logFns.debug(type, error.stack);
207
+ }
208
+ };
209
+ return {
210
+ get level() {
211
+ return level;
212
+ },
213
+ set level(newLevel) {
214
+ level = newLevel;
215
+ },
216
+ set onLog(fn) {
217
+ onLog = fn;
218
+ },
219
+ ...logFns,
220
+ caughtError,
221
+ print(data) {
222
+ console.dir(data, { depth: null, colors: true });
223
+ },
224
+ log: (data) => stdout(null)("", data),
225
+ time: {
226
+ info: timing("info"),
227
+ debug: timing("debug")
228
+ },
229
+ isDebug: Boolean(conf.isDebug)
230
+ };
231
+ };
232
+ var matches = (filters, value) => filters.some((search) => isMatch(value, search));
233
+ var createEntry = (level, type, data) => {
234
+ const msg = data instanceof Error ? import_kleur.default.red(data.stack ?? data.message) : data;
235
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
236
+ return { t: timestamp, type, level, msg };
237
+ };
238
+ var formatEntry = (entry) => {
239
+ const uword = entry.type ? import_kleur.default.gray(`[${entry.type}]`) : "";
240
+ let label = "";
241
+ let msg = "";
242
+ if (entry.level != null) {
243
+ const { msg: message, level } = entry;
244
+ const color = logLevels[level].color;
245
+ const levelLabel = import_kleur.default.bold(color(`${level}`));
246
+ label = [`\u{1F43C}`, levelLabel, uword].filter(Boolean).join(" ");
247
+ msg = message;
248
+ } else {
249
+ label = uword ?? "";
250
+ msg = entry.msg;
251
+ }
252
+ return { label, msg };
253
+ };
254
+ var logLevels = {
255
+ debug: { weight: 0, color: import_kleur.default.magenta },
256
+ info: { weight: 1, color: import_kleur.default.blue },
257
+ warn: { weight: 2, color: import_kleur.default.yellow },
258
+ error: { weight: 3, color: import_kleur.default.red },
259
+ silent: { weight: 4, color: import_kleur.default.white }
260
+ };
261
+
262
+ // src/index.ts
263
+ var quote = (...str) => import_kleur2.default.cyan(`\`${str.join("")}\``);
264
+ var debug = typeof process !== "undefined" ? process.env.BAMBOO_DEBUG : void 0;
265
+ var logger = createLogger({
266
+ filter: typeof process !== "undefined" ? debug : void 0,
267
+ isDebug: Boolean(debug)
268
+ });
269
+ // Annotate the CommonJS export names for ESM import in node:
270
+ 0 && (module.exports = {
271
+ colors,
272
+ logger,
273
+ quote
274
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,237 @@
1
+ // src/index.ts
2
+ import colors2 from "kleur";
3
+
4
+ // src/create-logger.ts
5
+ import colors from "kleur";
6
+
7
+ // ../../node_modules/.pnpm/escape-string-regexp@5.0.0/node_modules/escape-string-regexp/index.js
8
+ function escapeStringRegexp(string) {
9
+ if (typeof string !== "string") {
10
+ throw new TypeError("Expected a string");
11
+ }
12
+ return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
13
+ }
14
+
15
+ // ../../node_modules/.pnpm/matcher@6.0.0/node_modules/matcher/index.js
16
+ var regexpCache = /* @__PURE__ */ new Map();
17
+ var sanitizeArray = (input, inputName) => {
18
+ if (!Array.isArray(input)) {
19
+ switch (typeof input) {
20
+ case "string": {
21
+ input = [input];
22
+ break;
23
+ }
24
+ case "undefined": {
25
+ input = [];
26
+ break;
27
+ }
28
+ default: {
29
+ throw new TypeError(`Expected '${inputName}' to be a string or an array, but got a type of '${typeof input}'`);
30
+ }
31
+ }
32
+ }
33
+ return input.filter((string) => {
34
+ if (typeof string !== "string") {
35
+ if (string === void 0) {
36
+ return false;
37
+ }
38
+ throw new TypeError(`Expected '${inputName}' to be an array of strings, but found a type of '${typeof string}' in the array`);
39
+ }
40
+ return true;
41
+ });
42
+ };
43
+ var makeRegexp = (pattern, options) => {
44
+ options = {
45
+ caseSensitive: false,
46
+ ...options
47
+ };
48
+ const flags = "s" + (options.caseSensitive ? "" : "i");
49
+ const cacheKey = pattern + "|" + flags;
50
+ if (regexpCache.has(cacheKey)) {
51
+ return regexpCache.get(cacheKey);
52
+ }
53
+ const negated = pattern[0] === "!";
54
+ if (negated) {
55
+ pattern = pattern.slice(1);
56
+ }
57
+ pattern = pattern.replaceAll(String.raw`\*`, "__ESCAPED_STAR__").replaceAll("\\\\", "__ESCAPED_BACKSLASH__").replaceAll(/\\(.)/g, "$1");
58
+ pattern = escapeStringRegexp(pattern).replaceAll(String.raw`\*`, ".*");
59
+ pattern = pattern.replaceAll("__ESCAPED_STAR__", String.raw`\*`).replaceAll("__ESCAPED_BACKSLASH__", "\\\\");
60
+ const regexp = new RegExp(`^${pattern}$`, flags);
61
+ regexp.negated = negated;
62
+ regexpCache.set(cacheKey, regexp);
63
+ return regexp;
64
+ };
65
+ var baseMatcher = (inputs, patterns, options, firstMatchOnly) => {
66
+ inputs = sanitizeArray(inputs, "inputs");
67
+ patterns = sanitizeArray(patterns, "patterns");
68
+ if (patterns.length === 0) {
69
+ return [];
70
+ }
71
+ patterns = patterns.map((pattern) => makeRegexp(pattern, options));
72
+ const negatedPatterns = patterns.filter((pattern) => pattern.negated);
73
+ const positivePatterns = patterns.filter((pattern) => !pattern.negated);
74
+ const { allPatterns } = options || {};
75
+ const result = [];
76
+ if (allPatterns && firstMatchOnly && negatedPatterns.length > 1 && positivePatterns.length === 0) {
77
+ for (const input of inputs) {
78
+ for (const pattern of negatedPatterns) {
79
+ if (pattern.test(input)) {
80
+ return [];
81
+ }
82
+ }
83
+ }
84
+ return inputs.slice(0, 1);
85
+ }
86
+ for (const input of inputs) {
87
+ let excludedByNegation = false;
88
+ for (const pattern of negatedPatterns) {
89
+ if (pattern.test(input)) {
90
+ excludedByNegation = true;
91
+ break;
92
+ }
93
+ }
94
+ if (excludedByNegation) {
95
+ continue;
96
+ }
97
+ if (positivePatterns.length === 0) {
98
+ result.push(input);
99
+ } else if (allPatterns) {
100
+ const matchedPositive = Array.from({ length: positivePatterns.length }, () => false);
101
+ for (const [index, pattern] of positivePatterns.entries()) {
102
+ if (pattern.test(input)) {
103
+ matchedPositive[index] = true;
104
+ }
105
+ }
106
+ if (matchedPositive.every(Boolean)) {
107
+ result.push(input);
108
+ }
109
+ } else {
110
+ let matchedAny = false;
111
+ for (const pattern of positivePatterns) {
112
+ if (pattern.test(input)) {
113
+ matchedAny = true;
114
+ break;
115
+ }
116
+ }
117
+ if (matchedAny) {
118
+ result.push(input);
119
+ }
120
+ }
121
+ if (firstMatchOnly && result.length > 0) {
122
+ break;
123
+ }
124
+ }
125
+ return result;
126
+ };
127
+ function isMatch(inputs, patterns, options) {
128
+ return baseMatcher(inputs, patterns, options, true).length > 0;
129
+ }
130
+
131
+ // src/create-logger.ts
132
+ var createLogger = (conf = {}) => {
133
+ let onLog = conf.onLog;
134
+ let level = conf.isDebug ? "debug" : conf.level ?? "info";
135
+ const filter = conf.filter !== "*" ? conf.filter?.split(/[\s,]+/) ?? [] : [];
136
+ const getLevel = () => filter.length ? "debug" : level;
137
+ const isValid = (level2, type) => {
138
+ const badLevel = logLevels[getLevel()].weight > logLevels[level2].weight;
139
+ const badType = filter.length > 0 && !matches(filter, type);
140
+ return !(badType || badLevel);
141
+ };
142
+ const stdout = (level2) => (type, data) => {
143
+ const entry = createEntry(level2, type, data);
144
+ if (level2 != null && isValid(level2, type)) {
145
+ const logEntry = formatEntry(entry) ?? {};
146
+ console.log(logEntry.label, logEntry.msg);
147
+ } else if (getLevel() !== "silent" && level2 == null) {
148
+ console.log(...[type, data].filter(Boolean));
149
+ }
150
+ onLog?.(entry);
151
+ };
152
+ const logFns = {
153
+ debug: stdout("debug"),
154
+ info: stdout("info"),
155
+ warn: stdout("warn"),
156
+ error: stdout("error")
157
+ };
158
+ const timing = (level2) => (msg) => {
159
+ const start = performance.now();
160
+ return (_msg = msg) => {
161
+ const end = performance.now();
162
+ const ms = end - start;
163
+ logFns[level2]("hrtime", `${_msg} ${colors.gray(`(${ms.toFixed(2)}ms)`)}`);
164
+ };
165
+ };
166
+ const caughtError = (type, context, error) => {
167
+ const message = error instanceof Error ? error.message : String(error);
168
+ logFns.error(type, `${context}: ${message}`);
169
+ if (error instanceof Error && error.stack) {
170
+ logFns.debug(type, error.stack);
171
+ }
172
+ };
173
+ return {
174
+ get level() {
175
+ return level;
176
+ },
177
+ set level(newLevel) {
178
+ level = newLevel;
179
+ },
180
+ set onLog(fn) {
181
+ onLog = fn;
182
+ },
183
+ ...logFns,
184
+ caughtError,
185
+ print(data) {
186
+ console.dir(data, { depth: null, colors: true });
187
+ },
188
+ log: (data) => stdout(null)("", data),
189
+ time: {
190
+ info: timing("info"),
191
+ debug: timing("debug")
192
+ },
193
+ isDebug: Boolean(conf.isDebug)
194
+ };
195
+ };
196
+ var matches = (filters, value) => filters.some((search) => isMatch(value, search));
197
+ var createEntry = (level, type, data) => {
198
+ const msg = data instanceof Error ? colors.red(data.stack ?? data.message) : data;
199
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
200
+ return { t: timestamp, type, level, msg };
201
+ };
202
+ var formatEntry = (entry) => {
203
+ const uword = entry.type ? colors.gray(`[${entry.type}]`) : "";
204
+ let label = "";
205
+ let msg = "";
206
+ if (entry.level != null) {
207
+ const { msg: message, level } = entry;
208
+ const color = logLevels[level].color;
209
+ const levelLabel = colors.bold(color(`${level}`));
210
+ label = [`\u{1F43C}`, levelLabel, uword].filter(Boolean).join(" ");
211
+ msg = message;
212
+ } else {
213
+ label = uword ?? "";
214
+ msg = entry.msg;
215
+ }
216
+ return { label, msg };
217
+ };
218
+ var logLevels = {
219
+ debug: { weight: 0, color: colors.magenta },
220
+ info: { weight: 1, color: colors.blue },
221
+ warn: { weight: 2, color: colors.yellow },
222
+ error: { weight: 3, color: colors.red },
223
+ silent: { weight: 4, color: colors.white }
224
+ };
225
+
226
+ // src/index.ts
227
+ var quote = (...str) => colors2.cyan(`\`${str.join("")}\``);
228
+ var debug = typeof process !== "undefined" ? process.env.BAMBOO_DEBUG : void 0;
229
+ var logger = createLogger({
230
+ filter: typeof process !== "undefined" ? debug : void 0,
231
+ isDebug: Boolean(debug)
232
+ });
233
+ export {
234
+ colors2 as colors,
235
+ logger,
236
+ quote
237
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@bamboocss/logger",
3
+ "version": "1.11.1",
4
+ "description": "The core css bamboo library",
5
+ "homepage": "https://bamboo-css.com",
6
+ "license": "MIT",
7
+ "author": "Segun Adebayo <joseshegs@gmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/chakra-ui/bamboo.git",
11
+ "directory": "packages/logger"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "sideEffects": false,
17
+ "main": "dist/index.js",
18
+ "module": "dist/index.mjs",
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "source": "./src/index.ts",
23
+ "types": "./dist/index.d.ts",
24
+ "require": "./dist/index.js",
25
+ "import": {
26
+ "types": "./dist/index.d.mts",
27
+ "default": "./dist/index.mjs"
28
+ }
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "dependencies": {
36
+ "kleur": "4.1.5",
37
+ "@bamboocss/types": "1.11.1"
38
+ },
39
+ "devDependencies": {
40
+ "matcher": "6.0.0"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
44
+ "build-fast": "tsup src/index.ts --format=esm,cjs --no-dts",
45
+ "dev": "pnpm build-fast --watch"
46
+ }
47
+ }