@deot/dev-shared 1.0.2 → 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.
@@ -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
@@ -26,8 +26,31 @@ declare const formatBytes: (size: number, suffix?: number) => string;
26
26
 
27
27
  declare const getHost: () => string;
28
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
+
29
35
  export declare type Hash<T> = Indexable<T>;
30
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
+
31
54
  export declare type Indexable<T = any> = {
32
55
  [key: string]: T;
33
56
  };
@@ -39,6 +62,16 @@ declare const info: {
39
62
 
40
63
  declare const LOCAL_COMMAND_MAP: any;
41
64
 
65
+ declare namespace Locals {
66
+ export {
67
+ getNormalizePackage,
68
+ getPackageName,
69
+ getPackageFolderName,
70
+ impl
71
+ }
72
+ }
73
+ export { Locals }
74
+
42
75
  declare const log: {
43
76
  (...data: any[]): void;
44
77
  (message?: any, ...optionalParams: any[]): void;
@@ -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.2",
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": [
package/dist/index.js DELETED
@@ -1,113 +0,0 @@
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
-
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 command = (command$, args) => {
75
- const v = (command$ + SPACE + (args || []).join(SPACE))
76
- .match(/[^\s'"]+|'[^']*'|"[^"]*"/g);
77
- return v || [];
78
- };
79
- const exec = (command$, args) => {
80
- return util.promisify(childProcess.exec)(command(command$, args).join(SPACE));
81
- };
82
- const spawn = (command$, args, options) => {
83
- let [command$$, ...args$] = command(command$, args).map((i) => LOCAL_COMMAND_MAP[i] || i);
84
- args$ = args$.map((i) => i.replace(/^['"]|['"]$/g, ''));
85
- return new Promise((resolve, reject) => {
86
- const emit = childProcess.spawn(command$$, args$, {
87
- stdio: 'inherit',
88
- ...options
89
- });
90
- emit.on('close', (code) => {
91
- if (code === 0) {
92
- resolve(code);
93
- }
94
- else {
95
- reject(code);
96
- }
97
- });
98
- emit.on('error', (error) => {
99
- !process.exitCode && (process.exitCode = 1);
100
- reject(error);
101
- });
102
- });
103
- };
104
-
105
- var shell = /*#__PURE__*/Object.freeze({
106
- __proto__: null,
107
- LOCAL_COMMAND_MAP: LOCAL_COMMAND_MAP,
108
- command: command,
109
- exec: exec,
110
- spawn: spawn
111
- });
112
-
113
- export { logger as Logger, shell as Shell, utils as Utils };