@deot/dev-shared 1.0.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/dist/index.cjs.js +275 -0
- package/dist/index.d.ts +41 -2
- package/dist/index.es.js +248 -0
- package/package.json +4 -5
- package/LICENSE +0 -21
- package/dist/index.js +0 -107
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var os = require('node:os');
|
|
4
|
+
var path = require('node:path');
|
|
5
|
+
var childProcess = require('node:child_process');
|
|
6
|
+
var util = require('node:util');
|
|
7
|
+
var fs = require('node:fs');
|
|
8
|
+
var node_module = require('node:module');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
28
|
+
var childProcess__namespace = /*#__PURE__*/_interopNamespaceDefault(childProcess);
|
|
29
|
+
var util__namespace = /*#__PURE__*/_interopNamespaceDefault(util);
|
|
30
|
+
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
|
|
31
|
+
|
|
32
|
+
const getHost = () => {
|
|
33
|
+
const ips = [];
|
|
34
|
+
const ntwk = os.networkInterfaces();
|
|
35
|
+
for (const k in ntwk) {
|
|
36
|
+
for (let i = 0; i < ntwk[k].length; i++) {
|
|
37
|
+
const _add = ntwk[k][i].address;
|
|
38
|
+
if (_add && _add.split('.').length == 4 && !ntwk[k][i].internal && ntwk[k][i].family == 'IPv4') {
|
|
39
|
+
ips.push(_add);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return ips[0];
|
|
44
|
+
};
|
|
45
|
+
const formatBytes = (size, suffix = 2) => {
|
|
46
|
+
if (!size)
|
|
47
|
+
return "0B";
|
|
48
|
+
const base = 1024;
|
|
49
|
+
const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
50
|
+
const index = Math.floor(Math.log(size) / Math.log(base));
|
|
51
|
+
const value = parseFloat((size / (base ** index)).toFixed(suffix));
|
|
52
|
+
return value + units[index];
|
|
53
|
+
};
|
|
54
|
+
const autoCatch = async (impl, options = {}) => {
|
|
55
|
+
const { onError = console.error } = options;
|
|
56
|
+
let target = impl;
|
|
57
|
+
typeof target === 'function' && (target = target());
|
|
58
|
+
try {
|
|
59
|
+
const e = await target;
|
|
60
|
+
return e;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
onError(e);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
var utils = /*#__PURE__*/Object.freeze({
|
|
68
|
+
__proto__: null,
|
|
69
|
+
autoCatch: autoCatch,
|
|
70
|
+
formatBytes: formatBytes,
|
|
71
|
+
getHost: getHost
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const log = console.log;
|
|
75
|
+
const error = console.error;
|
|
76
|
+
const info = console.info;
|
|
77
|
+
|
|
78
|
+
var logger = /*#__PURE__*/Object.freeze({
|
|
79
|
+
__proto__: null,
|
|
80
|
+
error: error,
|
|
81
|
+
info: info,
|
|
82
|
+
log: log
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const SPACE = ' ';
|
|
86
|
+
const binDirectory = path__namespace.resolve(process.cwd(), './node_modules/.bin');
|
|
87
|
+
const LOCAL_COMMAND_MAP = fs__namespace.existsSync(binDirectory)
|
|
88
|
+
? fs__namespace
|
|
89
|
+
.readdirSync(binDirectory)
|
|
90
|
+
.reduce((pre, file) => {
|
|
91
|
+
const fullpath = path__namespace.resolve(binDirectory, file);
|
|
92
|
+
const stat = fs__namespace.statSync(fullpath);
|
|
93
|
+
if (stat.isFile()) {
|
|
94
|
+
pre[file] = `./node_modules/.bin/${file}`;
|
|
95
|
+
}
|
|
96
|
+
return pre;
|
|
97
|
+
}, {})
|
|
98
|
+
: {};
|
|
99
|
+
const command = (command$, args) => {
|
|
100
|
+
const v = (command$ + SPACE + (args || []).join(SPACE))
|
|
101
|
+
.match(/[^\s'"]+|'[^']*'|"[^"]*"/g);
|
|
102
|
+
return v || [];
|
|
103
|
+
};
|
|
104
|
+
const exec = (command$, args) => {
|
|
105
|
+
return util__namespace.promisify(childProcess__namespace.exec)(command(command$, args).join(SPACE));
|
|
106
|
+
};
|
|
107
|
+
const spawn = (command$, args, options) => {
|
|
108
|
+
let [command$$, ...args$] = command(command$, args).map((i) => LOCAL_COMMAND_MAP[i] || i);
|
|
109
|
+
args$ = args$.map((i) => i.replace(/^['"]|['"]$/g, ''));
|
|
110
|
+
return new Promise((resolve, reject) => {
|
|
111
|
+
const emit = childProcess__namespace.spawn(command$$, args$, {
|
|
112
|
+
stdio: 'inherit',
|
|
113
|
+
...options
|
|
114
|
+
});
|
|
115
|
+
emit.on('close', (code) => {
|
|
116
|
+
if (code === 0) {
|
|
117
|
+
resolve(code);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
reject(code);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
emit.on('error', (error) => {
|
|
124
|
+
!process.exitCode && (process.exitCode = 1);
|
|
125
|
+
reject(error);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
var shell = /*#__PURE__*/Object.freeze({
|
|
131
|
+
__proto__: null,
|
|
132
|
+
LOCAL_COMMAND_MAP: LOCAL_COMMAND_MAP,
|
|
133
|
+
command: command,
|
|
134
|
+
exec: exec,
|
|
135
|
+
spawn: spawn
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const cwd$ = process.cwd();
|
|
139
|
+
const require$ = node_module.createRequire(cwd$);
|
|
140
|
+
const getNormalizePackage = (dataMap) => {
|
|
141
|
+
Object.keys(dataMap).forEach(packageName => {
|
|
142
|
+
const relations = dataMap[packageName];
|
|
143
|
+
relations.forEach((packageName$) => {
|
|
144
|
+
if (dataMap[packageName$].includes(packageName)) {
|
|
145
|
+
throw new Error(`${packageName} ${packageName$} deps loop`);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
const needUseMap = Object.keys(dataMap).reduce((pre, key) => (pre[key] = 0, pre), {});
|
|
150
|
+
const queue = [];
|
|
151
|
+
for (let key in dataMap) {
|
|
152
|
+
const dependencies = dataMap[key];
|
|
153
|
+
dependencies.forEach((dependency) => {
|
|
154
|
+
needUseMap[dependency] += 1;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
for (let key in needUseMap) {
|
|
158
|
+
if (needUseMap[key] === 0) {
|
|
159
|
+
queue.push(key);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const result = [];
|
|
163
|
+
while (queue.length > 0) {
|
|
164
|
+
const node = queue.shift();
|
|
165
|
+
if (!node)
|
|
166
|
+
return [];
|
|
167
|
+
result.push(node);
|
|
168
|
+
const dependencies = dataMap[node];
|
|
169
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
170
|
+
const dependency = dependencies[i];
|
|
171
|
+
needUseMap[dependency] -= 1;
|
|
172
|
+
if (needUseMap[dependency] === 0) {
|
|
173
|
+
queue.push(dependency);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return result.reverse();
|
|
178
|
+
};
|
|
179
|
+
const getPackageName = (packageFolderName$) => {
|
|
180
|
+
const { workspace, packageFolderName, packageName } = impl();
|
|
181
|
+
if (!workspace
|
|
182
|
+
|| !packageFolderName$
|
|
183
|
+
|| packageFolderName$ === packageFolderName) {
|
|
184
|
+
return packageName;
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
return `${packageName}-${packageFolderName$.replace(new RegExp(`${packageName}-?`), '')}`;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const getPackageFolderName = (packageName$) => {
|
|
191
|
+
const { workspace, packageFolderName, packageName } = impl();
|
|
192
|
+
if (!workspace)
|
|
193
|
+
return '';
|
|
194
|
+
if (packageName$ === packageName)
|
|
195
|
+
return packageFolderName;
|
|
196
|
+
return packageName$?.replace(new RegExp(`${packageName}-?`), '');
|
|
197
|
+
};
|
|
198
|
+
let configMap = {};
|
|
199
|
+
const impl = (cwd) => {
|
|
200
|
+
cwd = cwd || cwd$;
|
|
201
|
+
if (configMap[cwd])
|
|
202
|
+
return configMap[cwd];
|
|
203
|
+
const rootPackageOptions = require$(`${cwd}/package.json`);
|
|
204
|
+
let workspace = 'packages';
|
|
205
|
+
let isMonorepo = fs__namespace.existsSync(path__namespace.resolve(cwd, workspace));
|
|
206
|
+
workspace = isMonorepo ? workspace : '';
|
|
207
|
+
const packageFolderName = isMonorepo ? 'index' : '';
|
|
208
|
+
const packageDir = path__namespace.resolve(cwd, workspace);
|
|
209
|
+
const packageOptions = require$(path__namespace.resolve(packageDir, packageFolderName, 'package.json'));
|
|
210
|
+
const packageName = packageOptions.name;
|
|
211
|
+
const packageVersion = packageOptions.version;
|
|
212
|
+
const packageFolderNames = !isMonorepo ? [] : fs__namespace
|
|
213
|
+
.readdirSync(packageDir)
|
|
214
|
+
.reduce((pre, file) => {
|
|
215
|
+
const fullpath = path__namespace.resolve(packageDir, file);
|
|
216
|
+
const stat = fs__namespace.statSync(fullpath);
|
|
217
|
+
if (!(/(^_|tpl)/.test(file))
|
|
218
|
+
&& stat.isDirectory()) {
|
|
219
|
+
pre.push(file);
|
|
220
|
+
}
|
|
221
|
+
return pre;
|
|
222
|
+
}, []);
|
|
223
|
+
const packageOptionsMap = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
224
|
+
pre[packageFolderName$] = require$(path__namespace.resolve(packageDir, packageFolderName$, 'package.json'));
|
|
225
|
+
return pre;
|
|
226
|
+
}, {});
|
|
227
|
+
const packageDirsMap = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
228
|
+
pre[packageFolderName$] = path__namespace.resolve(packageDir, packageFolderName$);
|
|
229
|
+
return pre;
|
|
230
|
+
}, {});
|
|
231
|
+
const packageRelation = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
232
|
+
let packagesOptions = packageOptionsMap[packageFolderName$];
|
|
233
|
+
let deps = {
|
|
234
|
+
...(packagesOptions.dependencies || {}),
|
|
235
|
+
...(packagesOptions.devDependencies || {}),
|
|
236
|
+
};
|
|
237
|
+
pre[packagesOptions.name] = Object.keys(deps).filter(i => new RegExp(`${packageName}`).test(i));
|
|
238
|
+
return pre;
|
|
239
|
+
}, {});
|
|
240
|
+
const normalizePackageNames = getNormalizePackage(packageRelation);
|
|
241
|
+
const normalizePackageFolderNames = normalizePackageNames
|
|
242
|
+
.map(i => i.replace(new RegExp(`${packageName}-?`), '') || packageFolderName);
|
|
243
|
+
const homepage = (rootPackageOptions.repository || packageOptions.repository || {}).url || '';
|
|
244
|
+
const config = {
|
|
245
|
+
cwd,
|
|
246
|
+
workspace,
|
|
247
|
+
homepage: homepage.replace(/(.*)(https?:\/\/.*)(#|\.git)/, '$2'),
|
|
248
|
+
packageFolderName,
|
|
249
|
+
packageDir,
|
|
250
|
+
packageOptions,
|
|
251
|
+
packageName,
|
|
252
|
+
packageVersion,
|
|
253
|
+
packageFolderNames,
|
|
254
|
+
packageOptionsMap,
|
|
255
|
+
packageDirsMap,
|
|
256
|
+
packageRelation,
|
|
257
|
+
normalizePackageNames,
|
|
258
|
+
normalizePackageFolderNames
|
|
259
|
+
};
|
|
260
|
+
configMap[cwd] = config;
|
|
261
|
+
return config;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
var locals = /*#__PURE__*/Object.freeze({
|
|
265
|
+
__proto__: null,
|
|
266
|
+
getNormalizePackage: getNormalizePackage,
|
|
267
|
+
getPackageFolderName: getPackageFolderName,
|
|
268
|
+
getPackageName: getPackageName,
|
|
269
|
+
impl: impl
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
exports.Locals = locals;
|
|
273
|
+
exports.Logger = logger;
|
|
274
|
+
exports.Shell = shell;
|
|
275
|
+
exports.Utils = utils;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export declare interface AnyFunction<T = void> {
|
|
|
8
8
|
|
|
9
9
|
declare const autoCatch: (impl: any, options?: Options) => Promise<any>;
|
|
10
10
|
|
|
11
|
+
declare const command: (command$: string, args?: string[]) => RegExpMatchArray | [];
|
|
12
|
+
|
|
11
13
|
export declare type Customized<Origin = any, Extend = any> = Origin & Extend;
|
|
12
14
|
|
|
13
15
|
declare const error: {
|
|
@@ -15,14 +17,40 @@ declare const error: {
|
|
|
15
17
|
(message?: any, ...optionalParams: any[]): void;
|
|
16
18
|
};
|
|
17
19
|
|
|
18
|
-
declare const exec:
|
|
20
|
+
declare const exec: (command$: string, args?: string[]) => childProcess.PromiseWithChild<{
|
|
21
|
+
stdout: string;
|
|
22
|
+
stderr: string;
|
|
23
|
+
}>;
|
|
19
24
|
|
|
20
25
|
declare const formatBytes: (size: number, suffix?: number) => string;
|
|
21
26
|
|
|
22
27
|
declare const getHost: () => string;
|
|
23
28
|
|
|
29
|
+
declare const getNormalizePackage: (dataMap: any) => string[];
|
|
30
|
+
|
|
31
|
+
declare const getPackageFolderName: (packageName$: string) => string;
|
|
32
|
+
|
|
33
|
+
declare const getPackageName: (packageFolderName$: string) => any;
|
|
34
|
+
|
|
24
35
|
export declare type Hash<T> = Indexable<T>;
|
|
25
36
|
|
|
37
|
+
declare const impl: (cwd?: string) => {
|
|
38
|
+
cwd: string;
|
|
39
|
+
workspace: string;
|
|
40
|
+
homepage: any;
|
|
41
|
+
packageFolderName: string;
|
|
42
|
+
packageDir: string;
|
|
43
|
+
packageOptions: any;
|
|
44
|
+
packageName: any;
|
|
45
|
+
packageVersion: any;
|
|
46
|
+
packageFolderNames: string[];
|
|
47
|
+
packageOptionsMap: {};
|
|
48
|
+
packageDirsMap: {};
|
|
49
|
+
packageRelation: {};
|
|
50
|
+
normalizePackageNames: string[];
|
|
51
|
+
normalizePackageFolderNames: string[];
|
|
52
|
+
};
|
|
53
|
+
|
|
26
54
|
export declare type Indexable<T = any> = {
|
|
27
55
|
[key: string]: T;
|
|
28
56
|
};
|
|
@@ -34,6 +62,16 @@ declare const info: {
|
|
|
34
62
|
|
|
35
63
|
declare const LOCAL_COMMAND_MAP: any;
|
|
36
64
|
|
|
65
|
+
declare namespace Locals {
|
|
66
|
+
export {
|
|
67
|
+
getNormalizePackage,
|
|
68
|
+
getPackageName,
|
|
69
|
+
getPackageFolderName,
|
|
70
|
+
impl
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export { Locals }
|
|
74
|
+
|
|
37
75
|
declare const log: {
|
|
38
76
|
(...data: any[]): void;
|
|
39
77
|
(message?: any, ...optionalParams: any[]): void;
|
|
@@ -55,13 +93,14 @@ export declare type Options<T = {}> = Indexable & T;
|
|
|
55
93
|
declare namespace Shell {
|
|
56
94
|
export {
|
|
57
95
|
LOCAL_COMMAND_MAP,
|
|
96
|
+
command,
|
|
58
97
|
exec,
|
|
59
98
|
spawn
|
|
60
99
|
}
|
|
61
100
|
}
|
|
62
101
|
export { Shell }
|
|
63
102
|
|
|
64
|
-
declare const spawn: (command
|
|
103
|
+
declare const spawn: (command$: string, args?: string[], options?: any) => Promise<unknown>;
|
|
65
104
|
|
|
66
105
|
export declare type TimeoutHandle = ReturnType<typeof global.setTimeout>;
|
|
67
106
|
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import * as childProcess from 'node:child_process';
|
|
4
|
+
import * as util from 'node:util';
|
|
5
|
+
import * as fs from 'node:fs';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
|
|
8
|
+
const getHost = () => {
|
|
9
|
+
const ips = [];
|
|
10
|
+
const ntwk = os.networkInterfaces();
|
|
11
|
+
for (const k in ntwk) {
|
|
12
|
+
for (let i = 0; i < ntwk[k].length; i++) {
|
|
13
|
+
const _add = ntwk[k][i].address;
|
|
14
|
+
if (_add && _add.split('.').length == 4 && !ntwk[k][i].internal && ntwk[k][i].family == 'IPv4') {
|
|
15
|
+
ips.push(_add);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return ips[0];
|
|
20
|
+
};
|
|
21
|
+
const formatBytes = (size, suffix = 2) => {
|
|
22
|
+
if (!size)
|
|
23
|
+
return "0B";
|
|
24
|
+
const base = 1024;
|
|
25
|
+
const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
26
|
+
const index = Math.floor(Math.log(size) / Math.log(base));
|
|
27
|
+
const value = parseFloat((size / (base ** index)).toFixed(suffix));
|
|
28
|
+
return value + units[index];
|
|
29
|
+
};
|
|
30
|
+
const autoCatch = async (impl, options = {}) => {
|
|
31
|
+
const { onError = console.error } = options;
|
|
32
|
+
let target = impl;
|
|
33
|
+
typeof target === 'function' && (target = target());
|
|
34
|
+
try {
|
|
35
|
+
const e = await target;
|
|
36
|
+
return e;
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
onError(e);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var utils = /*#__PURE__*/Object.freeze({
|
|
44
|
+
__proto__: null,
|
|
45
|
+
autoCatch: autoCatch,
|
|
46
|
+
formatBytes: formatBytes,
|
|
47
|
+
getHost: getHost
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const log = console.log;
|
|
51
|
+
const error = console.error;
|
|
52
|
+
const info = console.info;
|
|
53
|
+
|
|
54
|
+
var logger = /*#__PURE__*/Object.freeze({
|
|
55
|
+
__proto__: null,
|
|
56
|
+
error: error,
|
|
57
|
+
info: info,
|
|
58
|
+
log: log
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const SPACE = ' ';
|
|
62
|
+
const binDirectory = path.resolve(process.cwd(), './node_modules/.bin');
|
|
63
|
+
const LOCAL_COMMAND_MAP = fs.existsSync(binDirectory)
|
|
64
|
+
? fs
|
|
65
|
+
.readdirSync(binDirectory)
|
|
66
|
+
.reduce((pre, file) => {
|
|
67
|
+
const fullpath = path.resolve(binDirectory, file);
|
|
68
|
+
const stat = fs.statSync(fullpath);
|
|
69
|
+
if (stat.isFile()) {
|
|
70
|
+
pre[file] = `./node_modules/.bin/${file}`;
|
|
71
|
+
}
|
|
72
|
+
return pre;
|
|
73
|
+
}, {})
|
|
74
|
+
: {};
|
|
75
|
+
const command = (command$, args) => {
|
|
76
|
+
const v = (command$ + SPACE + (args || []).join(SPACE))
|
|
77
|
+
.match(/[^\s'"]+|'[^']*'|"[^"]*"/g);
|
|
78
|
+
return v || [];
|
|
79
|
+
};
|
|
80
|
+
const exec = (command$, args) => {
|
|
81
|
+
return util.promisify(childProcess.exec)(command(command$, args).join(SPACE));
|
|
82
|
+
};
|
|
83
|
+
const spawn = (command$, args, options) => {
|
|
84
|
+
let [command$$, ...args$] = command(command$, args).map((i) => LOCAL_COMMAND_MAP[i] || i);
|
|
85
|
+
args$ = args$.map((i) => i.replace(/^['"]|['"]$/g, ''));
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
const emit = childProcess.spawn(command$$, args$, {
|
|
88
|
+
stdio: 'inherit',
|
|
89
|
+
...options
|
|
90
|
+
});
|
|
91
|
+
emit.on('close', (code) => {
|
|
92
|
+
if (code === 0) {
|
|
93
|
+
resolve(code);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
reject(code);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
emit.on('error', (error) => {
|
|
100
|
+
!process.exitCode && (process.exitCode = 1);
|
|
101
|
+
reject(error);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
var shell = /*#__PURE__*/Object.freeze({
|
|
107
|
+
__proto__: null,
|
|
108
|
+
LOCAL_COMMAND_MAP: LOCAL_COMMAND_MAP,
|
|
109
|
+
command: command,
|
|
110
|
+
exec: exec,
|
|
111
|
+
spawn: spawn
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const cwd$ = process.cwd();
|
|
115
|
+
const require$ = createRequire(cwd$);
|
|
116
|
+
const getNormalizePackage = (dataMap) => {
|
|
117
|
+
Object.keys(dataMap).forEach(packageName => {
|
|
118
|
+
const relations = dataMap[packageName];
|
|
119
|
+
relations.forEach((packageName$) => {
|
|
120
|
+
if (dataMap[packageName$].includes(packageName)) {
|
|
121
|
+
throw new Error(`${packageName} ${packageName$} deps loop`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
const needUseMap = Object.keys(dataMap).reduce((pre, key) => (pre[key] = 0, pre), {});
|
|
126
|
+
const queue = [];
|
|
127
|
+
for (let key in dataMap) {
|
|
128
|
+
const dependencies = dataMap[key];
|
|
129
|
+
dependencies.forEach((dependency) => {
|
|
130
|
+
needUseMap[dependency] += 1;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
for (let key in needUseMap) {
|
|
134
|
+
if (needUseMap[key] === 0) {
|
|
135
|
+
queue.push(key);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const result = [];
|
|
139
|
+
while (queue.length > 0) {
|
|
140
|
+
const node = queue.shift();
|
|
141
|
+
if (!node)
|
|
142
|
+
return [];
|
|
143
|
+
result.push(node);
|
|
144
|
+
const dependencies = dataMap[node];
|
|
145
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
146
|
+
const dependency = dependencies[i];
|
|
147
|
+
needUseMap[dependency] -= 1;
|
|
148
|
+
if (needUseMap[dependency] === 0) {
|
|
149
|
+
queue.push(dependency);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return result.reverse();
|
|
154
|
+
};
|
|
155
|
+
const getPackageName = (packageFolderName$) => {
|
|
156
|
+
const { workspace, packageFolderName, packageName } = impl();
|
|
157
|
+
if (!workspace
|
|
158
|
+
|| !packageFolderName$
|
|
159
|
+
|| packageFolderName$ === packageFolderName) {
|
|
160
|
+
return packageName;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return `${packageName}-${packageFolderName$.replace(new RegExp(`${packageName}-?`), '')}`;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
const getPackageFolderName = (packageName$) => {
|
|
167
|
+
const { workspace, packageFolderName, packageName } = impl();
|
|
168
|
+
if (!workspace)
|
|
169
|
+
return '';
|
|
170
|
+
if (packageName$ === packageName)
|
|
171
|
+
return packageFolderName;
|
|
172
|
+
return packageName$?.replace(new RegExp(`${packageName}-?`), '');
|
|
173
|
+
};
|
|
174
|
+
let configMap = {};
|
|
175
|
+
const impl = (cwd) => {
|
|
176
|
+
cwd = cwd || cwd$;
|
|
177
|
+
if (configMap[cwd])
|
|
178
|
+
return configMap[cwd];
|
|
179
|
+
const rootPackageOptions = require$(`${cwd}/package.json`);
|
|
180
|
+
let workspace = 'packages';
|
|
181
|
+
let isMonorepo = fs.existsSync(path.resolve(cwd, workspace));
|
|
182
|
+
workspace = isMonorepo ? workspace : '';
|
|
183
|
+
const packageFolderName = isMonorepo ? 'index' : '';
|
|
184
|
+
const packageDir = path.resolve(cwd, workspace);
|
|
185
|
+
const packageOptions = require$(path.resolve(packageDir, packageFolderName, 'package.json'));
|
|
186
|
+
const packageName = packageOptions.name;
|
|
187
|
+
const packageVersion = packageOptions.version;
|
|
188
|
+
const packageFolderNames = !isMonorepo ? [] : fs
|
|
189
|
+
.readdirSync(packageDir)
|
|
190
|
+
.reduce((pre, file) => {
|
|
191
|
+
const fullpath = path.resolve(packageDir, file);
|
|
192
|
+
const stat = fs.statSync(fullpath);
|
|
193
|
+
if (!(/(^_|tpl)/.test(file))
|
|
194
|
+
&& stat.isDirectory()) {
|
|
195
|
+
pre.push(file);
|
|
196
|
+
}
|
|
197
|
+
return pre;
|
|
198
|
+
}, []);
|
|
199
|
+
const packageOptionsMap = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
200
|
+
pre[packageFolderName$] = require$(path.resolve(packageDir, packageFolderName$, 'package.json'));
|
|
201
|
+
return pre;
|
|
202
|
+
}, {});
|
|
203
|
+
const packageDirsMap = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
204
|
+
pre[packageFolderName$] = path.resolve(packageDir, packageFolderName$);
|
|
205
|
+
return pre;
|
|
206
|
+
}, {});
|
|
207
|
+
const packageRelation = packageFolderNames.reduce((pre, packageFolderName$) => {
|
|
208
|
+
let packagesOptions = packageOptionsMap[packageFolderName$];
|
|
209
|
+
let deps = {
|
|
210
|
+
...(packagesOptions.dependencies || {}),
|
|
211
|
+
...(packagesOptions.devDependencies || {}),
|
|
212
|
+
};
|
|
213
|
+
pre[packagesOptions.name] = Object.keys(deps).filter(i => new RegExp(`${packageName}`).test(i));
|
|
214
|
+
return pre;
|
|
215
|
+
}, {});
|
|
216
|
+
const normalizePackageNames = getNormalizePackage(packageRelation);
|
|
217
|
+
const normalizePackageFolderNames = normalizePackageNames
|
|
218
|
+
.map(i => i.replace(new RegExp(`${packageName}-?`), '') || packageFolderName);
|
|
219
|
+
const homepage = (rootPackageOptions.repository || packageOptions.repository || {}).url || '';
|
|
220
|
+
const config = {
|
|
221
|
+
cwd,
|
|
222
|
+
workspace,
|
|
223
|
+
homepage: homepage.replace(/(.*)(https?:\/\/.*)(#|\.git)/, '$2'),
|
|
224
|
+
packageFolderName,
|
|
225
|
+
packageDir,
|
|
226
|
+
packageOptions,
|
|
227
|
+
packageName,
|
|
228
|
+
packageVersion,
|
|
229
|
+
packageFolderNames,
|
|
230
|
+
packageOptionsMap,
|
|
231
|
+
packageDirsMap,
|
|
232
|
+
packageRelation,
|
|
233
|
+
normalizePackageNames,
|
|
234
|
+
normalizePackageFolderNames
|
|
235
|
+
};
|
|
236
|
+
configMap[cwd] = config;
|
|
237
|
+
return config;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
var locals = /*#__PURE__*/Object.freeze({
|
|
241
|
+
__proto__: null,
|
|
242
|
+
getNormalizePackage: getNormalizePackage,
|
|
243
|
+
getPackageFolderName: getPackageFolderName,
|
|
244
|
+
getPackageName: getPackageName,
|
|
245
|
+
impl: impl
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
export { locals as Locals, logger as Logger, shell as Shell, utils as Utils };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deot/dev-shared",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"main": "dist/index.js",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"main": "dist/index.es.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
@@ -10,6 +10,5 @@
|
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
}
|
|
13
|
+
}
|
|
14
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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/dist/index.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
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 };
|