@deot/dev-shared 1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 deot
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @deot/dev-shared
2
+
3
+ 公共方法
4
+
5
+ ## Logger
6
+
7
+ | 方法 | 说明 |
8
+ | ---------------------------- | ----------------------------------------------- |
9
+ | log | `console.log` |
10
+ | error | `console.error` |
11
+
12
+ ## Shell
13
+
14
+ | 方法 | 说明 |
15
+ | ---------------------------- | ----------------------------------------------- |
16
+ | exec | `exec` |
17
+ | spawn | `spawn` |
18
+
19
+ ## Utils
20
+
21
+ | 方法 | 说明 |
22
+ | ---------------------------- | ----------------------------------------------- |
23
+ | getHost | 获取当前ip |
24
+ | formatBytes | 字节转换 |
25
+ | autoCatch | 捕获异常 |
@@ -0,0 +1,77 @@
1
+ /// <reference types="node" />
2
+
3
+ import * as childProcess from 'node:child_process';
4
+
5
+ export declare interface AnyFunction<T = void> {
6
+ (...args: any[]): T;
7
+ }
8
+
9
+ declare const autoCatch: (impl: any, options?: Options) => Promise<any>;
10
+
11
+ export declare type Customized<Origin = any, Extend = any> = Origin & Extend;
12
+
13
+ declare const error: {
14
+ (...data: any[]): void;
15
+ (message?: any, ...optionalParams: any[]): void;
16
+ };
17
+
18
+ declare const exec: typeof childProcess.exec.__promisify__;
19
+
20
+ declare const formatBytes: (size: number, suffix?: number) => string;
21
+
22
+ declare const getHost: () => string;
23
+
24
+ export declare type Hash<T> = Indexable<T>;
25
+
26
+ export declare type Indexable<T = any> = {
27
+ [key: string]: T;
28
+ };
29
+
30
+ declare const info: {
31
+ (...data: any[]): void;
32
+ (message?: any, ...optionalParams: any[]): void;
33
+ };
34
+
35
+ declare const LOCAL_COMMAND_MAP: any;
36
+
37
+ declare const log: {
38
+ (...data: any[]): void;
39
+ (message?: any, ...optionalParams: any[]): void;
40
+ };
41
+
42
+ declare namespace Logger {
43
+ export {
44
+ log,
45
+ error,
46
+ info
47
+ }
48
+ }
49
+ export { Logger }
50
+
51
+ export declare type Nullable<T> = T | null;
52
+
53
+ export declare type Options<T = {}> = Indexable & T;
54
+
55
+ declare namespace Shell {
56
+ export {
57
+ LOCAL_COMMAND_MAP,
58
+ exec,
59
+ spawn
60
+ }
61
+ }
62
+ export { Shell }
63
+
64
+ declare const spawn: (command: string, args?: string[]) => Promise<unknown>;
65
+
66
+ export declare type TimeoutHandle = ReturnType<typeof global.setTimeout>;
67
+
68
+ declare namespace Utils {
69
+ export {
70
+ getHost,
71
+ formatBytes,
72
+ autoCatch
73
+ }
74
+ }
75
+ export { Utils }
76
+
77
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,107 @@
1
+ import os from 'node:os';
2
+ import path from 'node:path';
3
+ import * as childProcess from 'node:child_process';
4
+ import util from 'node:util';
5
+ import fs from 'node:fs';
6
+
7
+ const getHost = () => {
8
+ const ips = [];
9
+ const ntwk = os.networkInterfaces();
10
+ for (const k in ntwk) {
11
+ for (let i = 0; i < ntwk[k].length; i++) {
12
+ const _add = ntwk[k][i].address;
13
+ if (_add && _add.split('.').length == 4 && !ntwk[k][i].internal && ntwk[k][i].family == 'IPv4') {
14
+ ips.push(_add);
15
+ }
16
+ }
17
+ }
18
+ return ips[0];
19
+ };
20
+ const formatBytes = (size, suffix = 2) => {
21
+ if (!size)
22
+ return "0B";
23
+ const base = 1024;
24
+ const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
25
+ const index = Math.floor(Math.log(size) / Math.log(base));
26
+ const value = parseFloat((size / (base ** index)).toFixed(suffix));
27
+ return value + units[index];
28
+ };
29
+ const autoCatch = async (impl, options = {}) => {
30
+ const { onError = console.error } = options;
31
+ let target = impl;
32
+ typeof target === 'function' && (target = target());
33
+ try {
34
+ const e = await target;
35
+ return e;
36
+ }
37
+ catch (e) {
38
+ onError(e);
39
+ }
40
+ };
41
+
42
+ var utils = /*#__PURE__*/Object.freeze({
43
+ __proto__: null,
44
+ autoCatch: autoCatch,
45
+ formatBytes: formatBytes,
46
+ getHost: getHost
47
+ });
48
+
49
+ const log = console.log;
50
+ const error = console.error;
51
+ const info = console.info;
52
+
53
+ var logger = /*#__PURE__*/Object.freeze({
54
+ __proto__: null,
55
+ error: error,
56
+ info: info,
57
+ log: log
58
+ });
59
+
60
+ const SPACE = ' ';
61
+ const binDirectory = path.resolve(process.cwd(), './node_modules/.bin');
62
+ const LOCAL_COMMAND_MAP = fs.existsSync(binDirectory)
63
+ ? fs
64
+ .readdirSync(binDirectory)
65
+ .reduce((pre, file) => {
66
+ const fullpath = path.resolve(binDirectory, file);
67
+ const stat = fs.statSync(fullpath);
68
+ if (stat.isFile()) {
69
+ pre[file] = `./node_modules/.bin/${file}`;
70
+ }
71
+ return pre;
72
+ }, {})
73
+ : {};
74
+ const exec = util.promisify(childProcess.exec);
75
+ const spawn = (command, args = []) => {
76
+ const [command$, ...args$] = (command + SPACE + args.join(SPACE))
77
+ .replace(/\s+/g, SPACE)
78
+ .split(SPACE)
79
+ .filter(i => !!i)
80
+ .map(i => LOCAL_COMMAND_MAP[i] || i);
81
+ return new Promise((resolve, reject) => {
82
+ const emit = childProcess.spawn(command$, args$, {
83
+ stdio: 'inherit'
84
+ });
85
+ emit.on('close', (code) => {
86
+ if (code === 0) {
87
+ resolve(code);
88
+ }
89
+ else {
90
+ reject(code);
91
+ }
92
+ });
93
+ emit.on('error', (error) => {
94
+ !process.exitCode && (process.exitCode = 1);
95
+ reject(error);
96
+ });
97
+ });
98
+ };
99
+
100
+ var shell = /*#__PURE__*/Object.freeze({
101
+ __proto__: null,
102
+ LOCAL_COMMAND_MAP: LOCAL_COMMAND_MAP,
103
+ exec: exec,
104
+ spawn: spawn
105
+ });
106
+
107
+ export { logger as Logger, shell as Shell, utils as Utils };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@deot/dev-shared",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "gitHead": "f960c31945e60a492e50e835cbf0c987a9fc3d5c"
15
+ }