// // -------------------------------------------- // // Cscript // 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])} // } // } // var wait = function(cb, ms) { WScript.Sleep(ms); cb() } // var fso = new ActiveXObject('Scripting.FileSystemObject'); // var shell = new ActiveXObject('shell.application'); // 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() // } // , 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,'\\'); // } // } // var existsSyncFolder = function(path){ // return fso.FolderExists(path) // } // function nodeShellExec(cmd, cargs){ // var p = new Promise(function(resolve, reject){ // var runFile = selectedinstance.root + '\\' + stampedFilePfx(new Date()) + cmd + cargs + "out.txt"; // // console.log(runFile) // shell.ShellExecute('cmd', '/c ' + cmd + ' ' + cargs + " > " + runFile , "", "", 1); // // var WshFinished = 1 // // var WshFailed = 2 // // var strOutput = 'Did not work' // // switch(shell.Status){ // // case WshFinished : // // strOutput = oShell.StdOut.ReadAll; // // break; // // case WshFailed : // // strOutput = oShell.StdErr.ReadAll; // // break; // // } // // WScript.Echo(strOutput) // while(!existsSync(runFile)) { wait(500, function(){}) } // var strFileText = fs.readFileSync(runFile) // // console.log(strFileText) // fs.unlinkSync(runFile) // var result = { result : 'cmd /c ' + cmd + ' ' + cargs + " > " + runFile + ' exited with code ???', messages : [].push(strFileText), code : 0, success : true } // console.log('resolving ' + result) // resolve(result) // }) // return p; // } var __Promise = {}; // var ovrrides = { // resolve : function(v){ // if(v && v.then) return v; // var p = new Promise(function(resolve, reject){ resolve(v) }); // return p; // } // }; try{ __Promise = Promise } catch(e){ // __Promise = ovrrides } function createPromiseClass(overrides) { function reject(e){ var p = this; console.error(e) 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){ // console.log(result + ' resolve was called ' + p.chain.length ) var p = this; 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) { waitForResult(p.result, processchain.bind(p)) } else { // p.state = PromiseClass.FULFILLED; return processchain.bind(p)(result) } }; function processchain(r){ var __i = 0; var i = __i; var __e = null; p = this; function __processchain(r){ function waitForThen(p){ if(i < p.chain.length) { if(!p.chain[i].isCatch) { // console.log(i + ' Executing : then ' + p.result + ' ' + p.chain[i]) try { p.result = p.chain[i](p.result) if(PromiseClass.resolve(p.result) === p.result) { waitForResult(p.result, function(r){ 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(i + ' Executing : catch : ' + p.result + ' ' + p.chain[i]) try { p.result = p.chain[i](__e); if(PromiseClass.resolve(p.result) === p.result) { 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){ thenfn.isThen = true 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(resolve.bind(p), reject.bind(p)) } catch(e){ reject(e) } return p } , state : PromiseClass.PENDING , chain : [] } p.catch = p.pcatch p.chain.forEach = forEach; return p; } var PromiseClass = function(fn){ var p = create(fn) setTimeout(p.start.bind(p), 0) 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(){}); p.result = v; wait(0, function(){ processchain.bind(p)(v) }) return p; } function waitForResult(p, cb){ if(!p) return cb(p) if(p.state !== PromiseClass.PENDING) cb(p.result) if(p.runFile && false) { while(!existsSync(p.runFile) && p.state === PromiseClass.PENDING) { console.log('Waiting for ResultFle'); wait(500, function(){}) } 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(500, waiter); else return cb(p.result) } wait(500, waiter) // } } } PromiseClass.all = function(arr){ arr.forEach = forEach; var resultPs = []; var results = []; console.log('All : ' + arr.length) var pAll = new PromiseClass(function(resolve, reject){ arr.forEach(function(p){ if(!p.then) { var pfn = p; p = new PromiseClass(function(resolve, reject){ pfn() }) } 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 for(var rIdx =0; rIdx < resultPs.length; rIdx++ ){ if(resultPs[rIdx]) { allResolved = false waitForResult(resultPs[rIdx], function(r){ results[rIdx] = r; resultPs[rIdx] = null; }) break; } } if(allResolved) resolve(results); else wait(500, allwaitr) } wait(500, allwaitr) }) // pAll.chain = arr; return pAll } // PromiseClass.resolve = overrides.resolve; Promise = PromiseClass; return PromiseClass; } createPromiseClass(/*ovrrides*/) // any = function(arr){ // arr.forEach = forEach; // var results = []; // console.log('Any : ' + arr.length) // return new Promise(function(resolve, reject){ // arr.forEach(function(p){ // console.log('P is : ' + p) // if(!p.then) { var pfn = p; p = new Promise(function(resolve, reject){ try{ resolve(pfn()) } catch(e){reject(e) } }).start() } // else { // !p.start ? p.start = function(){return p} : null // p.start() // } // // results.push(waitForResult(p)) // } ) // resolve(results) // }).start(); // } // var any = function(iterable, continueOnFailure) { // var cancelsignal = Symbol() // return iterable.reduce( // function(p, tasq, i ,a) { // var handleError = function(err, pVal){ // if(err !== cancelsignal) { // // Cancel only once on first failure. // console.log('Failed : ' + err.message + ' ' + JSON.stringify(pVal)) // console.dir(p) // console.warn('Possible failure for task with result : ' + JSON.stringify(pVal)) // 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; // 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) // // if(!result) throw result; // Default failure detection for functions is falsy values. // 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 : ' + JSON.stringify(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 : 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() // }).pcatch(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) // ); // } // -------------------------------------------- // -------------------------------------------- // Node var wait = function(ms, cb) { return new __Promise(function(resolve){ setTimeout(resolve, ms) } ).then(function(){ cb() }); } const fs = require('fs') const { existsSync } = require('fs'); const existsSyncFolder = existsSync const path = require('path'); var cli = require('./cliverse') var nodeShellExec = cli.nodeShellExec; var utils = require('bbhverse'); var any = utils.any; // -------------------------------------------- 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]'; var runtimestamp = (new Date()).getTime(); function getVersion() { return BUILD_VERSION; } console.log(getVersion()) function forEach(eachFn){ for(var i=0; i -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' return any([['git', ['config', globalOrLocal, '--get-all', 'user.name']]].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.16.0/node-v14.17.3-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; return any([self.installcmd].map(callsheltask)) } , 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(){ return Promise.resolve(true); } } ] 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() verifyAndInstallPrerequisites()