|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969 |
-
- var __Promise = {};
- // var ovrrides = {
- // resolve : function(v){
- // if(v && v.then) return v;
- // var p = new Promise(function(resolve, reject){ resolve(v) });
- // return p;
- // }
- // };
-
- var stampedFilePfx = function(date) {
- return date.getFullYear() +
- ('0' + (date.getMonth() + 1)).slice(-2) +
- ('0' + date.getDate()).slice(-2) +
- ('0' + date.getHours()).slice(-2) +
- ('0' + date.getMinutes()).slice(-2) +
- ('0' + date.getSeconds()).slice(-2);
- }
- var runtimestamp = (new Date()).getTime();
-
- try{
- __Promise = Promise
- // --------------------------------------------
- // Node Exists. Lets launch ourselves in Node itself
-
- // var wait = function(ms, cb) {any
- // return new __Promise(function(resolve){ setTimeout(resolve, ms) } ).then(function(){ cb() });
- // }
- // var fs = require('fs')
- // var { existsSync } = require('fs');
- // var existsSyncFolder = existsSync
- // var path = require('path');
- // var cli = require('./cliverse')
- // var nodeShellExec = cli.nodeShellExec;
- // var utils = require('bbhverse');
- // var any = utils.any;
- // var wait = setTimeout
-
-
-
- // --------------------------------------------
-
- var selectedinstance = { root : path.resolve(".") }
- __main(selectedinstance)
- }
- catch(e){
- // // --------------------------------------------
- // // Cscript
- var wsh = true;
- // If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
-
- var clii = {
- question : function(q, answercb){
- WScript.Echo(q)
- // console.log('WScript.StdIn : ' + WScript.StdIn.ReadLine() + ' ==== ')
- var answer = WScript.StdIn.ReadLine()
- answercb(answer)
- }
- }
- var prompter = {
- ask : function(q){
- // Needs to be serialized. Parallel asks are not possible.
- // const clii = readline.createInterface({ input: process.stdin, output: process.stdout });
-
-
- return new Promise(function(resolve, reject){
- clii.question(q, function(answer){
- // clii.close();
- // console.log("resolve is being called");
- resolve(answer)
- })
- })
- }
- }
-
-
-
- var cli = {
- prompt : function(choices, label, defaultchoice){
- var options = [];
- choices.forEach = forEach
- choices.forEach(function(choice){ options.push( ((+choice) + 1) + ' ' + choices[choice] )})
- return prompter.ask( label + ' \\n ' + options.join('\n') + '\n default ( <= ' + (defaultchoice || choices[0]) + ' ) : '
- ).then( function(choice){
- if(!choice) return defaultchoice || choices[0];
- if(choice && isNaN(+choice)) return choice;
- return choices[(+choice) - 1];
- })
- }
- }
-
- var any = function(iterable, continueOnFailure) {
- // var cancelsignal = Symbol()
- if(!iterable.reduce) iterable.reduce = reduce
- // console.log('iterable.reduce ' + iterable.reduce)
- var cancelsignal = "cancelme{mangledrunid}"
- return iterable.reduce(
- function(p, tasq, i ,a) {
- // console.dir(a)
- // console.log('Entered')
- var handleError = function(err, pVal){
- if(err !== cancelsignal) {
- // Cancel only once on first failure.
- console.warn('Possible failure for task with result : ' )
- console.log('Failed : ' + err.message + ' ')
- console.dir(pVal)
- console.dir(p)
- if(i>0 && a[i-1].info) console.dir(a[i-1].info)
- console.error('Error : ' + err.stack)
- console.error(a[i-1])
- a[i-1] ? console.log("tasq : " + a[i-1].toString()) : null;
- if(!continueOnFailure) {console.log("Cancelling remaining on any one failure ..."); throw cancelsignal}
- else return pVal;
- // tasq ? console.log("tasq : " + tasq.toString()) : null; // Previous task is what failed not the one we are going to run.
- }
- }
-
- if(Promise.resolve(p) === p ) {
- if(i>0 && a[i-1].info) p.info = a[i-1].info;
- console.dir(' p.then is ' + p.then)
- return p.then( function(pVal){
- // Falsy values are no longer treated as task failure exceptions. Specially for Promises.
- // Even tasq function wrappers are required to return promises that eventually either resolve or reject..
- // Failures are known for promises on reject.
- // In future if we support direct sync function execution with a result examination for failure
- // we could examine the result of the function as falsy's... or a result evaluator handler needs to be passed in...
- // if(!pVal) handleError({ error : true, message : 'Failed without result' }, pVal)
-
- // Truthy values are failures if obj has error=true.
- if(pVal && pVal.error) handleError(pVal, pVal)
-
- var trycall = function(tasq){
- try {
- var result = tasq() // PB : TODO -- Handle scope for call
- if(tasq.resultHandler) return tasq.resultHandler(result)
- // else (Promise.resolve(result) === result ) ? result.start() : null
- return result
- } catch (error) {
- console.error(error);
- console.error('Error : ' + error ? error.stack : 'No stack')
- if(!continueOnFailure) throw error; // PB : TODO -- Support array of results for any with or without continueonfailure.
- }
- }
-
- var handleNext = function(){
- console.log('Task finished with result : ')
- console.dir(pVal)
- if(i>0 && a[i-1].info) console.dir(a[i-1].info)
- if(!tasq && !continueOnFailure) { console.log('Error : No task specified.'); throw false;}
- else if(!tasq) { console.log('Error : No task specified.'); return false;}
- return (Promise.resolve(tasq) === tasq ) ? tasq /*.start()*/ : trycall(tasq) ;
- }
-
- if(Promise.resolve(pVal) === pVal) {
- // Passed in function retured a promise. We still need to wait for it.
- pVal.then(function(pVal){ return handleNext(); })
- }
- else return handleNext()
-
- })['catch'](function(error) {
- if(error !== cancelsignal) {
- console.log('E3 : i = ' + i);
- if(error.result) console.error(error.result)
- console.error('Error : ' + (error.message || error.messages))
- console.error('Error : ' + error.stack)
- tasq ? console.log("tasq : " + tasq.toString()) : null;
- console.log('debugData 3-------------------------');
- // handleError()
- throw error
- }
- else throw cancelsignal;
- })
- }
- else if(!p) {
- handleError({ error : true, message : 'Failed without result' }, pVal)
- console.log("Bypass remaining on prior failure");
- return false; // All remaining tasks will return false in the any results even if they are promisies still running or functions not initiated.
- }
- else return p; // A truthy value
- }
- , Promise.resolve(true)
- );
- }
- var console = { log : function(m) {WScript.Echo(m)}, error : function(m) {WScript.Echo(m) }
- , dir : function(o) {
- for(var i in o){ console.log(i + ' : ' + o[i])}
- }
- }
-
- // __Promise = ovrrides
- function forEach(eachFn){
- for(var i=0; i<this.length; i++) eachFn(this[i])
- }
- function reduce(reducnFn, iv){
- var acc = iv
- for(var i=0; i<this.length; i++) {acc = reducnFn(acc, this[i], i, this) }
- return acc;
- }
-
- function map(eachFn){
- var mapped = []
- for(var i=0; i<this.length; i++) mapped.push(eachFn(this[i]))
- return mapped
- }
-
- var wait = function(cb, ms) { WScript.Sleep(ms); cb() }
- var fso = new ActiveXObject('Scripting.FileSystemObject');
- var existsSync = function(filepath){ return fso.FileExists(filepath) }
- var fs = {
- writeFileSync : function(filepath, text) {
- console.log(filepath)
- var fh = fso.CreateTextFile(filepath, true);
- fh.WriteLine(text);
- fh.Close();
- }
-
- , mkdirSync : function(path) {
- fso.CreateFolder(path)
- }
- , readFileSync : function(filepath){
- var objFileToRead = fso.OpenTextFile(filepath,1)
- var strFileText = objFileToRead.ReadAll()
- objFileToRead.Close()
- return strFileText
- }
- , unlinkSync : function(filepath){ fso.DeleteFile(filepath) }
- }
-
- var path = {
- resolve : function(path){ return fso.GetAbsolutePathName(path) }
- , dirname : function(filepath) {
- var normalized = this.normalize(filepath)
- var li = normalized.lastIndexOf("\\")
- if( li > -1) {
- return normalized.substring(0, li)
- }
- }
- , normalize : function(path){
- return path.replace(/\//g,'\\');
- }
- }
-
- // Detect or specify install directory.
- var selectedinstance = { root : path.resolve(".") }
-
- var existsSyncFolder = function(path){
- return fso.FolderExists(path)
- }
-
- var shell = new ActiveXObject('shell.application');
- var promises = [];
- promises.forEach = forEach
- function startPromises(){
- promises.forEach(function(p){
- // console.log(p.chain)
- p.start();
- // promises.splice(0,1)
- })
- }
-
- function nodeShellExec(command, cargs, options){
-
- var elevatedshellexecute = function(cmd, argstr){
- shell.ShellExecute(cmd, argstr , "", "", 1);
- }
-
- var shellExec = function(cmd, argstr){
-
- var objShell = WScript.createobject("wscript.shell")
- var oExec = objShell.Exec(cmd + ' ' + argstr.join(' '))
- // WScript.Echo(oExec.Status)
- // WScript.Echo(oExec.ProcessID)
- // WScript.Echo(oExec.ExitCode)
-
- // console.log(objShell.StdOut.ReadAll)
- // console.log(objShell.StdErr.ReadAll)
-
- return objShell;
- }
-
- var p = null;
- var pworker = function(resolve, reject){
- // console.dir(p)
- var pfx = selectedinstance.root + '/.elxr/run-' + runtimestamp + '/' + stampedFilePfx(new Date())
- console.log('p.chain.length ================ ' + p.chain.length)
- options = options || {
- runFile : path.normalize( pfx + "out.txt")
- // runFile : null
- }
- runFile = options.runFile
- // var runFile = selectedinstance.root + '\\' + stampedFilePfx(new Date()) + command + cargs + "out.txt";
- console.log(runFile)
- var args = cargs.concat()
- // runFile ? (args.push(">"), args.push(runFile)) : cargs
- console.log(command + ' ' + args.join(' '))
-
- // command = 'cmd'
- // args = ['/c', 'start',
- // '/WAIT', selectedinstance.root + '/Downloads' + '/' + 'Git-2.33.0.2-64-bit.exe'
- // , '/VERYSILENT'
- // // , '/MERGETASKS=!runcode' // This is required only for vscode...
- // ]
-
-
- var runbat = path.normalize(pfx + "run.bat")
- console.log('runbat : ' + runbat)
- fs.writeFileSync(runbat, command + ' ' + args.join(' ') + ' \r\n' + 'echo done > ' + runFile )
- // fs.writeFileSync(runFile, 'started')
- // WScript.Quit()
- // elevatedshellexecute(runFile)
- // while(!existsSync(runbat)) { wait(function(){
- // console.log('awiting batch : ' + runbat)
- // // shellExec( 'start', ['/W', '/b', runbat])
- // // shellExec( 'cmd', ['/k', '/b', runbat])
- // shellExec( command, args)
- // // cmd /b /c
- // }, 500) }
- shellExec( 'cmd', ['/c', runbat])
- // shellExec( command, args)
-
-
- var wrapup = function() {
- console.log('Wrapping up')
- try {
- var strFileText = fs.readFileSync(runFile)
- var messages = [];
- messages.push(strFileText)
- var result = { result : command + ' ' + cargs + ' exited with code ???', messages : messages, code : 0, success : true }
- console.log('resolving'); console.dir(result)
- resolve(result)
- // fs.unlinkSync(runFile)
- }
- catch(e){
- // console.dir(e)
- if(e.message === 'Input past end of file') {
- var result = { result : 'cmd /c ' + command + ' ' + cargs + " > " + runFile + ' exited with code ???', messages : [] , code : 0, success : false }
- // console.log('resolving'); console.dir(result)
- resolve(result)
- // fs.unlinkSync(runFile)
- }
- else {
- // fs.unlinkSync(runFile)
- reject(e)
- }
- }
- }
-
- if(runFile){
- var waitr = function(){
- console.log('awiting output : ' + runFile)
- if(existsSync(runFile)) return wrapup()
- wait( waitr, 500)
- }
- console.log(wait)
- wait( waitr, 500)
- }
- else wrapup()
- }
- p = new Promise(pworker)
- // promises.push(p)
- // // Promise tThens don't get hooked up... we need to postpone promise starting until all the thens are registered...
- // wait(function(){
- // console.log('p.chain.length' + p.chain.length)
- // p.start() } , 5000) // Bind and setTImeout is not supported by JScript
-
- return p;
- }
- createPromiseClass(/*ovrrides*/)
- __main( selectedinstance )
- }
-
- function createPromiseClass(overrides) {
-
- function reject(e){
- var p = this;
- console.log('Promise Rejection : ' + p.fn)
- console.dir(e)
- // throw e;f
- if(p.state !== PromiseClass.PENDING) { console.error ('Error : Promise Rejection can only be called once')}
- var __i = 0;
- var __e = e;
- do {
- for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) break;
- try {
- for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) { p.chain[i](__e); break; }
- __i++;
- __e = null;
- }
- catch(e){ __i++; __e = e}
- } while(__e)
- do {
- try { for(var i = __i; i < p.chain.length; i++, __i = i) if(!p.chain[i].isCatch) { p.result = p.chain[i](p.result); } }
- catch(e){
- __i ++;
- do {
- try {
- for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) { p.chain[i](__e); break; }
- __i ++;
- __e = null;
- }
- catch(e){ __i++; __e = e}
- } while(__e)
- }
- } while ( __i < p.chain.length )
- p.state = PromiseClass.REJECTED;
- }
-
- function resolve(result){
- var p = this;
- // console.log(result + ' resolve was called with chain length ' + p.chain.length )
- if(p.state !== PromiseClass.PENDING) { console.error ('Error : Promise Resolve can only be called once')}
- p.result = result;
- if(PromiseClass.resolve(p.result) === p.result) {
- // console.log( 'result is still a promise waiting for result and promisechain' )
- waitForResult(p.result, function(r){ p.processchain(r) })
- }
- else {
- // console.log( 'result is value waiting for promisechain' )
- console.dir(result)
- return p.processchain(result)
- }
- };
-
- function processchain(r){
-
- var __i = 0;
- var i = __i;
- var __e = null;
- p = this;
- console.log('processchain chain.length : ' + p.chain.length)
-
- function __processchain(r){
-
- function waitForThen(p){
- if(i < p.chain.length) {
- if(!p.chain[i].isCatch) {
- // console.log('chain idx : ' + i + ' Executing : then ' + p.result + ' ' + p.chain[i])
- try {
- p.result = p.chain[i](p.result)
-
- if(PromiseClass.resolve(p.result) === p.result) {
- // console.log('chain idx : ' + i + ' result is still a promise starting it ')
- // console.dir(p.result.chain)
- // console.log('p.result.start : ' + p.result.start + ' ------------------ ')
- p.result.start()
- // console.log(p.result.fn + ' ------------------ ')
- waitForResult(p.result, function(r){
- console.log('we waited')
- p.result = r; i++; __i = i;
- waitForThen(p)
- })
- }
- else {
- i++; __i = i;
- waitForThen(p)
- }
- }
- catch(e) {
- i++; __i = i;
- __e = e;
- console.log('failed on index ' + __i + p.chain[__i-1] )
- console.dir(e)
- waitForCatch(p);
- }
- }
- else {
- // console.log(i + ' Skipping catch : ' + p.result + ' ' + p.chain[i])
- i++; __i = i;
- waitForThen(p)
- }
- }
- else return p.state = PromiseClass.FULFILLED;
- }
- function waitForCatch(p) {
- if(i < p.chain.length) {
- if(p.chain[i].isCatch) {
- console.log('chain idx : ' + i + ' Executing : catch : ' + p.result + ' ' + p.chain[i])
- try {
- p.result = p.chain[i](__e);
-
- if(PromiseClass.resolve(p.result) === p.result) {
- p.result.start()
- waitForResult(p.result, function(r){
- p.result = r; i++; __i = i;
- })
- waitForThen(p)
- }
- else {
- p.result = r; i++; __i = i;
- waitForThen(p)
- }
- }
- catch(e){ i++; __i = i;; __e = e; waitForCatch(p) }
- }
- else {
- i++; __i = i;
- waitForCatch(p)
- }
- }
- else return p.state = PromiseClass.REJECTED
- }
- waitForThen(p);
- }
- __processchain(r)
- }
-
- var create = function(fn){
- var p = {
- then : function(thenfn){
- // console.log('Adding then')
- thenfn.isThen = true
- if(Object.prototype.toString.call(p.chain) !== '[object Array]') console.dir(p.chain)
- p.chain.push(thenfn)
- return p;
- }
- , pcatch : function(catchfn) {
- catchfn.isCatch = true
- p.chain.push(catchfn)
- return p;
- }
- , start : function(){
- if(this.started) {
- console.error('Cannot start more than once...')
- return p
- };
- this.started = true;
- this.fn = fn;
- try { fn( function(r){
- // console.log('calling presolve in starter ')
- p.resolve(r) } , function(r){ p.reject(r) } ) }
- catch(e){
- // console.log('start catch : ' + e)
- // console.log('p.chain.length : ' + p.chain.length)
- // // console.dir(p)
- // console.dir(e)
- // console.log(fn)
- p.reject(e)
- }
- return p
- }
- , state : PromiseClass.PENDING
- , chain : []
- , reject : reject
- , resolve : resolve
- , processchain : processchain
- }
-
- p['catch'] = p['pcatch']
- p.chain.forEach = forEach;
- return p;
- }
-
- var PromiseClass = function(fn /*, donotstart */){
- var p = create(fn)
- // if(!donotstart) {
- // wait(function(){
- // console.log('p.chain.length' + p.chain.length)
- // p.start() } , 500) // Bind and setTImeout is not supported by JScript
- // }
- return p
- }
-
- PromiseClass.PENDING = 1
- PromiseClass.FULFILLED = 2
- PromiseClass.REJECTED = 3
- // PromiseClass.STARTED = 4
- PromiseClass.isSETTLED = function(p){ p.state === PromiseClass.FULFILLED || p.state === PromiseClass.REJECTED }
-
- PromiseClass.resolve = function(v){
- if(v && v.then) return v;
- var p = create(function(resolve, reject){
- resolve(v)
- });
- // console.log('p.chain in resolve. length ' + p.chain.length)
- // p.result = v;
- // wait(function(){ p.processchain(v) }, 0)
- return p;
- }
-
- function waitForResult(p, cb){
- if(!p) return cb(p)
- if(p.state !== PromiseClass.PENDING) cb(p.result)
- // console.log(p.state + ' Waiting for ..... ' + p.runFile)
- if(p.runFile && false) {
- while(!existsSync(p.runFile) && p.state === PromiseClass.PENDING) {
- console.log('Waiting for ResultFle'); wait(cb, 500)
- }
- cb(p.result)
- }
- else {
- // while(p.state === PromiseClass.PENDING) {
- // console.log('Waiting for Result')
- function waiter(){
- // console.log(p.result)
- if(p.state === PromiseClass.PENDING) wait(waiter, 500);
- else return cb(p.result)
- }
- wait(waiter, 500)
- // }
- }
- }
-
- PromiseClass.all = any; // Serialized...
-
- PromiseClass.__all = function(arr){
- arr.forEach = forEach;
- var resultPs = [];
- var results = [];
- console.log('All : ' + arr.length)
- var pAll = new PromiseClass(function(resolve, reject){
-
- console.log('All started : ' + pAll)
- // console.dir(pAll)
-
- arr.forEach(function(p){
- if(!p.then) { var pfn = p;
- p = new PromiseClass(function(resolve, reject){
- try{
- resolve(pfn())
- }
- catch(e){
- reject(e)
- }
- })
- p.start()
- }
- else {
- // !p.start ? p.start = function(){return p} : null
- // p.start()
- // .then( function(){ waitForResult(p, function(r){ results.push(r) }) })
- }
- resultPs.push(p)
- // waitForResult(p, function(r){ results.push(r) })
- })
-
- // PB : TODO -- This is the same as processchain!!!
- var allwaitr = function(){
- var allResolved = true
- console.log('resultPs : ' + resultPs.length)
- for(var rIdx =0; rIdx < resultPs.length; rIdx++ ){
-
- if(resultPs[rIdx]) {
- allResolved = false
- waitForResult(resultPs[rIdx], function(r){
- if(Promise.resolve(r) !== r) results[rIdx] = r; resultPs[rIdx] = null;
- })
- break;
- }
- }
- if(allResolved) {
- console.log('All Reseloved')
- console.dir(results)
- resolve(results)
- }
- else wait(allwaitr, 500)
- }
- wait(allwaitr, 500)
- })
-
- // pAll.chain = arr;
- return pAll
- }
-
- // PromiseClass.resolve = overrides.resolve;
- Promise = PromiseClass;
- return PromiseClass;
- }
-
- function __main( selectedinstance ){
- var downloadsdir = selectedinstance.root + '/Downloads';
-
- var callsheltask = function(args) { return function() { return nodeShellExec.apply(null, args) } }
- var gitUser = 'guest';
- var gitEmail = 'guest@bbh.org.in';
-
- var BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
- function getVersion() { return BUILD_VERSION; }
- console.log(getVersion())
-
- function ensureDirectoryExistence(filePath) {
- var dirname = path.dirname(filePath);
- if (existsSyncFolder(dirname)) {
- return filePath;
- }
- ensureDirectoryExistence(dirname);
- fs.mkdirSync(dirname);
- return filePath;
- }
-
- var getTaskCheckExists = function(command, options) {
- options = options || {}
- return function() {
- var runFile = path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/' + stampedFilePfx(new Date()) + 'where' + command + "out.txt");
- var p = nodeShellExec.apply(null, ['cmd', ['/c', 'where', command], { runFile : runFile } ])
- p.runFile = runFile;
- var catchr = 'catch'
- // console.log(p['catch'])
- if(p['catch']) { catchr = 'catch' } else { catchr = 'pcatch';}
- if (options.ignorefailures) {
- return p.then(function(v) {
- // WScript.Echo('firstThen ' + v);
- return v })[catchr]( function(e) {
- console.error(e);
- // Ignore. Not a major error if where command fails !!!
- throw e;
- })
- }
- else return p.then(function() {
- // WScript.Echo('firstThen ddd');
- return v });
- }
- }
-
- function verifyAndInstallPrerequisites() {
- fs.writeFileSync(ensureDirectoryExistence(downloadsdir + '/readme.txt'), getVersion() + ' Your local downloads for this instance');
- var downloadbatch =
- "::************************************************************************** \r\n \
- :Download_ <url> <File> \r\n \
- Powershell.exe ^\r\n \
- $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^\r\n \
- [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^\r\n \
- (New-Object System.Net.WebClient).DownloadFile('%1','%2') \r\n \
- exit /b \r\n \
- ::**************************************************************************";
- ensureDirectoryExistence(path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/readme.txt'))
- fs.writeFileSync(path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/download.bat'), downloadbatch);
-
- var downloadtasks = [];
- var installtasks = [];
- prerequisites.forEach(function(preq) {
- var p = preq.exists().then(function(exists) {
- if (exists && false) console.log( preq.shellcmd + ' exists');
- else {
- console.log(preq.shellcmd + ' is not installed');
- return preq.preinstallsteps().then(function(){
- console.log(' task.install : ' + preq.install)
- installtasks.push( function(){ return preq.install() } );
- })
- }
- })
- // .then(function(res){ console.log( 'preinstallsteps ' + res)});
- downloadtasks.push( p )
- });
- // console.log('downloadtasks')
- // console.dir(downloadtasks[0])
- var p = Promise.all(downloadtasks).then(function(){
- console.log('calling install tasks : ' + installtasks.length)
- return any(installtasks) })
-
- // console.log('Promise.all.chain : ' + p.chain)
- return p
- }
-
- // var choiceHandler = function(choices, choice) {
- // console.log('chosen : ' + choice)
- // var decision = choices['d'];
- // if (choice && choice === 'd' || !choice) {
- // decision = choices['d']
- // }
- // else if (isNaN((+choice))) {
- // decision = choice
- // }
- // else decision = choices[choice-1]
- // if(!decision) throw 'Invalid selection : ' + decision
- // return decision
- // }
-
- var prerequisites = [
- // {
- // shellcmd: 'git',
- // url: 'https://github.com/git-for-windows/git/releases/download/v2.33.0.windows.2/Git-2.33.0.2-64-bit.exe'
- // , installer: 'Git-2.33.0.2-64-bit.exe'
- // , installcmd: ['cmd', ['/c', 'start',
- // '/WAIT', downloadsdir + '/' + 'Git-2.33.0.2-64-bit.exe'
- // , '/VERYSILENT'
- // // , '/MERGETASKS=!runcode' // This is required only for vscode...
- // ]]
- // , preinstallsteps: function() {
- // var self = this;
- // console.log('Git preinstall steps')
-
- // var steps = [];
- // steps.push(
- // function(){
- // // console.log('cli prompt steps')
- // var choices = { 0 : 'guest', 1 : 'chessdemo' }
- // return cli.prompt(choices, 'git user name', gitUser).then(function(choice){ gitUser = choice } )
- // }
- // )
-
- // steps.push(
- // function(){
- // var choices = { 0 : 'guest@bbh.org.in', 1 : 'chessdemo@bbh.org.in' }
- // return cli.prompt(choices, 'git user email', gitEmail).then(function(choice){ gitEmail = choice })
- // }
- // )
- // steps.push(
- // function(){
- // if (!existsSync(downloadsdir + '/' + self.installer)) {
- // return nodeShellExec(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/download.bat', [self.url, downloadsdir + '/' + self.installer])
- // }
- // else {
- // console.log(self.installer + ' Already exits Download skipped.')
- // return Promise.resolve(true)
- // }
- // }
- // )
- // return any(steps)
- // // return any([any(steps), any(prompts)])
- // }
- // , installsteps: function () {
- // var self = this;
- // console.log('Git Installsteps called')
- // var ifns = [self.installcmd]
- // ifns.map = map;
- // return any(ifns.map(callsheltask))['catch'](function(e){
- // if(e.code === 1602) {
- // console.warn("Installation was probably cancelled.")
- // }
- // else throw e
- // })
- // }
- // , postinstallsteps: function(){
-
- // // PB : TODO -- Detect failure or cancellation before attenpting postinstall steps...
- // var steps = [];
- // steps.push(
- // function(){
- // var choices = { 0 : 'guest', 1 : 'chessdemo' }
- // return cli.prompt(choices, 'git user name', gitUser).then(function(choice){ gitUser = choice } )
- // }
- // )
-
- // steps.push(
- // function(){
- // var choices = { 0 : 'guest@bbh.org.in', 1 : 'chessdemo@bbh.org.in' }
- // return cli.prompt(choices, 'git user email', gitEmail).then(function(choice){ gitEmail = choice })
- // }
- // )
-
- // return any(steps).then(function(){
- // var steps = [
- // ['git', ['config', '--global', '--add', 'user.name', gitUser]]
- // , ['git', ['config', '--global', '--add', 'user.email', gitEmail]]
- // ]
-
- // steps.map = map;
- // return any(steps.map(callsheltask)).then(function(){
-
- // })
- // });
- // }
- // , install: function () {
- // var self = this;
- // console.log('Git Install called')
- // return any([ /*self.preinstallsteps,*/ function(){ return self.installsteps() }, function(){ return self.postinstallsteps() } ])
- // }
- // , verifyAndInstall : function(){
- // var self = this;
- // return self.exists().then( function(exits) {
- // if(exists) return self.getUser(null, function(){ return self.postinstallsteps() } )
- // else return self.install();
- // })
- // }
- // , exists : function(next){
- // var self = this;
- // return getTaskCheckExists(self.shellcmd, { ignorefailures: true })().then(function(exists) {
- // console.dir(exists)
- // console.log(exists + ' git exists')
- // if(exists && exists.messages.join(' ').indexOf(self.shellcmd) > -1 ) {
- // return true;
- // }
- // else return false
- // })
- // }
- // , getUser : function(repo, onNoResult){
-
-
- // onNoResult = onNoResult || function(){return false}
- // var globalOrLocal = '--global';
- // if(!repo) globalOrLocal = '--global';
- // else globalOrLocal = '--local'
- // var fns = [['git', ['config', globalOrLocal, '--get-all', 'user.name']]]
- // fns.map = map;
- // return any(fns.map(callsheltask)).then(function(result){
- // // not yet configured.
- // if(!result.success) return onNoResult()
- // else {
- // var users = result.messages[0].trim().split('\n');
- // if(users.length === 0 ||
- // users.length === 1 && users[0] === 'guest') {
-
- // return onNoResult()
- // }
- // else return users[0]; // PB : TODO == We should probably prompt with all the users available for selection !
- // }
- // })
- // .pcatch(function(e){
- // console.log(e)
- // return onNoResult()
- // })
- // }
- // }
- // ,
- {
- shellcmd: 'node',
- url: 'https://nodejs.org/dist/v14.17.6/node-v14.17.6-x64.msi'
- , installer: 'node-v14.17.3-x64.msi'
- , installcmd: ['MSIEXEC.exe', ['/i'
- , path.normalize(downloadsdir + '/' + 'node-v14.17.3-x64.msi')
- , 'ACCEPT=YES', '/passive']]
- , install : function() {
- var self = this;
- var ifns = [self.installcmd]
- ifns.map = map;
- return any(ifns.map(callsheltask))['catch'](function(e){
- if(e.code === 1602) {
- console.warn("Installation was probably cancelled.")
- }
- else throw e
- })
- }
-
- , exists : function(next){
- var self = this;
- return getTaskCheckExists(self.shellcmd, { ignorefailures: true })().then(function(exists) {
- console.log(self.shellcmd + ' ' + exists + ' node exists')
- if(exists && exists.messages.join(' ').indexOf(self.shellcmd) > -1 ) {
- return true
- }
- else {
- // console.log(self.shellcmd + ' ' + exists + ' node doesnt exist')
- return false
- }
- })
- }
- , preinstallsteps: function() {
- var self = this;
- console.log('Node preinstall steps')
-
- var steps = [];
- steps.push(
- function(){
- if (!existsSync(downloadsdir + '/' + self.installer)) {
- return nodeShellExec(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/download.bat', [self.url, downloadsdir + '/' + self.installer])
- }
- else {
- console.log(self.installer + ' Already exits Download skipped.')
- return Promise.resolve(true)
- }
- }
- )
-
- return any(steps)
- // return any([any(steps), any(prompts)])
- }
- }
- ]
-
- if(!prerequisites.forEach) prerequisites.forEach = forEach;
-
- // nodeShellExec(selectedinstance.root + '/.elxr/run-' + '1629889572461' + '/download.bat'
- // , ['https://github.com/git-for-windows/git/releases/download/v2.31.0.windows.1/Git-2.32.0.2-64-bit.exe'
- // , downloadsdir + '/' + 'Git-2.32.0.2-64-bit.exe']).start()
- promises.push(verifyAndInstallPrerequisites())
-
- if(wsh) startPromises();
- }
-
|