-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
790 lines (699 loc) · 19.1 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
import * as process from 'process';
import * as fs from 'fs';
import * as path from 'path';
import { exec } from 'child_process';
import * as glob from 'glob';
import * as http from 'http';
import * as https from 'https';
import * as moment from 'moment';
import * as JSON5 from 'json5';
import Log from './logger';
export var NOW : moment.Moment;
export var TAG : string;
export var hasErrors : boolean = false;
export const tagFormat = 'YYYYMMDD_HHmm';
export const tagPattern = '????????_????';
export const tagPatternRegex = '[0-9]{8}_[0-9]{4}';
export function init(p:{log:Log, tag?:string}) : void
{
TAG = ( (p.tag != null) ? p.tag : moment(new Date()).format(tagFormat) );
NOW = moment( TAG, tagFormat ); // NB: 'NOW' is trimmed of the 'seconds' part
p.log.log( 'TAG:', TAG, NOW.toISOString() );
let exitHandler = function(options,err)
{
console.log(p.log.getLines().join('\n'));
switch( options.mode )
{
case "on exit":
if( hasErrors )
process.exit( -1 );
return;
case "on uncaughtException":
console.log( '*** uncaughtException', err );
process.exit( -1 );
default:
console.log( '*** Unknown exit mode', options, err );
process.exit( -1 );
}
};
process.on( 'exit', exitHandler.bind(null, {mode:'on exit'}) );
process.on( 'uncaughtException', exitHandler.bind(null,{mode:'on uncaughtException'}) );
if( typeof(window) === 'undefined' )
{
p.log.log( 'In NodeJS application => set up a fake DOM/JQuery/Knockout environment' );
// https://stackoverflow.com/questions/1801160/can-i-use-jquery-with-node-js
const jsdom = require( 'jsdom' );
const jquery = require( 'jquery' );
const knockout = require( 'knockout' );
const dom = new jsdom.JSDOM( '<html><body></body></html>' );
(<any>global).window = dom.window;
(<any>global).document = dom.window.document;
(<any>global).$ = jquery( window );
(<any>global).ko = knockout;
}
}
export function setHasErrors() : void
{
hasErrors = true;
}
/** Syntax hack for use in "xxx ?? throwError('error')" situations */
export function throwError(message: string, log?: Log): never {
throw new Error(message);
}
export function sleep(ms:number) : Promise<void>
{
return new Promise( callback=>setTimeout(callback, ms) );
}
export function run(p:{ log:Log, command:string, logstds?:boolean, stdin?:string, [key:string]:any }) : Promise<{stdout:string,stderr:string}>
{
p.log.log( 'Create command' );
let command = p.command;
Object.keys(p).forEach( function(key)
{
switch( key )
{
case 'log':
case 'command':
case 'logstds':
case 'stdin':
// Regular parameter
return;
}
// Command's parameter
command = command.replace( '{'+key+'}', p[key] );
});
function logStds(stdout:string, stderr:string) : void
{
p.log.child( 'stdout' ).logLines( stdout );
p.log.child( 'stderr' ).logLines( stderr );
}
return new Promise<{stdout:string,stderr:string}>( function(resolve, reject)
{
p.log.log( 'launch:', command );
const ps = exec( command, function(err, stdout, stderr)
{
if( err != null )
{
logStds( stdout, stderr );
reject( err ); // i.e. promise's 'throw'
return;
}
if( (p.logstds == null) || (p.logstds == true) )
logStds( stdout, stderr );
p.log.log( 'exited' );
resolve({ stdout:stdout, stderr:stderr });
} );
if( p.stdin != null )
{
p.log.log( `Write '${p.stdin.length}' characters to stdin` );
const rc = ps.stdin.write( p.stdin );
ps.stdin.end();
p.log.log( `Write rc='${rc}'` );
}
} );
}
export function ls(path:string) : Promise<string[]>
{
return new Promise<string[]>( (resolve,reject)=>
{
fs.readdir( path, (err,items)=>
{
if( err != null )
reject( err );
else
resolve( items );
} );
} );
}
export async function dirPattern(p:{ log:Log, dir:string, pattern:string, remoteServer?:string }) : Promise<string[]>
{
if( p.remoteServer == null )
{
// Simple local search
p.log.log( 'Local search:', path.join(p.dir, p.pattern) );
return new Promise<string[]>( (resolve, reject)=>
{
glob.Glob( p.pattern, { cwd:p.dir }, (err,files)=>
{
if( err != null )
{
p.log.log( 'Error:', err );
reject( err );
}
else
{
// p.log.log( 'Found:', files );
p.log.log( 'Found', files.length, 'entries' );
resolve( files );
}
} );
} );
}
else
{
// Use SSH
p.log.log( 'Remote search:', path.join(p.dir, p.pattern), 'on server', p.remoteServer );
try
{
const dirPattern = path.join( p.dir, p.pattern );
const {stdout,stderr} = await run({ log:p.log, logstds:false, command:'ssh "{HOSTNAME}" ls -d "{DIR_PATTERN}"', 'HOSTNAME':p.remoteServer, 'DIR_PATTERN':dirPattern });
const list = stdout.split( '\n' );
return list.map( str=>path.basename(str) ).filter( str=> (str != null) && (str.length > 0) );
}
catch
{
// e.g. no such file or directory ...
return [];
}
}
}
type DirNameOrPath = { path :string, dir?:never, name?:never }
| { path?:never, dir :string,name :string };
export function stat(p:DirNameOrPath) : Promise<fs.Stats>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
return new Promise<fs.Stats>( (resolve,reject)=>
{
fs.stat( path_, (err, stats)=>
{
if( err )
reject( err );
else
resolve( stats );
} )
} );
}
export function exists(p:DirNameOrPath) : Promise<boolean>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
return new Promise<boolean>( (resolve)=>
{
fs.stat( path_, (err, stats)=>
{
resolve( (err == null) ? true : false );
} )
} );
}
export function mv(p:{ srcPath:string, dstPath:string }) : Promise<void>
{
return new Promise<void>( (resolve,reject)=>
{
fs.rename( p.srcPath, p.dstPath, (err)=>
{
(err == null) ? resolve() : reject(err);
} );
} );
}
export function mkdir(p:DirNameOrPath) : Promise<void>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
return new Promise<void>( (resolve,reject)=>
{
fs.mkdir( path_, (err)=>
{
(err == null) ? resolve() : reject(err);
} );
} )
}
export function rm(p:DirNameOrPath) : Promise<void>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
return new Promise<void>( (resolve,reject)=>
{
fs.unlink( path_, (err)=>
{
if( err != null )
reject( err );
else
resolve();
} );
} );
}
export function rmdir(p:DirNameOrPath) : Promise<void>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
return new Promise<void>( (resolve,reject)=>
{
fs.rmdir( path_, (err)=>
{
(err == null) ? resolve() : reject(err);
} );
} );
}
export async function rmrf(p:DirNameOrPath) : Promise<void>
{
const path_ = (p.path != null) ? p.path : path.join( p.dir, p.name );
const stat_ = await stat( p );
if( stat_.isDirectory() )
{
// Directory
const items = await ls( path_ );
for( let i=0; i<items.length; ++i )
{
// Recurse
const item = items[ i ];
await rmrf({ dir:path_, name:item });
}
// This one
await rmdir( p );
}
else
{
// File
await rm( p );
}
}
export async function readFile(p:{ filePath:string }) : Promise<string>
{
return new Promise<string>( (resolve,reject)=>
{
fs.readFile( p.filePath, 'utf8', (err,content)=>
{
if( err )
reject( err );
else
resolve( content );
} );
} );
}
export async function writeFile(p:{ filePath:string, stringContent:string }) : Promise<void>
{
return new Promise<void>( (resolve,reject)=>
{
fs.writeFile( p.filePath, p.stringContent, (err)=>
{
if( err )
reject( err );
else
resolve();
} );
} );
}
/** https://json5.org/ */
export async function readJSON<T>(p:{ filePath?:string, jsonText?:string }) : Promise<T>
{
let jsonText = p.jsonText;
if( p.filePath != null )
jsonText = await readFile({ filePath:p.filePath });
if( jsonText == null )
throw `'readJSON()': Missing JSON source`;
return JSON5.parse( jsonText );
}
export async function writeJSON(p:{ filePath:string, content:any }) : Promise<void>
{
const json = JSON.stringify( p.content, null, '\t' );
await writeFile({ filePath:p.filePath, stringContent:json });
}
/** https://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable-string */
export function humanFileSize(bytes?:number, si?:boolean) : string|null
{
if( bytes == null )
return null;
if( si == null )
si = false;
const thresh = si ? 1000 : 1024;
if( Math.abs(bytes) < thresh )
return bytes + ' B';
const units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
let u = -1;
do
{
bytes /= thresh;
++u;
} while( Math.abs(bytes) >= thresh && u < units.length - 1 );
return bytes.toFixed(1)+' '+units[u];
}
export async function readFileLines(filePath:string) : Promise<string[]>
{
const buffer = await fs.promises.readFile( filePath );
const txt = buffer.toString();
const lines = txt.match( /[^\r\n]+/g ) ?? [];
return lines;
}
export function isNullOrWhiteSpace(str?:string) : boolean
{
if( str == null )
return true;
if( str.length == 0 )
return true;
if( str.trim() == '' )
return true;
return false;
}
export async function forEach<T>(t:T[], callback:(item:T,i:number)=>Promise<void>) : Promise<void>
{
for( let i=0; i<t.length; ++i )
await callback( t[i], i );
}
export function constArray<T extends string>(a: Array<T>): T[] {
return a;
}
export function arraySum<T>(a:T[], f:(e:T)=>number) : number
{
let rc = 0;
a.forEach( function(e)
{
rc += f(e);
} );
return rc;
}
export function arrayToDictionary<T>(a:T[], keyCast:(v:T)=>string) : {[key:string]:T}
{
const dict : {[key:string]:T} = {};
a.forEach( v=>
{
dict[ keyCast(v) ] = v
} );
return dict;
}
/** Split the specified array into multiple arrays according to a grouping key */
export function arrayToListOfArrays<T>(a:T[], groupingKeyCast:(v:T)=>string) : T[][]
{
// Group items into an object "key->T[]"
const grouped = a.reduce<{[key:string]:T[]}>( (accu,current)=>
{
const key = groupingKeyCast( current );
let list = accu[ key ];
if( list == null )
{
list = [];
accu[ key ] = list;
}
list.push( current );
return accu;
}, {} );
// From the object, create arrays
const list : T[][] = [];
for( let key in grouped )
list.push( grouped[key] );
return list;
}
/** Throttles the concurrent execution of Promises (e.g. the reduce the number of concurrent requests to a server) */
export class TasksThrotther
{
private readonly limit : number;
private runnings : number = 0;
private throttled : (()=>void)[] = [];
constructor(limit:number)
{
this.limit = limit;
}
/** nb: all the magic is here... */
public async do<T>(callback:()=>Promise<T>) : Promise<T>
{
const self = this;
++ self.runnings;
if( self.runnings <= self.limit )
{
// Execute immediately
const rc = await callback();
self.checkNext();
return rc;
}
// Push a promise in 'throttled' & wait for it
const waitFor = new Promise<void>( (resolve)=>
{
self.throttled.push( resolve );
} );
await waitFor;
// Now we can execute
const rc = await callback();
self.checkNext();
return rc;
}
private checkNext() : void
{
const self = this;
-- self.runnings;
const next = self.throttled.shift();
if( next != null )
next();
}
}
export namespace url
{
/** Transform a dictionary like {foo:'bar',hello:'world'} to a parameters string like 'foo=bar&hello=world' */
export function stringifyParameters(parms:{[key:string]:any}) : string
{
var pairs = <string[]>[];
Object.keys(parms).forEach( function(key)
{
let value = parms[ key ];
key = encodeURIComponent( key );
if( (value == null) || (typeof(value) === 'string') || (typeof(value) === 'number') || (typeof(value) === 'boolean') )
{/*Keep as-is*/}
else
// Convert to JSON
value = JSON.stringify( value );
value = encodeURIComponent( value );
pairs.push( key+"="+value );
} );
return pairs.join( '&' );
}
export function getRequest(url:string, request?:{[key:string]:any}) : Promise<string>
{
if( request != null )
{
const parms = stringifyParameters( request );
url = `${url}?${parms}`;
}
const ht = url.startsWith('https:') ? https : http;
let data = '';
return new Promise<string>( (resolve,reject)=>
{
ht.get( url, (resp)=>
{
resp.on( 'data', (chunk)=>
{
data += chunk;
} );
resp.on( 'end', ()=>
{
if( resp.statusCode != 200 ) // HTTP OK
reject( `Request failed with status code ${resp.statusCode}` );
else
resolve( data );
} );
} )
.on( 'error', (err)=>
{
reject( err );
} );
} );
}
// nb: ES5 incompatible ; requires "Promise" library
export function postRequest<T>(url:string, request:{[key:string]:any}) : Promise<T>
{
let requestStr = JSON.stringify( request );
return new Promise<T>( (resolve,reject)=>
{
$.ajax({ type : 'POST',
url : url,
contentType : 'application/json',
data : requestStr,
dataType : 'json',
success : (data,textStatus,jqXHR)=>resolve( data ),
error : (jqXHR,textStatus,errorThrown)=>
{
reject( textStatus );
}
});
} );
}
}
export namespace html
{
/** TODO ! */
export function showError(message:string) : void
{
console.error( message );
}
/** TODO ! */
export function showMessage(message:string) : void
{
alert( message );
}
/** Invoke jQuery.blockUI's '.block()' on the specified element but supports multiple invokation on the same element */
export function block($e:JQuery) : JQuery
{
// Insert/increment a block counter as jQuery 'data()'
var blockCounter = ( $e.data('common_blockCounter')|0 ) + 1;
$e.data( 'common_blockCounter', blockCounter );
if( blockCounter == 1 )
// This element is not blocked yet
(<any>$e).block(); // TODO: ACA: jQuery.blockUI typings ...
return $e;
}
/** Invoke jQuery.blockUI's '.unblock()' on the specified element except if it has been block()ed more than once */
export function unblock($e:JQuery) : JQuery
{
// Decrement the block counter in the jQuery 'data()'
var blockCounter = ( $e.data('common_blockCounter')|0 ) - 1;
$e.data( 'common_blockCounter', blockCounter );
if( blockCounter < 0 )
{
// There is a logic error somewhere...
showError( 'INTERNAL ERROR: Unblock count > block count: '+blockCounter );
// Reset counter
blockCounter = 0;
$e.data( 'common_blockCounter', 0 );
}
if( blockCounter == 0 )
// This element is no more blocked by anything else
(<any>$e).unblock(); // TODO: ACA: jQuery.blockUI typings ...
return $e;
}
export function contextMenu($triggerControl:JQuery, items:{label:string,callback:()=>void}[]) : void
{
$triggerControl.contextmenu( ()=>
{
let clickHandler : (evt:any)=>void = <any>null;
let closeMe : ()=>void = <any>null;
const $popup = $('<div style="z-index:999;position:absolute;padding:1px;background-color:white;border:1px solid black"></div>');
items.forEach( item=>
{
var $item = $('<div style="cursor:pointer;white-space:nowrap"/>')
.text( item.label )
.click( ()=>
{
closeMe();
item.callback();
} );
$popup.append( $item );
} );
$popup.insertAfter( $triggerControl );
closeMe = ()=>
{
$popup.remove();
// Deactivate global click handler
$(document).unbind( 'mouseup', clickHandler );
};
clickHandler = function(evt)
{
if( (! $popup.is(evt.target))
&& ($popup.has(evt.target).length == 0) )
{
// Click not inside the popup
if( ($triggerControl.is(evt.target))
|| ($triggerControl.has(evt.target).length != 0) )
// Click inside the triggering button => Discard
return;
closeMe();
}
};
// Activate global click handler
$(document).mouseup( clickHandler );
} );
}
export class DropDownDiv
{
public readonly $triggerControl : JQuery;
public readonly $content : JQuery;
private shown : boolean;
public readonly $popup : JQuery;
public show : ()=>void;
public hide : ()=>void;
constructor(p:{ $triggerControl : JQuery,
$content : JQuery,
popupTemplate? : string,
})
{
var self = this;
this.$triggerControl = p.$triggerControl;
this.$content = p.$content;
var popupTemplate = (p.popupTemplate != null) ? p.popupTemplate : '<div style="z-index:999;position:absolute;display:none;padding:1px"></div>';
self.shown = false;
this.$popup = $(popupTemplate)
.append( self.$content )
.insertAfter( self.$triggerControl );
var clickHandler = function(evt:any)
{
if( (! self.$popup.is(evt.target))
&& (self.$popup.has(evt.target).length == 0) )
{
// Click not inside the popup
if( (self.$triggerControl.is(evt.target))
|| (self.$triggerControl.has(evt.target).length != 0) )
// Click inside the triggering button => Discard
return;
self.hide();
}
};
self.show = function()
{
if( self.shown )
// Already shown
return;
self.$popup.slideDown('fast');
// Active click handler on the whole document
$(document).mouseup( clickHandler );
self.shown = true;
};
self.hide = function()
{
if(! self.shown )
// Already hidden
return;
self.$popup.slideUp('fast');
// Deactivate global click handler
$(document).unbind( 'mouseup', clickHandler );
self.shown = false;
};
self.$triggerControl.on('click', function()
{
if( self.shown )
self.hide();
else
self.show();
} );
self.$triggerControl.on('keyup', function(evt:any)
{
if( evt.keyCode == 27 ) // ESC key pressed
self.hide();
else if( evt.keyCode == 40 ) // DOWN key pressed
self.show();
} );
}
}
}
export namespace events
{
export interface EventsHandler
{
bind : (name:string, callback:(evt?:any,p?:any)=>void)=>EventsHandler;
unbind : (name:string, callback?:(evt?:any,p?:any)=>void)=>EventsHandler;
trigger : (name:string, p?:any)=>EventsHandler;
}
export function createEventHandler() : EventsHandler
{
return $({});
}
/** Creates an 'onXXX()' function for event binding */
export function eventBind<Self,T>(eventName:string, events:EventsHandler, self:Self) : (callback:(p:T)=>void, p?:{executeOnce?:boolean})=>Self
{
return function(callback:(p:T)=>void, pp?:{executeOnce?:boolean}) : Self
{
var handler : (evt:any,p:T)=>void;
handler = function(evt:any,p:T)
{
if( pp?.executeOnce == true )
// Unregister myself
events.unbind( eventName, handler );
try { callback( p ); }
catch( ex ) { console.error( 'Unexpected error:', ex ); }
};
events.bind( eventName, handler );
return self;
};
}
/** Creates a 'triggerXXX()' function for event triggering */
export function eventTrigger<T>(eventName:string, events:EventsHandler) : (p:T)=>void
{
return function(p) : void
{
events.trigger( eventName, p );
};
}
} // namespace events