| var __Promise = {}; | var __Promise = {}; | ||||
| // if(!String.prototype.trim) String.prototype.trim = function(){ | |||||
| // return this.replace(/^\s+|\s+$/g, ''); | |||||
| // }; | |||||
| // var ovrrides = { | // var ovrrides = { | ||||
| // resolve : function(v){ | // resolve : function(v){ | ||||
| // if(v && v.then) return v; | // if(v && v.then) return v; | ||||
| } | } | ||||
| var runtimestamp = (new Date()).getTime(); | var runtimestamp = (new Date()).getTime(); | ||||
| var promises = []; | |||||
| function startPromises(){ | |||||
| promises.forEach(function(p){ | |||||
| // console.log(p.chain) | |||||
| p(); | |||||
| // promises.splice(0,1) | |||||
| }) | |||||
| } | |||||
| function isWin(){ return /^win/.test(process.platform) } | |||||
| try{ | try{ | ||||
| __Promise = Promise | __Promise = Promise | ||||
| var __require = require | |||||
| var map = Array.map | |||||
| // -------------------------------------------- | // -------------------------------------------- | ||||
| // Node Exists. Lets launch ourselves in Node itself | // Node Exists. Lets launch ourselves in Node itself | ||||
| var selectedinstance = { root : path.resolve(".") } | var selectedinstance = { root : path.resolve(".") } | ||||
| __main(selectedinstance) | __main(selectedinstance) | ||||
| } | } | ||||
| // catch(e){} | |||||
| catch(e){ | catch(e){ | ||||
| // // -------------------------------------------- | // // -------------------------------------------- | ||||
| // // Cscript | // // Cscript | ||||
| var wsh = true; | var wsh = true; | ||||
| function isWin(){ return true; } | |||||
| // If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then | // If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then | ||||
| var clii = { | var clii = { | ||||
| } | } | ||||
| var shell = new ActiveXObject('shell.application'); | var shell = new ActiveXObject('shell.application'); | ||||
| var promises = []; | |||||
| promises.forEach = forEach | promises.forEach = forEach | ||||
| function startPromises(){ | function startPromises(){ | ||||
| promises.forEach(function(p){ | promises.forEach(function(p){ | ||||
| var shellExec = function(cmd, argstr){ | var shellExec = function(cmd, argstr){ | ||||
| var objShell = WScript.createobject("wscript.shell") | var objShell = WScript.createobject("wscript.shell") | ||||
| // console.log(argstr.join( ' ')) | |||||
| var oExec = objShell.Exec(cmd + ' ' + argstr.join(' ')) | var oExec = objShell.Exec(cmd + ' ' + argstr.join(' ')) | ||||
| var result = {} | var result = {} | ||||
| var shellresult = { shell : objShell, result : result } | var shellresult = { shell : objShell, result : result } | ||||
| var WshFinished = 1 | var WshFinished = 1 | ||||
| var WshFailed = 2 | var WshFailed = 2 | ||||
| while(oExec.Status === WshRunning){ | while(oExec.Status === WshRunning){ | ||||
| WScript.StdOut.write('.') | |||||
| WScript.StdOut.write('s.') | |||||
| WScript.Sleep(500) | WScript.Sleep(500) | ||||
| } | } | ||||
| var strOutput = '' | |||||
| var strOutput = '\n' | |||||
| switch(oExec.Status) { | switch(oExec.Status) { | ||||
| case WshFinished : | case WshFinished : | ||||
| strOutput = oExec.StdOut.ReadAll() | strOutput = oExec.StdOut.ReadAll() | ||||
| } | } | ||||
| result.result = command + ' ' + cargs + ' exited with code ' + result.code | result.result = command + ' ' + cargs + ' exited with code ' + result.code | ||||
| result.messages = [strOutput] | result.messages = [strOutput] | ||||
| // console.log(strOutput) | |||||
| // WScript.Echo(oExec.Status) | // WScript.Echo(oExec.Status) | ||||
| // WScript.Echo(oExec.ProcessID) | // WScript.Echo(oExec.ProcessID) | ||||
| // WScript.Echo(oExec.ExitCode) | // WScript.Echo(oExec.ExitCode) | ||||
| // console.log(objShell.StdOut.ReadAll) | // console.log(objShell.StdOut.ReadAll) | ||||
| // console.log(objShell.StdErr.ReadAll) | // console.log(objShell.StdErr.ReadAll) | ||||
| // objShell = WScript.createobject("wscript.shell") | |||||
| objShell = null; | |||||
| return shellresult; | return shellresult; | ||||
| } | } | ||||
| var p = null; | var p = null; | ||||
| var pworker = function(resolve, reject){ | var pworker = function(resolve, reject){ | ||||
| // console.dir(p) | // console.dir(p) | ||||
| var pfx = selectedinstance.root + '/.elxr/run-' + runtimestamp + '/' + stampedFilePfx(new Date()) | |||||
| var pfx = selectedinstance.root + '\\.elxr\\run-' + runtimestamp + '\\' + stampedFilePfx(new Date()) | |||||
| // console.log('p.chain.length ================ ' + p.chain.length) | // console.log('p.chain.length ================ ' + p.chain.length) | ||||
| options = options || { | options = options || { | ||||
| runFile : path.normalize( pfx + "out.txt") | runFile : path.normalize( pfx + "out.txt") | ||||
| // runFile : null | // runFile : null | ||||
| } | } | ||||
| var runFile = null; | var runFile = null; | ||||
| // var runFile = options.runFile | |||||
| // var runFile = selectedinstance.root + '\\' + stampedFilePfx(new Date()) + command + cargs + "out.txt"; | |||||
| var runFile = options.runFile || pfx + command + cargs + "out.txt"; | |||||
| // console.log(runFile) | // console.log(runFile) | ||||
| var args = cargs.concat() | var args = cargs.concat() | ||||
| // runFile ? (args.push(">"), args.push(runFile)) : cargs | // runFile ? (args.push(">"), args.push(runFile)) : cargs | ||||
| var runbat = path.normalize(pfx + "run.bat") | var runbat = path.normalize(pfx + "run.bat") | ||||
| // console.log('runbat : ' + runbat) | // console.log('runbat : ' + runbat) | ||||
| fs.writeFileSync(runbat, command + ' ' + args.join(' ') + ' \r\n' + 'echo done > ' + runFile ) | |||||
| fs.writeFileSync(runbat, | |||||
| '@echo off \r\n' + | |||||
| (options.cwd ? 'cd ' + options.cwd + ' \r\n' : '') + | |||||
| // ' cmd /k notepad.exe \r\n' + | |||||
| command + ' ' + args.join(' ') + ' \r\n' + | |||||
| 'cmd /c echo done >> ' + runFile + ' \r\n' + | |||||
| 'echo done' | |||||
| ) | |||||
| // fs.writeFileSync(runFile, 'started') | // fs.writeFileSync(runFile, 'started') | ||||
| // WScript.Quit() | // WScript.Quit() | ||||
| // elevatedshellexecute(runFile) | // elevatedshellexecute(runFile) | ||||
| // // cmd /b /c | // // cmd /b /c | ||||
| // }, 500) } | // }, 500) } | ||||
| console.log(options.waitmsg || ('awaiting ' + command + ' ' + args.join(' '))) | console.log(options.waitmsg || ('awaiting ' + command + ' ' + args.join(' '))) | ||||
| var shellresult = shellExec( 'cmd', [ '/c', runbat]) | |||||
| // var shellresult = shellExec( 'cmd', ['/c', runbat]) | // var shellresult = shellExec( 'cmd', ['/c', runbat]) | ||||
| var shellresult = shellExec( command, args) | |||||
| // var shellresult = shellExec( command, args) | |||||
| var wrapup = function(result) { | var wrapup = function(result) { | ||||
| // console.log('Wrapping up') | // console.log('Wrapping up') | ||||
| try { | try { | ||||
| // console.log('resolving.....................'); | |||||
| // console.log('--------------P' + result.messages.join(' ').trim() +'P--------------') | |||||
| // if(result.messages.join().trim()) resolve(result) | |||||
| // else reject(result) | |||||
| resolve(result) | resolve(result) | ||||
| } | } | ||||
| catch(e){ | catch(e){ | ||||
| // console.dir(e) | // console.dir(e) | ||||
| if(e.message === 'Input past end of file') { | if(e.message === 'Input past end of file') { | ||||
| // console.log('resolving'); console.dir(result) | |||||
| // console.log('---------------------------------------') | |||||
| // console.dir(result) | |||||
| resolve(result) | resolve(result) | ||||
| } | } | ||||
| else { | else { | ||||
| // console.dir(e) | |||||
| reject(e) | reject(e) | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| if(runFile){ | |||||
| var waitr = function(){ | |||||
| WScript.StdOut.write('.') | |||||
| if(existsSync(runFile)) { | |||||
| var strOutput = fs.readFileSync(runFile) | |||||
| shellresult.result.messages = [strOutput] | |||||
| wrapup(shellresult.result) | |||||
| sfs.unlinkSync(runFile) | |||||
| } | |||||
| wait( waitr, 500) | |||||
| } | |||||
| // console.log(wait) | |||||
| // if(runFile){ | |||||
| // var waitr = function(){ | |||||
| // WScript.StdOut.write('w.') | |||||
| // if(existsSync(runFile)) { | |||||
| // var strOutput = fs.readFileSync(runFile) | |||||
| // shellresult.result.messages = [strOutput] | |||||
| // wrapup(shellresult.result) | |||||
| // fs.unlinkSync(runFile) | |||||
| // } | |||||
| // // console.dir(shellresult.result) | |||||
| // wait( waitr, 500) | |||||
| // } | |||||
| // // console.log(wait) | |||||
| wait( waitr, 500) | |||||
| } | |||||
| else { | |||||
| // wait( waitr, 500) | |||||
| // } | |||||
| // else { | |||||
| // console.log('There is no runfile.') | // console.log('There is no runfile.') | ||||
| // console.log(shellresult.strOutput) | // console.log(shellresult.strOutput) | ||||
| wrapup(shellresult.result) | wrapup(shellresult.result) | ||||
| } | |||||
| // } | |||||
| } | } | ||||
| p = new Promise(pworker) | p = new Promise(pworker) | ||||
| // promises.push(p) | // promises.push(p) | ||||
| p.chain.push(thenfn) | p.chain.push(thenfn) | ||||
| return p; | return p; | ||||
| } | } | ||||
| , pcatch : function(catchfn) { | |||||
| , 'catch' : function(catchfn) { | |||||
| catchfn.isCatch = true | catchfn.isCatch = true | ||||
| p.chain.push(catchfn) | p.chain.push(catchfn) | ||||
| return p; | return p; | ||||
| , resolve : resolve | , resolve : resolve | ||||
| , processchain : processchain | , processchain : processchain | ||||
| } | } | ||||
| p['catch'] = p['pcatch'] | |||||
| p.chain.forEach = forEach; | p.chain.forEach = forEach; | ||||
| return p; | return p; | ||||
| } | } | ||||
| var runFile = path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/' + stampedFilePfx(new Date()) + 'where' + command + "out.txt"); | 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 } ]) | var p = nodeShellExec.apply(null, ['cmd', ['/c', 'where', command], { runFile : runFile } ]) | ||||
| p.runFile = runFile; | p.runFile = runFile; | ||||
| var catchr = 'catch' | |||||
| // console.log(p['catch']) | |||||
| if(p['catch']) { catchr = 'catch' } else { catchr = 'pcatch';} | |||||
| if (options.ignorefailures) { | if (options.ignorefailures) { | ||||
| return p.then(function(v) { | return p.then(function(v) { | ||||
| // WScript.Echo('firstThen ' + v); | // WScript.Echo('firstThen ' + v); | ||||
| return v })[catchr]( function(e) { | |||||
| return v })['catch']( function(e) { | |||||
| console.error(e); | console.error(e); | ||||
| // Ignore. Not a major error if where command fails !!! | // Ignore. Not a major error if where command fails !!! | ||||
| throw e; | throw e; | ||||
| // return decision | // return decision | ||||
| // } | // } | ||||
| // prereq definition helpers. We can't do proper inheritance in cscript. So fallback to global functions. | |||||
| function exists(next){ | |||||
| var self = this; | |||||
| console.log('checking existence of ' + self.shellcmd) | |||||
| return getTaskCheckExists(self.shellcmd, { ignorefailures: true })().then(function(exists) { | |||||
| // console.log('-------------exists=======================') | |||||
| // console.dir(exists) | |||||
| // console.log(exists + ' ' + self.shellcmd + ' exists') | |||||
| if(exists && exists.messages.join(' ').indexOf(self.shellcmd) > -1 ) { | |||||
| return true; | |||||
| } | |||||
| else return false | |||||
| })['catch'](function(e){ | |||||
| // console.log('-------------exists catch=======================') | |||||
| console.dir(e) | |||||
| return false; | |||||
| }) | |||||
| } | |||||
| var prerequisites = [ | var prerequisites = [ | ||||
| { | { | ||||
| shellcmd: 'git', | shellcmd: 'git', | ||||
| var self = this; | var self = this; | ||||
| console.log('Git Installsteps called') | console.log('Git Installsteps called') | ||||
| var ifns = [self.installcmd] | var ifns = [self.installcmd] | ||||
| ifns.map = map; | |||||
| if(!ifns.map) ifns.map = map; | |||||
| return any(ifns.map(callsheltask))['catch'](function(e){ | return any(ifns.map(callsheltask))['catch'](function(e){ | ||||
| if(e.code === 1602) { | if(e.code === 1602) { | ||||
| console.warn("Installation was probably cancelled.") | console.warn("Installation was probably cancelled.") | ||||
| , ['git', ['config', '--global', '--add', 'user.email', gitEmail]] | , ['git', ['config', '--global', '--add', 'user.email', gitEmail]] | ||||
| ] | ] | ||||
| steps.map = map; | |||||
| if(!steps.map) steps.map = map; | |||||
| return any(steps.map(callsheltask)).then(function(){ | return any(steps.map(callsheltask)).then(function(){ | ||||
| }) | }) | ||||
| else return self.install(); | 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 | |||||
| }) | |||||
| } | |||||
| , exists : exists | |||||
| , getUser : function(repo, onNoResult){ | , getUser : function(repo, onNoResult){ | ||||
| if(!repo) globalOrLocal = '--global'; | if(!repo) globalOrLocal = '--global'; | ||||
| else globalOrLocal = '--local' | else globalOrLocal = '--local' | ||||
| var fns = [['git', ['config', globalOrLocal, '--get-all', 'user.name']]] | var fns = [['git', ['config', globalOrLocal, '--get-all', 'user.name']]] | ||||
| fns.map = map; | |||||
| if(!ifns.map) ifns.map = map; | |||||
| return any(fns.map(callsheltask)).then(function(result){ | return any(fns.map(callsheltask)).then(function(result){ | ||||
| // not yet configured. | // not yet configured. | ||||
| if(!result.success) return onNoResult() | if(!result.success) return onNoResult() | ||||
| else return users[0]; // PB : TODO == We should probably prompt with all the users available for selection ! | else return users[0]; // PB : TODO == We should probably prompt with all the users available for selection ! | ||||
| } | } | ||||
| }) | }) | ||||
| .pcatch(function(e){ | |||||
| ['catch'](function(e){ | |||||
| console.log(e) | console.log(e) | ||||
| return onNoResult() | return onNoResult() | ||||
| }) | }) | ||||
| , install : function() { | , install : function() { | ||||
| var self = this; | var self = this; | ||||
| var ifns = [self.installcmd] | var ifns = [self.installcmd] | ||||
| ifns.map = map; | |||||
| if(!ifns.map) ifns.map = map; | |||||
| return any(ifns.map(callsheltask))['catch'](function(e){ | return any(ifns.map(callsheltask))['catch'](function(e){ | ||||
| if(e.code === 1602) { | if(e.code === 1602) { | ||||
| console.warn("Installation was probably cancelled.") | console.warn("Installation was probably cancelled.") | ||||
| }) | }) | ||||
| } | } | ||||
| , 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 | |||||
| } | |||||
| }) | |||||
| } | |||||
| , exists : exists | |||||
| , preinstallsteps: function() { | , preinstallsteps: function() { | ||||
| var self = this; | var self = this; | ||||
| console.log('Node preinstall steps') | console.log('Node preinstall steps') | ||||
| // return any([any(steps), any(prompts)]) | // return any([any(steps), any(prompts)]) | ||||
| } | } | ||||
| } | } | ||||
| , | |||||
| { | |||||
| shellcmd: 'elxr' | |||||
| , installcmd: [ isWin() ? 'npm.cmd' : 'npm' , ['link'], { cwd : selectedinstance.root + '\\elxr' /* cwd should be the cloned dir*/}] | |||||
| , preinstallsteps: function() { | |||||
| var self = this; | |||||
| console.log('Elxr preinstall steps') | |||||
| var steps = []; | |||||
| steps.push( | |||||
| function(){ | |||||
| console.log('Elxr PreInstallsteps called') | |||||
| var ifns = [ ['git', ['clone', 'http://git.bbh/chess\\elxr'] ] ] | |||||
| if(existsSyncFolder( selectedinstance.root + '\\elxr')) { | |||||
| if(existsSyncFolder( selectedinstance.root + '\\elxr\\.git')) { | |||||
| // PB : TODO -- use a elxr guid signature to detect more reliably folders named the same that is not us. | |||||
| ifns = [ ['git', ['pull'], { cwd : selectedinstance.root + '\\elxr' } ] ] | |||||
| } | |||||
| else { | |||||
| throw 'elxr subfolder not recognized as a git repository. Please cleanup and continue.' | |||||
| } | |||||
| } | |||||
| else console.log(selectedinstance.root + '\\elxr' + ' NOT FOUND ') | |||||
| if(!ifns.map) ifns.map = map; | |||||
| return any(ifns.map(callsheltask))['catch'](function(e){ | |||||
| if(e.code === 1602) { | |||||
| console.warn("Installation was probably cancelled.") | |||||
| } | |||||
| else throw e | |||||
| }) | |||||
| } | |||||
| ) | |||||
| return any([any(steps)]) | |||||
| } | |||||
| , installsteps: function () { | |||||
| var self = this; | |||||
| console.log('Elxr Installsteps called') | |||||
| var ifns = [self.installcmd] | |||||
| if(!ifns.map) ifns.map = map; | |||||
| return any(ifns.map(callsheltask))['catch'](function(e){ | |||||
| if(e.code === 1602) { | |||||
| console.warn("Installation was probably cancelled.") | |||||
| } | |||||
| else throw e | |||||
| }) | |||||
| } | |||||
| , install: function () { | |||||
| var self = this; | |||||
| console.log('Elxr Install called') | |||||
| return any([ /*self.preinstallsteps,*/ function(){ return self.installsteps() } ]) | |||||
| } | |||||
| // , exists : function(){ | |||||
| // console.log('Elxr PreInstallsteps called') | |||||
| // var ifns = [ ['git', ['clone', 'http://git.bbh/chess\\elxr'] ] ] | |||||
| // if(existsSyncFolder( selectedinstance.root + '\\elxr')) { | |||||
| // if(existsSyncFolder( selectedinstance.root + '\\elxr\\.git')) { | |||||
| // // PB : TODO -- use a elxr guid signature to detect more reliably folders named the same that is not us. | |||||
| // ifns = [ ['git', ['pull'], { cwd : selectedinstance.root + '\\elxr' } ] ] | |||||
| // } | |||||
| // else { | |||||
| // throw 'elxr subfolder not recognized as a git repository. Please cleanup and continue.' | |||||
| // } | |||||
| // } | |||||
| // else console.log(selectedinstance.root + '\\elxr' + ' NOT FOUND ') | |||||
| // if(!ifns.map) 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 : exists | |||||
| } | |||||
| ] | ] | ||||
| if(!prerequisites.forEach) prerequisites.forEach = forEach; | if(!prerequisites.forEach) prerequisites.forEach = forEach; | ||||
| // , downloadsdir + '/' + 'Git-2.32.0.2-64-bit.exe']).start() | // , downloadsdir + '/' + 'Git-2.32.0.2-64-bit.exe']).start() | ||||
| promises.push(verifyAndInstallPrerequisites()) | promises.push(verifyAndInstallPrerequisites()) | ||||
| if(wsh) startPromises(); | |||||
| startPromises(); | |||||
| } | } | ||||