tnp-core 18.0.46 → 18.0.49
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 +20 -20
- package/assets/shared/shared_folder_info.txt +1 -1
- package/browser/README.md +24 -24
- package/browser/esm2022/lib/core-imports.mjs +65 -9
- package/browser/esm2022/lib/core-models.mjs +61 -9
- package/browser/esm2022/lib/helpers-messages.mjs +9 -4
- package/browser/esm2022/lib/helpers.mjs +164 -373
- package/browser/esm2022/lib/utils.mjs +769 -190
- package/browser/fesm2022/tnp-core.mjs +1548 -1062
- 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 +68 -38
- package/browser/lib/helpers-messages.d.ts +6 -3
- package/browser/lib/helpers.d.ts +22 -0
- package/browser/lib/utils.d.ts +77 -13
- package/cli.backend.d.ts +1 -0
- package/cli.backend.js +15 -2
- package/cli.backend.js.map +1 -1
- package/client/README.md +24 -24
- package/client/esm2022/lib/core-imports.mjs +65 -9
- package/client/esm2022/lib/core-models.mjs +61 -9
- package/client/esm2022/lib/helpers-messages.mjs +9 -4
- package/client/esm2022/lib/helpers.mjs +164 -373
- package/client/esm2022/lib/utils.mjs +769 -190
- package/client/fesm2022/tnp-core.mjs +1548 -1062
- 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 +68 -38
- package/client/lib/helpers-messages.d.ts +6 -3
- package/client/lib/helpers.d.ts +22 -0
- package/client/lib/utils.d.ts +77 -13
- 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 -25
- package/lib/core-imports.js +202 -80
- package/lib/core-imports.js.map +1 -1
- package/lib/core-models.d.ts +68 -38
- package/lib/core-models.js +62 -8
- 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 +723 -971
- 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 +89 -14
- package/lib/utils.js +1261 -1075
- 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 +25 -91
- package/src.d.ts +1 -1
- package/tmp-environment.json +8 -404
- package/websql/README.md +24 -24
- package/websql/esm2022/lib/core-imports.mjs +65 -9
- package/websql/esm2022/lib/core-models.mjs +61 -9
- package/websql/esm2022/lib/helpers-messages.mjs +9 -4
- package/websql/esm2022/lib/helpers.mjs +164 -373
- package/websql/esm2022/lib/utils.mjs +766 -187
- package/websql/fesm2022/tnp-core.mjs +1546 -1060
- 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 +68 -38
- package/websql/lib/helpers-messages.d.ts +6 -3
- package/websql/lib/helpers.d.ts +22 -0
- package/websql/lib/utils.d.ts +77 -13
- package/browser/package.json +0 -25
- package/taon.jsonc +0 -65
- 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,97 +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
|
-
max = 5000;
|
78
|
-
i = 0;
|
79
|
-
if (core_imports_1._.isNumber(startFrom)) {
|
80
|
-
while (takenPorts.includes(startFrom)) {
|
81
|
-
startFrom += 1 + howManyFreePortsAfterThatPort;
|
82
|
-
}
|
83
|
-
}
|
84
|
-
_a.label = 1;
|
85
|
-
case 1:
|
86
|
-
if (!true) return [3 /*break*/, 6];
|
87
|
-
_a.label = 2;
|
88
|
-
case 2:
|
89
|
-
_a.trys.push([2, 4, , 5]);
|
90
|
-
return [4 /*yield*/, core_imports_1.portfinder.getPortPromise({ port: startFrom })];
|
91
|
-
case 3:
|
92
|
-
port = _a.sent();
|
93
|
-
takenPorts.push(port);
|
94
|
-
return [2 /*return*/, port];
|
95
|
-
case 4:
|
96
|
-
err_1 = _a.sent();
|
97
|
-
console.log(err_1);
|
98
|
-
index_1.Helpers.warn("Trying to assign port :".concat(startFrom, " but already in use."), false);
|
99
|
-
return [3 /*break*/, 5];
|
100
|
-
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)) {
|
101
93
|
startFrom += 1;
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
94
|
+
continue;
|
95
|
+
}
|
96
|
+
const port = startFrom;
|
97
|
+
takenPorts.push(port);
|
98
|
+
return port;
|
107
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];
|
108
147
|
});
|
109
|
-
|
110
|
-
|
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];
|
153
|
+
}
|
154
|
+
});
|
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;
|
111
179
|
(function (json) {
|
112
|
-
json.getAtrributies =
|
113
|
-
fileContent) {
|
114
|
-
|
115
|
-
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');
|
116
183
|
// split path to parts but keep part if is for example 'sql.js
|
117
|
-
|
184
|
+
const pathParts = jsonDeepPath.split('.').reduce((a, b) => {
|
118
185
|
if (a.length === 0) {
|
119
186
|
return [b];
|
120
187
|
}
|
121
|
-
|
122
|
-
if ((last.startsWith(
|
123
|
-
(last.startsWith(
|
188
|
+
const last = a[a.length - 1];
|
189
|
+
if ((last.startsWith(`['`) && b.endsWith(`']`)) ||
|
190
|
+
(last.startsWith(`["`) && b.endsWith(`"]`))) {
|
124
191
|
a[a.length - 1] = [last, b].join('.');
|
125
192
|
}
|
126
193
|
else {
|
@@ -130,100 +197,76 @@ var Utils;
|
|
130
197
|
}, []);
|
131
198
|
// console.log({ pathParts });
|
132
199
|
// const pathParts = jsonDeepPath.split('.');
|
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
|
-
|
165
|
-
|
166
|
-
attributes = extractAttributesFromComments(collectedComments);
|
167
|
-
break;
|
168
|
-
}
|
169
|
-
// Reset collected comments as they only relate to the next key
|
170
|
-
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;
|
171
233
|
}
|
234
|
+
// Reset collected comments as they only relate to the next key
|
235
|
+
collectedComments = [];
|
172
236
|
}
|
173
237
|
}
|
174
238
|
}
|
175
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
176
|
-
finally {
|
177
|
-
try {
|
178
|
-
if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1);
|
179
|
-
}
|
180
|
-
finally { if (e_1) throw e_1.error; }
|
181
|
-
}
|
182
239
|
return attributes;
|
183
240
|
};
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
}
|
201
|
-
else {
|
202
|
-
existingAttribute.value = [existingAttribute.value, value];
|
203
|
-
}
|
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];
|
204
257
|
}
|
205
258
|
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
}
|
212
|
-
};
|
213
|
-
while ((match = attrRegex.exec(comment)) !== null) {
|
214
|
-
_loop_1();
|
259
|
+
}
|
260
|
+
else {
|
261
|
+
attributes.push({
|
262
|
+
name: `@${name}`,
|
263
|
+
value: value ? value : true,
|
264
|
+
});
|
215
265
|
}
|
216
266
|
}
|
217
267
|
}
|
218
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
219
|
-
finally {
|
220
|
-
try {
|
221
|
-
if (comments_1_1 && !comments_1_1.done && (_a = comments_1.return)) _a.call(comments_1);
|
222
|
-
}
|
223
|
-
finally { if (e_2) throw e_2.error; }
|
224
|
-
}
|
225
268
|
// Normalize single values not to be arrays
|
226
|
-
attributes.forEach(
|
269
|
+
attributes.forEach(attr => {
|
227
270
|
if (Array.isArray(attr.value) && attr.value.length === 1) {
|
228
271
|
attr.value = attr.value[0];
|
229
272
|
}
|
@@ -234,7 +277,7 @@ var Utils;
|
|
234
277
|
//#endregion
|
235
278
|
//#region binary
|
236
279
|
//#region db binary format type
|
237
|
-
|
280
|
+
let DbBinaryFormatEnum;
|
238
281
|
(function (DbBinaryFormatEnum) {
|
239
282
|
DbBinaryFormatEnum["Blob"] = "Blob";
|
240
283
|
DbBinaryFormatEnum["File"] = "File";
|
@@ -245,7 +288,7 @@ var Utils;
|
|
245
288
|
})(DbBinaryFormatEnum = Utils.DbBinaryFormatEnum || (Utils.DbBinaryFormatEnum = {}));
|
246
289
|
//#endregion
|
247
290
|
//#endregion
|
248
|
-
|
291
|
+
let binary;
|
249
292
|
(function (binary) {
|
250
293
|
//#region binay utils / array buffer to blob
|
251
294
|
/* */
|
@@ -270,21 +313,14 @@ var Utils;
|
|
270
313
|
* @param blob
|
271
314
|
* @returns
|
272
315
|
*/
|
273
|
-
function blobToArrayBuffer(blob) {
|
274
|
-
return
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
var reader = new FileReader();
|
279
|
-
reader.addEventListener('loadend', function () {
|
280
|
-
resolve(reader.result);
|
281
|
-
});
|
282
|
-
reader.addEventListener('error', reject); // @ts-ignore
|
283
|
-
reader.readAsArrayBuffer(blob);
|
284
|
-
})];
|
285
|
-
case 1: return [2 /*return*/, _a.sent()];
|
286
|
-
}
|
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);
|
287
321
|
});
|
322
|
+
reader.addEventListener('error', reject); // @ts-ignore
|
323
|
+
reader.readAsArrayBuffer(blob);
|
288
324
|
});
|
289
325
|
}
|
290
326
|
binary.blobToArrayBuffer = blobToArrayBuffer;
|
@@ -296,9 +332,9 @@ var Utils;
|
|
296
332
|
* @returns
|
297
333
|
*/
|
298
334
|
function blobToBase64(blob) {
|
299
|
-
return new Promise(
|
300
|
-
|
301
|
-
reader.onloadend =
|
335
|
+
return new Promise((resolve, _) => {
|
336
|
+
const reader = new FileReader();
|
337
|
+
reader.onloadend = () => resolve(reader.result); // @ts-ignore
|
302
338
|
reader.readAsDataURL(blob);
|
303
339
|
});
|
304
340
|
}
|
@@ -310,419 +346,242 @@ var Utils;
|
|
310
346
|
* @param base64Data
|
311
347
|
* @returns
|
312
348
|
*/
|
313
|
-
function base64toBlob(base64Data, contentTypeOverride) {
|
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
|
-
|
344
|
-
}
|
345
|
-
return [2 /*return*/, new buffer_1.Blob(byteArrays, { type: content_type })];
|
346
|
-
});
|
347
|
-
});
|
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 });
|
348
380
|
}
|
349
381
|
binary.base64toBlob = base64toBlob;
|
350
382
|
//#endregion
|
351
383
|
//#region binay utils / base64 string to db binary format
|
352
|
-
function base64toDbBinaryFormat(text) {
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
case 1:
|
370
|
-
/* */
|
371
|
-
/* */
|
372
|
-
/* */
|
373
|
-
/* */
|
374
|
-
/* */
|
375
|
-
/* */
|
376
|
-
/* */
|
377
|
-
/* */
|
378
|
-
//#region @backend
|
379
|
-
result = _a.sent();
|
380
|
-
//#endregion
|
381
|
-
return [2 /*return*/, result];
|
382
|
-
}
|
383
|
-
});
|
384
|
-
});
|
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;
|
385
401
|
}
|
386
402
|
binary.base64toDbBinaryFormat = base64toDbBinaryFormat;
|
387
403
|
//#endregion
|
388
404
|
//#region binay utils / db binary format to base64 string
|
389
|
-
function dbBinaryFormatToBase64(binaryFormat) {
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
case 1:
|
407
|
-
/* */
|
408
|
-
/* */
|
409
|
-
/* */
|
410
|
-
/* */
|
411
|
-
/* */
|
412
|
-
/* */
|
413
|
-
/* */
|
414
|
-
/* */
|
415
|
-
//#region @backend
|
416
|
-
result = _a.sent();
|
417
|
-
//#endregion
|
418
|
-
return [2 /*return*/, result];
|
419
|
-
}
|
420
|
-
});
|
421
|
-
});
|
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;
|
422
422
|
}
|
423
423
|
binary.dbBinaryFormatToBase64 = dbBinaryFormatToBase64;
|
424
424
|
//#endregion
|
425
425
|
//#region binay utils / base64 string to db binary format
|
426
|
-
function textToDbBinaryFormat(text) {
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
case 1:
|
444
|
-
/* */
|
445
|
-
/* */
|
446
|
-
/* */
|
447
|
-
/* */
|
448
|
-
/* */
|
449
|
-
/* */
|
450
|
-
/* */
|
451
|
-
/* */
|
452
|
-
//#region @backend
|
453
|
-
result = _a.sent();
|
454
|
-
//#endregion
|
455
|
-
return [2 /*return*/, result];
|
456
|
-
}
|
457
|
-
});
|
458
|
-
});
|
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;
|
459
443
|
}
|
460
444
|
binary.textToDbBinaryFormat = textToDbBinaryFormat;
|
461
445
|
//#endregion
|
462
446
|
//#region binay utils / db binary format to base64 string
|
463
|
-
function dbBinaryFormatToText(binaryFormat) {
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
case 1:
|
481
|
-
/* */
|
482
|
-
/* */
|
483
|
-
/* */
|
484
|
-
/* */
|
485
|
-
/* */
|
486
|
-
/* */
|
487
|
-
/* */
|
488
|
-
/* */
|
489
|
-
//#region @backend
|
490
|
-
result = _a.sent();
|
491
|
-
//#endregion
|
492
|
-
return [2 /*return*/, result];
|
493
|
-
}
|
494
|
-
});
|
495
|
-
});
|
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;
|
496
464
|
}
|
497
465
|
binary.dbBinaryFormatToText = dbBinaryFormatToText;
|
498
466
|
//#endregion
|
499
467
|
//#region binay utils / base64 string to nodejs buffer
|
500
468
|
//#region @backend
|
501
|
-
function base64toBuffer(base64Data, contentTypeOverride) {
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
switch (_a.label) {
|
506
|
-
case 0: return [4 /*yield*/, base64toBlob(base64Data, contentTypeOverride)];
|
507
|
-
case 1:
|
508
|
-
blob = _a.sent();
|
509
|
-
return [4 /*yield*/, blobToBuffer(blob)];
|
510
|
-
case 2:
|
511
|
-
buffer = _a.sent();
|
512
|
-
return [2 /*return*/, buffer];
|
513
|
-
}
|
514
|
-
});
|
515
|
-
});
|
469
|
+
async function base64toBuffer(base64Data, contentTypeOverride) {
|
470
|
+
const blob = await base64toBlob(base64Data, contentTypeOverride);
|
471
|
+
const buffer = await blobToBuffer(blob);
|
472
|
+
return buffer;
|
516
473
|
}
|
517
474
|
binary.base64toBuffer = base64toBuffer;
|
518
475
|
//#endregion
|
519
476
|
//#endregion
|
520
477
|
//#region binay utils / nodejs buffer to base64 string
|
521
478
|
//#region @backend
|
522
|
-
function bufferToBase64(bufferData) {
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
switch (_a.label) {
|
527
|
-
case 0: return [4 /*yield*/, bufferToBlob(bufferData)];
|
528
|
-
case 1:
|
529
|
-
blob = _a.sent();
|
530
|
-
return [4 /*yield*/, blobToBase64(blob)];
|
531
|
-
case 2:
|
532
|
-
text = _a.sent();
|
533
|
-
return [2 /*return*/, text];
|
534
|
-
}
|
535
|
-
});
|
536
|
-
});
|
479
|
+
async function bufferToBase64(bufferData) {
|
480
|
+
const blob = await bufferToBlob(bufferData);
|
481
|
+
const text = await blobToBase64(blob);
|
482
|
+
return text;
|
537
483
|
}
|
538
484
|
binary.bufferToBase64 = bufferToBase64;
|
539
485
|
//#endregion
|
540
486
|
//#endregion
|
541
487
|
//#region binay utils / file to blob
|
542
|
-
function fileToBlob(file) {
|
543
|
-
return
|
544
|
-
|
545
|
-
return tslib_1.__generator(this, function (_c) {
|
546
|
-
switch (_c.label) {
|
547
|
-
case 0:
|
548
|
-
_a = buffer_1.Blob.bind;
|
549
|
-
_b = Uint8Array.bind;
|
550
|
-
return [4 /*yield*/, file.arrayBuffer()];
|
551
|
-
case 1: return [2 /*return*/, new (_a.apply(buffer_1.Blob, [void 0, [new (_b.apply(Uint8Array, [void 0, _c.sent()]))()], {
|
552
|
-
type: file.type,
|
553
|
-
}]))()];
|
554
|
-
}
|
555
|
-
});
|
488
|
+
async function fileToBlob(file) {
|
489
|
+
return new buffer_1.Blob([new Uint8Array(await file.arrayBuffer())], {
|
490
|
+
type: file.type,
|
556
491
|
});
|
557
492
|
}
|
558
493
|
binary.fileToBlob = fileToBlob;
|
559
494
|
//#endregion
|
560
495
|
//#region binay utils / blob to file
|
561
|
-
function blobToFile(
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
}
|
568
|
-
// @ts-ignore
|
569
|
-
return [2 /*return*/, new File([blob], nameForFile)];
|
570
|
-
});
|
571
|
-
});
|
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);
|
572
502
|
}
|
573
503
|
binary.blobToFile = blobToFile;
|
574
504
|
//#endregion
|
575
505
|
//#region binay utils / nodejs blob to nodejs buffer
|
576
506
|
//#region @backend
|
577
|
-
function blobToBuffer(blob) {
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
switch (_a.label) {
|
582
|
-
case 0: return [4 /*yield*/, blob.arrayBuffer()];
|
583
|
-
case 1:
|
584
|
-
arrayBuffer = _a.sent();
|
585
|
-
buffer = Buffer.from(arrayBuffer);
|
586
|
-
return [2 /*return*/, buffer];
|
587
|
-
}
|
588
|
-
});
|
589
|
-
});
|
507
|
+
async function blobToBuffer(blob) {
|
508
|
+
const arrayBuffer = await blob.arrayBuffer();
|
509
|
+
const buffer = Buffer.from(arrayBuffer);
|
510
|
+
return buffer;
|
590
511
|
}
|
591
512
|
binary.blobToBuffer = blobToBuffer;
|
592
513
|
//#endregion
|
593
514
|
//#endregion
|
594
515
|
//#region binay utils / nodejs buffer to nodejs blob
|
595
516
|
//#region @backend
|
596
|
-
function bufferToBlob(buffer) {
|
597
|
-
|
598
|
-
|
599
|
-
return tslib_1.__generator(this, function (_a) {
|
600
|
-
blob = new buffer_1.Blob([buffer]);
|
601
|
-
return [2 /*return*/, blob];
|
602
|
-
});
|
603
|
-
});
|
517
|
+
async function bufferToBlob(buffer) {
|
518
|
+
const blob = new buffer_1.Blob([buffer]); // JavaScript Blob
|
519
|
+
return blob;
|
604
520
|
}
|
605
521
|
binary.bufferToBlob = bufferToBlob;
|
606
522
|
//#endregion
|
607
523
|
//#endregion
|
608
524
|
//#region binay utils / text to nodejs buffer
|
609
525
|
//#region @backend
|
610
|
-
function textToBuffer(
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
return tslib_1.__generator(this, function (_a) {
|
615
|
-
switch (_a.label) {
|
616
|
-
case 0: return [4 /*yield*/, textToBlob(text, type)];
|
617
|
-
case 1:
|
618
|
-
blob = _a.sent();
|
619
|
-
return [4 /*yield*/, blobToBuffer(blob)];
|
620
|
-
case 2:
|
621
|
-
buffer = _a.sent();
|
622
|
-
return [2 /*return*/, buffer];
|
623
|
-
}
|
624
|
-
});
|
625
|
-
});
|
526
|
+
async function textToBuffer(text, type = 'text/plain') {
|
527
|
+
const blob = await textToBlob(text, type);
|
528
|
+
const buffer = await blobToBuffer(blob);
|
529
|
+
return buffer;
|
626
530
|
}
|
627
531
|
binary.textToBuffer = textToBuffer;
|
628
532
|
//#endregion
|
629
533
|
//#endregion
|
630
534
|
//#region binay utils / nodejs buffer to text
|
631
535
|
//#region @backend
|
632
|
-
function bufferToText(buffer) {
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
switch (_a.label) {
|
637
|
-
case 0: return [4 /*yield*/, bufferToBlob(buffer)];
|
638
|
-
case 1:
|
639
|
-
blob = _a.sent();
|
640
|
-
return [4 /*yield*/, blobToText(blob)];
|
641
|
-
case 2:
|
642
|
-
text = _a.sent();
|
643
|
-
return [2 /*return*/, text];
|
644
|
-
}
|
645
|
-
});
|
646
|
-
});
|
536
|
+
async function bufferToText(buffer) {
|
537
|
+
const blob = await bufferToBlob(buffer);
|
538
|
+
const text = await blobToText(blob);
|
539
|
+
return text;
|
647
540
|
}
|
648
541
|
binary.bufferToText = bufferToText;
|
649
542
|
//#endregion
|
650
543
|
//#endregion
|
651
544
|
//#region binay utils / text to blob
|
652
|
-
function textToBlob(
|
653
|
-
|
654
|
-
|
655
|
-
if (type === void 0) { type = 'text/plain'; }
|
656
|
-
return tslib_1.__generator(this, function (_a) {
|
657
|
-
blob = new buffer_1.Blob([text], { type: type });
|
658
|
-
return [2 /*return*/, blob];
|
659
|
-
});
|
660
|
-
});
|
545
|
+
async function textToBlob(text, type = 'text/plain') {
|
546
|
+
const blob = new buffer_1.Blob([text], { type });
|
547
|
+
return blob;
|
661
548
|
}
|
662
549
|
binary.textToBlob = textToBlob;
|
663
550
|
//#endregion
|
664
551
|
//#region binay utils / blob to text
|
665
|
-
function blobToText(blob) {
|
666
|
-
return
|
667
|
-
return tslib_1.__generator(this, function (_a) {
|
668
|
-
switch (_a.label) {
|
669
|
-
case 0: return [4 /*yield*/, blob.text()];
|
670
|
-
case 1: return [2 /*return*/, _a.sent()];
|
671
|
-
}
|
672
|
-
});
|
673
|
-
});
|
552
|
+
async function blobToText(blob) {
|
553
|
+
return await blob.text();
|
674
554
|
}
|
675
555
|
binary.blobToText = blobToText;
|
676
556
|
//#endregion
|
677
557
|
//#region binay utils / text to file
|
678
|
-
function textToFile(text, fileRelativePathOrName) {
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
// console.log({
|
691
|
-
// ext,
|
692
|
-
// blob, file
|
693
|
-
// });
|
694
|
-
// debugger
|
695
|
-
return [2 /*return*/, file];
|
696
|
-
}
|
697
|
-
});
|
698
|
-
});
|
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;
|
699
570
|
}
|
700
571
|
binary.textToFile = textToFile;
|
701
572
|
//#endregion
|
702
573
|
//#region binay utils / file to text
|
703
|
-
function fileToText(file) {
|
704
|
-
return
|
705
|
-
return tslib_1.__generator(this, function (_a) {
|
706
|
-
switch (_a.label) {
|
707
|
-
case 0: return [4 /*yield*/, file.text()];
|
708
|
-
case 1: return [2 /*return*/, _a.sent()];
|
709
|
-
}
|
710
|
-
});
|
711
|
-
});
|
574
|
+
async function fileToText(file) {
|
575
|
+
return await file.text();
|
712
576
|
}
|
713
577
|
binary.fileToText = fileToText;
|
714
578
|
//#endregion
|
715
579
|
//#region binay utils / json to blob
|
716
|
-
function jsonToBlob(jsonObj) {
|
717
|
-
|
718
|
-
|
719
|
-
return tslib_1.__generator(this, function (_a) {
|
720
|
-
blob = new buffer_1.Blob([JSON.stringify(jsonObj, null, 2)], {
|
721
|
-
type: 'application/json',
|
722
|
-
});
|
723
|
-
return [2 /*return*/, blob];
|
724
|
-
});
|
580
|
+
async function jsonToBlob(jsonObj) {
|
581
|
+
const blob = new buffer_1.Blob([JSON.stringify(jsonObj, null, 2)], {
|
582
|
+
type: 'application/json',
|
725
583
|
});
|
584
|
+
return blob;
|
726
585
|
}
|
727
586
|
binary.jsonToBlob = jsonToBlob;
|
728
587
|
//#endregion
|
@@ -730,45 +589,26 @@ var Utils;
|
|
730
589
|
/**
|
731
590
|
* TODO NOT TESTED
|
732
591
|
*/
|
733
|
-
function blobToJson(blob) {
|
734
|
-
return
|
735
|
-
var _a, _b;
|
736
|
-
return tslib_1.__generator(this, function (_c) {
|
737
|
-
switch (_c.label) {
|
738
|
-
case 0:
|
739
|
-
_b = (_a = JSON).parse;
|
740
|
-
return [4 /*yield*/, blob.text()];
|
741
|
-
case 1: return [2 /*return*/, _b.apply(_a, [_c.sent()])];
|
742
|
-
}
|
743
|
-
});
|
744
|
-
});
|
592
|
+
async function blobToJson(blob) {
|
593
|
+
return JSON.parse(await blob.text());
|
745
594
|
}
|
746
595
|
binary.blobToJson = blobToJson;
|
747
596
|
//#endregion
|
748
597
|
//#region binay utils / get blob from url
|
749
|
-
function getBlobFrom(url) {
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
case 0: return [4 /*yield*/, (0, axios_1.default)({
|
755
|
-
url: url,
|
756
|
-
method: 'get',
|
757
|
-
responseType: 'blob',
|
758
|
-
})];
|
759
|
-
case 1:
|
760
|
-
response = _a.sent();
|
761
|
-
return [2 /*return*/, response.data];
|
762
|
-
}
|
763
|
-
});
|
598
|
+
async function getBlobFrom(url) {
|
599
|
+
const response = await (0, axios_1.default)({
|
600
|
+
url,
|
601
|
+
method: 'get',
|
602
|
+
responseType: 'blob',
|
764
603
|
});
|
604
|
+
return response.data;
|
765
605
|
}
|
766
606
|
binary.getBlobFrom = getBlobFrom;
|
767
607
|
//#endregion
|
768
608
|
})(binary = Utils.binary || (Utils.binary = {}));
|
769
609
|
//#endregion
|
770
610
|
//#region css
|
771
|
-
|
611
|
+
let css;
|
772
612
|
(function (css) {
|
773
613
|
//#region css utils / numeric value of pixels
|
774
614
|
/**
|
@@ -777,9 +617,8 @@ var Utils;
|
|
777
617
|
* @returns
|
778
618
|
*/
|
779
619
|
function numValue(pixelsCss) {
|
780
|
-
var _a;
|
781
620
|
// tslint:disable-next-line:radix
|
782
|
-
return parseInt(
|
621
|
+
return parseInt(pixelsCss?.toString()?.replace('px', ''));
|
783
622
|
}
|
784
623
|
css.numValue = numValue;
|
785
624
|
//#endregion
|
@@ -790,19 +629,229 @@ var Utils;
|
|
790
629
|
//#region utils process
|
791
630
|
var UtilsProcess;
|
792
631
|
(function (UtilsProcess) {
|
793
|
-
var _this = this;
|
794
632
|
//#endregion
|
795
633
|
//#region utils process / TODO start async
|
796
634
|
/**
|
797
635
|
* TODO IMPLEMENT
|
798
636
|
* start async node process
|
799
637
|
*/
|
800
|
-
|
801
|
-
//
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
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
|
+
};
|
806
855
|
//#endregion
|
807
856
|
//#region utils process / TODO start sync
|
808
857
|
// /**
|
@@ -818,80 +867,75 @@ var UtilsProcess;
|
|
818
867
|
* condition is met. It is useful for example when you want to start
|
819
868
|
* process and wait until some output is in stdout or stderr.
|
820
869
|
*/
|
821
|
-
UtilsProcess.startAsyncChildProcessCommandUntil =
|
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
|
-
|
876
|
-
|
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,9 +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
|
+
};
|
1499
|
+
//#endregion
|
1342
1500
|
//#region clear
|
1343
|
-
UtilsTerminal.clearConsole =
|
1501
|
+
UtilsTerminal.clearConsole = () => {
|
1344
1502
|
//#region @backendFunc
|
1345
1503
|
index_1.Helpers.msgCacheClear();
|
1346
1504
|
console.log('\x1Bc');
|
@@ -1354,343 +1512,387 @@ var UtilsTerminal;
|
|
1354
1512
|
};
|
1355
1513
|
//#endregion
|
1356
1514
|
//#region transform choices
|
1357
|
-
|
1515
|
+
const transformChoices = (choices) => {
|
1358
1516
|
//#region @backendFunc
|
1359
1517
|
if (!core_imports_1._.isArray(choices) && core_imports_1._.isObject(choices)) {
|
1360
1518
|
choices = Object.keys(choices)
|
1361
|
-
.map(
|
1519
|
+
.map(key => {
|
1362
1520
|
return {
|
1363
1521
|
name: choices[key].name,
|
1364
1522
|
value: key,
|
1365
1523
|
};
|
1366
1524
|
})
|
1367
|
-
.reduce(
|
1525
|
+
.reduce((a, b) => a.concat(b), []);
|
1368
1526
|
}
|
1369
|
-
return choices.map(
|
1527
|
+
return choices.map(c => ({ name: c.name, value: c.value }));
|
1370
1528
|
//#endregion
|
1371
1529
|
};
|
1372
1530
|
//#endregion
|
1373
1531
|
//#region multiselect
|
1374
|
-
UtilsTerminal.multiselect =
|
1375
|
-
|
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
|
-
case 3:
|
1417
|
-
res = _a.sent();
|
1418
|
-
return [2 /*return*/, (Array.isArray(res) ? res : [res])];
|
1419
|
-
}
|
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
|
+
},
|
1420
1573
|
});
|
1421
|
-
|
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
|
+
};
|
1608
|
+
//#endregion
|
1609
|
+
//#region select and execute
|
1422
1610
|
/**
|
1423
1611
|
* Similar to select but executes action if provided
|
1424
1612
|
* @returns selected and executed value
|
1425
1613
|
*/
|
1426
|
-
UtilsTerminal.
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
// console.log(`Response from select: "${res}"`);
|
1451
|
-
// pipeEnterToStdin();
|
1452
|
-
return [2 /*return*/, { selected: res, action: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
|
1453
|
-
switch (_a.label) {
|
1454
|
-
case 0: return [4 /*yield*/, choices[res].action()];
|
1455
|
-
case 1: return [2 /*return*/, _a.sent()];
|
1456
|
-
}
|
1457
|
-
}); }); } }];
|
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,
|
1630
|
+
});
|
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
|
+
}
|
1458
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
|
+
};
|
1652
|
+
//#endregion
|
1653
|
+
//#region select and execute
|
1654
|
+
/**
|
1655
|
+
* Similar to select but executes action if provided
|
1656
|
+
* @returns selected and executed value
|
1657
|
+
*/
|
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,
|
1459
1674
|
});
|
1460
|
-
|
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
|
+
};
|
1461
1692
|
//#endregion
|
1462
1693
|
//#region select
|
1463
|
-
UtilsTerminal.select =
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
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
|
+
};
|
1516
1753
|
//#endregion
|
1517
1754
|
//#region pipe enter to stdin
|
1518
|
-
UtilsTerminal.pipeEnterToStdin =
|
1755
|
+
UtilsTerminal.pipeEnterToStdin = () => {
|
1519
1756
|
//#region @backendFunc
|
1520
1757
|
process.stdin.push('\n');
|
1521
1758
|
//#endregion
|
1522
1759
|
};
|
1523
1760
|
//#endregion
|
1524
1761
|
//#region input
|
1525
|
-
UtilsTerminal.input =
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
// required: _.isNil(required) ? true : required,
|
1547
|
-
})];
|
1548
|
-
case 4:
|
1549
|
-
response = _c.sent();
|
1550
|
-
anwser = response.name;
|
1551
|
-
if (required && !anwser) {
|
1552
|
-
console.warn("Answer is required...");
|
1553
|
-
return [3 /*break*/, 2];
|
1554
|
-
}
|
1555
|
-
return [2 /*return*/, anwser];
|
1556
|
-
case 5:
|
1557
|
-
error_1 = _c.sent();
|
1558
|
-
console.error(error_1);
|
1559
|
-
if (required) {
|
1560
|
-
console.warn("Something went wrong, please try again...");
|
1561
|
-
return [3 /*break*/, 2];
|
1562
|
-
}
|
1563
|
-
else {
|
1564
|
-
return [2 /*return*/, ''];
|
1565
|
-
}
|
1566
|
-
return [3 /*break*/, 6];
|
1567
|
-
case 6: return [3 /*break*/, 2];
|
1568
|
-
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;
|
1569
1783
|
}
|
1570
|
-
|
1571
|
-
|
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
|
+
};
|
1572
1797
|
//#endregion
|
1573
1798
|
//#region confirm
|
1574
|
-
UtilsTerminal.confirm =
|
1575
|
-
|
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,
|
1576
1808
|
// mustAnswerQuestion,
|
1577
|
-
callbackFalse, callbackTrue,
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
case 2:
|
1600
|
-
inquirerToggle = (_b.sent()).default;
|
1601
|
-
return [4 /*yield*/, inquirerToggle({
|
1602
|
-
message: message,
|
1603
|
-
default: defaultValue,
|
1604
|
-
theme: {
|
1605
|
-
style: {
|
1606
|
-
highlight: core_imports_1.chalk.bold.cyan.underline,
|
1607
|
-
},
|
1608
|
-
},
|
1609
|
-
})];
|
1610
|
-
case 3:
|
1611
|
-
answer = _b.sent();
|
1612
|
-
response = {
|
1613
|
-
value: answer,
|
1614
|
-
};
|
1615
|
-
return [3 /*break*/, 11];
|
1616
|
-
case 4:
|
1617
|
-
if (!(options.engine === '@inquirer/prompts')) return [3 /*break*/, 7];
|
1618
|
-
return [4 /*yield*/, Promise.resolve().then(function () { return require('@inquirer/prompts'); })];
|
1619
|
-
case 5:
|
1620
|
-
confirm_1 = (_b.sent()).confirm;
|
1621
|
-
return [4 /*yield*/, confirm_1({
|
1622
|
-
message: message,
|
1623
|
-
default: defaultValue,
|
1624
|
-
})];
|
1625
|
-
case 6:
|
1626
|
-
answer = _b.sent();
|
1627
|
-
response = {
|
1628
|
-
value: answer,
|
1629
|
-
};
|
1630
|
-
return [3 /*break*/, 11];
|
1631
|
-
case 7:
|
1632
|
-
if (!(options.engine === 'prompts')) return [3 /*break*/, 9];
|
1633
|
-
prompts = require('prompts');
|
1634
|
-
return [4 /*yield*/, prompts({
|
1635
|
-
type: 'toggle',
|
1636
|
-
name: 'value',
|
1637
|
-
message: message,
|
1638
|
-
initial: defaultValue,
|
1639
|
-
active: 'yes',
|
1640
|
-
inactive: 'no',
|
1641
|
-
})];
|
1642
|
-
case 8:
|
1643
|
-
response = _b.sent();
|
1644
|
-
return [3 /*break*/, 11];
|
1645
|
-
case 9:
|
1646
|
-
if (!(options.engine === 'enquirer')) return [3 /*break*/, 11];
|
1647
|
-
Select = require('enquirer').Select;
|
1648
|
-
choices = defaultValue ? ['yes', 'no'] : ['no', 'yes'];
|
1649
|
-
prompt_1 = new Select({
|
1650
|
-
name: 'question',
|
1651
|
-
message: message,
|
1652
|
-
choices: choices,
|
1653
|
-
});
|
1654
|
-
_a = {};
|
1655
|
-
return [4 /*yield*/, prompt_1.run()];
|
1656
|
-
case 10:
|
1657
|
-
response = (_a.value = (_b.sent()) === 'yes',
|
1658
|
-
_a);
|
1659
|
-
_b.label = 11;
|
1660
|
-
case 11:
|
1661
|
-
if (!response.value) return [3 /*break*/, 14];
|
1662
|
-
if (!callbackTrue) return [3 /*break*/, 13];
|
1663
|
-
return [4 /*yield*/, index_1.Helpers.runSyncOrAsync({ functionFn: callbackTrue })];
|
1664
|
-
case 12:
|
1665
|
-
_b.sent();
|
1666
|
-
_b.label = 13;
|
1667
|
-
case 13: return [3 /*break*/, 16];
|
1668
|
-
case 14:
|
1669
|
-
if (!callbackFalse) return [3 /*break*/, 16];
|
1670
|
-
return [4 /*yield*/, index_1.Helpers.runSyncOrAsync({ functionFn: callbackFalse })];
|
1671
|
-
case 15:
|
1672
|
-
_b.sent();
|
1673
|
-
_b.label = 16;
|
1674
|
-
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
|
+
};
|
1675
1831
|
}
|
1676
|
-
|
1677
|
-
|
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
|
+
};
|
1678
1880
|
//#endregion
|
1679
1881
|
//#region press any key to continue
|
1680
|
-
UtilsTerminal.pressAnyKeyToContinueAsync =
|
1882
|
+
UtilsTerminal.pressAnyKeyToContinueAsync = (options) => {
|
1681
1883
|
//#region @backendFunc
|
1682
1884
|
options = options || {};
|
1683
1885
|
options.message =
|
1684
1886
|
options.message || core_imports_1.chalk.bold('Press any key to continue...');
|
1685
|
-
|
1686
|
-
|
1687
|
-
return new Promise(
|
1688
|
-
|
1887
|
+
const { message } = options;
|
1888
|
+
const readline = require('readline');
|
1889
|
+
return new Promise(resolve => {
|
1890
|
+
const rl = readline.createInterface({
|
1689
1891
|
input: process.stdin,
|
1690
1892
|
output: process.stdout,
|
1691
1893
|
});
|
1692
1894
|
// Prompt user with the question
|
1693
|
-
rl.question(message,
|
1895
|
+
rl.question(message, answer => {
|
1694
1896
|
rl.close();
|
1695
1897
|
resolve(answer);
|
1696
1898
|
});
|
@@ -1702,18 +1904,18 @@ var UtilsTerminal;
|
|
1702
1904
|
/**
|
1703
1905
|
* @deprecated use UtilsTerminal.pressAnyKeyToContinueAsync()
|
1704
1906
|
*/
|
1705
|
-
UtilsTerminal.pressAnyKey =
|
1907
|
+
UtilsTerminal.pressAnyKey = (options) => {
|
1706
1908
|
//#region @backendFunc
|
1707
1909
|
options = options || {};
|
1708
1910
|
options.message = options.message || 'Press any key to continue...';
|
1709
|
-
|
1911
|
+
const { message } = options;
|
1710
1912
|
if (process.platform === 'win32') {
|
1711
|
-
|
1913
|
+
const terminal = UtilsProcess.getBashOrShellName();
|
1712
1914
|
// console.log({ terminal });
|
1713
1915
|
if (terminal === 'gitbash') {
|
1714
|
-
|
1916
|
+
const getGitBashPath = UtilsProcess.getGitBashPath();
|
1715
1917
|
// console.log({ getGitBashPath });
|
1716
|
-
|
1918
|
+
const gitbashCommand = `read -p "${core_imports_1.chalk.bold(message)}"`;
|
1717
1919
|
core_imports_3.child_process.execSync(gitbashCommand, {
|
1718
1920
|
stdio: 'inherit',
|
1719
1921
|
shell: getGitBashPath,
|
@@ -1734,41 +1936,25 @@ var UtilsTerminal;
|
|
1734
1936
|
};
|
1735
1937
|
//#endregion
|
1736
1938
|
//#region preview long list as select
|
1737
|
-
UtilsTerminal.previewLongList =
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
}
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
//#region @backendFunc
|
1749
|
-
if (!Array.isArray(list)) {
|
1750
|
-
list = list.split('\n');
|
1751
|
-
}
|
1752
|
-
choices = list.reduce(function (a, b) {
|
1753
|
-
var _a;
|
1754
|
-
return core_imports_1._.merge(a, (_a = {},
|
1755
|
-
_a[b] = {
|
1756
|
-
name: b,
|
1757
|
-
// action: () => {},
|
1758
|
-
},
|
1759
|
-
_a));
|
1760
|
-
}, {});
|
1761
|
-
return [4 /*yield*/, UtilsTerminal.selectActionAndExecute(choices, {
|
1762
|
-
autocomplete: true,
|
1763
|
-
question: listName,
|
1764
|
-
hint: 'Press enter to return',
|
1765
|
-
})];
|
1766
|
-
case 1:
|
1767
|
-
_a.sent();
|
1768
|
-
return [2 /*return*/];
|
1769
|
-
}
|
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
|
+
},
|
1770
1950
|
});
|
1951
|
+
}, {});
|
1952
|
+
await UtilsTerminal.selectActionAndExecute(choices, {
|
1953
|
+
autocomplete: true,
|
1954
|
+
question: listName,
|
1955
|
+
hint: 'Press enter to return',
|
1771
1956
|
});
|
1957
|
+
//#endregion
|
1772
1958
|
};
|
1773
1959
|
//#endregion
|
1774
1960
|
//#region preview long list with 'less' (git log like)
|
@@ -1779,26 +1965,26 @@ var UtilsTerminal;
|
|
1779
1965
|
* @param {string} list - The long string content to display.
|
1780
1966
|
* @returns {Promise<void>} A Promise that resolves when the pager exits.
|
1781
1967
|
*/
|
1782
|
-
UtilsTerminal.previewLongListGitLogLike =
|
1968
|
+
UtilsTerminal.previewLongListGitLogLike = (list) => {
|
1783
1969
|
//#region @backendFunc
|
1784
1970
|
if (Array.isArray(list)) {
|
1785
1971
|
list = list.join('\n');
|
1786
1972
|
}
|
1787
|
-
return new Promise(
|
1788
|
-
|
1973
|
+
return new Promise((resolve, reject) => {
|
1974
|
+
const less = (0, core_imports_3.spawn)('less', [], {
|
1789
1975
|
stdio: ['pipe', process.stdout, process.stderr],
|
1790
1976
|
});
|
1791
1977
|
less.stdin.write(list); // Write the list content to the less process
|
1792
1978
|
less.stdin.end(); // Signal that writing is complete
|
1793
|
-
less.on('close',
|
1979
|
+
less.on('close', code => {
|
1794
1980
|
if (code === 0) {
|
1795
1981
|
resolve(void 0);
|
1796
1982
|
}
|
1797
1983
|
else {
|
1798
|
-
reject(new Error(
|
1984
|
+
reject(new Error(`less process exited with code ${code}`));
|
1799
1985
|
}
|
1800
1986
|
});
|
1801
|
-
less.on('error',
|
1987
|
+
less.on('error', err => {
|
1802
1988
|
reject(err);
|
1803
1989
|
});
|
1804
1990
|
});
|