@qqi/log 0.1.1 → 1.1.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/src/index.mjs DELETED
@@ -1,104 +0,0 @@
1
- import { createConstructor, getRandomString } from 'a-js-tools';
2
- import { setType } from './setType.mjs';
3
- import { blankCall } from './blankCall.mjs';
4
- import { managePrint } from './managePrint.mjs';
5
- import { isUndefined, isBoolean, isString } from 'a-type-of-js';
6
- import { platform } from './platform.mjs';
7
- import { esc } from '@color-pen/static';
8
-
9
- /**
10
- *
11
- * 创建 dev log 工厂函数
12
- *
13
- * @param options - 配置项
14
- * @returns - dev log 工厂函数
15
- *
16
- *
17
- */
18
- function Dog(options) {
19
- if (isUndefined(options))
20
- options = {
21
- name: getRandomString(10),
22
- type: false,
23
- };
24
- if (isBoolean(options))
25
- options = {
26
- name: getRandomString(10),
27
- type: options,
28
- };
29
- if (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 === 'node' &&
39
- (process.env[name.toUpperCase().concat('_DEV')] ??
40
- 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 === 'node' ? setType(env ?? type) : true;
45
- /// 原始的调用方法,在 type 值变化时会触发该值的更替
46
- // 私有方法 error
47
- const _privateFunc = {
48
- error: blankCall,
49
- warn: blankCall,
50
- info: blankCall,
51
- };
52
- 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(value);
67
- if (new_type !== type) {
68
- type = new_type;
69
- 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 === 'browser') {
90
- console.clear();
91
- }
92
- else {
93
- console.log(esc.concat('c'));
94
- }
95
- };
96
- /**
97
- *
98
- * @param options 配置项
99
- * @returns 函数对象
100
- *
101
- */
102
- const DogConstructor = createConstructor(Dog);
103
-
104
- export { DogConstructor as Dog };
@@ -1,53 +0,0 @@
1
- 'use strict';
2
-
3
- var aTypeOfJs = require('a-type-of-js');
4
- var notSupport = require('./notSupport.cjs');
5
- var platform = require('./platform.cjs');
6
- var blankCall = require('./blankCall.cjs');
7
- var printError = require('./printError.cjs');
8
- var printWarn = require('./printWarn.cjs');
9
- var printInfo = require('./printInfo.cjs');
10
- var setType = require('./setType.cjs');
11
-
12
- /** 管理是否打印 */
13
- function managePrint(type, privateFunc, name) {
14
- if (platform.platform === 'node') {
15
- const dev = process.env[name.toUpperCase().concat('_DEV')] ||
16
- process.env[name.toLowerCase().concat('_dev')];
17
- /// 当前禁止使用打印
18
- /// node 环境未配置 NAME_DEV 环境变量
19
- /// 显式配置 NAME_DEV 环境变量为 false
20
- if (type === false || aTypeOfJs.isUndefined(dev) || dev === 'false') {
21
- return Reflect.apply(notSupport.notSupport, privateFunc, []);
22
- }
23
- /// 将显示配置环境变量的值给 type
24
- if ([...setType.typeList, 'true', 'false'].includes(dev)) {
25
- type = dev === 'true' ? 'all' : dev;
26
- }
27
- privateFunc.info = ['all', 'info', true].includes(type)
28
- ? printInfo.printInfo
29
- : blankCall.blankCall;
30
- privateFunc.error = ['all', 'error', true].includes(type)
31
- ? printError.printError
32
- : blankCall.blankCall;
33
- privateFunc.warn = ['all', 'warn', true].includes(type)
34
- ? printWarn.printWarn
35
- : blankCall.blankCall;
36
- }
37
- // 在 非 node 环境始终执行,搭配 @qqi/babel-plugin-remove-dog-calls 使用
38
- else {
39
- if (type === false)
40
- return Reflect.apply(notSupport.notSupport, privateFunc, []);
41
- privateFunc.info = ['all', 'info', true].includes(type)
42
- ? printInfo.printInfo
43
- : blankCall.blankCall;
44
- privateFunc.error = ['all', 'error', true].includes(type)
45
- ? printError.printError
46
- : blankCall.blankCall;
47
- privateFunc.warn = ['all', 'warn', true].includes(type)
48
- ? printWarn.printWarn
49
- : blankCall.blankCall;
50
- }
51
- }
52
-
53
- exports.managePrint = managePrint;
@@ -1,3 +0,0 @@
1
- import { DevLogType, PrivateFunc } from './type';
2
- /** 管理是否打印 */
3
- export declare function managePrint(type: DevLogType, privateFunc: PrivateFunc, name: string): void;
@@ -1,51 +0,0 @@
1
- import { isUndefined } from 'a-type-of-js';
2
- import { notSupport } from './notSupport.mjs';
3
- import { platform } from './platform.mjs';
4
- import { blankCall } from './blankCall.mjs';
5
- import { printError } from './printError.mjs';
6
- import { printWarn } from './printWarn.mjs';
7
- import { printInfo } from './printInfo.mjs';
8
- import { typeList } from './setType.mjs';
9
-
10
- /** 管理是否打印 */
11
- function managePrint(type, privateFunc, name) {
12
- if (platform === 'node') {
13
- const dev = process.env[name.toUpperCase().concat('_DEV')] ||
14
- process.env[name.toLowerCase().concat('_dev')];
15
- /// 当前禁止使用打印
16
- /// node 环境未配置 NAME_DEV 环境变量
17
- /// 显式配置 NAME_DEV 环境变量为 false
18
- if (type === false || isUndefined(dev) || dev === 'false') {
19
- return Reflect.apply(notSupport, privateFunc, []);
20
- }
21
- /// 将显示配置环境变量的值给 type
22
- if ([...typeList, 'true', 'false'].includes(dev)) {
23
- type = dev === 'true' ? 'all' : dev;
24
- }
25
- privateFunc.info = ['all', 'info', true].includes(type)
26
- ? printInfo
27
- : blankCall;
28
- privateFunc.error = ['all', 'error', true].includes(type)
29
- ? printError
30
- : blankCall;
31
- privateFunc.warn = ['all', 'warn', true].includes(type)
32
- ? printWarn
33
- : blankCall;
34
- }
35
- // 在 非 node 环境始终执行,搭配 @qqi/babel-plugin-remove-dog-calls 使用
36
- else {
37
- if (type === false)
38
- return Reflect.apply(notSupport, privateFunc, []);
39
- privateFunc.info = ['all', 'info', true].includes(type)
40
- ? printInfo
41
- : blankCall;
42
- privateFunc.error = ['all', 'error', true].includes(type)
43
- ? printError
44
- : blankCall;
45
- privateFunc.warn = ['all', 'warn', true].includes(type)
46
- ? printWarn
47
- : blankCall;
48
- }
49
- }
50
-
51
- export { managePrint };
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- var blankCall = require('./blankCall.cjs');
4
-
5
- /**
6
- * 当前环境不支持
7
- *
8
- * 三个方法都是空方法
9
- */
10
- function notSupport() {
11
- this.error = this.info = this.warn = blankCall.blankCall;
12
- }
13
-
14
- exports.notSupport = notSupport;
@@ -1,7 +0,0 @@
1
- import { PrivateFunc } from './type';
2
- /**
3
- * 当前环境不支持
4
- *
5
- * 三个方法都是空方法
6
- */
7
- export declare function notSupport(this: PrivateFunc): void;
@@ -1,12 +0,0 @@
1
- import { blankCall } from './blankCall.mjs';
2
-
3
- /**
4
- * 当前环境不支持
5
- *
6
- * 三个方法都是空方法
7
- */
8
- function notSupport() {
9
- this.error = this.info = this.warn = blankCall;
10
- }
11
-
12
- export { notSupport };
@@ -1,42 +0,0 @@
1
- 'use strict';
2
-
3
- var colorPen = require('color-pen');
4
-
5
- /**
6
- *
7
- * 解析 error
8
- *
9
- */
10
- function parseError(type) {
11
- try {
12
- throw new Error();
13
- }
14
- catch (error) {
15
- const parseErrorResult = (error.stack?.split('\n') || []).map(item => {
16
- const reg = /at\s(.*)\s\((.*):(\d*):(\d*)\)/;
17
- const res = reg.exec(item);
18
- if (res) {
19
- return {
20
- name: res[1],
21
- path: res[2],
22
- line: res[3],
23
- column: res[4],
24
- };
25
- }
26
- return {
27
- name: '',
28
- };
29
- });
30
- const result = parseErrorResult.filter(e => e.name !== '' && e.path !== undefined);
31
- const res = result[3] ?? result[2] ?? result[1] ?? result[0];
32
- const startStr = ` ${type === 'info' ? '💡' : type === 'error' ? '❌' : '⚠️ '} ${new Date().toLocaleString()} `;
33
- const printStartPenStr = (type === 'info'
34
- ? colorPen.pen.bgCyan.brightWhite
35
- : type === 'error'
36
- ? colorPen.pen.bgBlack.red
37
- : colorPen.pen.bgBrightYellow.brightGreen)(startStr);
38
- console.log(`${printStartPenStr} ${res?.name ?? ''} ${res?.line?.concat(' 行')} ${res?.column?.concat(' 列')}`);
39
- }
40
- }
41
-
42
- exports.parseError = parseError;
@@ -1,7 +0,0 @@
1
- import { DevLogType } from './type';
2
- /**
3
- *
4
- * 解析 error
5
- *
6
- */
7
- export declare function parseError(type: DevLogType): void;
@@ -1,40 +0,0 @@
1
- import { pen } from 'color-pen';
2
-
3
- /**
4
- *
5
- * 解析 error
6
- *
7
- */
8
- function parseError(type) {
9
- try {
10
- throw new Error();
11
- }
12
- catch (error) {
13
- const parseErrorResult = (error.stack?.split('\n') || []).map(item => {
14
- const reg = /at\s(.*)\s\((.*):(\d*):(\d*)\)/;
15
- const res = reg.exec(item);
16
- if (res) {
17
- return {
18
- name: res[1],
19
- path: res[2],
20
- line: res[3],
21
- column: res[4],
22
- };
23
- }
24
- return {
25
- name: '',
26
- };
27
- });
28
- const result = parseErrorResult.filter(e => e.name !== '' && e.path !== undefined);
29
- const res = result[3] ?? result[2] ?? result[1] ?? result[0];
30
- const startStr = ` ${type === 'info' ? '💡' : type === 'error' ? '❌' : '⚠️ '} ${new Date().toLocaleString()} `;
31
- const printStartPenStr = (type === 'info'
32
- ? pen.bgCyan.brightWhite
33
- : type === 'error'
34
- ? pen.bgBlack.red
35
- : pen.bgBrightYellow.brightGreen)(startStr);
36
- console.log(`${printStartPenStr} ${res?.name ?? ''} ${res?.line?.concat(' 行')} ${res?.column?.concat(' 列')}`);
37
- }
38
- }
39
-
40
- export { parseError };
package/src/platform.cjs DELETED
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- var aJsTools = require('a-js-tools');
4
-
5
- const platform = aJsTools.isNode() ? 'node' : 'browser';
6
-
7
- exports.platform = platform;
package/src/platform.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const platform: string;
package/src/platform.mjs DELETED
@@ -1,5 +0,0 @@
1
- import { isNode } from 'a-js-tools';
2
-
3
- const platform = isNode() ? 'node' : 'browser';
4
-
5
- export { platform };
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- var parseError = require('./parseError.cjs');
4
-
5
- /**
6
- *
7
- * 打印 error 信息
8
- *
9
- */
10
- function printError(...str) {
11
- parseError.parseError('error');
12
- console.error(...str);
13
- }
14
-
15
- exports.printError = printError;
@@ -1,6 +0,0 @@
1
- /**
2
- *
3
- * 打印 error 信息
4
- *
5
- */
6
- export declare function printError(...str: unknown[]): void;
@@ -1,13 +0,0 @@
1
- import { parseError } from './parseError.mjs';
2
-
3
- /**
4
- *
5
- * 打印 error 信息
6
- *
7
- */
8
- function printError(...str) {
9
- parseError('error');
10
- console.error(...str);
11
- }
12
-
13
- export { printError };
package/src/printInfo.cjs DELETED
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- var parseError = require('./parseError.cjs');
4
-
5
- /**
6
- *
7
- * 打印 info 消息
8
- *
9
- */
10
- function printInfo(...str) {
11
- parseError.parseError('info');
12
- console.info(...str);
13
- }
14
-
15
- exports.printInfo = printInfo;
@@ -1,6 +0,0 @@
1
- /**
2
- *
3
- * 打印 info 消息
4
- *
5
- */
6
- export declare function printInfo(...str: unknown[]): void;
package/src/printInfo.mjs DELETED
@@ -1,13 +0,0 @@
1
- import { parseError } from './parseError.mjs';
2
-
3
- /**
4
- *
5
- * 打印 info 消息
6
- *
7
- */
8
- function printInfo(...str) {
9
- parseError('info');
10
- console.info(...str);
11
- }
12
-
13
- export { printInfo };
package/src/printWarn.cjs DELETED
@@ -1,15 +0,0 @@
1
- 'use strict';
2
-
3
- var parseError = require('./parseError.cjs');
4
-
5
- /**
6
- *
7
- * 打印 warn 信息
8
- *
9
- */
10
- function printWarn(...str) {
11
- parseError.parseError('warn');
12
- console.warn(...str);
13
- }
14
-
15
- exports.printWarn = printWarn;
@@ -1,6 +0,0 @@
1
- /**
2
- *
3
- * 打印 warn 信息
4
- *
5
- */
6
- export declare function printWarn(...str: unknown[]): void;
package/src/printWarn.mjs DELETED
@@ -1,13 +0,0 @@
1
- import { parseError } from './parseError.mjs';
2
-
3
- /**
4
- *
5
- * 打印 warn 信息
6
- *
7
- */
8
- function printWarn(...str) {
9
- parseError('warn');
10
- console.warn(...str);
11
- }
12
-
13
- export { printWarn };
package/src/setType.cjs DELETED
@@ -1,21 +0,0 @@
1
- 'use strict';
2
-
3
- const typeList = [
4
- false,
5
- true,
6
- 'all',
7
- 'info',
8
- 'error',
9
- 'warn',
10
- ];
11
- /**
12
- * 设置 type 的类型
13
- */
14
- function setType(type) {
15
- if (typeList.includes(type))
16
- return type;
17
- return false;
18
- }
19
-
20
- exports.setType = setType;
21
- exports.typeList = typeList;
package/src/setType.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { DevLogType } from './type';
2
- export declare const typeList: DevLogType[];
3
- /**
4
- * 设置 type 的类型
5
- */
6
- export declare function setType(type: DevLogType): DevLogType;
package/src/setType.mjs DELETED
@@ -1,18 +0,0 @@
1
- const typeList = [
2
- false,
3
- true,
4
- 'all',
5
- 'info',
6
- 'error',
7
- 'warn',
8
- ];
9
- /**
10
- * 设置 type 的类型
11
- */
12
- function setType(type) {
13
- if (typeList.includes(type))
14
- return type;
15
- return false;
16
- }
17
-
18
- export { setType, typeList };