tnp-core 18.0.47 → 18.0.50
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 +1 -1
- package/assets/shared/shared_folder_info.txt +1 -1
- package/browser/esm2022/lib/core-imports.mjs +66 -5
- package/browser/esm2022/lib/core-models.mjs +51 -48
- package/browser/esm2022/lib/helpers-messages.mjs +9 -4
- package/browser/esm2022/lib/helpers.mjs +152 -369
- package/browser/esm2022/lib/utils.mjs +521 -8
- package/browser/fesm2022/tnp-core.mjs +1469 -1105
- package/browser/fesm2022/tnp-core.mjs.map +1 -1
- package/browser/lib/core-imports.d.ts +41 -1
- package/browser/lib/core-models.d.ts +51 -83
- package/browser/lib/helpers-messages.d.ts +6 -3
- package/browser/lib/helpers.d.ts +22 -0
- package/browser/lib/utils.d.ts +48 -0
- package/cli.backend.d.ts +1 -0
- package/cli.backend.js +15 -2
- package/cli.backend.js.map +1 -1
- package/client/esm2022/lib/core-imports.mjs +66 -5
- package/client/esm2022/lib/core-models.mjs +51 -48
- package/client/esm2022/lib/helpers-messages.mjs +9 -4
- package/client/esm2022/lib/helpers.mjs +152 -369
- package/client/esm2022/lib/utils.mjs +521 -8
- package/client/fesm2022/tnp-core.mjs +1469 -1105
- package/client/fesm2022/tnp-core.mjs.map +1 -1
- package/client/lib/core-imports.d.ts +41 -1
- package/client/lib/core-models.d.ts +51 -83
- package/client/lib/helpers-messages.d.ts +6 -3
- package/client/lib/helpers.d.ts +22 -0
- package/client/lib/utils.d.ts +48 -0
- package/index.js +15 -2
- package/index.js.map +1 -1
- package/lib/build-info._auto-generated_.js.map +1 -1
- package/lib/command-exists.js +20 -20
- package/lib/command-exists.js.map +1 -1
- package/lib/core-cli.js +49 -66
- package/lib/core-cli.js.map +1 -1
- package/lib/core-config.js.map +1 -1
- package/lib/core-imports.d.ts +41 -20
- package/lib/core-imports.js +202 -78
- package/lib/core-imports.js.map +1 -1
- package/lib/core-models.d.ts +51 -83
- package/lib/core-models.js +52 -47
- package/lib/core-models.js.map +1 -1
- package/lib/framework-name.js +2 -2
- package/lib/framework-name.js.map +1 -1
- package/lib/helpers-isomorphic.js +33 -52
- package/lib/helpers-isomorphic.js.map +1 -1
- package/lib/helpers-messages.d.ts +6 -3
- package/lib/helpers-messages.js +79 -102
- package/lib/helpers-messages.js.map +1 -1
- package/lib/helpers.d.ts +21 -3
- package/lib/helpers.js +718 -967
- package/lib/helpers.js.map +1 -1
- package/lib/index._auto-generated_.js.map +1 -1
- package/lib/index.js +23 -10
- package/lib/index.js.map +1 -1
- package/lib/node-chalk-mock.js +2 -2
- package/lib/node-chalk-mock.js.map +1 -1
- package/lib/node-path-mock.js +2 -2
- package/lib/node-path-mock.js.map +1 -1
- package/lib/progress-data.js +21 -23
- package/lib/progress-data.js.map +1 -1
- package/lib/public-api.js +19 -6
- package/lib/public-api.js.map +1 -1
- package/lib/required-global-npm-packages.js.map +1 -1
- package/lib/required.d.ts +44 -0
- package/lib/safe-types/glob.d.ts +102 -0
- package/lib/safe-types/minimatch/assert-valid-pattern.d.ts +1 -0
- package/lib/safe-types/minimatch/ast.d.ts +17 -0
- package/lib/safe-types/minimatch/brace-expressions.d.ts +7 -0
- package/lib/safe-types/minimatch/escape.d.ts +11 -0
- package/lib/safe-types/minimatch/index.d copy.d.ts +93 -0
- package/lib/safe-types/minimatch/index.d.ts +93 -0
- package/lib/safe-types/minimatch/unescapd.d.ts +16 -0
- package/lib/safe-types/minimatch/unescape.d.ts +16 -0
- package/lib/safe-types/minimatch.d.ts +0 -0
- package/lib/safe-types/rimraf.d.ts +28 -0
- package/lib/utils.d.ts +48 -1
- package/lib/utils.js +1252 -1146
- package/lib/utils.js.map +1 -1
- package/migrations/index.js +15 -2
- package/migrations/index.js.map +1 -1
- package/migrations/migrations_index._auto-generated_.js.map +1 -1
- package/package.json +26 -89
- package/src.d.ts +1 -1
- package/taon.jsonc +48 -58
- package/tmp-environment.json +8 -403
- package/websql/esm2022/lib/core-imports.mjs +66 -5
- package/websql/esm2022/lib/core-models.mjs +51 -48
- package/websql/esm2022/lib/helpers-messages.mjs +9 -4
- package/websql/esm2022/lib/helpers.mjs +152 -369
- package/websql/esm2022/lib/utils.mjs +521 -8
- package/websql/fesm2022/tnp-core.mjs +1466 -1102
- package/websql/fesm2022/tnp-core.mjs.map +1 -1
- package/websql/lib/core-imports.d.ts +41 -1
- package/websql/lib/core-models.d.ts +51 -83
- package/websql/lib/helpers-messages.d.ts +6 -3
- package/websql/lib/helpers.d.ts +22 -0
- package/websql/lib/utils.d.ts +48 -0
- package/browser/package.json +0 -25
- package/websql/package.json +0 -25
package/lib/utils.js
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.UtilsTerminal = exports.UtilsMigrations = exports.UtilsString = exports.UtilsOs = exports.UtilsProcess = exports.Utils = void 0;
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
//#region imports
|
5
|
+
const buffer_1 = require("buffer");
|
6
|
+
const net = require("net");
|
7
|
+
const util_1 = require("util");
|
8
|
+
const axios_1 = require("axios");
|
9
|
+
const core_imports_1 = require("./core-imports");
|
10
|
+
const core_imports_2 = require("./core-imports");
|
11
|
+
const core_imports_3 = require("./core-imports");
|
12
|
+
const core_imports_4 = require("./core-imports");
|
13
|
+
const core_models_1 = require("./core-models");
|
14
|
+
const index_1 = require("./index");
|
15
15
|
//#endregion
|
16
|
-
|
16
|
+
const BLOB_SUPPORTED_IN_SQLJS = false;
|
17
17
|
//#region utils
|
18
18
|
var Utils;
|
19
19
|
(function (Utils) {
|
20
|
-
var _this = this;
|
21
20
|
//#region utils / uniq array
|
22
|
-
Utils.uniqArray =
|
21
|
+
Utils.uniqArray = (array, uniqueProperty) => {
|
23
22
|
var seen = {};
|
24
23
|
return array
|
25
|
-
.filter(
|
24
|
+
.filter(f => !!f)
|
26
25
|
.filter(function (item) {
|
27
26
|
return seen.hasOwnProperty(uniqueProperty ? item[uniqueProperty] : item)
|
28
27
|
? false
|
@@ -30,95 +29,165 @@ var Utils;
|
|
30
29
|
});
|
31
30
|
};
|
32
31
|
//#endregion
|
32
|
+
//#region utils / recursive sort keys in object
|
33
|
+
/**
|
34
|
+
* @param anyObject
|
35
|
+
* @returns object with sorted keys
|
36
|
+
*/
|
37
|
+
Utils.sortKeys = (anyObject) => {
|
38
|
+
if (core_imports_1._.isArray(anyObject)) {
|
39
|
+
return anyObject.map(Utils.sortKeys);
|
40
|
+
}
|
41
|
+
if (core_imports_1._.isObject(anyObject)) {
|
42
|
+
return core_imports_1._.fromPairs(core_imports_1._.keys(anyObject)
|
43
|
+
.sort()
|
44
|
+
.map(key => [key, Utils.sortKeys(anyObject[key])]));
|
45
|
+
}
|
46
|
+
return anyObject;
|
47
|
+
};
|
48
|
+
//#endregion
|
33
49
|
//#region utils / escape string for reg exp
|
34
50
|
/**
|
35
51
|
* Example:
|
36
52
|
* new RegExp(escapeStringForRegEx('a.b.c'),'g') => /a\.b\.c/g
|
37
53
|
*/
|
38
|
-
Utils.escapeStringForRegEx =
|
54
|
+
Utils.escapeStringForRegEx = (stringForRegExp) => {
|
39
55
|
return stringForRegExp.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
40
56
|
};
|
41
57
|
//#endregion
|
42
58
|
//#region utils / remove chalk special chars
|
43
59
|
function removeChalkSpecialChars(str) {
|
44
60
|
// Regex to match ANSI escape sequences used by Chalk
|
45
|
-
|
61
|
+
const ansiRegex = /\u001b\[[0-9;]*m/g;
|
46
62
|
// Replace all ANSI escape sequences with an empty string
|
47
63
|
return str.replace(ansiRegex, '');
|
48
64
|
}
|
49
65
|
Utils.removeChalkSpecialChars = removeChalkSpecialChars;
|
50
66
|
//#endregion
|
51
67
|
//#region utils / full date time
|
52
|
-
Utils.fullDateTime =
|
68
|
+
Utils.fullDateTime = () => {
|
53
69
|
//#region @backendFunc
|
54
70
|
return (0, core_imports_2.dateformat)(new Date(), 'dd-mm-yyyy HH:MM:ss');
|
55
71
|
//#endregion
|
56
72
|
};
|
57
73
|
//#endregion
|
58
74
|
//#region utils / full date
|
59
|
-
Utils.fullDate =
|
75
|
+
Utils.fullDate = () => {
|
60
76
|
//#region @backendFunc
|
61
77
|
return (0, core_imports_2.dateformat)(new Date(), 'dd-mm-yyyy');
|
62
78
|
//#endregion
|
63
79
|
};
|
64
80
|
//#endregion
|
65
81
|
//#region utils / get free port
|
66
|
-
|
67
|
-
Utils.getFreePort =
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
i = 0;
|
78
|
-
_a.label = 1;
|
79
|
-
case 1:
|
80
|
-
if (!true) return [3 /*break*/, 6];
|
81
|
-
_a.label = 2;
|
82
|
-
case 2:
|
83
|
-
_a.trys.push([2, 4, , 5]);
|
84
|
-
return [4 /*yield*/, UtilsOs.isPortInUse(startFrom)];
|
85
|
-
case 3:
|
86
|
-
if (_a.sent()) {
|
87
|
-
startFrom += 1;
|
88
|
-
return [3 /*break*/, 1];
|
89
|
-
}
|
90
|
-
port = startFrom;
|
91
|
-
takenPorts.push(port);
|
92
|
-
return [2 /*return*/, port];
|
93
|
-
case 4:
|
94
|
-
err_1 = _a.sent();
|
95
|
-
console.log(err_1);
|
96
|
-
index_1.Helpers.warn("Trying to assign port :".concat(startFrom, " but already in use."), false);
|
97
|
-
return [3 /*break*/, 5];
|
98
|
-
case 5:
|
82
|
+
const takenPorts = [];
|
83
|
+
Utils.getFreePort = async (options) => {
|
84
|
+
//#region @backendFunc
|
85
|
+
options = options || {};
|
86
|
+
options.startFrom = options.startFrom || 3000;
|
87
|
+
let startFrom = options.startFrom;
|
88
|
+
const max = 5000;
|
89
|
+
let i = 0;
|
90
|
+
while (true) {
|
91
|
+
try {
|
92
|
+
if (await UtilsOs.isPortInUse(startFrom)) {
|
99
93
|
startFrom += 1;
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
94
|
+
continue;
|
95
|
+
}
|
96
|
+
const port = startFrom;
|
97
|
+
takenPorts.push(port);
|
98
|
+
return port;
|
99
|
+
}
|
100
|
+
catch (err) {
|
101
|
+
console.log(err);
|
102
|
+
index_1.Helpers.warn(`Trying to assign port :${startFrom} but already in use.`, false);
|
103
|
+
}
|
104
|
+
startFrom += 1;
|
105
|
+
if (i++ === max) {
|
106
|
+
index_1.Helpers.error(`[taon-helpers]] failed to assign free port after ${max} trys...`);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
//#endregion
|
110
|
+
};
|
111
|
+
//#endregion
|
112
|
+
//#region utils / required uncached
|
113
|
+
/**
|
114
|
+
* Traverses the cache to search for all the cached
|
115
|
+
* files of the specified module name
|
116
|
+
*/
|
117
|
+
const searchCache = (moduleName, callback) => {
|
118
|
+
//#region @backendFunc
|
119
|
+
// Resolve the module identified by the specified name
|
120
|
+
var mod = require.resolve(moduleName);
|
121
|
+
// Check if the module has been resolved and found within
|
122
|
+
// the cache
|
123
|
+
if (mod && (mod = require.cache[mod]) !== undefined) {
|
124
|
+
// Recursively go over the results
|
125
|
+
(function traverse(mod) {
|
126
|
+
// Go over each of the module's children and
|
127
|
+
// traverse them
|
128
|
+
mod['children'].forEach(function (child) {
|
129
|
+
traverse(child);
|
130
|
+
});
|
131
|
+
// Call the specified callback providing the
|
132
|
+
// found cached module
|
133
|
+
callback(mod);
|
134
|
+
})(mod);
|
135
|
+
}
|
136
|
+
//#endregion
|
137
|
+
};
|
138
|
+
/**
|
139
|
+
* Removes a module from the cache
|
140
|
+
*/
|
141
|
+
const purgeCache = moduleName => {
|
142
|
+
//#region @backendFunc
|
143
|
+
// Traverse the cache looking for the files
|
144
|
+
// loaded by the specified module name
|
145
|
+
searchCache(moduleName, function (mod) {
|
146
|
+
delete require.cache[mod.id];
|
147
|
+
});
|
148
|
+
// Remove cached paths to the module.
|
149
|
+
// Thanks to @bentael for pointing this out.
|
150
|
+
Object.keys(module.constructor['_pathCache']).forEach(function (cacheKey) {
|
151
|
+
if (cacheKey.indexOf(moduleName) > 0) {
|
152
|
+
delete module.constructor['_pathCache'][cacheKey];
|
105
153
|
}
|
106
154
|
});
|
107
|
-
|
108
|
-
|
155
|
+
//#endregion
|
156
|
+
};
|
157
|
+
Utils.requireUncached = (module) => {
|
158
|
+
//#region @backendFunc
|
159
|
+
const result = core_imports_1._.cloneDeep(require(module));
|
160
|
+
purgeCache(module);
|
161
|
+
return result;
|
162
|
+
//#endregion
|
163
|
+
};
|
164
|
+
//#endregion
|
165
|
+
//#region utils / camelize
|
166
|
+
/**
|
167
|
+
* similar to camelCase but remove
|
168
|
+
* all non word / repeated characters
|
169
|
+
*/
|
170
|
+
Utils.camelize = (str = '') => {
|
171
|
+
str = str.replace(/\W/g, '').toLowerCase();
|
172
|
+
return str
|
173
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
174
|
+
return index == 0 ? word.toLowerCase() : word.toUpperCase();
|
175
|
+
})
|
176
|
+
.replace(/\s+/g, '');
|
177
|
+
};
|
178
|
+
let json;
|
109
179
|
(function (json) {
|
110
|
-
json.getAtrributies =
|
111
|
-
fileContent) {
|
112
|
-
|
113
|
-
var lines = fileContent.split('\n');
|
180
|
+
json.getAtrributies = (jsonDeepPath, // lodash path to property in json ex. deep.path.to.prop
|
181
|
+
fileContent) => {
|
182
|
+
const lines = fileContent.split('\n');
|
114
183
|
// split path to parts but keep part if is for example 'sql.js
|
115
|
-
|
184
|
+
const pathParts = jsonDeepPath.split('.').reduce((a, b) => {
|
116
185
|
if (a.length === 0) {
|
117
186
|
return [b];
|
118
187
|
}
|
119
|
-
|
120
|
-
if ((last.startsWith(
|
121
|
-
(last.startsWith(
|
188
|
+
const last = a[a.length - 1];
|
189
|
+
if ((last.startsWith(`['`) && b.endsWith(`']`)) ||
|
190
|
+
(last.startsWith(`["`) && b.endsWith(`"]`))) {
|
122
191
|
a[a.length - 1] = [last, b].join('.');
|
123
192
|
}
|
124
193
|
else {
|
@@ -128,100 +197,76 @@ var Utils;
|
|
128
197
|
}, []);
|
129
198
|
// console.log({ pathParts });
|
130
199
|
// const pathParts = jsonDeepPath.split('.');
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
attributes = extractAttributesFromComments(collectedComments);
|
165
|
-
break;
|
166
|
-
}
|
167
|
-
// Reset collected comments as they only relate to the next key
|
168
|
-
collectedComments = [];
|
200
|
+
const keyName = pathParts.pop().replace(/^\["(.+)"\]$/, '$1');
|
201
|
+
let currentPath = '';
|
202
|
+
let attributes = [];
|
203
|
+
let collectedComments = [];
|
204
|
+
for (const line of lines) {
|
205
|
+
const trimmedLine = line.trim();
|
206
|
+
if (trimmedLine.startsWith('//')) {
|
207
|
+
// Collect comments
|
208
|
+
collectedComments.push(trimmedLine);
|
209
|
+
// trimmedLine.startsWith('//pizda') &&
|
210
|
+
// console.log('pushlin line', { trimmedLine });
|
211
|
+
}
|
212
|
+
else if (trimmedLine.startsWith('"') || trimmedLine.startsWith("'")) {
|
213
|
+
// Extract the key from the line
|
214
|
+
const match = trimmedLine.match(/["']([^"']+)["']\s*:/);
|
215
|
+
// console.log({ match0: match && match[0], match1: match && match[1] });
|
216
|
+
if (match) {
|
217
|
+
const key = match[1];
|
218
|
+
currentPath = currentPath
|
219
|
+
? `${currentPath}.${key.includes('.') ? `['${key}']` : key}`
|
220
|
+
: key;
|
221
|
+
// console.log({ key });
|
222
|
+
// Check if the current path matches the jsonDeepPath
|
223
|
+
if ((currentPath.endsWith(keyName) &&
|
224
|
+
!currentPath.endsWith('/' + keyName)) ||
|
225
|
+
currentPath.endsWith(`['${keyName}']`)) {
|
226
|
+
// console.log('extract attributes', {
|
227
|
+
// keyName,
|
228
|
+
// collectedCommentsLength: collectedComments.length,
|
229
|
+
// });
|
230
|
+
// Process the collected comments to extract attributes
|
231
|
+
attributes = extractAttributesFromComments(collectedComments);
|
232
|
+
break;
|
169
233
|
}
|
234
|
+
// Reset collected comments as they only relate to the next key
|
235
|
+
collectedComments = [];
|
170
236
|
}
|
171
237
|
}
|
172
238
|
}
|
173
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
174
|
-
finally {
|
175
|
-
try {
|
176
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
177
|
-
}
|
178
|
-
finally { if (e_1) throw e_1.error; }
|
179
|
-
}
|
180
239
|
return attributes;
|
181
240
|
};
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
}
|
199
|
-
else {
|
200
|
-
existingAttribute.value = [existingAttribute.value, value];
|
201
|
-
}
|
241
|
+
const extractAttributesFromComments = (comments) => {
|
242
|
+
const attributes = [];
|
243
|
+
const attrRegex = /@(\w+)(?:\s*=\s*([^\s@]+))?/g;
|
244
|
+
// console.log({ comments });
|
245
|
+
for (const comment of comments) {
|
246
|
+
let match;
|
247
|
+
while ((match = attrRegex.exec(comment)) !== null) {
|
248
|
+
const [, name, value] = match;
|
249
|
+
const existingAttribute = attributes.find(attr => attr.name === `@${name}`);
|
250
|
+
if (existingAttribute) {
|
251
|
+
if (value) {
|
252
|
+
if (Array.isArray(existingAttribute.value)) {
|
253
|
+
existingAttribute.value.push(value);
|
254
|
+
}
|
255
|
+
else {
|
256
|
+
existingAttribute.value = [existingAttribute.value, value];
|
202
257
|
}
|
203
258
|
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
}
|
210
|
-
};
|
211
|
-
while ((match = attrRegex.exec(comment)) !== null) {
|
212
|
-
_loop_1();
|
259
|
+
}
|
260
|
+
else {
|
261
|
+
attributes.push({
|
262
|
+
name: `@${name}`,
|
263
|
+
value: value ? value : true,
|
264
|
+
});
|
213
265
|
}
|
214
266
|
}
|
215
267
|
}
|
216
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
217
|
-
finally {
|
218
|
-
try {
|
219
|
-
if (comments_1_1 && !comments_1_1.done && (_a = comments_1.return)) _a.call(comments_1);
|
220
|
-
}
|
221
|
-
finally { if (e_2) throw e_2.error; }
|
222
|
-
}
|
223
268
|
// Normalize single values not to be arrays
|
224
|
-
attributes.forEach(
|
269
|
+
attributes.forEach(attr => {
|
225
270
|
if (Array.isArray(attr.value) && attr.value.length === 1) {
|
226
271
|
attr.value = attr.value[0];
|
227
272
|
}
|
@@ -232,7 +277,7 @@ var Utils;
|
|
232
277
|
//#endregion
|
233
278
|
//#region binary
|
234
279
|
//#region db binary format type
|
235
|
-
|
280
|
+
let DbBinaryFormatEnum;
|
236
281
|
(function (DbBinaryFormatEnum) {
|
237
282
|
DbBinaryFormatEnum["Blob"] = "Blob";
|
238
283
|
DbBinaryFormatEnum["File"] = "File";
|
@@ -243,7 +288,7 @@ var Utils;
|
|
243
288
|
})(DbBinaryFormatEnum = Utils.DbBinaryFormatEnum || (Utils.DbBinaryFormatEnum = {}));
|
244
289
|
//#endregion
|
245
290
|
//#endregion
|
246
|
-
|
291
|
+
let binary;
|
247
292
|
(function (binary) {
|
248
293
|
//#region binay utils / array buffer to blob
|
249
294
|
/* */
|
@@ -268,21 +313,14 @@ var Utils;
|
|
268
313
|
* @param blob
|
269
314
|
* @returns
|
270
315
|
*/
|
271
|
-
function blobToArrayBuffer(blob) {
|
272
|
-
return
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
var reader = new FileReader();
|
277
|
-
reader.addEventListener('loadend', function () {
|
278
|
-
resolve(reader.result);
|
279
|
-
});
|
280
|
-
reader.addEventListener('error', reject); // @ts-ignore
|
281
|
-
reader.readAsArrayBuffer(blob);
|
282
|
-
})];
|
283
|
-
case 1: return [2 /*return*/, _a.sent()];
|
284
|
-
}
|
316
|
+
async function blobToArrayBuffer(blob) {
|
317
|
+
return await new Promise((resolve, reject) => {
|
318
|
+
const reader = new FileReader();
|
319
|
+
reader.addEventListener('loadend', () => {
|
320
|
+
resolve(reader.result);
|
285
321
|
});
|
322
|
+
reader.addEventListener('error', reject); // @ts-ignore
|
323
|
+
reader.readAsArrayBuffer(blob);
|
286
324
|
});
|
287
325
|
}
|
288
326
|
binary.blobToArrayBuffer = blobToArrayBuffer;
|
@@ -294,9 +332,9 @@ var Utils;
|
|
294
332
|
* @returns
|
295
333
|
*/
|
296
334
|
function blobToBase64(blob) {
|
297
|
-
return new Promise(
|
298
|
-
|
299
|
-
reader.onloadend =
|
335
|
+
return new Promise((resolve, _) => {
|
336
|
+
const reader = new FileReader();
|
337
|
+
reader.onloadend = () => resolve(reader.result); // @ts-ignore
|
300
338
|
reader.readAsDataURL(blob);
|
301
339
|
});
|
302
340
|
}
|
@@ -308,419 +346,242 @@ var Utils;
|
|
308
346
|
* @param base64Data
|
309
347
|
* @returns
|
310
348
|
*/
|
311
|
-
function base64toBlob(base64Data, contentTypeOverride) {
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
}
|
343
|
-
return [2 /*return*/, new buffer_1.Blob(byteArrays, { type: content_type })];
|
344
|
-
});
|
345
|
-
});
|
349
|
+
async function base64toBlob(base64Data, contentTypeOverride) {
|
350
|
+
let content_type = void 0;
|
351
|
+
let file_base64 = void 0;
|
352
|
+
if (!contentTypeOverride) {
|
353
|
+
const m = /^data:(.+?);base64,(.+)$/.exec(base64Data);
|
354
|
+
if (!m) {
|
355
|
+
throw new Error(`[taon-framework][base64toBlob] Not a base64 blob [${base64Data}]`);
|
356
|
+
}
|
357
|
+
// tslint:disable-next-line:prefer-const
|
358
|
+
var [__, contenttype, filebase64] = m;
|
359
|
+
content_type = contenttype;
|
360
|
+
file_base64 = filebase64;
|
361
|
+
}
|
362
|
+
content_type = (contentTypeOverride ? contentTypeOverride : content_type || '');
|
363
|
+
base64Data = contentTypeOverride ? base64Data : file_base64;
|
364
|
+
const sliceSize = 1024;
|
365
|
+
const byteCharacters = atob(base64Data);
|
366
|
+
const bytesLength = byteCharacters.length;
|
367
|
+
const slicesCount = Math.ceil(bytesLength / sliceSize);
|
368
|
+
const byteArrays = new Array(slicesCount);
|
369
|
+
for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
|
370
|
+
const begin = sliceIndex * sliceSize;
|
371
|
+
const end = Math.min(begin + sliceSize, bytesLength);
|
372
|
+
const bytes = new Array(end - begin);
|
373
|
+
// tslint:disable-next-line:one-variable-per-declaration
|
374
|
+
for (let offset = begin, i = 0; offset < end; ++i, ++offset) {
|
375
|
+
bytes[i] = byteCharacters[offset].charCodeAt(0);
|
376
|
+
}
|
377
|
+
byteArrays[sliceIndex] = new Uint8Array(bytes);
|
378
|
+
}
|
379
|
+
return new buffer_1.Blob(byteArrays, { type: content_type });
|
346
380
|
}
|
347
381
|
binary.base64toBlob = base64toBlob;
|
348
382
|
//#endregion
|
349
383
|
//#region binay utils / base64 string to db binary format
|
350
|
-
function base64toDbBinaryFormat(text) {
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
case 1:
|
368
|
-
/* */
|
369
|
-
/* */
|
370
|
-
/* */
|
371
|
-
/* */
|
372
|
-
/* */
|
373
|
-
/* */
|
374
|
-
/* */
|
375
|
-
/* */
|
376
|
-
//#region @backend
|
377
|
-
result = _a.sent();
|
378
|
-
//#endregion
|
379
|
-
return [2 /*return*/, result];
|
380
|
-
}
|
381
|
-
});
|
382
|
-
});
|
384
|
+
async function base64toDbBinaryFormat(text) {
|
385
|
+
let result;
|
386
|
+
/* */
|
387
|
+
/* */
|
388
|
+
/* */
|
389
|
+
/* */
|
390
|
+
/* */
|
391
|
+
/* */
|
392
|
+
/* */
|
393
|
+
/* */
|
394
|
+
//#region @backend
|
395
|
+
result = await (async () => {
|
396
|
+
const buffer = await base64toBuffer(text);
|
397
|
+
return buffer;
|
398
|
+
})();
|
399
|
+
//#endregion
|
400
|
+
return result;
|
383
401
|
}
|
384
402
|
binary.base64toDbBinaryFormat = base64toDbBinaryFormat;
|
385
403
|
//#endregion
|
386
404
|
//#region binay utils / db binary format to base64 string
|
387
|
-
function dbBinaryFormatToBase64(binaryFormat) {
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
case 1:
|
405
|
-
/* */
|
406
|
-
/* */
|
407
|
-
/* */
|
408
|
-
/* */
|
409
|
-
/* */
|
410
|
-
/* */
|
411
|
-
/* */
|
412
|
-
/* */
|
413
|
-
//#region @backend
|
414
|
-
result = _a.sent();
|
415
|
-
//#endregion
|
416
|
-
return [2 /*return*/, result];
|
417
|
-
}
|
418
|
-
});
|
419
|
-
});
|
405
|
+
async function dbBinaryFormatToBase64(binaryFormat) {
|
406
|
+
let result;
|
407
|
+
/* */
|
408
|
+
/* */
|
409
|
+
/* */
|
410
|
+
/* */
|
411
|
+
/* */
|
412
|
+
/* */
|
413
|
+
/* */
|
414
|
+
/* */
|
415
|
+
//#region @backend
|
416
|
+
result = await (async () => {
|
417
|
+
const text = await bufferToBase64(binaryFormat);
|
418
|
+
return text;
|
419
|
+
})();
|
420
|
+
//#endregion
|
421
|
+
return result;
|
420
422
|
}
|
421
423
|
binary.dbBinaryFormatToBase64 = dbBinaryFormatToBase64;
|
422
424
|
//#endregion
|
423
425
|
//#region binay utils / base64 string to db binary format
|
424
|
-
function textToDbBinaryFormat(text) {
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
case 1:
|
442
|
-
/* */
|
443
|
-
/* */
|
444
|
-
/* */
|
445
|
-
/* */
|
446
|
-
/* */
|
447
|
-
/* */
|
448
|
-
/* */
|
449
|
-
/* */
|
450
|
-
//#region @backend
|
451
|
-
result = _a.sent();
|
452
|
-
//#endregion
|
453
|
-
return [2 /*return*/, result];
|
454
|
-
}
|
455
|
-
});
|
456
|
-
});
|
426
|
+
async function textToDbBinaryFormat(text) {
|
427
|
+
let result;
|
428
|
+
/* */
|
429
|
+
/* */
|
430
|
+
/* */
|
431
|
+
/* */
|
432
|
+
/* */
|
433
|
+
/* */
|
434
|
+
/* */
|
435
|
+
/* */
|
436
|
+
//#region @backend
|
437
|
+
result = await (async () => {
|
438
|
+
const buffer = await textToBuffer(text);
|
439
|
+
return buffer;
|
440
|
+
})();
|
441
|
+
//#endregion
|
442
|
+
return result;
|
457
443
|
}
|
458
444
|
binary.textToDbBinaryFormat = textToDbBinaryFormat;
|
459
445
|
//#endregion
|
460
446
|
//#region binay utils / db binary format to base64 string
|
461
|
-
function dbBinaryFormatToText(binaryFormat) {
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
case 1:
|
479
|
-
/* */
|
480
|
-
/* */
|
481
|
-
/* */
|
482
|
-
/* */
|
483
|
-
/* */
|
484
|
-
/* */
|
485
|
-
/* */
|
486
|
-
/* */
|
487
|
-
//#region @backend
|
488
|
-
result = _a.sent();
|
489
|
-
//#endregion
|
490
|
-
return [2 /*return*/, result];
|
491
|
-
}
|
492
|
-
});
|
493
|
-
});
|
447
|
+
async function dbBinaryFormatToText(binaryFormat) {
|
448
|
+
let result;
|
449
|
+
/* */
|
450
|
+
/* */
|
451
|
+
/* */
|
452
|
+
/* */
|
453
|
+
/* */
|
454
|
+
/* */
|
455
|
+
/* */
|
456
|
+
/* */
|
457
|
+
//#region @backend
|
458
|
+
result = await (async () => {
|
459
|
+
const text = await bufferToText(binaryFormat);
|
460
|
+
return text;
|
461
|
+
})();
|
462
|
+
//#endregion
|
463
|
+
return result;
|
494
464
|
}
|
495
465
|
binary.dbBinaryFormatToText = dbBinaryFormatToText;
|
496
466
|
//#endregion
|
497
467
|
//#region binay utils / base64 string to nodejs buffer
|
498
468
|
//#region @backend
|
499
|
-
function base64toBuffer(base64Data, contentTypeOverride) {
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
switch (_a.label) {
|
504
|
-
case 0: return [4 /*yield*/, base64toBlob(base64Data, contentTypeOverride)];
|
505
|
-
case 1:
|
506
|
-
blob = _a.sent();
|
507
|
-
return [4 /*yield*/, blobToBuffer(blob)];
|
508
|
-
case 2:
|
509
|
-
buffer = _a.sent();
|
510
|
-
return [2 /*return*/, buffer];
|
511
|
-
}
|
512
|
-
});
|
513
|
-
});
|
469
|
+
async function base64toBuffer(base64Data, contentTypeOverride) {
|
470
|
+
const blob = await base64toBlob(base64Data, contentTypeOverride);
|
471
|
+
const buffer = await blobToBuffer(blob);
|
472
|
+
return buffer;
|
514
473
|
}
|
515
474
|
binary.base64toBuffer = base64toBuffer;
|
516
475
|
//#endregion
|
517
476
|
//#endregion
|
518
477
|
//#region binay utils / nodejs buffer to base64 string
|
519
478
|
//#region @backend
|
520
|
-
function bufferToBase64(bufferData) {
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
switch (_a.label) {
|
525
|
-
case 0: return [4 /*yield*/, bufferToBlob(bufferData)];
|
526
|
-
case 1:
|
527
|
-
blob = _a.sent();
|
528
|
-
return [4 /*yield*/, blobToBase64(blob)];
|
529
|
-
case 2:
|
530
|
-
text = _a.sent();
|
531
|
-
return [2 /*return*/, text];
|
532
|
-
}
|
533
|
-
});
|
534
|
-
});
|
479
|
+
async function bufferToBase64(bufferData) {
|
480
|
+
const blob = await bufferToBlob(bufferData);
|
481
|
+
const text = await blobToBase64(blob);
|
482
|
+
return text;
|
535
483
|
}
|
536
484
|
binary.bufferToBase64 = bufferToBase64;
|
537
485
|
//#endregion
|
538
486
|
//#endregion
|
539
487
|
//#region binay utils / file to blob
|
540
|
-
function fileToBlob(file) {
|
541
|
-
return
|
542
|
-
|
543
|
-
return tslib_1.__generator(this, function (_c) {
|
544
|
-
switch (_c.label) {
|
545
|
-
case 0:
|
546
|
-
_a = buffer_1.Blob.bind;
|
547
|
-
_b = Uint8Array.bind;
|
548
|
-
return [4 /*yield*/, file.arrayBuffer()];
|
549
|
-
case 1: return [2 /*return*/, new (_a.apply(buffer_1.Blob, [void 0, [new (_b.apply(Uint8Array, [void 0, _c.sent()]))()], {
|
550
|
-
type: file.type,
|
551
|
-
}]))()];
|
552
|
-
}
|
553
|
-
});
|
488
|
+
async function fileToBlob(file) {
|
489
|
+
return new buffer_1.Blob([new Uint8Array(await file.arrayBuffer())], {
|
490
|
+
type: file.type,
|
554
491
|
});
|
555
492
|
}
|
556
493
|
binary.fileToBlob = fileToBlob;
|
557
494
|
//#endregion
|
558
495
|
//#region binay utils / blob to file
|
559
|
-
function blobToFile(
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
}
|
566
|
-
// @ts-ignore
|
567
|
-
return [2 /*return*/, new File([blob], nameForFile)];
|
568
|
-
});
|
569
|
-
});
|
496
|
+
async function blobToFile(blob, nameForFile = 'my-file-name') {
|
497
|
+
if (!nameForFile) {
|
498
|
+
nameForFile = 'nonamefile' + new Date().getTime();
|
499
|
+
}
|
500
|
+
// @ts-ignore
|
501
|
+
return new File([blob], nameForFile);
|
570
502
|
}
|
571
503
|
binary.blobToFile = blobToFile;
|
572
504
|
//#endregion
|
573
505
|
//#region binay utils / nodejs blob to nodejs buffer
|
574
506
|
//#region @backend
|
575
|
-
function blobToBuffer(blob) {
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
switch (_a.label) {
|
580
|
-
case 0: return [4 /*yield*/, blob.arrayBuffer()];
|
581
|
-
case 1:
|
582
|
-
arrayBuffer = _a.sent();
|
583
|
-
buffer = Buffer.from(arrayBuffer);
|
584
|
-
return [2 /*return*/, buffer];
|
585
|
-
}
|
586
|
-
});
|
587
|
-
});
|
507
|
+
async function blobToBuffer(blob) {
|
508
|
+
const arrayBuffer = await blob.arrayBuffer();
|
509
|
+
const buffer = Buffer.from(arrayBuffer);
|
510
|
+
return buffer;
|
588
511
|
}
|
589
512
|
binary.blobToBuffer = blobToBuffer;
|
590
513
|
//#endregion
|
591
514
|
//#endregion
|
592
515
|
//#region binay utils / nodejs buffer to nodejs blob
|
593
516
|
//#region @backend
|
594
|
-
function bufferToBlob(buffer) {
|
595
|
-
|
596
|
-
|
597
|
-
return tslib_1.__generator(this, function (_a) {
|
598
|
-
blob = new buffer_1.Blob([buffer]);
|
599
|
-
return [2 /*return*/, blob];
|
600
|
-
});
|
601
|
-
});
|
517
|
+
async function bufferToBlob(buffer) {
|
518
|
+
const blob = new buffer_1.Blob([buffer]); // JavaScript Blob
|
519
|
+
return blob;
|
602
520
|
}
|
603
521
|
binary.bufferToBlob = bufferToBlob;
|
604
522
|
//#endregion
|
605
523
|
//#endregion
|
606
524
|
//#region binay utils / text to nodejs buffer
|
607
525
|
//#region @backend
|
608
|
-
function textToBuffer(
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
return tslib_1.__generator(this, function (_a) {
|
613
|
-
switch (_a.label) {
|
614
|
-
case 0: return [4 /*yield*/, textToBlob(text, type)];
|
615
|
-
case 1:
|
616
|
-
blob = _a.sent();
|
617
|
-
return [4 /*yield*/, blobToBuffer(blob)];
|
618
|
-
case 2:
|
619
|
-
buffer = _a.sent();
|
620
|
-
return [2 /*return*/, buffer];
|
621
|
-
}
|
622
|
-
});
|
623
|
-
});
|
526
|
+
async function textToBuffer(text, type = 'text/plain') {
|
527
|
+
const blob = await textToBlob(text, type);
|
528
|
+
const buffer = await blobToBuffer(blob);
|
529
|
+
return buffer;
|
624
530
|
}
|
625
531
|
binary.textToBuffer = textToBuffer;
|
626
532
|
//#endregion
|
627
533
|
//#endregion
|
628
534
|
//#region binay utils / nodejs buffer to text
|
629
535
|
//#region @backend
|
630
|
-
function bufferToText(buffer) {
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
switch (_a.label) {
|
635
|
-
case 0: return [4 /*yield*/, bufferToBlob(buffer)];
|
636
|
-
case 1:
|
637
|
-
blob = _a.sent();
|
638
|
-
return [4 /*yield*/, blobToText(blob)];
|
639
|
-
case 2:
|
640
|
-
text = _a.sent();
|
641
|
-
return [2 /*return*/, text];
|
642
|
-
}
|
643
|
-
});
|
644
|
-
});
|
536
|
+
async function bufferToText(buffer) {
|
537
|
+
const blob = await bufferToBlob(buffer);
|
538
|
+
const text = await blobToText(blob);
|
539
|
+
return text;
|
645
540
|
}
|
646
541
|
binary.bufferToText = bufferToText;
|
647
542
|
//#endregion
|
648
543
|
//#endregion
|
649
544
|
//#region binay utils / text to blob
|
650
|
-
function textToBlob(
|
651
|
-
|
652
|
-
|
653
|
-
if (type === void 0) { type = 'text/plain'; }
|
654
|
-
return tslib_1.__generator(this, function (_a) {
|
655
|
-
blob = new buffer_1.Blob([text], { type: type });
|
656
|
-
return [2 /*return*/, blob];
|
657
|
-
});
|
658
|
-
});
|
545
|
+
async function textToBlob(text, type = 'text/plain') {
|
546
|
+
const blob = new buffer_1.Blob([text], { type });
|
547
|
+
return blob;
|
659
548
|
}
|
660
549
|
binary.textToBlob = textToBlob;
|
661
550
|
//#endregion
|
662
551
|
//#region binay utils / blob to text
|
663
|
-
function blobToText(blob) {
|
664
|
-
return
|
665
|
-
return tslib_1.__generator(this, function (_a) {
|
666
|
-
switch (_a.label) {
|
667
|
-
case 0: return [4 /*yield*/, blob.text()];
|
668
|
-
case 1: return [2 /*return*/, _a.sent()];
|
669
|
-
}
|
670
|
-
});
|
671
|
-
});
|
552
|
+
async function blobToText(blob) {
|
553
|
+
return await blob.text();
|
672
554
|
}
|
673
555
|
binary.blobToText = blobToText;
|
674
556
|
//#endregion
|
675
557
|
//#region binay utils / text to file
|
676
|
-
function textToFile(text, fileRelativePathOrName) {
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
// console.log({
|
689
|
-
// ext,
|
690
|
-
// blob, file
|
691
|
-
// });
|
692
|
-
// debugger
|
693
|
-
return [2 /*return*/, file];
|
694
|
-
}
|
695
|
-
});
|
696
|
-
});
|
558
|
+
async function textToFile(text, fileRelativePathOrName) {
|
559
|
+
// console.log({ path })
|
560
|
+
const ext = core_imports_1.path.extname(fileRelativePathOrName);
|
561
|
+
const type = core_models_1.CoreModels.mimeTypes[ext];
|
562
|
+
const blob = new buffer_1.Blob([text], { type });
|
563
|
+
const file = await blobToFile(blob, fileRelativePathOrName);
|
564
|
+
// console.log({
|
565
|
+
// ext,
|
566
|
+
// blob, file
|
567
|
+
// });
|
568
|
+
// debugger
|
569
|
+
return file;
|
697
570
|
}
|
698
571
|
binary.textToFile = textToFile;
|
699
572
|
//#endregion
|
700
573
|
//#region binay utils / file to text
|
701
|
-
function fileToText(file) {
|
702
|
-
return
|
703
|
-
return tslib_1.__generator(this, function (_a) {
|
704
|
-
switch (_a.label) {
|
705
|
-
case 0: return [4 /*yield*/, file.text()];
|
706
|
-
case 1: return [2 /*return*/, _a.sent()];
|
707
|
-
}
|
708
|
-
});
|
709
|
-
});
|
574
|
+
async function fileToText(file) {
|
575
|
+
return await file.text();
|
710
576
|
}
|
711
577
|
binary.fileToText = fileToText;
|
712
578
|
//#endregion
|
713
579
|
//#region binay utils / json to blob
|
714
|
-
function jsonToBlob(jsonObj) {
|
715
|
-
|
716
|
-
|
717
|
-
return tslib_1.__generator(this, function (_a) {
|
718
|
-
blob = new buffer_1.Blob([JSON.stringify(jsonObj, null, 2)], {
|
719
|
-
type: 'application/json',
|
720
|
-
});
|
721
|
-
return [2 /*return*/, blob];
|
722
|
-
});
|
580
|
+
async function jsonToBlob(jsonObj) {
|
581
|
+
const blob = new buffer_1.Blob([JSON.stringify(jsonObj, null, 2)], {
|
582
|
+
type: 'application/json',
|
723
583
|
});
|
584
|
+
return blob;
|
724
585
|
}
|
725
586
|
binary.jsonToBlob = jsonToBlob;
|
726
587
|
//#endregion
|
@@ -728,45 +589,26 @@ var Utils;
|
|
728
589
|
/**
|
729
590
|
* TODO NOT TESTED
|
730
591
|
*/
|
731
|
-
function blobToJson(blob) {
|
732
|
-
return
|
733
|
-
var _a, _b;
|
734
|
-
return tslib_1.__generator(this, function (_c) {
|
735
|
-
switch (_c.label) {
|
736
|
-
case 0:
|
737
|
-
_b = (_a = JSON).parse;
|
738
|
-
return [4 /*yield*/, blob.text()];
|
739
|
-
case 1: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
|
740
|
-
}
|
741
|
-
});
|
742
|
-
});
|
592
|
+
async function blobToJson(blob) {
|
593
|
+
return JSON.parse(await blob.text());
|
743
594
|
}
|
744
595
|
binary.blobToJson = blobToJson;
|
745
596
|
//#endregion
|
746
597
|
//#region binay utils / get blob from url
|
747
|
-
function getBlobFrom(url) {
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
case 0: return [4 /*yield*/, (0, axios_1.default)({
|
753
|
-
url: url,
|
754
|
-
method: 'get',
|
755
|
-
responseType: 'blob',
|
756
|
-
})];
|
757
|
-
case 1:
|
758
|
-
response = _a.sent();
|
759
|
-
return [2 /*return*/, response.data];
|
760
|
-
}
|
761
|
-
});
|
598
|
+
async function getBlobFrom(url) {
|
599
|
+
const response = await (0, axios_1.default)({
|
600
|
+
url,
|
601
|
+
method: 'get',
|
602
|
+
responseType: 'blob',
|
762
603
|
});
|
604
|
+
return response.data;
|
763
605
|
}
|
764
606
|
binary.getBlobFrom = getBlobFrom;
|
765
607
|
//#endregion
|
766
608
|
})(binary = Utils.binary || (Utils.binary = {}));
|
767
609
|
//#endregion
|
768
610
|
//#region css
|
769
|
-
|
611
|
+
let css;
|
770
612
|
(function (css) {
|
771
613
|
//#region css utils / numeric value of pixels
|
772
614
|
/**
|
@@ -775,9 +617,8 @@ var Utils;
|
|
775
617
|
* @returns
|
776
618
|
*/
|
777
619
|
function numValue(pixelsCss) {
|
778
|
-
var _a;
|
779
620
|
// tslint:disable-next-line:radix
|
780
|
-
return parseInt(
|
621
|
+
return parseInt(pixelsCss?.toString()?.replace('px', ''));
|
781
622
|
}
|
782
623
|
css.numValue = numValue;
|
783
624
|
//#endregion
|
@@ -788,19 +629,229 @@ var Utils;
|
|
788
629
|
//#region utils process
|
789
630
|
var UtilsProcess;
|
790
631
|
(function (UtilsProcess) {
|
791
|
-
var _this = this;
|
792
632
|
//#endregion
|
793
633
|
//#region utils process / TODO start async
|
794
634
|
/**
|
795
635
|
* TODO IMPLEMENT
|
796
636
|
* start async node process
|
797
637
|
*/
|
798
|
-
|
799
|
-
//
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
638
|
+
UtilsProcess.startAsync = async (command, cwd,
|
639
|
+
// options?: ProcessStartOptions, // TODO change to this
|
640
|
+
options) => {
|
641
|
+
//#region @backendFunc
|
642
|
+
//#region preapre options
|
643
|
+
let { hideOutput, resolvePromiseMsg, outputLineReplace, prefix, extractFromLine, exitOnErrorCallback, askToTryAgainOnError, resolvePromiseMsgCallback, similarProcessKey, onChildProcessChange, outputBuffer, outputBufferMaxSize, } = options || {};
|
644
|
+
outputBufferMaxSize = outputBufferMaxSize || 1000;
|
645
|
+
command = index_1.Helpers._fixCommand(command);
|
646
|
+
const { stderr: stderResolvePromiseMsgCallback, stdout: stdoutResolvePromiseMsgCallback, exitCode: exitCodeResolvePromiseMsgCallback, } = resolvePromiseMsgCallback || {};
|
647
|
+
let childProcess;
|
648
|
+
// let {
|
649
|
+
// stderMsgForPromiseResolve,
|
650
|
+
// stdoutMsgForPromiseResolve
|
651
|
+
// } = resolvePromiseMsg || {};
|
652
|
+
// processes.push(proc);
|
653
|
+
if (!resolvePromiseMsg) {
|
654
|
+
resolvePromiseMsg = {};
|
655
|
+
}
|
656
|
+
if (!hideOutput) {
|
657
|
+
hideOutput = {};
|
658
|
+
}
|
659
|
+
let isResolved = false;
|
660
|
+
if (core_imports_1._.isString(resolvePromiseMsg.stdout)) {
|
661
|
+
resolvePromiseMsg.stdout = [resolvePromiseMsg.stdout];
|
662
|
+
}
|
663
|
+
if (core_imports_1._.isString(resolvePromiseMsg.stderr)) {
|
664
|
+
resolvePromiseMsg.stderr = [resolvePromiseMsg.stderr];
|
665
|
+
}
|
666
|
+
//#endregion
|
667
|
+
const handlProc = (proc) => {
|
668
|
+
return new Promise((resolve, reject) => {
|
669
|
+
// console.log(
|
670
|
+
// `[execute] Process started...`,
|
671
|
+
// (resolvePromiseMsg.stdout as string[]).map(c => `"${c}"`).join(','),
|
672
|
+
// );
|
673
|
+
//#region handle stdout data
|
674
|
+
proc.stdout.on('data', rawData => {
|
675
|
+
let data = rawData?.toString() || '';
|
676
|
+
data = index_1.Helpers.modifyLineByLine(data, // @ts-ignore
|
677
|
+
outputLineReplace, prefix, extractFromLine);
|
678
|
+
if (!core_imports_1._.isUndefined(outputBuffer)) {
|
679
|
+
outputBuffer.push(data);
|
680
|
+
if (outputBuffer.length > outputBufferMaxSize) {
|
681
|
+
outputBuffer.shift();
|
682
|
+
}
|
683
|
+
}
|
684
|
+
if (!hideOutput.stdout) {
|
685
|
+
process.stdout.write(data);
|
686
|
+
}
|
687
|
+
if (core_imports_1._.isArray(resolvePromiseMsg.stdout)) {
|
688
|
+
for (let index = 0; index < resolvePromiseMsg.stdout.length; index++) {
|
689
|
+
// console.log(`DATA STDOUT: ${chalk.gray(data)}`);
|
690
|
+
const resolveCompilationMessage = resolvePromiseMsg.stdout[index];
|
691
|
+
if (data.search(resolveCompilationMessage) !== -1) {
|
692
|
+
// Helpers.info(`[unitlOutputContains] AAA...`);
|
693
|
+
stdoutResolvePromiseMsgCallback &&
|
694
|
+
stdoutResolvePromiseMsgCallback();
|
695
|
+
if (!isResolved) {
|
696
|
+
isResolved = true;
|
697
|
+
resolve(void 0);
|
698
|
+
}
|
699
|
+
break;
|
700
|
+
}
|
701
|
+
}
|
702
|
+
}
|
703
|
+
// TODO NOT NEEDED
|
704
|
+
if (core_imports_1._.isArray(resolvePromiseMsg.stderr)) {
|
705
|
+
for (let index = 0; index < resolvePromiseMsg.stderr.length; index++) {
|
706
|
+
const rejectm = resolvePromiseMsg.stderr[index];
|
707
|
+
if (data.search(rejectm) !== -1) {
|
708
|
+
// Helpers.info(`[unitlOutputContains] Rejected move to next step...`);
|
709
|
+
stdoutResolvePromiseMsgCallback &&
|
710
|
+
stdoutResolvePromiseMsgCallback();
|
711
|
+
if (!isResolved) {
|
712
|
+
isResolved = true;
|
713
|
+
reject();
|
714
|
+
proc.kill('SIGINT');
|
715
|
+
}
|
716
|
+
break;
|
717
|
+
}
|
718
|
+
}
|
719
|
+
}
|
720
|
+
});
|
721
|
+
//#endregion
|
722
|
+
//#region handle exit process
|
723
|
+
proc.on('exit', async (code) => {
|
724
|
+
// console.log(`Command exit code: ${code}`)
|
725
|
+
if (hideOutput.acceptAllExitCodeAsSuccess) {
|
726
|
+
exitCodeResolvePromiseMsgCallback &&
|
727
|
+
exitCodeResolvePromiseMsgCallback(code);
|
728
|
+
resolve(void 0);
|
729
|
+
}
|
730
|
+
else {
|
731
|
+
if (code !== 0) {
|
732
|
+
if (core_imports_1._.isFunction(exitOnErrorCallback)) {
|
733
|
+
try {
|
734
|
+
await exitOnErrorCallback(code);
|
735
|
+
// await this.runSyncOrAsync({
|
736
|
+
// functionFn: exitOnErrorCallback,
|
737
|
+
// arrayOfParams: [code],
|
738
|
+
// });
|
739
|
+
reject(`Command failed with code=${code}`);
|
740
|
+
}
|
741
|
+
catch (error) {
|
742
|
+
reject(error);
|
743
|
+
}
|
744
|
+
}
|
745
|
+
else {
|
746
|
+
reject(`Command failed with code=${code}`);
|
747
|
+
}
|
748
|
+
}
|
749
|
+
else {
|
750
|
+
resolve(void 0);
|
751
|
+
}
|
752
|
+
}
|
753
|
+
});
|
754
|
+
//#endregion
|
755
|
+
//#region handle stdout error
|
756
|
+
proc.stdout.on('error', rawData => {
|
757
|
+
let data = rawData?.toString() || '';
|
758
|
+
data = index_1.Helpers.modifyLineByLine(data, // @ts-ignore
|
759
|
+
outputLineReplace, prefix, extractFromLine);
|
760
|
+
if (!core_imports_1._.isUndefined(outputBuffer)) {
|
761
|
+
outputBuffer.push(data);
|
762
|
+
if (outputBuffer.length > outputBufferMaxSize) {
|
763
|
+
outputBuffer.shift();
|
764
|
+
}
|
765
|
+
}
|
766
|
+
if (!hideOutput.stdout) {
|
767
|
+
process.stdout.write(JSON.stringify(data));
|
768
|
+
}
|
769
|
+
// console.log(data);
|
770
|
+
});
|
771
|
+
//#endregion
|
772
|
+
//#region handle stder data
|
773
|
+
proc.stderr.on('data', rawData => {
|
774
|
+
let data = rawData?.toString() || '';
|
775
|
+
data = index_1.Helpers.modifyLineByLine(data, // @ts-ignore
|
776
|
+
outputLineReplace, prefix, extractFromLine);
|
777
|
+
if (!core_imports_1._.isUndefined(outputBuffer)) {
|
778
|
+
outputBuffer.push(data);
|
779
|
+
if (outputBuffer.length > outputBufferMaxSize) {
|
780
|
+
outputBuffer.shift();
|
781
|
+
}
|
782
|
+
}
|
783
|
+
if (!hideOutput.stderr) {
|
784
|
+
process.stderr.write(data);
|
785
|
+
}
|
786
|
+
if (core_imports_1._.isArray(resolvePromiseMsg.stderr)) {
|
787
|
+
// @ts-ignore
|
788
|
+
for (let index = 0; index < resolvePromiseMsg.stderr.length; index++) {
|
789
|
+
// @ts-ignore
|
790
|
+
const rejectm = resolvePromiseMsg.stderr[index];
|
791
|
+
if (data.search(rejectm) !== -1) {
|
792
|
+
// Helpers.info(`[unitlOutputContains] Rejected move to next step...`);
|
793
|
+
stderResolvePromiseMsgCallback &&
|
794
|
+
stderResolvePromiseMsgCallback();
|
795
|
+
if (!isResolved) {
|
796
|
+
isResolved = true;
|
797
|
+
reject();
|
798
|
+
proc.kill('SIGINT');
|
799
|
+
}
|
800
|
+
break;
|
801
|
+
}
|
802
|
+
}
|
803
|
+
}
|
804
|
+
});
|
805
|
+
//#endregion
|
806
|
+
//#region handle stder error
|
807
|
+
proc.stderr.on('error', rawData => {
|
808
|
+
let data = rawData?.toString() || '';
|
809
|
+
data = index_1.Helpers.modifyLineByLine(data, // @ts-ignore
|
810
|
+
outputLineReplace, prefix, extractFromLine);
|
811
|
+
if (!core_imports_1._.isUndefined(outputBuffer)) {
|
812
|
+
outputBuffer.push(data);
|
813
|
+
if (outputBuffer.length > outputBufferMaxSize) {
|
814
|
+
outputBuffer.shift();
|
815
|
+
}
|
816
|
+
}
|
817
|
+
// @ts-ignore
|
818
|
+
if (!hideOutput.stderr) {
|
819
|
+
process.stderr.write(JSON.stringify(data));
|
820
|
+
}
|
821
|
+
// console.log(data);
|
822
|
+
});
|
823
|
+
//#endregion
|
824
|
+
});
|
825
|
+
};
|
826
|
+
while (true) {
|
827
|
+
const maxBuffer = options?.biggerBuffer ? index_1.Helpers.bigMaxBuffer : void 0;
|
828
|
+
const env = { ...process.env, FORCE_COLOR: '1', NODE_ENV: 'development' };
|
829
|
+
childProcess = core_imports_3.child_process.exec(command, { cwd, env, maxBuffer });
|
830
|
+
onChildProcessChange && onChildProcessChange(childProcess);
|
831
|
+
try {
|
832
|
+
await handlProc(childProcess);
|
833
|
+
break;
|
834
|
+
}
|
835
|
+
catch (error) {
|
836
|
+
index_1.Helpers.error(`Command failed:
|
837
|
+
|
838
|
+
${command}
|
839
|
+
|
840
|
+
in location: ${cwd}
|
841
|
+
|
842
|
+
`, true, true);
|
843
|
+
if (askToTryAgainOnError) {
|
844
|
+
if (!(await index_1.Helpers.questionYesNo(`Try again this command ?`))) {
|
845
|
+
throw error;
|
846
|
+
}
|
847
|
+
}
|
848
|
+
else {
|
849
|
+
throw error;
|
850
|
+
}
|
851
|
+
}
|
852
|
+
}
|
853
|
+
//#endregion
|
854
|
+
};
|
804
855
|
//#endregion
|
805
856
|
//#region utils process / TODO start sync
|
806
857
|
// /**
|
@@ -816,82 +867,75 @@ var UtilsProcess;
|
|
816
867
|
* condition is met. It is useful for example when you want to start
|
817
868
|
* process and wait until some output is in stdout or stderr.
|
818
869
|
*/
|
819
|
-
UtilsProcess.startAsyncChildProcessCommandUntil =
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
(
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
else {
|
876
|
-
reject(new Error("Process exited with code ".concat(exitCode)));
|
877
|
-
}
|
878
|
-
});
|
879
|
-
childProc.on('error', function (error) {
|
880
|
-
reject(error);
|
881
|
-
});
|
882
|
-
})];
|
870
|
+
UtilsProcess.startAsyncChildProcessCommandUntil = async (command, options) => {
|
871
|
+
//#region @backendFunc
|
872
|
+
options = options || {};
|
873
|
+
const { stdout, stderr, resolveAfterAnyExitCode } = options.untilOptions || {};
|
874
|
+
options.cwd = options.cwd || process.cwd();
|
875
|
+
return new Promise((resolve, reject) => {
|
876
|
+
const childProc = core_imports_3.child_process.exec(command, {
|
877
|
+
cwd: options.cwd,
|
878
|
+
maxBuffer: index_1.Helpers.bigMaxBuffer,
|
879
|
+
});
|
880
|
+
const stdoutConditions = Array.isArray(stdout)
|
881
|
+
? stdout
|
882
|
+
: stdout
|
883
|
+
? [stdout]
|
884
|
+
: [];
|
885
|
+
const stderrConditions = Array.isArray(stderr)
|
886
|
+
? stderr
|
887
|
+
: stderr
|
888
|
+
? [stderr]
|
889
|
+
: [];
|
890
|
+
const checkConditions = (output, conditions) => {
|
891
|
+
const conditionReady = conditions.some(condition => output.includes(condition));
|
892
|
+
// if(conditionReady){
|
893
|
+
// console.log('conditionReady MOVE ON', conditionReady);
|
894
|
+
// }
|
895
|
+
return conditionReady;
|
896
|
+
};
|
897
|
+
childProc.stdout?.on('data', data => {
|
898
|
+
if (options.displayOutputInParentProcess) {
|
899
|
+
process.stdout?.write(data);
|
900
|
+
}
|
901
|
+
if (checkConditions(data, stdoutConditions)) {
|
902
|
+
resolve();
|
903
|
+
childProc.kill();
|
904
|
+
}
|
905
|
+
});
|
906
|
+
childProc.stderr?.on('data', data => {
|
907
|
+
if (options.displayOutputInParentProcess) {
|
908
|
+
process.stderr?.write(data);
|
909
|
+
}
|
910
|
+
if (checkConditions(data, stderrConditions)) {
|
911
|
+
resolve();
|
912
|
+
childProc.kill();
|
913
|
+
}
|
914
|
+
});
|
915
|
+
childProc.on('close', exitCode => {
|
916
|
+
if (resolveAfterAnyExitCode || exitCode === 0) {
|
917
|
+
resolve();
|
918
|
+
}
|
919
|
+
else {
|
920
|
+
reject(new Error(`Process exited with code ${exitCode}`));
|
921
|
+
}
|
922
|
+
});
|
923
|
+
childProc.on('error', error => {
|
924
|
+
reject(error);
|
925
|
+
});
|
883
926
|
});
|
884
|
-
|
927
|
+
//#endregion
|
928
|
+
};
|
885
929
|
//#endregion
|
886
930
|
//#region utils process / get git bash path
|
887
|
-
UtilsProcess.getGitBashPath =
|
931
|
+
UtilsProcess.getGitBashPath = () => {
|
888
932
|
//#region @backendFunc
|
889
933
|
if (process.platform !== 'win32') {
|
890
934
|
return null;
|
891
935
|
}
|
892
936
|
try {
|
893
937
|
// Execute the 'where' command to find bash.exe
|
894
|
-
|
938
|
+
const gitBashPath = core_imports_3.child_process
|
895
939
|
.execSync('where bash.exe', { encoding: 'utf8' })
|
896
940
|
.split('\n')[0]
|
897
941
|
.trim();
|
@@ -911,53 +955,58 @@ var UtilsProcess;
|
|
911
955
|
* TODO IMPLEMENT
|
912
956
|
* start async node process
|
913
957
|
*/
|
914
|
-
UtilsProcess.startInNewTerminalWindow =
|
915
|
-
var e_3, _a;
|
958
|
+
UtilsProcess.startInNewTerminalWindow = (command, options) => {
|
916
959
|
//#region @backendFunc
|
917
|
-
|
960
|
+
const platform = process.platform;
|
918
961
|
options = options || {};
|
919
962
|
options.cwd = options.cwd || process.cwd();
|
920
963
|
if (platform === 'win32') {
|
921
|
-
|
964
|
+
const gitBashPath = UtilsProcess.getGitBashPath();
|
922
965
|
// const currentBash = getBashOrShellName();
|
923
966
|
// console.log('gitBashPath', gitBashPath);
|
924
967
|
// console.log('currentBash', currentBash);
|
925
968
|
if (gitBashPath) {
|
926
|
-
return (0, core_imports_3.spawn)('start bash', ['-c',
|
969
|
+
return (0, core_imports_3.spawn)('start bash', ['-c', `${command}`], // Use '-c' to execute a single command in Git Bash
|
927
970
|
{
|
928
971
|
detached: true, // Detached process
|
929
972
|
stdio: 'ignore', // Ignore stdio
|
930
|
-
cwd: options
|
973
|
+
cwd: options?.cwd,
|
931
974
|
}).unref(); // Ensure the parent process can exit independently
|
932
975
|
}
|
933
|
-
console.error(
|
976
|
+
console.error(`
|
977
|
+
|
978
|
+
|
979
|
+
Please install git bash to use this cli (https://gitforwindows.org/)
|
980
|
+
|
981
|
+
|
982
|
+
`);
|
934
983
|
// For Windows
|
935
|
-
return (0, core_imports_3.spawn)('cmd', ['/c', 'start', 'cmd', '/k',
|
984
|
+
return (0, core_imports_3.spawn)('cmd', ['/c', 'start', 'cmd', '/k', `${command}`], {
|
936
985
|
detached: true,
|
937
986
|
stdio: 'ignore',
|
938
|
-
cwd: options
|
987
|
+
cwd: options?.cwd,
|
939
988
|
}).unref();
|
940
989
|
}
|
941
990
|
else if (platform === 'darwin') {
|
942
991
|
// For macOS
|
943
|
-
return (0, core_imports_3.spawn)('osascript', ['-e',
|
992
|
+
return (0, core_imports_3.spawn)('osascript', ['-e', `tell application "Terminal" to do script "${command}"`], {
|
944
993
|
detached: true,
|
945
994
|
stdio: 'ignore',
|
946
|
-
cwd: options
|
995
|
+
cwd: options?.cwd,
|
947
996
|
}).unref();
|
948
997
|
}
|
949
998
|
else if (platform === 'linux') {
|
950
999
|
if (!UtilsOs.isRunningInLinuxGraphicsCapableEnvironment()) {
|
951
|
-
|
1000
|
+
const child = core_imports_3.child_process.spawn(command, {
|
952
1001
|
detached: true,
|
953
|
-
cwd: options
|
1002
|
+
cwd: options?.cwd,
|
954
1003
|
stdio: 'ignore',
|
955
1004
|
});
|
956
|
-
|
1005
|
+
child.unref();
|
957
1006
|
return;
|
958
1007
|
}
|
959
1008
|
// For Linux (gnome-terminal as an example)
|
960
|
-
|
1009
|
+
const terminals = [
|
961
1010
|
{ cmd: 'gnome-terminal', args: ['--', 'bash', '-c'] }, // GNOME Terminal
|
962
1011
|
{ cmd: 'konsole', args: ['-e', 'bash', '-c'] }, // Konsole
|
963
1012
|
{ cmd: 'xfce4-terminal', args: ['-e', 'bash', '-c'] }, // XFCE4 Terminal
|
@@ -969,55 +1018,45 @@ var UtilsProcess;
|
|
969
1018
|
{ cmd: 'alacritty', args: ['-e', 'bash', '-c'] }, // Alacritty
|
970
1019
|
{ cmd: 'urxvt', args: ['-e', 'bash', '-c'] }, // URxvt
|
971
1020
|
];
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
for (var terminals_1 = tslib_1.__values(terminals), terminals_1_1 = terminals_1.next(); !terminals_1_1.done; terminals_1_1 = terminals_1.next()) {
|
976
|
-
var term = terminals_1_1.value;
|
977
|
-
try {
|
978
|
-
core_imports_3.child_process.execSync("which ".concat(term.cmd), {
|
979
|
-
stdio: 'ignore',
|
980
|
-
cwd: options === null || options === void 0 ? void 0 : options.cwd,
|
981
|
-
});
|
982
|
-
terminalCommand = term.cmd;
|
983
|
-
terminalArgs = tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(term.args), false), [command], false);
|
984
|
-
break;
|
985
|
-
}
|
986
|
-
catch (err) {
|
987
|
-
// Terminal not found, continue to the next
|
988
|
-
}
|
989
|
-
}
|
990
|
-
}
|
991
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
992
|
-
finally {
|
1021
|
+
let terminalCommand = '';
|
1022
|
+
let terminalArgs = [];
|
1023
|
+
for (const term of terminals) {
|
993
1024
|
try {
|
994
|
-
|
1025
|
+
core_imports_3.child_process.execSync(`which ${term.cmd}`, {
|
1026
|
+
stdio: 'ignore',
|
1027
|
+
cwd: options?.cwd,
|
1028
|
+
});
|
1029
|
+
terminalCommand = term.cmd;
|
1030
|
+
terminalArgs = [...term.args, command];
|
1031
|
+
break;
|
1032
|
+
}
|
1033
|
+
catch (err) {
|
1034
|
+
// Terminal not found, continue to the next
|
995
1035
|
}
|
996
|
-
finally { if (e_3) throw e_3.error; }
|
997
1036
|
}
|
998
1037
|
if (!terminalCommand) {
|
999
1038
|
console.error('No supported terminal emulator found.');
|
1000
1039
|
return;
|
1001
1040
|
}
|
1002
|
-
|
1041
|
+
const child = (0, core_imports_3.spawn)(terminalCommand, terminalArgs, {
|
1003
1042
|
detached: true,
|
1004
1043
|
stdio: 'ignore',
|
1005
1044
|
});
|
1006
1045
|
child.unref();
|
1007
1046
|
}
|
1008
1047
|
else {
|
1009
|
-
index_1.Helpers.throw(
|
1048
|
+
index_1.Helpers.throw(`Unsupported platform: ${platform}`);
|
1010
1049
|
}
|
1011
1050
|
//#endregion
|
1012
1051
|
};
|
1013
1052
|
//#endregion
|
1014
1053
|
//#region utils process / get bash or shell name
|
1015
|
-
UtilsProcess.getBashOrShellName =
|
1054
|
+
UtilsProcess.getBashOrShellName = () => {
|
1016
1055
|
/* */
|
1017
1056
|
/* */
|
1018
1057
|
//#region @backendFunc
|
1019
|
-
|
1020
|
-
|
1058
|
+
const platform = process.platform; // Identify the platform: 'win32', 'darwin', 'linux'
|
1059
|
+
const shell = process.env.SHELL || process.env.ComSpec || ''; // Common shell environment variables
|
1021
1060
|
if (platform === 'win32') {
|
1022
1061
|
if (shell.includes('cmd.exe'))
|
1023
1062
|
return 'cmd';
|
@@ -1049,19 +1088,133 @@ var UtilsProcess;
|
|
1049
1088
|
//#endregion
|
1050
1089
|
};
|
1051
1090
|
//#endregion
|
1091
|
+
/**
|
1092
|
+
* Get CPU and memory usage for a single PID.
|
1093
|
+
*/
|
1094
|
+
UtilsProcess.getUsageForPid = async (pid) => {
|
1095
|
+
//#region @backendFunc
|
1096
|
+
// pidusage returns cpu usage as a percentage (e.g., 10.0 for ~10%)
|
1097
|
+
// memory usage is returned in bytes.
|
1098
|
+
try {
|
1099
|
+
var pidusage = require('pidusage');
|
1100
|
+
const stat = await pidusage(pid);
|
1101
|
+
const memoryInMB = stat.memory / (1024 * 1024);
|
1102
|
+
const memoryInGB = stat.memory / (1024 * 1024 * 1024);
|
1103
|
+
return {
|
1104
|
+
cpu: stat.cpu, // CPU usage (percent)
|
1105
|
+
memoryInMB,
|
1106
|
+
memoryInGB,
|
1107
|
+
};
|
1108
|
+
}
|
1109
|
+
catch (error) {
|
1110
|
+
return {
|
1111
|
+
cpu: NaN, // CPU usage (percent)
|
1112
|
+
memoryInMB: NaN, // Memory usage (bytes)
|
1113
|
+
memoryInGB: NaN, // Memory usage
|
1114
|
+
};
|
1115
|
+
}
|
1116
|
+
//#endregion
|
1117
|
+
};
|
1118
|
+
/**
|
1119
|
+
* Return a list of direct child PIDs for the given PID on a Unix-like system.
|
1120
|
+
* Uses `ps -o pid= --ppid <pid>` to find child processes.
|
1121
|
+
*/
|
1122
|
+
async function getChildPidsUnix(pid) {
|
1123
|
+
//#region @backendFunc
|
1124
|
+
const cmd = `ps -o pid= --ppid ${pid}`;
|
1125
|
+
try {
|
1126
|
+
const execAsync = (0, util_1.promisify)(core_imports_3.child_process.exec);
|
1127
|
+
const { stdout } = await execAsync(cmd);
|
1128
|
+
// Each line should contain just the PID
|
1129
|
+
return stdout
|
1130
|
+
.split('\n')
|
1131
|
+
.map(line => line.trim())
|
1132
|
+
.filter(line => line !== '')
|
1133
|
+
.map(line => Number(line))
|
1134
|
+
.filter(n => !isNaN(n));
|
1135
|
+
}
|
1136
|
+
catch {
|
1137
|
+
return [];
|
1138
|
+
}
|
1139
|
+
//#endregion
|
1140
|
+
}
|
1141
|
+
/**
|
1142
|
+
* Return a list of direct child PIDs for the given PID on Windows.
|
1143
|
+
* Uses `wmic process where (ParentProcessId=<pid>) get ProcessId` to find child processes.
|
1144
|
+
*/
|
1145
|
+
async function getChildPidsWindows(pid) {
|
1146
|
+
//#region @backendFunc
|
1147
|
+
const cmd = `wmic process where (ParentProcessId=${pid}) get ProcessId`;
|
1148
|
+
try {
|
1149
|
+
const execAsync = (0, util_1.promisify)(core_imports_3.child_process.exec);
|
1150
|
+
const { stdout } = await execAsync(cmd);
|
1151
|
+
// The output generally has lines, including one that says "ProcessId" and then the PIDs
|
1152
|
+
// We'll parse out any numeric lines
|
1153
|
+
return stdout
|
1154
|
+
.split('\n')
|
1155
|
+
.map(line => line.trim())
|
1156
|
+
.filter(line => /^\d+$/.test(line)) // only keep pure digits
|
1157
|
+
.map(line => Number(line))
|
1158
|
+
.filter(n => !isNaN(n));
|
1159
|
+
}
|
1160
|
+
catch {
|
1161
|
+
return [];
|
1162
|
+
}
|
1163
|
+
//#endregion
|
1164
|
+
}
|
1165
|
+
/**
|
1166
|
+
* Cross-platform function to list *direct* child PIDs of a given PID.
|
1167
|
+
* Uses the appropriate command depending on `process.platform`.
|
1168
|
+
*/
|
1169
|
+
async function getChildPidsOnce(pid) {
|
1170
|
+
//#region @backendFunc
|
1171
|
+
if (process.platform === 'win32') {
|
1172
|
+
return getChildPidsWindows(pid);
|
1173
|
+
}
|
1174
|
+
else {
|
1175
|
+
return getChildPidsUnix(pid);
|
1176
|
+
}
|
1177
|
+
//#endregion
|
1178
|
+
}
|
1179
|
+
UtilsProcess.getChildPidsOnce = getChildPidsOnce;
|
1180
|
+
/**
|
1181
|
+
* Get CPU and memory usage for the current process (the Node.js process itself),
|
1182
|
+
* plus any child processes spawned by it.
|
1183
|
+
*/
|
1184
|
+
UtilsProcess.getCurrentProcessAndChildUsage = async () => {
|
1185
|
+
//#region @backendFunc
|
1186
|
+
const currentPid = process.pid;
|
1187
|
+
// Get stats for current Node.js process
|
1188
|
+
const currentUsage = await UtilsProcess.getUsageForPid(currentPid);
|
1189
|
+
// Get list of child PIDs
|
1190
|
+
const childPids = await getChildPidsOnce(currentPid);
|
1191
|
+
// Gather usage for each child
|
1192
|
+
const childrenUsage = await Promise.all(childPids.map(async (pid) => {
|
1193
|
+
const usage = await UtilsProcess.getUsageForPid(pid);
|
1194
|
+
return {
|
1195
|
+
pid,
|
1196
|
+
cpu: usage.cpu,
|
1197
|
+
memoryInMB: usage.memoryInMB,
|
1198
|
+
};
|
1199
|
+
}));
|
1200
|
+
return {
|
1201
|
+
current: currentUsage,
|
1202
|
+
children: childrenUsage,
|
1203
|
+
};
|
1204
|
+
//#endregion
|
1205
|
+
};
|
1052
1206
|
})(UtilsProcess || (exports.UtilsProcess = UtilsProcess = {}));
|
1053
1207
|
//#endregion
|
1054
1208
|
//#region utils os
|
1055
1209
|
var UtilsOs;
|
1056
1210
|
(function (UtilsOs) {
|
1057
|
-
var _this = this;
|
1058
1211
|
//#region utils os / is running in browser
|
1059
1212
|
/**
|
1060
1213
|
* check if script is running in client browser
|
1061
1214
|
* (websql model -> is also considered as browser
|
1062
1215
|
* because it is running in browser)
|
1063
1216
|
*/
|
1064
|
-
UtilsOs.isRunningInBrowser =
|
1217
|
+
UtilsOs.isRunningInBrowser = () => {
|
1065
1218
|
//#region @backend
|
1066
1219
|
return false;
|
1067
1220
|
//#endregion
|
@@ -1073,7 +1226,7 @@ var UtilsOs;
|
|
1073
1226
|
* check if script is running in nodejs
|
1074
1227
|
* (backend script or electron script)
|
1075
1228
|
*/
|
1076
|
-
UtilsOs.isRunningInNode =
|
1229
|
+
UtilsOs.isRunningInNode = () => {
|
1077
1230
|
//#region @backend
|
1078
1231
|
return true;
|
1079
1232
|
//#endregion
|
@@ -1086,7 +1239,7 @@ var UtilsOs;
|
|
1086
1239
|
* browser mode that has sql.js backend
|
1087
1240
|
* and executes sql queries in browser
|
1088
1241
|
*/
|
1089
|
-
UtilsOs.isRunningInWebSQL =
|
1242
|
+
UtilsOs.isRunningInWebSQL = () => {
|
1090
1243
|
//#region @backend
|
1091
1244
|
return false;
|
1092
1245
|
//#endregion
|
@@ -1100,7 +1253,7 @@ var UtilsOs;
|
|
1100
1253
|
* check whether the current process is running inside
|
1101
1254
|
* Electron backend or browser.
|
1102
1255
|
*/
|
1103
|
-
UtilsOs.isRunningInElectron =
|
1256
|
+
UtilsOs.isRunningInElectron = () => {
|
1104
1257
|
// Renderer process
|
1105
1258
|
// @ts-ignore
|
1106
1259
|
if (typeof window !== 'undefined' &&
|
@@ -1124,12 +1277,30 @@ var UtilsOs;
|
|
1124
1277
|
return false;
|
1125
1278
|
};
|
1126
1279
|
//#endregion
|
1280
|
+
//#region utils os / is running in vscode extension
|
1281
|
+
/**
|
1282
|
+
* Check whether the current process is running inside
|
1283
|
+
* a Visual Studio Code extension.
|
1284
|
+
*/
|
1285
|
+
UtilsOs.isRunningInVscodeExtension = () => {
|
1286
|
+
//#region @backendFunc
|
1287
|
+
try {
|
1288
|
+
const vscode = require('vscode');
|
1289
|
+
return !!vscode;
|
1290
|
+
}
|
1291
|
+
catch (error) {
|
1292
|
+
return false;
|
1293
|
+
}
|
1294
|
+
// return !!process.env.VSCODE_PID || process.execPath.includes('Code');
|
1295
|
+
//#endregion
|
1296
|
+
};
|
1297
|
+
//#endregion
|
1127
1298
|
//#region utils os / is running in wsl
|
1128
1299
|
/**
|
1129
1300
|
* Check wether the current process is running inside
|
1130
1301
|
* windows subsystem for linux (WSL).
|
1131
1302
|
*/
|
1132
|
-
UtilsOs.isRunningInWsl =
|
1303
|
+
UtilsOs.isRunningInWsl = () => {
|
1133
1304
|
/* */
|
1134
1305
|
/* */
|
1135
1306
|
//#region @backendFunc
|
@@ -1151,13 +1322,23 @@ var UtilsOs;
|
|
1151
1322
|
//#endregion
|
1152
1323
|
};
|
1153
1324
|
//#endregion
|
1325
|
+
//#region utils os / is running in windows cmd
|
1326
|
+
UtilsOs.isRunningInWindowsCmd = () => {
|
1327
|
+
/* */
|
1328
|
+
/* */
|
1329
|
+
//#region @backendFunc
|
1330
|
+
const shell = process.env.ComSpec || process.env.SHELL;
|
1331
|
+
return shell && shell.toLowerCase().includes('cmd.exe');
|
1332
|
+
//#endregion
|
1333
|
+
};
|
1334
|
+
//#endregion
|
1154
1335
|
//#region utils os / is running in docker
|
1155
|
-
UtilsOs.isRunningInDocker =
|
1336
|
+
UtilsOs.isRunningInDocker = () => {
|
1156
1337
|
/* */
|
1157
1338
|
/* */
|
1158
1339
|
//#region @backendFunc
|
1159
1340
|
try {
|
1160
|
-
|
1341
|
+
const cgroup = core_imports_4.fse.readFileSync('/proc/1/cgroup', 'utf8');
|
1161
1342
|
return /docker|kubepods|containerd/.test(cgroup);
|
1162
1343
|
}
|
1163
1344
|
catch (e) {
|
@@ -1167,7 +1348,7 @@ var UtilsOs;
|
|
1167
1348
|
};
|
1168
1349
|
//#endregion
|
1169
1350
|
//#region utils os / is running in linux graphics capable environment
|
1170
|
-
UtilsOs.isRunningInLinuxGraphicsCapableEnvironment =
|
1351
|
+
UtilsOs.isRunningInLinuxGraphicsCapableEnvironment = () => {
|
1171
1352
|
//#region @backendFunc
|
1172
1353
|
if (core_imports_1.os.platform() !== 'linux') {
|
1173
1354
|
return false;
|
@@ -1181,7 +1362,7 @@ var UtilsOs;
|
|
1181
1362
|
/**
|
1182
1363
|
* Check whether the current process is running in CLI mode.
|
1183
1364
|
*/
|
1184
|
-
UtilsOs.isRunningInCliMode =
|
1365
|
+
UtilsOs.isRunningInCliMode = () => {
|
1185
1366
|
/* */
|
1186
1367
|
/* */
|
1187
1368
|
//#region @backendFunc
|
@@ -1193,7 +1374,7 @@ var UtilsOs;
|
|
1193
1374
|
/**
|
1194
1375
|
* Check whether the current process is running in mocha test.
|
1195
1376
|
*/
|
1196
|
-
UtilsOs.isRunningInMochaTest =
|
1377
|
+
UtilsOs.isRunningInMochaTest = () => {
|
1197
1378
|
/* */
|
1198
1379
|
/* */
|
1199
1380
|
//#region @backendFunc
|
@@ -1202,33 +1383,29 @@ var UtilsOs;
|
|
1202
1383
|
};
|
1203
1384
|
//#endregion
|
1204
1385
|
//#region utils os / is port in use
|
1205
|
-
|
1386
|
+
const isPortInUseOnHost = (port, host) => {
|
1206
1387
|
//#region @backendFunc
|
1207
|
-
return new Promise(
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
//
|
1212
|
-
|
1213
|
-
//
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
// console.log(`closing ${port} on ${host}`);
|
1225
|
-
resolve(false); // Port is not in use
|
1226
|
-
});
|
1388
|
+
return new Promise(async (resolve, reject) => {
|
1389
|
+
const server = net.createServer();
|
1390
|
+
// If the port is already in use, you'll get an EADDRINUSE error.
|
1391
|
+
server.once('error', (err) => {
|
1392
|
+
// console.log('error', err);
|
1393
|
+
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
|
1394
|
+
resolve(true); // Port is in use
|
1395
|
+
}
|
1396
|
+
else {
|
1397
|
+
reject(err); // Some other error occurred
|
1398
|
+
}
|
1399
|
+
});
|
1400
|
+
// If the server successfully starts listening, the port is free.
|
1401
|
+
server.once('listening', () => {
|
1402
|
+
server.close(() => {
|
1403
|
+
// console.log(`closing ${port} on ${host}`);
|
1404
|
+
resolve(false); // Port is not in use
|
1227
1405
|
});
|
1228
|
-
server.listen(port, host);
|
1229
|
-
return [2 /*return*/];
|
1230
1406
|
});
|
1231
|
-
|
1407
|
+
server.listen(port, host);
|
1408
|
+
});
|
1232
1409
|
//#endregion
|
1233
1410
|
};
|
1234
1411
|
/**
|
@@ -1238,49 +1415,21 @@ var UtilsOs;
|
|
1238
1415
|
* @param host - The hostname or IP address to bind to (default: '127.0.0.1').
|
1239
1416
|
* @returns Promise<boolean> - Resolves to `true` if the port is in use, otherwise `false`.
|
1240
1417
|
*/
|
1241
|
-
UtilsOs.isPortInUse =
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
: ['::', '::1', '0.0.0.0', '127.0.0.1', 'localhost'];
|
1252
|
-
_b.label = 1;
|
1253
|
-
case 1:
|
1254
|
-
_b.trys.push([1, 6, 7, 8]);
|
1255
|
-
hostArr_1 = tslib_1.__values(hostArr), hostArr_1_1 = hostArr_1.next();
|
1256
|
-
_b.label = 2;
|
1257
|
-
case 2:
|
1258
|
-
if (!!hostArr_1_1.done) return [3 /*break*/, 5];
|
1259
|
-
host = hostArr_1_1.value;
|
1260
|
-
return [4 /*yield*/, isPortInUseOnHost(port, host)];
|
1261
|
-
case 3:
|
1262
|
-
if (_b.sent()) {
|
1263
|
-
return [2 /*return*/, true];
|
1264
|
-
}
|
1265
|
-
_b.label = 4;
|
1266
|
-
case 4:
|
1267
|
-
hostArr_1_1 = hostArr_1.next();
|
1268
|
-
return [3 /*break*/, 2];
|
1269
|
-
case 5: return [3 /*break*/, 8];
|
1270
|
-
case 6:
|
1271
|
-
e_4_1 = _b.sent();
|
1272
|
-
e_4 = { error: e_4_1 };
|
1273
|
-
return [3 /*break*/, 8];
|
1274
|
-
case 7:
|
1275
|
-
try {
|
1276
|
-
if (hostArr_1_1 && !hostArr_1_1.done && (_a = hostArr_1.return)) _a.call(hostArr_1);
|
1277
|
-
}
|
1278
|
-
finally { if (e_4) throw e_4.error; }
|
1279
|
-
return [7 /*endfinally*/];
|
1280
|
-
case 8: return [2 /*return*/, false];
|
1418
|
+
UtilsOs.isPortInUse = async (port, options) => {
|
1419
|
+
//#region @backendFunc
|
1420
|
+
options = options || {};
|
1421
|
+
const hostArr = options.checkForSpecificOnlyHost
|
1422
|
+
? [options.checkForSpecificOnlyHost]
|
1423
|
+
: ['::', '::1', '0.0.0.0', '127.0.0.1', 'localhost'];
|
1424
|
+
// console.log({ hostArr });
|
1425
|
+
for (const host of hostArr) {
|
1426
|
+
if (await isPortInUseOnHost(port, host)) {
|
1427
|
+
return true;
|
1281
1428
|
}
|
1282
|
-
}
|
1283
|
-
|
1429
|
+
}
|
1430
|
+
return false;
|
1431
|
+
//#endregion
|
1432
|
+
};
|
1284
1433
|
//#endregion
|
1285
1434
|
})(UtilsOs || (exports.UtilsOs = UtilsOs = {}));
|
1286
1435
|
//#endregion
|
@@ -1288,7 +1437,7 @@ var UtilsOs;
|
|
1288
1437
|
var UtilsString;
|
1289
1438
|
(function (UtilsString) {
|
1290
1439
|
//#region utils string / kebab case no split numbers
|
1291
|
-
UtilsString.kebabCaseNoSplitNumbers =
|
1440
|
+
UtilsString.kebabCaseNoSplitNumbers = (input) => {
|
1292
1441
|
return (input
|
1293
1442
|
// Match spaces or any kind of whitespace and replace with a hyphen
|
1294
1443
|
.replace(/\s+/g, '-')
|
@@ -1306,31 +1455,31 @@ var UtilsString;
|
|
1306
1455
|
*/
|
1307
1456
|
var UtilsMigrations;
|
1308
1457
|
(function (UtilsMigrations) {
|
1309
|
-
UtilsMigrations.getTimestampFromClassName =
|
1310
|
-
|
1458
|
+
UtilsMigrations.getTimestampFromClassName = (className) => {
|
1459
|
+
const [maybeTimestamp1, maybeTimestamp2] = className.split('_') || [];
|
1311
1460
|
// console.log({ maybeTimestamp1, maybeTimestamp2 });
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1461
|
+
const timestamp1 = parseInt(maybeTimestamp1);
|
1462
|
+
const timestamp2 = parseInt(maybeTimestamp2);
|
1463
|
+
const timestamp = !core_imports_1._.isNaN(timestamp1) ? timestamp1 : timestamp2;
|
1315
1464
|
return UtilsMigrations.isValidTimestamp(timestamp) ? timestamp : void 0;
|
1316
1465
|
};
|
1317
|
-
UtilsMigrations.getFormattedTimestampFromClassName =
|
1318
|
-
|
1466
|
+
UtilsMigrations.getFormattedTimestampFromClassName = (className) => {
|
1467
|
+
const timestamp = UtilsMigrations.getTimestampFromClassName(className);
|
1319
1468
|
if (!timestamp) {
|
1320
1469
|
return void 0;
|
1321
1470
|
}
|
1322
1471
|
return UtilsMigrations.formatTimestamp(timestamp);
|
1323
1472
|
};
|
1324
|
-
UtilsMigrations.formatTimestamp =
|
1325
|
-
|
1326
|
-
return
|
1473
|
+
UtilsMigrations.formatTimestamp = (timestamp) => {
|
1474
|
+
const dateFromTimestamp = new Date(timestamp);
|
1475
|
+
return `${(0, core_imports_2.dateformat)(dateFromTimestamp, 'dd-mm-yyyy HH:MM:ss')}`;
|
1327
1476
|
};
|
1328
|
-
UtilsMigrations.isValidTimestamp =
|
1477
|
+
UtilsMigrations.isValidTimestamp = (value) => {
|
1329
1478
|
if (typeof value !== 'number') {
|
1330
1479
|
return false; // Must be a number
|
1331
1480
|
}
|
1332
|
-
|
1333
|
-
|
1481
|
+
const minTimestamp = 0; // Minimum possible timestamp (Unix epoch)
|
1482
|
+
const maxTimestamp = 8640000000000000; // Max safe timestamp in JS (represents year ~275760)
|
1334
1483
|
return value >= minTimestamp && value <= maxTimestamp;
|
1335
1484
|
};
|
1336
1485
|
})(UtilsMigrations || (exports.UtilsMigrations = UtilsMigrations = {}));
|
@@ -1338,10 +1487,18 @@ var UtilsMigrations;
|
|
1338
1487
|
//#region utils terminal
|
1339
1488
|
var UtilsTerminal;
|
1340
1489
|
(function (UtilsTerminal) {
|
1341
|
-
|
1490
|
+
//#endregion
|
1491
|
+
//#region wait
|
1492
|
+
UtilsTerminal.wait = (second) => {
|
1493
|
+
return new Promise((resolve, reject) => {
|
1494
|
+
setTimeout(() => {
|
1495
|
+
resolve(void 0);
|
1496
|
+
}, second * 1000);
|
1497
|
+
});
|
1498
|
+
};
|
1342
1499
|
//#endregion
|
1343
1500
|
//#region clear
|
1344
|
-
UtilsTerminal.clearConsole =
|
1501
|
+
UtilsTerminal.clearConsole = () => {
|
1345
1502
|
//#region @backendFunc
|
1346
1503
|
index_1.Helpers.msgCacheClear();
|
1347
1504
|
console.log('\x1Bc');
|
@@ -1355,422 +1512,387 @@ var UtilsTerminal;
|
|
1355
1512
|
};
|
1356
1513
|
//#endregion
|
1357
1514
|
//#region transform choices
|
1358
|
-
|
1515
|
+
const transformChoices = (choices) => {
|
1359
1516
|
//#region @backendFunc
|
1360
1517
|
if (!core_imports_1._.isArray(choices) && core_imports_1._.isObject(choices)) {
|
1361
1518
|
choices = Object.keys(choices)
|
1362
|
-
.map(
|
1519
|
+
.map(key => {
|
1363
1520
|
return {
|
1364
1521
|
name: choices[key].name,
|
1365
1522
|
value: key,
|
1366
1523
|
};
|
1367
1524
|
})
|
1368
|
-
.reduce(
|
1525
|
+
.reduce((a, b) => a.concat(b), []);
|
1369
1526
|
}
|
1370
|
-
return choices.map(
|
1527
|
+
return choices.map(c => ({ name: c.name, value: c.value }));
|
1371
1528
|
//#endregion
|
1372
1529
|
};
|
1373
1530
|
//#endregion
|
1374
1531
|
//#region multiselect
|
1375
|
-
UtilsTerminal.multiselect =
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1415
|
-
|
1416
|
-
var fuzzyResult = fuzzy.filter(input, choices.map(function (f) { return f.name; }));
|
1417
|
-
return fuzzyResult.map(function (el) {
|
1418
|
-
return {
|
1419
|
-
name: el.original,
|
1420
|
-
value: choices.find(function (c) { return c.name === el.original; }).value,
|
1421
|
-
};
|
1422
|
-
});
|
1423
|
-
},
|
1424
|
-
})];
|
1425
|
-
case 5:
|
1426
|
-
res = _a.sent();
|
1427
|
-
return [2 /*return*/, (Array.isArray(res) ? res : [res])];
|
1428
|
-
}
|
1532
|
+
UtilsTerminal.multiselect = async (options) => {
|
1533
|
+
//#region @backendFunc
|
1534
|
+
const { select } = await Promise.resolve().then(() => require('inquirer-select-pro'));
|
1535
|
+
const fuzzy = await Promise.resolve().then(() => require('fuzzy'));
|
1536
|
+
options = core_imports_1._.cloneDeep(options);
|
1537
|
+
options.autocomplete = core_imports_1._.isNil(options.autocomplete)
|
1538
|
+
? true
|
1539
|
+
: options.autocomplete;
|
1540
|
+
const choices = transformChoices(options.choices);
|
1541
|
+
if (Object.keys(choices || {}).length === 0) {
|
1542
|
+
await UtilsTerminal.pressAnyKeyToContinueAsync({
|
1543
|
+
message: 'No choices available. Press any key to continue...',
|
1544
|
+
});
|
1545
|
+
return [];
|
1546
|
+
}
|
1547
|
+
const defaultValue = options.defaultSelected || [];
|
1548
|
+
// console.log({ defaultValue, choices });
|
1549
|
+
const res = await select({
|
1550
|
+
message: options.question,
|
1551
|
+
// options: choices,
|
1552
|
+
clearInputWhenSelected: true,
|
1553
|
+
emptyText: '<< No results >>',
|
1554
|
+
multiple: !options.onlyOneChoice,
|
1555
|
+
canToggleAll: true,
|
1556
|
+
pageSize: 10,
|
1557
|
+
loop: true,
|
1558
|
+
defaultValue,
|
1559
|
+
options: !options.autocomplete
|
1560
|
+
? choices
|
1561
|
+
: (input = '') => {
|
1562
|
+
if (!input) {
|
1563
|
+
return choices;
|
1564
|
+
}
|
1565
|
+
const fuzzyResult = fuzzy.filter(input, choices.map(f => f.name));
|
1566
|
+
return fuzzyResult.map(el => {
|
1567
|
+
return {
|
1568
|
+
name: el.original,
|
1569
|
+
value: choices.find(c => c.name === el.original).value,
|
1570
|
+
};
|
1571
|
+
});
|
1572
|
+
},
|
1429
1573
|
});
|
1430
|
-
|
1574
|
+
return (Array.isArray(res) ? res : [res]);
|
1575
|
+
//#region old autocomplete
|
1576
|
+
// const prompt = new AutoComplete({
|
1577
|
+
// name: 'value',
|
1578
|
+
// message: question,
|
1579
|
+
// limit: 10,
|
1580
|
+
// multiple: true,
|
1581
|
+
// choices,
|
1582
|
+
// initial: (selected || []).map(s => s.name),
|
1583
|
+
// // selected,
|
1584
|
+
// hint: '- Space to select. Return to submit',
|
1585
|
+
// footer() {
|
1586
|
+
// return CLI.chalk.green('(Scroll up and down to reveal more choices)');
|
1587
|
+
// },
|
1588
|
+
// result(names) {
|
1589
|
+
// return _.values(this.map(names)) || [];
|
1590
|
+
// },
|
1591
|
+
// });
|
1592
|
+
// const res = await prompt.run();
|
1593
|
+
//#endregion
|
1594
|
+
//#region old inquirer
|
1595
|
+
// const res = (await inquirer.prompt({
|
1596
|
+
// type: 'checkbox',
|
1597
|
+
// name: 'value',
|
1598
|
+
// message: question,
|
1599
|
+
// default: selected.map(s => s.name),
|
1600
|
+
// choices,
|
1601
|
+
// pageSize: 10,
|
1602
|
+
// loop: false,
|
1603
|
+
// } as any)) as any;
|
1604
|
+
// return res.value;
|
1605
|
+
//#endregion
|
1606
|
+
//#endregion
|
1607
|
+
};
|
1431
1608
|
//#endregion
|
1432
1609
|
//#region select and execute
|
1433
1610
|
/**
|
1434
1611
|
* Similar to select but executes action if provided
|
1435
1612
|
* @returns selected and executed value
|
1436
1613
|
*/
|
1437
|
-
UtilsTerminal.multiselectActionAndExecute =
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
_a = res;
|
1454
|
-
_b = [];
|
1455
|
-
for (_c in _a)
|
1456
|
-
_b.push(_c);
|
1457
|
-
_i = 0;
|
1458
|
-
_f.label = 2;
|
1459
|
-
case 2:
|
1460
|
-
if (!(_i < _b.length)) return [3 /*break*/, 5];
|
1461
|
-
_c = _b[_i];
|
1462
|
-
if (!(_c in _a)) return [3 /*break*/, 4];
|
1463
|
-
key = _c;
|
1464
|
-
if (!(res[key] && choices[key] && core_imports_1._.isFunction(choices[key].action))) return [3 /*break*/, 4];
|
1465
|
-
_e = (_d = actionResults).push;
|
1466
|
-
return [4 /*yield*/, choices[key].action()];
|
1467
|
-
case 3:
|
1468
|
-
_e.apply(_d, [_f.sent()]);
|
1469
|
-
_f.label = 4;
|
1470
|
-
case 4:
|
1471
|
-
_i++;
|
1472
|
-
return [3 /*break*/, 2];
|
1473
|
-
case 5:
|
1474
|
-
// console.log(`Response from select: "${res}"`);
|
1475
|
-
// pipeEnterToStdin();
|
1476
|
-
return [2 /*return*/, {
|
1477
|
-
selected: res,
|
1478
|
-
actionResults: actionResults,
|
1479
|
-
/**
|
1480
|
-
* object containing all selected actions
|
1481
|
-
*/
|
1482
|
-
actions: res.map(function (r) { return choices[r].action; }),
|
1483
|
-
}];
|
1484
|
-
}
|
1614
|
+
UtilsTerminal.multiselectActionAndExecute = async (choices, options) => {
|
1615
|
+
//#region @backendFunc
|
1616
|
+
options = options || {};
|
1617
|
+
options.question = options.question || 'Select actions to execute';
|
1618
|
+
options.executeActionsOnDefault = core_imports_1._.isBoolean(options.executeActionsOnDefault)
|
1619
|
+
? options.executeActionsOnDefault
|
1620
|
+
: true;
|
1621
|
+
// if (Object.keys(choices || {}).length === 0) {
|
1622
|
+
// await UtilsTerminal.pressAnyKeyToContinueAsync({
|
1623
|
+
// message: 'No choices available. Press any key to continue...',
|
1624
|
+
// });
|
1625
|
+
// return { selected: [] as (keyof CHOICE)[], action: async () => void 0 };
|
1626
|
+
// }
|
1627
|
+
const res = await UtilsTerminal.multiselect({
|
1628
|
+
...options,
|
1629
|
+
choices,
|
1485
1630
|
});
|
1486
|
-
|
1631
|
+
// clearConsole();
|
1632
|
+
let actionResults = [];
|
1633
|
+
if (options.executeActionsOnDefault) {
|
1634
|
+
for (const key in res) {
|
1635
|
+
if (res[key] && choices[key] && core_imports_1._.isFunction(choices[key].action)) {
|
1636
|
+
actionResults.push(await choices[key].action());
|
1637
|
+
}
|
1638
|
+
}
|
1639
|
+
}
|
1640
|
+
// console.log(`Response from select: "${res}"`);
|
1641
|
+
// pipeEnterToStdin();
|
1642
|
+
return {
|
1643
|
+
selected: res,
|
1644
|
+
actionResults,
|
1645
|
+
/**
|
1646
|
+
* object containing all selected actions
|
1647
|
+
*/
|
1648
|
+
actions: res.map(r => choices[r].action),
|
1649
|
+
};
|
1650
|
+
//#endregion
|
1651
|
+
};
|
1487
1652
|
//#endregion
|
1488
1653
|
//#region select and execute
|
1489
1654
|
/**
|
1490
1655
|
* Similar to select but executes action if provided
|
1491
1656
|
* @returns selected and executed value
|
1492
1657
|
*/
|
1493
|
-
UtilsTerminal.selectActionAndExecute =
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
case 1:
|
1510
|
-
_a.sent();
|
1511
|
-
return [2 /*return*/, { selected: void 0, action: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
|
1512
|
-
return [2 /*return*/, void 0];
|
1513
|
-
}); }); } }];
|
1514
|
-
case 2: return [4 /*yield*/, UtilsTerminal.select(tslib_1.__assign(tslib_1.__assign({}, options), { choices: choices }))];
|
1515
|
-
case 3:
|
1516
|
-
res = _a.sent();
|
1517
|
-
if (!(res &&
|
1518
|
-
choices[res] &&
|
1519
|
-
core_imports_1._.isFunction(choices[res].action) &&
|
1520
|
-
options.executeActionOnDefault)) return [3 /*break*/, 5];
|
1521
|
-
return [4 /*yield*/, choices[res].action()];
|
1522
|
-
case 4:
|
1523
|
-
actionResult = _a.sent();
|
1524
|
-
_a.label = 5;
|
1525
|
-
case 5:
|
1526
|
-
// console.log(`Response from select: "${res}"`);
|
1527
|
-
// pipeEnterToStdin();
|
1528
|
-
return [2 /*return*/, {
|
1529
|
-
selected: res,
|
1530
|
-
actionResult: actionResult,
|
1531
|
-
action: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
|
1532
|
-
switch (_a.label) {
|
1533
|
-
case 0: return [4 /*yield*/, choices[res].action()];
|
1534
|
-
case 1: return [2 /*return*/, _a.sent()];
|
1535
|
-
}
|
1536
|
-
}); }); },
|
1537
|
-
}];
|
1538
|
-
}
|
1658
|
+
UtilsTerminal.selectActionAndExecute = async (choices, options) => {
|
1659
|
+
//#region @backendFunc
|
1660
|
+
options = options || {};
|
1661
|
+
options.question = options.question || 'Select action to execute';
|
1662
|
+
options.executeActionOnDefault = core_imports_1._.isBoolean(options.executeActionOnDefault)
|
1663
|
+
? options.executeActionOnDefault
|
1664
|
+
: true;
|
1665
|
+
if (Object.keys(choices || {}).length === 0) {
|
1666
|
+
await UtilsTerminal.pressAnyKeyToContinueAsync({
|
1667
|
+
message: 'No choices available. Press any key to continue...',
|
1668
|
+
});
|
1669
|
+
return { selected: void 0, action: async () => void 0 };
|
1670
|
+
}
|
1671
|
+
const res = await UtilsTerminal.select({
|
1672
|
+
...options,
|
1673
|
+
choices,
|
1539
1674
|
});
|
1540
|
-
|
1675
|
+
// clearConsole();
|
1676
|
+
let actionResult;
|
1677
|
+
if (res &&
|
1678
|
+
choices[res] &&
|
1679
|
+
core_imports_1._.isFunction(choices[res].action) &&
|
1680
|
+
options.executeActionOnDefault) {
|
1681
|
+
actionResult = await choices[res].action();
|
1682
|
+
}
|
1683
|
+
// console.log(`Response from select: "${res}"`);
|
1684
|
+
// pipeEnterToStdin();
|
1685
|
+
return {
|
1686
|
+
selected: res,
|
1687
|
+
actionResult,
|
1688
|
+
action: async () => await choices[res].action(),
|
1689
|
+
};
|
1690
|
+
//#endregion
|
1691
|
+
};
|
1541
1692
|
//#endregion
|
1542
1693
|
//#region select
|
1543
|
-
UtilsTerminal.select =
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1694
|
+
UtilsTerminal.select = async (options) => {
|
1695
|
+
//#region @backendFunc
|
1696
|
+
options = core_imports_1._.cloneDeep(options);
|
1697
|
+
options.hint = core_imports_1._.isNil(options.hint)
|
1698
|
+
? '- Space to select. Return to submit'
|
1699
|
+
: options.hint;
|
1700
|
+
options.autocomplete = core_imports_1._.isNil(options.autocomplete)
|
1701
|
+
? true
|
1702
|
+
: options.autocomplete;
|
1703
|
+
const choices = transformChoices(options.choices);
|
1704
|
+
let preselectedIndex = choices.findIndex(c => c.value === options.defaultSelected) || 0;
|
1705
|
+
if (preselectedIndex === -1) {
|
1706
|
+
preselectedIndex = 0;
|
1707
|
+
}
|
1708
|
+
let prompt;
|
1709
|
+
// console.log({ choicesBefore: choices });
|
1710
|
+
if (options.autocomplete) {
|
1711
|
+
const { AutoComplete } = require('enquirer');
|
1712
|
+
prompt = new AutoComplete({
|
1713
|
+
name: 'value',
|
1714
|
+
message: options.question,
|
1715
|
+
limit: 10,
|
1716
|
+
multiple: false,
|
1717
|
+
initial: preselectedIndex,
|
1718
|
+
choices,
|
1719
|
+
hint: options.hint,
|
1720
|
+
footer() {
|
1721
|
+
return core_imports_1.chalk.green('(Scroll up and down to reveal more choices)');
|
1722
|
+
},
|
1723
|
+
});
|
1724
|
+
const res = await prompt.run();
|
1725
|
+
// console.log({choices})
|
1726
|
+
// console.log(`Selected!!!: "${res}" `);
|
1727
|
+
return res;
|
1728
|
+
}
|
1729
|
+
else {
|
1730
|
+
const { Select } = require('enquirer');
|
1731
|
+
prompt = new Select({
|
1732
|
+
// name: 'value',
|
1733
|
+
message: options.question,
|
1734
|
+
choices,
|
1735
|
+
});
|
1736
|
+
const res = await prompt.run();
|
1737
|
+
return choices.find(c => c.name === res)?.value;
|
1738
|
+
}
|
1739
|
+
//#region does not work
|
1740
|
+
// const choice = await multiselect<T>({
|
1741
|
+
// ...{
|
1742
|
+
// question,
|
1743
|
+
// choices,
|
1744
|
+
// autocomplete,
|
1745
|
+
// defaultSelected: [defaultSelected],
|
1746
|
+
// },
|
1747
|
+
// onlyOneChoice: true,
|
1748
|
+
// });
|
1749
|
+
// return _.first(choice) as T;
|
1750
|
+
//#endregion
|
1751
|
+
//#endregion
|
1752
|
+
};
|
1596
1753
|
//#endregion
|
1597
1754
|
//#region pipe enter to stdin
|
1598
|
-
UtilsTerminal.pipeEnterToStdin =
|
1755
|
+
UtilsTerminal.pipeEnterToStdin = () => {
|
1599
1756
|
//#region @backendFunc
|
1600
1757
|
process.stdin.push('\n');
|
1601
1758
|
//#endregion
|
1602
1759
|
};
|
1603
1760
|
//#endregion
|
1604
1761
|
//#region input
|
1605
|
-
UtilsTerminal.input =
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
// required: _.isNil(required) ? true : required,
|
1627
|
-
})];
|
1628
|
-
case 4:
|
1629
|
-
response = _c.sent();
|
1630
|
-
anwser = response.name;
|
1631
|
-
if (required && !anwser) {
|
1632
|
-
console.warn("Answer is required...");
|
1633
|
-
return [3 /*break*/, 2];
|
1634
|
-
}
|
1635
|
-
return [2 /*return*/, anwser];
|
1636
|
-
case 5:
|
1637
|
-
error_1 = _c.sent();
|
1638
|
-
console.error(error_1);
|
1639
|
-
if (required) {
|
1640
|
-
console.warn("Something went wrong, please try again...");
|
1641
|
-
return [3 /*break*/, 2];
|
1642
|
-
}
|
1643
|
-
else {
|
1644
|
-
return [2 /*return*/, ''];
|
1645
|
-
}
|
1646
|
-
return [3 /*break*/, 6];
|
1647
|
-
case 6: return [3 /*break*/, 2];
|
1648
|
-
case 7: return [2 /*return*/];
|
1762
|
+
UtilsTerminal.input = async ({ defaultValue, question, required, // TODO something is werid with required
|
1763
|
+
}) => {
|
1764
|
+
//#region @backendFunc
|
1765
|
+
const initial = defaultValue || '';
|
1766
|
+
const inquirer = await Promise.resolve().then(() => require('inquirer'));
|
1767
|
+
while (true) {
|
1768
|
+
try {
|
1769
|
+
// Create an input prompt
|
1770
|
+
const response = await inquirer.prompt({
|
1771
|
+
type: 'input',
|
1772
|
+
name: 'name',
|
1773
|
+
message: question,
|
1774
|
+
default: initial,
|
1775
|
+
// required: _.isNil(required) ? true : required,
|
1776
|
+
});
|
1777
|
+
const anwser = response.name;
|
1778
|
+
if (required && !anwser) {
|
1779
|
+
console.warn(`Answer is required...`);
|
1780
|
+
continue;
|
1781
|
+
}
|
1782
|
+
return anwser;
|
1649
1783
|
}
|
1650
|
-
|
1651
|
-
|
1784
|
+
catch (error) {
|
1785
|
+
console.error(error);
|
1786
|
+
if (required) {
|
1787
|
+
console.warn(`Something went wrong, please try again...`);
|
1788
|
+
continue;
|
1789
|
+
}
|
1790
|
+
else {
|
1791
|
+
return '';
|
1792
|
+
}
|
1793
|
+
}
|
1794
|
+
}
|
1795
|
+
//#endregion
|
1796
|
+
};
|
1652
1797
|
//#endregion
|
1653
1798
|
//#region confirm
|
1654
|
-
UtilsTerminal.confirm =
|
1655
|
-
|
1799
|
+
UtilsTerminal.confirm = async (options) => {
|
1800
|
+
//#region @backendFunc
|
1801
|
+
options = options || {};
|
1802
|
+
options.defaultValue = core_imports_1._.isBoolean(options.defaultValue)
|
1803
|
+
? options.defaultValue
|
1804
|
+
: true;
|
1805
|
+
options.message = options.message || 'Are you sure?';
|
1806
|
+
options.engine = options.engine || 'inquirer-toggle';
|
1807
|
+
const { defaultValue, message,
|
1656
1808
|
// mustAnswerQuestion,
|
1657
|
-
callbackFalse, callbackTrue,
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
case 2:
|
1680
|
-
inquirerToggle = (_b.sent()).default;
|
1681
|
-
return [4 /*yield*/, inquirerToggle({
|
1682
|
-
message: message,
|
1683
|
-
default: defaultValue,
|
1684
|
-
theme: {
|
1685
|
-
style: {
|
1686
|
-
highlight: core_imports_1.chalk.bold.cyan.underline,
|
1687
|
-
},
|
1688
|
-
},
|
1689
|
-
})];
|
1690
|
-
case 3:
|
1691
|
-
answer = _b.sent();
|
1692
|
-
response = {
|
1693
|
-
value: answer,
|
1694
|
-
};
|
1695
|
-
return [3 /*break*/, 11];
|
1696
|
-
case 4:
|
1697
|
-
if (!(options.engine === '@inquirer/prompts')) return [3 /*break*/, 7];
|
1698
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return require('@inquirer/prompts'); })];
|
1699
|
-
case 5:
|
1700
|
-
confirm_1 = (_b.sent()).confirm;
|
1701
|
-
return [4 /*yield*/, confirm_1({
|
1702
|
-
message: message,
|
1703
|
-
default: defaultValue,
|
1704
|
-
})];
|
1705
|
-
case 6:
|
1706
|
-
answer = _b.sent();
|
1707
|
-
response = {
|
1708
|
-
value: answer,
|
1709
|
-
};
|
1710
|
-
return [3 /*break*/, 11];
|
1711
|
-
case 7:
|
1712
|
-
if (!(options.engine === 'prompts')) return [3 /*break*/, 9];
|
1713
|
-
prompts = require('prompts');
|
1714
|
-
return [4 /*yield*/, prompts({
|
1715
|
-
type: 'toggle',
|
1716
|
-
name: 'value',
|
1717
|
-
message: message,
|
1718
|
-
initial: defaultValue,
|
1719
|
-
active: 'yes',
|
1720
|
-
inactive: 'no',
|
1721
|
-
})];
|
1722
|
-
case 8:
|
1723
|
-
response = _b.sent();
|
1724
|
-
return [3 /*break*/, 11];
|
1725
|
-
case 9:
|
1726
|
-
if (!(options.engine === 'enquirer')) return [3 /*break*/, 11];
|
1727
|
-
Select = require('enquirer').Select;
|
1728
|
-
choices = defaultValue ? ['yes', 'no'] : ['no', 'yes'];
|
1729
|
-
prompt_1 = new Select({
|
1730
|
-
name: 'question',
|
1731
|
-
message: message,
|
1732
|
-
choices: choices,
|
1733
|
-
});
|
1734
|
-
_a = {};
|
1735
|
-
return [4 /*yield*/, prompt_1.run()];
|
1736
|
-
case 10:
|
1737
|
-
response = (_a.value = (_b.sent()) === 'yes',
|
1738
|
-
_a);
|
1739
|
-
_b.label = 11;
|
1740
|
-
case 11:
|
1741
|
-
if (!response.value) return [3 /*break*/, 14];
|
1742
|
-
if (!callbackTrue) return [3 /*break*/, 13];
|
1743
|
-
return [4 /*yield*/, index_1.Helpers.runSyncOrAsync({ functionFn: callbackTrue })];
|
1744
|
-
case 12:
|
1745
|
-
_b.sent();
|
1746
|
-
_b.label = 13;
|
1747
|
-
case 13: return [3 /*break*/, 16];
|
1748
|
-
case 14:
|
1749
|
-
if (!callbackFalse) return [3 /*break*/, 16];
|
1750
|
-
return [4 /*yield*/, index_1.Helpers.runSyncOrAsync({ functionFn: callbackFalse })];
|
1751
|
-
case 15:
|
1752
|
-
_b.sent();
|
1753
|
-
_b.label = 16;
|
1754
|
-
case 16: return [2 /*return*/, response.value];
|
1809
|
+
callbackFalse, callbackTrue, } = options;
|
1810
|
+
let response = {
|
1811
|
+
value: defaultValue,
|
1812
|
+
};
|
1813
|
+
if (global.tnpNonInteractive) {
|
1814
|
+
index_1.Helpers.info(`${message} - AUTORESPONSE: ${defaultValue ? 'YES' : 'NO'}`);
|
1815
|
+
}
|
1816
|
+
else {
|
1817
|
+
if (options.engine === 'inquirer-toggle') {
|
1818
|
+
const inquirerToggle = (await Promise.resolve().then(() => require('inquirer-toggle'))).default;
|
1819
|
+
const answer = await inquirerToggle({
|
1820
|
+
message,
|
1821
|
+
default: defaultValue,
|
1822
|
+
theme: {
|
1823
|
+
style: {
|
1824
|
+
highlight: core_imports_1.chalk.bold.cyan.underline,
|
1825
|
+
},
|
1826
|
+
},
|
1827
|
+
});
|
1828
|
+
response = {
|
1829
|
+
value: answer,
|
1830
|
+
};
|
1755
1831
|
}
|
1756
|
-
|
1757
|
-
|
1832
|
+
else if (options.engine === '@inquirer/prompts') {
|
1833
|
+
// @ts-ignore
|
1834
|
+
const { confirm } = await Promise.resolve().then(() => require('@inquirer/prompts'));
|
1835
|
+
const answer = await confirm({
|
1836
|
+
message,
|
1837
|
+
default: defaultValue,
|
1838
|
+
});
|
1839
|
+
response = {
|
1840
|
+
value: answer,
|
1841
|
+
};
|
1842
|
+
}
|
1843
|
+
else if (options.engine === 'prompts') {
|
1844
|
+
const prompts = require('prompts');
|
1845
|
+
response = await prompts({
|
1846
|
+
type: 'toggle',
|
1847
|
+
name: 'value',
|
1848
|
+
message,
|
1849
|
+
initial: defaultValue,
|
1850
|
+
active: 'yes',
|
1851
|
+
inactive: 'no',
|
1852
|
+
});
|
1853
|
+
}
|
1854
|
+
else if (options.engine === 'enquirer') {
|
1855
|
+
const { Select } = require('enquirer');
|
1856
|
+
const choices = defaultValue ? ['yes', 'no'] : ['no', 'yes'];
|
1857
|
+
const prompt = new Select({
|
1858
|
+
name: 'question',
|
1859
|
+
message,
|
1860
|
+
choices,
|
1861
|
+
});
|
1862
|
+
response = {
|
1863
|
+
value: (await prompt.run()) === 'yes',
|
1864
|
+
};
|
1865
|
+
}
|
1866
|
+
}
|
1867
|
+
if (response.value) {
|
1868
|
+
if (callbackTrue) {
|
1869
|
+
await index_1.Helpers.runSyncOrAsync({ functionFn: callbackTrue });
|
1870
|
+
}
|
1871
|
+
}
|
1872
|
+
else {
|
1873
|
+
if (callbackFalse) {
|
1874
|
+
await index_1.Helpers.runSyncOrAsync({ functionFn: callbackFalse });
|
1875
|
+
}
|
1876
|
+
}
|
1877
|
+
return response.value;
|
1878
|
+
//#endregion
|
1879
|
+
};
|
1758
1880
|
//#endregion
|
1759
1881
|
//#region press any key to continue
|
1760
|
-
UtilsTerminal.pressAnyKeyToContinueAsync =
|
1882
|
+
UtilsTerminal.pressAnyKeyToContinueAsync = (options) => {
|
1761
1883
|
//#region @backendFunc
|
1762
1884
|
options = options || {};
|
1763
1885
|
options.message =
|
1764
1886
|
options.message || core_imports_1.chalk.bold('Press any key to continue...');
|
1765
|
-
|
1766
|
-
|
1767
|
-
return new Promise(
|
1768
|
-
|
1887
|
+
const { message } = options;
|
1888
|
+
const readline = require('readline');
|
1889
|
+
return new Promise(resolve => {
|
1890
|
+
const rl = readline.createInterface({
|
1769
1891
|
input: process.stdin,
|
1770
1892
|
output: process.stdout,
|
1771
1893
|
});
|
1772
1894
|
// Prompt user with the question
|
1773
|
-
rl.question(message,
|
1895
|
+
rl.question(message, answer => {
|
1774
1896
|
rl.close();
|
1775
1897
|
resolve(answer);
|
1776
1898
|
});
|
@@ -1782,18 +1904,18 @@ var UtilsTerminal;
|
|
1782
1904
|
/**
|
1783
1905
|
* @deprecated use UtilsTerminal.pressAnyKeyToContinueAsync()
|
1784
1906
|
*/
|
1785
|
-
UtilsTerminal.pressAnyKey =
|
1907
|
+
UtilsTerminal.pressAnyKey = (options) => {
|
1786
1908
|
//#region @backendFunc
|
1787
1909
|
options = options || {};
|
1788
1910
|
options.message = options.message || 'Press any key to continue...';
|
1789
|
-
|
1911
|
+
const { message } = options;
|
1790
1912
|
if (process.platform === 'win32') {
|
1791
|
-
|
1913
|
+
const terminal = UtilsProcess.getBashOrShellName();
|
1792
1914
|
// console.log({ terminal });
|
1793
1915
|
if (terminal === 'gitbash') {
|
1794
|
-
|
1916
|
+
const getGitBashPath = UtilsProcess.getGitBashPath();
|
1795
1917
|
// console.log({ getGitBashPath });
|
1796
|
-
|
1918
|
+
const gitbashCommand = `read -p "${core_imports_1.chalk.bold(message)}"`;
|
1797
1919
|
core_imports_3.child_process.execSync(gitbashCommand, {
|
1798
1920
|
stdio: 'inherit',
|
1799
1921
|
shell: getGitBashPath,
|
@@ -1814,41 +1936,25 @@ var UtilsTerminal;
|
|
1814
1936
|
};
|
1815
1937
|
//#endregion
|
1816
1938
|
//#region preview long list as select
|
1817
|
-
UtilsTerminal.previewLongList =
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
}
|
1822
|
-
|
1823
|
-
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
//#region @backendFunc
|
1829
|
-
if (!Array.isArray(list)) {
|
1830
|
-
list = list.split('\n');
|
1831
|
-
}
|
1832
|
-
choices = list.reduce(function (a, b) {
|
1833
|
-
var _a;
|
1834
|
-
return core_imports_1._.merge(a, (_a = {},
|
1835
|
-
_a[b] = {
|
1836
|
-
name: b,
|
1837
|
-
// action: () => {},
|
1838
|
-
},
|
1839
|
-
_a));
|
1840
|
-
}, {});
|
1841
|
-
return [4 /*yield*/, UtilsTerminal.selectActionAndExecute(choices, {
|
1842
|
-
autocomplete: true,
|
1843
|
-
question: listName,
|
1844
|
-
hint: 'Press enter to return',
|
1845
|
-
})];
|
1846
|
-
case 1:
|
1847
|
-
_a.sent();
|
1848
|
-
return [2 /*return*/];
|
1849
|
-
}
|
1939
|
+
UtilsTerminal.previewLongList = async (list, listName = 'List') => {
|
1940
|
+
//#region @backendFunc
|
1941
|
+
if (!Array.isArray(list)) {
|
1942
|
+
list = list.split('\n');
|
1943
|
+
}
|
1944
|
+
const choices = list.reduce((a, b) => {
|
1945
|
+
return core_imports_1._.merge(a, {
|
1946
|
+
[b]: {
|
1947
|
+
name: b,
|
1948
|
+
// action: () => {},
|
1949
|
+
},
|
1850
1950
|
});
|
1951
|
+
}, {});
|
1952
|
+
await UtilsTerminal.selectActionAndExecute(choices, {
|
1953
|
+
autocomplete: true,
|
1954
|
+
question: listName,
|
1955
|
+
hint: 'Press enter to return',
|
1851
1956
|
});
|
1957
|
+
//#endregion
|
1852
1958
|
};
|
1853
1959
|
//#endregion
|
1854
1960
|
//#region preview long list with 'less' (git log like)
|
@@ -1859,26 +1965,26 @@ var UtilsTerminal;
|
|
1859
1965
|
* @param {string} list - The long string content to display.
|
1860
1966
|
* @returns {Promise<void>} A Promise that resolves when the pager exits.
|
1861
1967
|
*/
|
1862
|
-
UtilsTerminal.previewLongListGitLogLike =
|
1968
|
+
UtilsTerminal.previewLongListGitLogLike = (list) => {
|
1863
1969
|
//#region @backendFunc
|
1864
1970
|
if (Array.isArray(list)) {
|
1865
1971
|
list = list.join('\n');
|
1866
1972
|
}
|
1867
|
-
return new Promise(
|
1868
|
-
|
1973
|
+
return new Promise((resolve, reject) => {
|
1974
|
+
const less = (0, core_imports_3.spawn)('less', [], {
|
1869
1975
|
stdio: ['pipe', process.stdout, process.stderr],
|
1870
1976
|
});
|
1871
1977
|
less.stdin.write(list); // Write the list content to the less process
|
1872
1978
|
less.stdin.end(); // Signal that writing is complete
|
1873
|
-
less.on('close',
|
1979
|
+
less.on('close', code => {
|
1874
1980
|
if (code === 0) {
|
1875
1981
|
resolve(void 0);
|
1876
1982
|
}
|
1877
1983
|
else {
|
1878
|
-
reject(new Error(
|
1984
|
+
reject(new Error(`less process exited with code ${code}`));
|
1879
1985
|
}
|
1880
1986
|
});
|
1881
|
-
less.on('error',
|
1987
|
+
less.on('error', err => {
|
1882
1988
|
reject(err);
|
1883
1989
|
});
|
1884
1990
|
});
|