@leostacowski/pkgs 0.0.0-stage → 0.0.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/LICENSE +24 -0
- package/README.md +35 -3
- package/modules/index.js +1 -0
- package/modules/logger/README.md +1 -0
- package/modules/logger/index.js +155 -0
- package/package.json +37 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @leostacowski/pkgs
|
|
2
|
+
|
|
3
|
+
_"It's ugly but it's mine"._
|
|
4
|
+
|
|
5
|
+
\- _Me describing my code._
|
|
6
|
+
|
|
7
|
+
## Motivation
|
|
8
|
+
|
|
9
|
+
After a long time repeatedly starting things from scratch:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
I decided to finally create a centralized repo for my scripts.
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This decision was directly inspired by Eskil Steenberg's talk: "_[You should finish your software](https://youtu.be/EGLoKbBn-VI?si=uFVlbKIcRolcOzCy)_" from BSC 2025.
|
|
16
|
+
|
|
17
|
+
If you are using any of these packages and:
|
|
18
|
+
|
|
19
|
+
1. Something is breaking;
|
|
20
|
+
2. Have any suggestions.
|
|
21
|
+
|
|
22
|
+
Please reach out via:
|
|
23
|
+
|
|
24
|
+
- [Linkedin](https://www.linkedin.com/in/leonardo-ribeiro-stacowski/);
|
|
25
|
+
- [Opening an Issue](https://github.com/leostacowski/pkgs/issues/).
|
|
26
|
+
|
|
27
|
+
I intend to keep adding stuff to this repo as long as I continue to reinvent the wheel!
|
|
28
|
+
|
|
29
|
+
## Packages
|
|
30
|
+
|
|
31
|
+
### @leostacowski/pkgs/Logger
|
|
32
|
+
|
|
33
|
+
Simple Logger that supports aliases, types, colors and other knobs.
|
|
34
|
+
|
|
35
|
+
Check it's [README.md](/modules/logger/README.md) for details.
|
package/modules/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as Logger } from './logger/index.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @leostacowski/pkgs/logger
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
const LOGGER_COLORS = {
|
|
2
|
+
RED: {
|
|
3
|
+
code: '\x1b[91m',
|
|
4
|
+
},
|
|
5
|
+
GREEN: {
|
|
6
|
+
code: '\x1b[92m',
|
|
7
|
+
},
|
|
8
|
+
YELLOW: {
|
|
9
|
+
code: '\x1b[93m',
|
|
10
|
+
},
|
|
11
|
+
DEFAULT: {
|
|
12
|
+
code: '\x1b[94m',
|
|
13
|
+
},
|
|
14
|
+
WHITE: {
|
|
15
|
+
code: '\x1b[37m',
|
|
16
|
+
},
|
|
17
|
+
RESET: {
|
|
18
|
+
code: '\x1b[0m',
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const LOGGER_TYPES = [
|
|
23
|
+
{
|
|
24
|
+
level: 2,
|
|
25
|
+
prefix: 'ERROR',
|
|
26
|
+
emitter: console.error,
|
|
27
|
+
color: LOGGER_COLORS.RED,
|
|
28
|
+
aliases: ['error', 'err', 'e'],
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
level: 1,
|
|
32
|
+
prefix: 'WARNING',
|
|
33
|
+
emitter: console.warn,
|
|
34
|
+
color: LOGGER_COLORS.YELLOW,
|
|
35
|
+
aliases: ['warning', 'warn', 'w'],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
level: 0,
|
|
39
|
+
prefix: 'SUCCESS',
|
|
40
|
+
emitter: console.log,
|
|
41
|
+
color: LOGGER_COLORS.GREEN,
|
|
42
|
+
aliases: ['success', 's'],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
level: 0,
|
|
46
|
+
prefix: 'DEBUG',
|
|
47
|
+
emitter: console.log,
|
|
48
|
+
color: LOGGER_COLORS.DEFAULT,
|
|
49
|
+
aliases: ['log', 'info', 'debug', 'l', 'i', 'd'],
|
|
50
|
+
},
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
const BASE_OPTS = {
|
|
54
|
+
prefix: '',
|
|
55
|
+
group: true,
|
|
56
|
+
colors: true,
|
|
57
|
+
text_color: '',
|
|
58
|
+
level: 0,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function emit(content = [], opts = {}, logger_type = {}) {
|
|
62
|
+
if (opts?.level > logger_type.level) return
|
|
63
|
+
if (!content.length) return
|
|
64
|
+
|
|
65
|
+
const prefix = (() => {
|
|
66
|
+
const target = []
|
|
67
|
+
|
|
68
|
+
target.push(`> [${new Date().toISOString()}] ${logger_type.prefix}`)
|
|
69
|
+
|
|
70
|
+
if (opts?.prefix) {
|
|
71
|
+
target.push(opts.prefix)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
target[target.length - 1] = `${target[target.length - 1]}:`
|
|
75
|
+
|
|
76
|
+
if (opts?.colors) {
|
|
77
|
+
target.unshift(logger_type.color.code)
|
|
78
|
+
target.push(LOGGER_COLORS.RESET.code)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return target.join(' ')
|
|
82
|
+
})()
|
|
83
|
+
|
|
84
|
+
const message = (() => {
|
|
85
|
+
const target = []
|
|
86
|
+
let text_color = ''
|
|
87
|
+
|
|
88
|
+
for (const item of content) {
|
|
89
|
+
target.push(typeof item === 'string' ? item : JSON.stringify(item))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (opts?.colors && opts?.text_color) {
|
|
93
|
+
text_color = opts.text_color
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (text_color) {
|
|
97
|
+
return `${text_color}${target.join()}${LOGGER_COLORS.RESET.code}`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return target.join(' ')
|
|
101
|
+
})()
|
|
102
|
+
|
|
103
|
+
if (opts.group) {
|
|
104
|
+
console.group(prefix)
|
|
105
|
+
logger_type.emitter(message)
|
|
106
|
+
console.groupEnd()
|
|
107
|
+
} else {
|
|
108
|
+
logger_type.emitter(prefix, message)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function get_emitters(args) {
|
|
115
|
+
const opts = {}
|
|
116
|
+
|
|
117
|
+
Object.keys(BASE_OPTS).forEach((key) => {
|
|
118
|
+
if (typeof args?.[key] === typeof BASE_OPTS[key]) {
|
|
119
|
+
opts[key] = args[key]
|
|
120
|
+
} else {
|
|
121
|
+
opts[key] = BASE_OPTS[key]
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
const target = {}
|
|
126
|
+
|
|
127
|
+
for (const logger_type of LOGGER_TYPES) {
|
|
128
|
+
for (const alias of logger_type.aliases) {
|
|
129
|
+
target[alias] = function () {
|
|
130
|
+
return emit(Array.from(arguments), opts, logger_type)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return target
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const Logger = function (args) {
|
|
139
|
+
if (typeof args === 'string') {
|
|
140
|
+
args = {
|
|
141
|
+
prefix: args,
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const emitters = get_emitters(args)
|
|
146
|
+
const methods = {}
|
|
147
|
+
|
|
148
|
+
for (const emitter of Object.keys(emitters)) {
|
|
149
|
+
methods[emitter] = emitters[emitter]
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return methods
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export default Logger
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@leostacowski/pkgs",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@leostacowski/pkgs",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Centralized package of reusable javascript tools.",
|
|
5
|
+
"homepage": "https://github.com/leostacowski/pkgs#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/leostacowski/pkgs/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/leostacowski/pkgs.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "Unlicense",
|
|
14
|
+
"author": {
|
|
15
|
+
"name": "leostacowski",
|
|
16
|
+
"email": "leonardo.stacowski@hotmail.com",
|
|
17
|
+
"url": "git+https://github.com/leostacowski/"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"files": [
|
|
24
|
+
"modules"
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./modules/index.js",
|
|
28
|
+
"./logger": "./modules/logger/index.js"
|
|
29
|
+
},
|
|
30
|
+
"imports": {
|
|
31
|
+
"#logger/*": "./modules/logger/*"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"dev": "node --watch ./modules/index.js",
|
|
35
|
+
"deploy": "npm publish"
|
|
36
|
+
}
|
|
37
|
+
}
|