1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function __export(m) {
|
19 | for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
20 | }
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const child_process_1 = require("child_process");
|
23 | const _ = require("lodash");
|
24 | const crypto = require("crypto");
|
25 | const fsExtra = require("fs-extra");
|
26 | const path = require("path");
|
27 | const moment = require("moment-timezone");
|
28 | const util = require("util");
|
29 | const UUID = require("uuid");
|
30 | const UUID_v5 = require("uuid/v5");
|
31 | const yargs = require("yargs-parser");
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function applyFuncFor(func, thisArg) {
|
41 | return function () {
|
42 | return func.apply(thisArg, arguments);
|
43 | };
|
44 | }
|
45 | exports.applyFuncFor = applyFuncFor;
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function asArray(val, noEmpty = true) {
|
55 | if (!Array.isArray(val)) {
|
56 | val = [val];
|
57 | }
|
58 | return val.filter(x => {
|
59 | if (noEmpty) {
|
60 | return !_.isNil(x);
|
61 | }
|
62 | return true;
|
63 | });
|
64 | }
|
65 | exports.asArray = asArray;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function asLocal(time) {
|
74 | let result = asMoment(time);
|
75 | if (!_.isNil(result)) {
|
76 | if (result.isValid()) {
|
77 | if (!result.isLocal()) {
|
78 | result = result.local();
|
79 | }
|
80 | }
|
81 | }
|
82 | return result;
|
83 | }
|
84 | exports.asLocal = asLocal;
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function asMoment(val) {
|
93 | if (_.isNil(val)) {
|
94 | return val;
|
95 | }
|
96 | let result;
|
97 | if (moment.isMoment(val)) {
|
98 | result = val;
|
99 | }
|
100 | else if (moment.isDate(val)) {
|
101 | result = moment(val);
|
102 | }
|
103 | else {
|
104 | const TIME_STR = toStringSafe(val).trim();
|
105 | if (/^([0-9]{1,})$/i.test(TIME_STR)) {
|
106 |
|
107 | result = moment(parseInt(TIME_STR));
|
108 | }
|
109 | else {
|
110 | result = moment(TIME_STR);
|
111 | }
|
112 | }
|
113 | return result;
|
114 | }
|
115 | exports.asMoment = asMoment;
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function asUTC(time) {
|
124 | let result = asMoment(time);
|
125 | if (!_.isNil(result)) {
|
126 | if (result.isValid()) {
|
127 | if (!result.isUTC()) {
|
128 | result = result.utc();
|
129 | }
|
130 | }
|
131 | }
|
132 | return result;
|
133 | }
|
134 | exports.asUTC = asUTC;
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function cloneObj(obj) {
|
143 | if (!obj) {
|
144 | return obj;
|
145 | }
|
146 | return JSON.parse(JSON.stringify(obj));
|
147 | }
|
148 | exports.cloneObj = cloneObj;
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | function compareValues(x, y) {
|
158 | return compareValuesBy(x, y, i => i);
|
159 | }
|
160 | exports.compareValues = compareValues;
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function compareValuesBy(x, y, selector) {
|
171 | const VAL_X = selector(x);
|
172 | const VAL_Y = selector(y);
|
173 | if (VAL_X !== VAL_Y) {
|
174 | if (VAL_X < VAL_Y) {
|
175 | return -1;
|
176 | }
|
177 | if (VAL_X > VAL_Y) {
|
178 | return 1;
|
179 | }
|
180 | }
|
181 | return 0;
|
182 | }
|
183 | exports.compareValuesBy = compareValuesBy;
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | function createCompletedAction(resolve, reject) {
|
193 | let completedInvoked = false;
|
194 | return (err, result) => {
|
195 | if (completedInvoked) {
|
196 | return;
|
197 | }
|
198 | completedInvoked = true;
|
199 | if (err) {
|
200 | if (reject) {
|
201 | reject(err);
|
202 | }
|
203 | }
|
204 | else {
|
205 | if (resolve) {
|
206 | resolve(result);
|
207 | }
|
208 | }
|
209 | };
|
210 | }
|
211 | exports.createCompletedAction = createCompletedAction;
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | async function exec(command) {
|
220 | command = toStringSafe(command);
|
221 | return new Promise((resolve, reject) => {
|
222 | try {
|
223 | child_process_1.exec(command, (err, stdout, stderr) => {
|
224 | if (err) {
|
225 | reject(err);
|
226 | }
|
227 | else {
|
228 | resolve({
|
229 | stderr: stderr,
|
230 | stdout: stdout,
|
231 | });
|
232 | }
|
233 | });
|
234 | }
|
235 | catch (e) {
|
236 | reject(e);
|
237 | }
|
238 | });
|
239 | }
|
240 | exports.exec = exec;
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | async function forEachAsync(seq, action) {
|
248 | let i = -1;
|
249 | for (const ITEM of seq) {
|
250 | ++i;
|
251 | await Promise.resolve(action(ITEM, i));
|
252 | }
|
253 | }
|
254 | exports.forEachAsync = forEachAsync;
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | function getAppVersionSync(opts) {
|
263 | if (!opts) {
|
264 | opts = {};
|
265 | }
|
266 |
|
267 | let cwd = toStringSafe(opts.cwd);
|
268 | if (isEmptyString(cwd)) {
|
269 | cwd = process.cwd();
|
270 | }
|
271 | if (!path.isAbsolute(cwd)) {
|
272 | cwd = path.join(process.cwd(), cwd);
|
273 | }
|
274 | cwd = path.resolve(cwd);
|
275 | const VERSION = {};
|
276 |
|
277 | try {
|
278 | VERSION.date = asUTC(moment(child_process_1.execSync('git log -n1 --pretty=%cI HEAD', {
|
279 | cwd: cwd,
|
280 | }).toString('utf8')
|
281 | .trim()));
|
282 | }
|
283 | catch {
|
284 | VERSION.date = false;
|
285 | }
|
286 |
|
287 | try {
|
288 | VERSION.hash = normalizeString(child_process_1.execSync('git log --pretty="%H" -n1 HEAD', {
|
289 | cwd: cwd,
|
290 | }).toString('utf8'));
|
291 | }
|
292 | catch {
|
293 | VERSION.hash = false;
|
294 | }
|
295 |
|
296 | try {
|
297 | let buildNr = false;
|
298 |
|
299 | const BUILD_NR_FILE = path.resolve(path.join(cwd, 'BUILD_NR'));
|
300 | if (fsExtra.existsSync(BUILD_NR_FILE)) {
|
301 | if (fsExtra.statSync(BUILD_NR_FILE).isFile()) {
|
302 | buildNr = parseInt(fsExtra.readFileSync(BUILD_NR_FILE, 'utf8')
|
303 | .trim());
|
304 | }
|
305 | }
|
306 | if (false === buildNr) {
|
307 |
|
308 | const BUILD_NR = toStringSafe(process.env.BUILD_NR)
|
309 | .trim();
|
310 | if ('' !== BUILD_NR) {
|
311 | buildNr = parseInt(BUILD_NR);
|
312 | }
|
313 | }
|
314 | if (false !== buildNr) {
|
315 | VERSION.code = isNaN(buildNr) ?
|
316 | false : buildNr;
|
317 | }
|
318 | }
|
319 | catch (e) {
|
320 | VERSION.code = false;
|
321 | }
|
322 |
|
323 | try {
|
324 | const PACKAGE_JSON_FILE = path.resolve(path.join(cwd, 'package.json'));
|
325 | if (fsExtra.existsSync(PACKAGE_JSON_FILE)) {
|
326 | if (fsExtra.statSync(PACKAGE_JSON_FILE).isFile()) {
|
327 | const PACKAGE_JSON = JSON.parse(fsExtra.readFileSync(PACKAGE_JSON_FILE, 'utf8')
|
328 | .trim());
|
329 | if (PACKAGE_JSON && !isEmptyString(PACKAGE_JSON.version)) {
|
330 | VERSION.name = toStringSafe(PACKAGE_JSON.version)
|
331 | .trim();
|
332 | }
|
333 | }
|
334 | }
|
335 | }
|
336 | catch (e) {
|
337 | VERSION.name = false;
|
338 | }
|
339 | return VERSION;
|
340 | }
|
341 | exports.getAppVersionSync = getAppVersionSync;
|
342 |
|
343 |
|
344 |
|
345 | function guid(version) {
|
346 | return uuid.apply(null, arguments);
|
347 | }
|
348 | exports.guid = guid;
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | function isEmptyString(val) {
|
357 | return '' === toStringSafe(val)
|
358 | .trim();
|
359 | }
|
360 | exports.isEmptyString = isEmptyString;
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | function normalizeString(val) {
|
369 | return toStringSafe(val).toLowerCase()
|
370 | .trim();
|
371 | }
|
372 | exports.normalizeString = normalizeString;
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | function now(timezone) {
|
381 | timezone = toStringSafe(timezone).trim();
|
382 | const NOW = moment();
|
383 | return '' === timezone ? NOW
|
384 | : NOW.tz(timezone);
|
385 | }
|
386 | exports.now = now;
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 | function parseCommandLine(cmd) {
|
395 | cmd = toStringSafe(cmd).trim();
|
396 | const ARGS = yargs(cmd);
|
397 | cmd = normalizeString(ARGS._[0]);
|
398 | ARGS._ = ARGS._.filter((a, i) => i > 0).map((a) => {
|
399 | if (_.isString(a)) {
|
400 | if (a.startsWith('"') && a.endsWith('"')) {
|
401 | a = a.substr(1, a.length - 2);
|
402 | a = a.split('\\"')
|
403 | .join('"');
|
404 | }
|
405 | }
|
406 | return a;
|
407 | });
|
408 | if ('' === cmd) {
|
409 | cmd = undefined;
|
410 | }
|
411 | return {
|
412 | arguments: ARGS,
|
413 | command: cmd,
|
414 | };
|
415 | }
|
416 | exports.parseCommandLine = parseCommandLine;
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | async function randChars(size, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
|
426 | return randCharsInner(await util.promisify(crypto.randomBytes)(size), chars);
|
427 | }
|
428 | exports.randChars = randChars;
|
429 | function randCharsInner(randBlob, chars) {
|
430 | chars = toStringSafe(chars);
|
431 | let str = '';
|
432 | for (let i = 0; i < randBlob.length; i++) {
|
433 | str += chars.substr(randBlob.readInt8(i) % chars.length, 1);
|
434 | }
|
435 | return str;
|
436 | }
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 | function randCharsSync(size, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
|
446 | return randCharsInner(crypto.randomBytes(size), chars);
|
447 | }
|
448 | exports.randCharsSync = randCharsSync;
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 | function toBooleanSafe(val, defaultValue = false) {
|
458 | if (_.isBoolean(val)) {
|
459 | return val;
|
460 | }
|
461 | if (_.isNil(val)) {
|
462 | return !!defaultValue;
|
463 | }
|
464 | return !!val;
|
465 | }
|
466 | exports.toBooleanSafe = toBooleanSafe;
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | function toStringSafe(val, defaultValue = '') {
|
476 | if (_.isString(val)) {
|
477 | return val;
|
478 | }
|
479 | if (_.isNil(val)) {
|
480 | return '' + defaultValue;
|
481 | }
|
482 | if (val instanceof Error) {
|
483 | return `[${val.name}] ${val.message}${_.isNil(val.stack) ? '' : ("\n\n" + val.stack)}`;
|
484 | }
|
485 | if (_.isFunction(val['toString'])) {
|
486 | return '' + val.toString();
|
487 | }
|
488 | if (Array.isArray(val) || _.isObjectLike(val)) {
|
489 | return JSON.stringify(val);
|
490 | }
|
491 | return '' + val;
|
492 | }
|
493 | exports.toStringSafe = toStringSafe;
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 | function utc() {
|
500 | return moment.utc();
|
501 | }
|
502 | exports.utc = utc;
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 | function uuid(version, ...args) {
|
512 | version = normalizeString(version);
|
513 | switch (version) {
|
514 | case '':
|
515 | case '4':
|
516 | case 'v4':
|
517 | return UUID.v4
|
518 | .apply(null, args);
|
519 | case '1':
|
520 | case 'v1':
|
521 | return UUID.v1
|
522 | .apply(null, args);
|
523 | case '5':
|
524 | case 'v5':
|
525 | return UUID_v5.apply(null, args);
|
526 | }
|
527 | throw new Error(`Version '${version}' is not supported`);
|
528 | }
|
529 | exports.uuid = uuid;
|
530 | __export(require("./apis/host"));
|
531 | __export(require("./apis/index"));
|
532 | __export(require("./apis/statistics"));
|
533 | __export(require("./apis/validation"));
|
534 | __export(require("./azure/storage"));
|
535 | __export(require("./cache"));
|
536 | __export(require("./cache/redis"));
|
537 | __export(require("./dev"));
|
538 | __export(require("./diagnostics/logger"));
|
539 | __export(require("./diagnostics/stopwatch"));
|
540 | __export(require("./env"));
|
541 | __export(require("./events"));
|
542 | __export(require("./fs"));
|
543 | __export(require("./geo"));
|
544 | __export(require("./http"));
|
545 | __export(require("./http/websockets"));
|
546 | __export(require("./mail"));
|
547 | __export(require("./mongo/index"));
|
548 | __export(require("./mongo/statistics"));
|
549 | __export(require("./oauth/microsoft"));
|
550 | __export(require("./queues/index"));
|
551 | __export(require("./schemas"));
|
552 | __export(require("./services/feedback"));
|
553 | __export(require("./statistics"));
|
554 | __export(require("./streams"));
|
555 | __export(require("./strings"));
|
556 | __export(require("./system"));
|
557 | __export(require("./zip/builder"));
|
558 | var swagger_jsdoc_express_1 = require("swagger-jsdoc-express");
|
559 | exports.setupSwaggerUIFromSourceFiles = swagger_jsdoc_express_1.setupSwaggerUIFromSourceFiles;
|
560 |
|
\ | No newline at end of file |