@qqi/log 1.0.0 → 1.1.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/README.md +10 -0
- package/cjs/core.js +142 -0
- package/cjs/index.js +5 -100
- package/cjs/util.js +87 -0
- package/cjs/virtual-dog.js +60 -0
- package/es/core.js +140 -0
- package/es/eslint.config.d.ts +1222 -0
- package/es/index.js +2 -102
- package/es/rollup.config.d.ts +17 -0
- package/es/rollup.config.eg.d.ts +14 -0
- package/es/src/core.d.ts +9 -0
- package/es/src/index.d.ts +12 -7
- package/es/src/type.d.ts +11 -9
- package/es/src/util.d.ts +35 -0
- package/es/src/virtual-dog.d.ts +16 -0
- package/es/util.js +81 -0
- package/es/virtual-dog.js +58 -0
- package/package.json +8 -2
- package/cjs/blankCall.js +0 -11
- package/cjs/managePrint.js +0 -53
- package/cjs/notSupport.js +0 -14
- package/cjs/parseError.js +0 -42
- package/cjs/platform.js +0 -7
- package/cjs/printError.js +0 -15
- package/cjs/printInfo.js +0 -15
- package/cjs/printWarn.js +0 -15
- package/cjs/setType.js +0 -21
- package/es/blankCall.js +0 -9
- package/es/managePrint.js +0 -51
- package/es/notSupport.js +0 -12
- package/es/parseError.js +0 -40
- package/es/platform.js +0 -5
- package/es/printError.js +0 -13
- package/es/printInfo.js +0 -13
- package/es/printWarn.js +0 -13
- package/es/setType.js +0 -18
- package/es/src/blankCall.d.ts +0 -6
- package/es/src/managePrint.d.ts +0 -3
- package/es/src/notSupport.d.ts +0 -7
- package/es/src/parseError.d.ts +0 -7
- package/es/src/platform.d.ts +0 -1
- package/es/src/printError.d.ts +0 -6
- package/es/src/printInfo.d.ts +0 -6
- package/es/src/printWarn.d.ts +0 -6
- package/es/src/setType.d.ts +0 -6
package/README.md
CHANGED
|
@@ -32,6 +32,16 @@ dog('你好'); // 该值是否打印还依赖于环境变量中有没有配置 `
|
|
|
32
32
|
a_node_tools_dev=true npm run dev
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
### 折叠同名方法打印消息
|
|
36
|
+
|
|
37
|
+
使用 `fold` 参数 :
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import { Dog } from '@qqi/log';
|
|
41
|
+
|
|
42
|
+
const dog = Dog();
|
|
43
|
+
```
|
|
44
|
+
|
|
35
45
|
## 文档地址
|
|
36
46
|
|
|
37
47
|
参看 [https://earthnut.dev/npm/qqi/log](https://earthnut.dev/npm/qqi/log)
|
package/cjs/core.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _static = require('@color-pen/static');
|
|
4
|
+
var aJsTools = require('a-js-tools');
|
|
5
|
+
var colorPen = require('color-pen');
|
|
6
|
+
var util = require('./util.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param options 配置项
|
|
11
|
+
* @returns 函数对象
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
var DogConstructor = aJsTools.createConstructor(Dog);
|
|
15
|
+
/**
|
|
16
|
+
* ## 创建 dev log 工厂函数
|
|
17
|
+
* @param options - 配置项
|
|
18
|
+
* @returns - dev log 工厂函数
|
|
19
|
+
*/
|
|
20
|
+
function Dog(options) {
|
|
21
|
+
var _this = this;
|
|
22
|
+
var _p = util.parseOption(options);
|
|
23
|
+
this.name = _p.name || aJsTools.getRandomString(12);
|
|
24
|
+
this.fold = Boolean(_p.fold);
|
|
25
|
+
var env = util.getEnv(this.name);
|
|
26
|
+
var type = _p.type || false;
|
|
27
|
+
this.mark = '';
|
|
28
|
+
/** 默认 node 环境以获取到的环境值为准,而非 node 环境默认开启,并通过自定义的 @qqi/babel-plugin-remove-dog-calls 来进行过滤正式环境(环境值需要自定义) */
|
|
29
|
+
this.type = util.platform === 'node' ? util.setType(env !== null && env !== void 0 ? env : type) : true;
|
|
30
|
+
/**
|
|
31
|
+
* ## 解析 error
|
|
32
|
+
* @param type
|
|
33
|
+
*/
|
|
34
|
+
var prefix = function (type) {
|
|
35
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
36
|
+
try {
|
|
37
|
+
throw new Error();
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
var parseErrorResult = (((_a = error.stack) === null || _a === void 0 ? void 0 : _a.split('\n')) || []).map(function (item) {
|
|
41
|
+
var reg = /at\s(.*)\s\((.*):(\d*):(\d*)\)/;
|
|
42
|
+
var res = reg.exec(item);
|
|
43
|
+
if (res) {
|
|
44
|
+
return {
|
|
45
|
+
name: res[1],
|
|
46
|
+
path: res[2],
|
|
47
|
+
line: res[3],
|
|
48
|
+
column: res[4],
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
name: '',
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
var result = parseErrorResult.filter(function (e) { return e.name !== '' && e.path !== undefined; });
|
|
56
|
+
var res = (_d = (_c = (_b = result[3]) !== null && _b !== void 0 ? _b : result[2]) !== null && _c !== void 0 ? _c : result[1]) !== null && _d !== void 0 ? _d : result[0];
|
|
57
|
+
var startStr = " ".concat(type === 'info' ? '💡' : type === 'error' ? '❌' : '⚠️ ', " ").concat(new Date().toLocaleString(), " ");
|
|
58
|
+
var printStartPenStr = (type === 'info'
|
|
59
|
+
? colorPen.bgCyanPen.brightWhite
|
|
60
|
+
: type === 'error'
|
|
61
|
+
? colorPen.bgBlackPen.red
|
|
62
|
+
: colorPen.bgBrightYellowPen.brightGreen)(startStr);
|
|
63
|
+
var mark = (_e = res === null || res === void 0 ? void 0 : res.name) !== null && _e !== void 0 ? _e : '';
|
|
64
|
+
if (_this.fold && mark) {
|
|
65
|
+
if (mark === _this.mark) ;
|
|
66
|
+
else {
|
|
67
|
+
if (_this.mark) {
|
|
68
|
+
console.groupEnd();
|
|
69
|
+
}
|
|
70
|
+
console.groupCollapsed(mark);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
_this.mark = mark;
|
|
74
|
+
return "".concat(printStartPenStr, " ").concat(mark, " ").concat((_f = res === null || res === void 0 ? void 0 : res.line) === null || _f === void 0 ? void 0 : _f.concat(' 行'), " ").concat((_g = res === null || res === void 0 ? void 0 : res.column) === null || _g === void 0 ? void 0 : _g.concat(' 列'));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
this.info = function () {
|
|
78
|
+
var msg = [];
|
|
79
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
80
|
+
msg[_i] = arguments[_i];
|
|
81
|
+
}
|
|
82
|
+
if (_this.type === 'all' || _this.type === 'info' || _this.type === true) {
|
|
83
|
+
var _prefix = prefix('info');
|
|
84
|
+
msg.unshift(_prefix);
|
|
85
|
+
console.log.apply(console, msg);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* @param msg
|
|
90
|
+
*/
|
|
91
|
+
this.warn = function () {
|
|
92
|
+
var msg = [];
|
|
93
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
94
|
+
msg[_i] = arguments[_i];
|
|
95
|
+
}
|
|
96
|
+
if (_this.type === 'all' || _this.type === 'warn' || _this.type === true) {
|
|
97
|
+
var _prefix = prefix('warn');
|
|
98
|
+
msg.unshift(_prefix);
|
|
99
|
+
console.warn.apply(console, msg);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @param msg
|
|
105
|
+
*/
|
|
106
|
+
this.error = function () {
|
|
107
|
+
var msg = [];
|
|
108
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
109
|
+
msg[_i] = arguments[_i];
|
|
110
|
+
}
|
|
111
|
+
if (_this.type === 'all' || _this.type === 'error' || _this.type === true) {
|
|
112
|
+
var _prefix = prefix('error');
|
|
113
|
+
msg.unshift(_prefix);
|
|
114
|
+
console.error.apply(console, msg);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* 本体方法
|
|
119
|
+
* @param str
|
|
120
|
+
*/
|
|
121
|
+
var dog = function () {
|
|
122
|
+
var str = [];
|
|
123
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
124
|
+
str[_i] = arguments[_i];
|
|
125
|
+
}
|
|
126
|
+
_this.info(str);
|
|
127
|
+
};
|
|
128
|
+
// 设置 prototype
|
|
129
|
+
Object.setPrototypeOf(dog, this);
|
|
130
|
+
return dog;
|
|
131
|
+
}
|
|
132
|
+
/** 原型上添加 clear 方法 */
|
|
133
|
+
Dog.prototype.clear = function () {
|
|
134
|
+
if (util.platform === 'browser') {
|
|
135
|
+
console.clear();
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
console.log(_static.esc.concat('c'));
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
exports.Dog = DogConstructor;
|
package/cjs/index.js
CHANGED
|
@@ -1,104 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var blankCall = require('./blankCall.js');
|
|
6
|
-
var managePrint = require('./managePrint.js');
|
|
7
|
-
var aTypeOfJs = require('a-type-of-js');
|
|
8
|
-
var platform = require('./platform.js');
|
|
9
|
-
var _static = require('@color-pen/static');
|
|
3
|
+
var core = require('./core.js');
|
|
4
|
+
var virtualDog = require('./virtual-dog.js');
|
|
10
5
|
|
|
11
|
-
/**
|
|
12
|
-
*
|
|
13
|
-
* 创建 dev log 工厂函数
|
|
14
|
-
*
|
|
15
|
-
* @param options - 配置项
|
|
16
|
-
* @returns - dev log 工厂函数
|
|
17
|
-
*/
|
|
18
|
-
function Dog(options) {
|
|
19
|
-
if (aTypeOfJs.isUndefined(options))
|
|
20
|
-
options = {
|
|
21
|
-
name: aJsTools.getRandomString(10),
|
|
22
|
-
type: false,
|
|
23
|
-
};
|
|
24
|
-
if (aTypeOfJs.isBoolean(options))
|
|
25
|
-
options = {
|
|
26
|
-
name: aJsTools.getRandomString(10),
|
|
27
|
-
type: options,
|
|
28
|
-
};
|
|
29
|
-
if (aTypeOfJs.isString(options))
|
|
30
|
-
options = {
|
|
31
|
-
name: options,
|
|
32
|
-
type: false,
|
|
33
|
-
};
|
|
34
|
-
let { name = '', type = false } = options;
|
|
35
|
-
// 处理 name
|
|
36
|
-
name = name.trim().replace(/\s+/g, '_');
|
|
37
|
-
/** 当前获取环境值 */
|
|
38
|
-
const _env = (platform.platform === 'node' &&
|
|
39
|
-
(globalThis?.process.env[name.toUpperCase().concat('_DEV')] ??
|
|
40
|
-
globalThis?.process.env[name.toLowerCase().concat('_dev')])) ||
|
|
41
|
-
false;
|
|
42
|
-
const env = _env === 'false' ? false : _env === 'true' ? true : _env;
|
|
43
|
-
/** 默认 node 环境以获取到的环境值为准,而非 node 环境默认开启,并通过自定义的 @qqi/babel-plugin-remove-dog-calls 来进行过滤正式环境(环境值需要自定义) */
|
|
44
|
-
type = platform.platform === 'node' ? setType.setType(env ?? type) : true;
|
|
45
|
-
/// 原始的调用方法,在 type 值变化时会触发该值的更替
|
|
46
|
-
// 私有方法 error
|
|
47
|
-
const _privateFunc = {
|
|
48
|
-
error: blankCall.blankCall,
|
|
49
|
-
warn: blankCall.blankCall,
|
|
50
|
-
info: blankCall.blankCall,
|
|
51
|
-
};
|
|
52
|
-
managePrint.managePrint(type, _privateFunc, name);
|
|
53
|
-
/** 本体方法 */
|
|
54
|
-
const dog = (...str) => {
|
|
55
|
-
Reflect.apply(_privateFunc.info, this, str);
|
|
56
|
-
};
|
|
57
|
-
// 设置 prototype
|
|
58
|
-
Object.setPrototypeOf(dog, this);
|
|
59
|
-
// 设置属性和方法
|
|
60
|
-
Object.defineProperties(this, {
|
|
61
|
-
type: {
|
|
62
|
-
get() {
|
|
63
|
-
return type || false;
|
|
64
|
-
},
|
|
65
|
-
set(value) {
|
|
66
|
-
const new_type = setType.setType(value);
|
|
67
|
-
if (new_type !== type) {
|
|
68
|
-
type = new_type;
|
|
69
|
-
managePrint.managePrint(type, _privateFunc, name);
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
},
|
|
73
|
-
error: {
|
|
74
|
-
value: (...str) => Reflect.apply(_privateFunc.error, this, str),
|
|
75
|
-
configurable: false,
|
|
76
|
-
enumerable: false,
|
|
77
|
-
writable: false,
|
|
78
|
-
},
|
|
79
|
-
warn: {
|
|
80
|
-
value: (...str) => Reflect.apply(_privateFunc.warn, this, str),
|
|
81
|
-
configurable: false,
|
|
82
|
-
enumerable: false,
|
|
83
|
-
writable: false,
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
return dog;
|
|
87
|
-
}
|
|
88
|
-
Dog.prototype.clear = () => {
|
|
89
|
-
if (platform.platform === 'browser') {
|
|
90
|
-
console.clear();
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
console.log(_static.esc.concat('c'));
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
/**
|
|
97
|
-
*
|
|
98
|
-
* @param options 配置项
|
|
99
|
-
* @returns 函数对象
|
|
100
|
-
*
|
|
101
|
-
*/
|
|
102
|
-
const DogConstructor = aJsTools.createConstructor(Dog);
|
|
103
6
|
|
|
104
|
-
|
|
7
|
+
|
|
8
|
+
exports.Dog = core.Dog;
|
|
9
|
+
exports.DogVirtual = virtualDog.DogVirtual;
|
package/cjs/util.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aJsTools = require('a-js-tools');
|
|
4
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
* @module @qqi/log/util
|
|
9
|
+
* @file util.ts
|
|
10
|
+
* @description 工具
|
|
11
|
+
* @author MrMudBean <Mr.MudBean@outlook.com>
|
|
12
|
+
* @license MIT
|
|
13
|
+
* @copyright 2026 ©️ MrMudBean
|
|
14
|
+
* @since 2026-01-28 03:11
|
|
15
|
+
* @version 1.0.1
|
|
16
|
+
* @lastModified 2026-01-28 04:34
|
|
17
|
+
*/
|
|
18
|
+
var typeList = [
|
|
19
|
+
false,
|
|
20
|
+
true,
|
|
21
|
+
'all',
|
|
22
|
+
'info',
|
|
23
|
+
'error',
|
|
24
|
+
'warn',
|
|
25
|
+
];
|
|
26
|
+
/**
|
|
27
|
+
* ## 解析参数
|
|
28
|
+
* @param options
|
|
29
|
+
*/
|
|
30
|
+
function parseOption(options) {
|
|
31
|
+
var result = {
|
|
32
|
+
name: aJsTools.getRandomString(10),
|
|
33
|
+
type: false,
|
|
34
|
+
fold: false,
|
|
35
|
+
};
|
|
36
|
+
if (aTypeOfJs.isUndefined(options))
|
|
37
|
+
return result;
|
|
38
|
+
if (aTypeOfJs.isBoolean(options)) {
|
|
39
|
+
result.type = options;
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
if (aTypeOfJs.isString(options)) {
|
|
43
|
+
// 处理 name
|
|
44
|
+
result.name = options.trim().replace(/\s+/g, '_');
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
if (aTypeOfJs.isString(options.name)) {
|
|
48
|
+
result.name = options.name.trim().replace(/\s+/g, '_');
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
return options;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 获取当前的环境变量
|
|
55
|
+
* @param name
|
|
56
|
+
*/
|
|
57
|
+
function getEnv(name) {
|
|
58
|
+
var _a;
|
|
59
|
+
if (name === void 0) { name = aJsTools.getRandomString(10); }
|
|
60
|
+
/** 当前获取环境值 */
|
|
61
|
+
var _env = false;
|
|
62
|
+
if (platform === 'node' && ((_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.process) === null || _a === void 0 ? void 0 : _a.env)) {
|
|
63
|
+
var processEnv = process.env;
|
|
64
|
+
_env =
|
|
65
|
+
processEnv[name.toUpperCase().concat('_DEV')] ||
|
|
66
|
+
processEnv[name.toLowerCase().concat('_dev')] ||
|
|
67
|
+
false;
|
|
68
|
+
}
|
|
69
|
+
var env = _env === 'false' ? false : _env === 'true' ? true : _env;
|
|
70
|
+
return env;
|
|
71
|
+
}
|
|
72
|
+
var platform = aJsTools.isNode() ? 'node' : 'browser';
|
|
73
|
+
/**
|
|
74
|
+
* 设置 type 的类型
|
|
75
|
+
* @param type 新的类型
|
|
76
|
+
*/
|
|
77
|
+
function setType(type) {
|
|
78
|
+
if (typeList.includes(type))
|
|
79
|
+
return type;
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
exports.getEnv = getEnv;
|
|
84
|
+
exports.parseOption = parseOption;
|
|
85
|
+
exports.platform = platform;
|
|
86
|
+
exports.setType = setType;
|
|
87
|
+
exports.typeList = typeList;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aJsTools = require('a-js-tools');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module @qqi/log/virtual-dog
|
|
8
|
+
* @file virtual-dog.ts
|
|
9
|
+
* @description _
|
|
10
|
+
* @author MrMudBean <Mr.MudBean@outlook.com>
|
|
11
|
+
* @license MIT
|
|
12
|
+
* @copyright 2026 ©️ MrMudBean
|
|
13
|
+
* @since 2026-01-28 04:52
|
|
14
|
+
* @version 1.1.0
|
|
15
|
+
* @lastModified 2026-01-28 04:53
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
function DogVirtualImt() {
|
|
21
|
+
/**
|
|
22
|
+
* 模拟类的构建
|
|
23
|
+
* @param _arg
|
|
24
|
+
*/
|
|
25
|
+
var _dev = function () {
|
|
26
|
+
};
|
|
27
|
+
Object.setPrototypeOf(_dev, this);
|
|
28
|
+
Object.defineProperties(this, {
|
|
29
|
+
info: {
|
|
30
|
+
value: function () {
|
|
31
|
+
},
|
|
32
|
+
configurable: false,
|
|
33
|
+
writable: false,
|
|
34
|
+
},
|
|
35
|
+
warn: {
|
|
36
|
+
value: function () {
|
|
37
|
+
},
|
|
38
|
+
configurable: false,
|
|
39
|
+
writable: false,
|
|
40
|
+
},
|
|
41
|
+
error: {
|
|
42
|
+
value: function () {
|
|
43
|
+
},
|
|
44
|
+
configurable: false,
|
|
45
|
+
writable: false,
|
|
46
|
+
},
|
|
47
|
+
type: {
|
|
48
|
+
get: function () {
|
|
49
|
+
return false;
|
|
50
|
+
},
|
|
51
|
+
set: function (_) { },
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
return _dev;
|
|
55
|
+
}
|
|
56
|
+
DogVirtualImt.prototype.clear = console.clear;
|
|
57
|
+
/** 虚拟狗,没有实现不打印 */
|
|
58
|
+
var DogVirtualConstructor = aJsTools.createConstructor(DogVirtualImt);
|
|
59
|
+
|
|
60
|
+
exports.DogVirtual = DogVirtualConstructor;
|
package/es/core.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { esc } from '@color-pen/static';
|
|
2
|
+
import { createConstructor, getRandomString } from 'a-js-tools';
|
|
3
|
+
import { bgCyanPen, bgBlackPen, bgBrightYellowPen } from 'color-pen';
|
|
4
|
+
import { parseOption, getEnv, platform, setType } from './util.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param options 配置项
|
|
9
|
+
* @returns 函数对象
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
var DogConstructor = createConstructor(Dog);
|
|
13
|
+
/**
|
|
14
|
+
* ## 创建 dev log 工厂函数
|
|
15
|
+
* @param options - 配置项
|
|
16
|
+
* @returns - dev log 工厂函数
|
|
17
|
+
*/
|
|
18
|
+
function Dog(options) {
|
|
19
|
+
var _this = this;
|
|
20
|
+
var _p = parseOption(options);
|
|
21
|
+
this.name = _p.name || getRandomString(12);
|
|
22
|
+
this.fold = Boolean(_p.fold);
|
|
23
|
+
var env = getEnv(this.name);
|
|
24
|
+
var type = _p.type || false;
|
|
25
|
+
this.mark = '';
|
|
26
|
+
/** 默认 node 环境以获取到的环境值为准,而非 node 环境默认开启,并通过自定义的 @qqi/babel-plugin-remove-dog-calls 来进行过滤正式环境(环境值需要自定义) */
|
|
27
|
+
this.type = platform === 'node' ? setType(env !== null && env !== void 0 ? env : type) : true;
|
|
28
|
+
/**
|
|
29
|
+
* ## 解析 error
|
|
30
|
+
* @param type
|
|
31
|
+
*/
|
|
32
|
+
var prefix = function (type) {
|
|
33
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
34
|
+
try {
|
|
35
|
+
throw new Error();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
var parseErrorResult = (((_a = error.stack) === null || _a === void 0 ? void 0 : _a.split('\n')) || []).map(function (item) {
|
|
39
|
+
var reg = /at\s(.*)\s\((.*):(\d*):(\d*)\)/;
|
|
40
|
+
var res = reg.exec(item);
|
|
41
|
+
if (res) {
|
|
42
|
+
return {
|
|
43
|
+
name: res[1],
|
|
44
|
+
path: res[2],
|
|
45
|
+
line: res[3],
|
|
46
|
+
column: res[4],
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
name: '',
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
var result = parseErrorResult.filter(function (e) { return e.name !== '' && e.path !== undefined; });
|
|
54
|
+
var res = (_d = (_c = (_b = result[3]) !== null && _b !== void 0 ? _b : result[2]) !== null && _c !== void 0 ? _c : result[1]) !== null && _d !== void 0 ? _d : result[0];
|
|
55
|
+
var startStr = " ".concat(type === 'info' ? '💡' : type === 'error' ? '❌' : '⚠️ ', " ").concat(new Date().toLocaleString(), " ");
|
|
56
|
+
var printStartPenStr = (type === 'info'
|
|
57
|
+
? bgCyanPen.brightWhite
|
|
58
|
+
: type === 'error'
|
|
59
|
+
? bgBlackPen.red
|
|
60
|
+
: bgBrightYellowPen.brightGreen)(startStr);
|
|
61
|
+
var mark = (_e = res === null || res === void 0 ? void 0 : res.name) !== null && _e !== void 0 ? _e : '';
|
|
62
|
+
if (_this.fold && mark) {
|
|
63
|
+
if (mark === _this.mark) ;
|
|
64
|
+
else {
|
|
65
|
+
if (_this.mark) {
|
|
66
|
+
console.groupEnd();
|
|
67
|
+
}
|
|
68
|
+
console.groupCollapsed(mark);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_this.mark = mark;
|
|
72
|
+
return "".concat(printStartPenStr, " ").concat(mark, " ").concat((_f = res === null || res === void 0 ? void 0 : res.line) === null || _f === void 0 ? void 0 : _f.concat(' 行'), " ").concat((_g = res === null || res === void 0 ? void 0 : res.column) === null || _g === void 0 ? void 0 : _g.concat(' 列'));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
this.info = function () {
|
|
76
|
+
var msg = [];
|
|
77
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
78
|
+
msg[_i] = arguments[_i];
|
|
79
|
+
}
|
|
80
|
+
if (_this.type === 'all' || _this.type === 'info' || _this.type === true) {
|
|
81
|
+
var _prefix = prefix('info');
|
|
82
|
+
msg.unshift(_prefix);
|
|
83
|
+
console.log.apply(console, msg);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* @param msg
|
|
88
|
+
*/
|
|
89
|
+
this.warn = function () {
|
|
90
|
+
var msg = [];
|
|
91
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
92
|
+
msg[_i] = arguments[_i];
|
|
93
|
+
}
|
|
94
|
+
if (_this.type === 'all' || _this.type === 'warn' || _this.type === true) {
|
|
95
|
+
var _prefix = prefix('warn');
|
|
96
|
+
msg.unshift(_prefix);
|
|
97
|
+
console.warn.apply(console, msg);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
*
|
|
102
|
+
* @param msg
|
|
103
|
+
*/
|
|
104
|
+
this.error = function () {
|
|
105
|
+
var msg = [];
|
|
106
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
107
|
+
msg[_i] = arguments[_i];
|
|
108
|
+
}
|
|
109
|
+
if (_this.type === 'all' || _this.type === 'error' || _this.type === true) {
|
|
110
|
+
var _prefix = prefix('error');
|
|
111
|
+
msg.unshift(_prefix);
|
|
112
|
+
console.error.apply(console, msg);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* 本体方法
|
|
117
|
+
* @param str
|
|
118
|
+
*/
|
|
119
|
+
var dog = function () {
|
|
120
|
+
var str = [];
|
|
121
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
122
|
+
str[_i] = arguments[_i];
|
|
123
|
+
}
|
|
124
|
+
_this.info(str);
|
|
125
|
+
};
|
|
126
|
+
// 设置 prototype
|
|
127
|
+
Object.setPrototypeOf(dog, this);
|
|
128
|
+
return dog;
|
|
129
|
+
}
|
|
130
|
+
/** 原型上添加 clear 方法 */
|
|
131
|
+
Dog.prototype.clear = function () {
|
|
132
|
+
if (platform === 'browser') {
|
|
133
|
+
console.clear();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
console.log(esc.concat('c'));
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export { DogConstructor as Dog };
|