Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // // --------------------------------------------
  2. // // Cscript
  3. // var console = { log : function(m) {WScript.Echo(m)}, error : function(m) {WScript.Echo(m)}
  4. // , dir : function(o) {
  5. // for(var i in o){ console.log(i + ' : ' + o[i])}
  6. // }
  7. // }
  8. // var wait = function(cb, ms) { WScript.Sleep(ms); cb() }
  9. // var fso = new ActiveXObject('Scripting.FileSystemObject');
  10. // var shell = new ActiveXObject('shell.application');
  11. // var existsSync = function(filepath){ return fso.FileExists(filepath) }
  12. // var fs = {
  13. // writeFileSync : function(filepath, text) {
  14. // console.log(filepath)
  15. // var fh = fso.CreateTextFile(filepath, true);
  16. // fh.WriteLine(text);
  17. // fh.Close();
  18. // }
  19. // , mkdirSync : function(path) {
  20. // fso.CreateFolder(path)
  21. // }
  22. // , readFileSync : function(filepath){
  23. // var objFileToRead = fso.OpenTextFile(filepath,1)
  24. // var strFileText = objFileToRead.ReadAll()
  25. // objFileToRead.Close()
  26. // }
  27. // , unlinkSync : function(filepath){ fso.DeleteFile(filepath) }
  28. // }
  29. // var path = {
  30. // resolve : function(path){ return fso.GetAbsolutePathName(path) }
  31. // , dirname : function(filepath) {
  32. // var normalized = this.normalize(filepath)
  33. // var li = normalized.lastIndexOf("\\")
  34. // if( li > -1) {
  35. // return normalized.substring(0, li)
  36. // }
  37. // }
  38. // , normalize : function(path){
  39. // return path.replace(/\//g,'\\');
  40. // }
  41. // }
  42. // var existsSyncFolder = function(path){
  43. // return fso.FolderExists(path)
  44. // }
  45. // function nodeShellExec(cmd, cargs){
  46. // var p = new Promise(function(resolve, reject){
  47. // var runFile = selectedinstance.root + '\\' + stampedFilePfx(new Date()) + cmd + cargs + "out.txt";
  48. // // console.log(runFile)
  49. // shell.ShellExecute('cmd', '/c ' + cmd + ' ' + cargs + " > " + runFile , "", "", 1);
  50. // // var WshFinished = 1
  51. // // var WshFailed = 2
  52. // // var strOutput = 'Did not work'
  53. // // switch(shell.Status){
  54. // // case WshFinished :
  55. // // strOutput = oShell.StdOut.ReadAll;
  56. // // break;
  57. // // case WshFailed :
  58. // // strOutput = oShell.StdErr.ReadAll;
  59. // // break;
  60. // // }
  61. // // WScript.Echo(strOutput)
  62. // while(!existsSync(runFile)) { wait(500, function(){}) }
  63. // var strFileText = fs.readFileSync(runFile)
  64. // // console.log(strFileText)
  65. // fs.unlinkSync(runFile)
  66. // var result = { result : 'cmd /c ' + cmd + ' ' + cargs + " > " + runFile + ' exited with code ???', messages : [].push(strFileText), code : 0, success : true }
  67. // console.log('resolving ' + result)
  68. // resolve(result)
  69. // })
  70. // return p;
  71. // }
  72. var __Promise = {};
  73. // var ovrrides = {
  74. // resolve : function(v){
  75. // if(v && v.then) return v;
  76. // var p = new Promise(function(resolve, reject){ resolve(v) });
  77. // return p;
  78. // }
  79. // };
  80. try{ __Promise = Promise }
  81. catch(e){
  82. // __Promise = ovrrides
  83. }
  84. function createPromiseClass(overrides) {
  85. function reject(e){
  86. var p = this;
  87. console.error(e)
  88. if(p.state !== PromiseClass.PENDING) { console.error ('Error : Promise Rejection can only be called once')}
  89. var __i = 0;
  90. var __e = e;
  91. do {
  92. for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) break;
  93. try {
  94. for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) { p.chain[i](__e); break; }
  95. __i++;
  96. __e = null;
  97. }
  98. catch(e){ __i++; __e = e}
  99. } while(__e)
  100. do {
  101. try { for(var i = __i; i < p.chain.length; i++, __i = i) if(!p.chain[i].isCatch) { p.result = p.chain[i](p.result); } }
  102. catch(e){
  103. __i ++;
  104. do {
  105. try {
  106. for(var i = __i; i < p.chain.length; i++, __i = i) if(p.chain[i].isCatch) { p.chain[i](__e); break; }
  107. __i ++;
  108. __e = null;
  109. }
  110. catch(e){ __i++; __e = e}
  111. } while(__e)
  112. }
  113. } while ( __i < p.chain.length )
  114. p.state = PromiseClass.REJECTED;
  115. }
  116. function resolve(result){
  117. // console.log(result + ' resolve was called ' + p.chain.length )
  118. var p = this;
  119. if(p.state !== PromiseClass.PENDING) { console.error ('Error : Promise Resolve can only be called once')}
  120. p.result = result;
  121. if(PromiseClass.resolve(p.result) === p.result) {
  122. waitForResult(p.result, processchain.bind(p))
  123. }
  124. else {
  125. // p.state = PromiseClass.FULFILLED;
  126. return processchain.bind(p)(result)
  127. }
  128. };
  129. function processchain(r){
  130. var __i = 0;
  131. var i = __i;
  132. var __e = null;
  133. p = this;
  134. function __processchain(r){
  135. function waitForThen(p){
  136. if(i < p.chain.length) {
  137. if(!p.chain[i].isCatch) {
  138. // console.log(i + ' Executing : then ' + p.result + ' ' + p.chain[i])
  139. try {
  140. p.result = p.chain[i](p.result)
  141. if(PromiseClass.resolve(p.result) === p.result) {
  142. waitForResult(p.result, function(r){
  143. p.result = r; i++; __i = i;
  144. waitForThen(p)
  145. })
  146. }
  147. else {
  148. i++; __i = i;
  149. waitForThen(p)
  150. }
  151. }
  152. catch(e) {
  153. i++; __i = i;
  154. __e = e;
  155. console.log('failed on index ' + __i + p.chain[__i-1] )
  156. console.dir(e)
  157. waitForCatch(p);
  158. }
  159. }
  160. else {
  161. // console.log(i + ' Skipping catch : ' + p.result + ' ' + p.chain[i])
  162. i++; __i = i;
  163. waitForThen(p)
  164. }
  165. }
  166. else return p.state = PromiseClass.FULFILLED;
  167. }
  168. function waitForCatch(p) {
  169. if(i < p.chain.length) {
  170. if(p.chain[i].isCatch) {
  171. console.log(i + ' Executing : catch : ' + p.result + ' ' + p.chain[i])
  172. try {
  173. p.result = p.chain[i](__e);
  174. if(PromiseClass.resolve(p.result) === p.result) {
  175. waitForResult(p.result, function(r){
  176. p.result = r; i++; __i = i;
  177. })
  178. waitForThen(p)
  179. }
  180. else {
  181. p.result = r; i++; __i = i;
  182. waitForThen(p)
  183. }
  184. }
  185. catch(e){ i++; __i = i;; __e = e; waitForCatch(p) }
  186. }
  187. else {
  188. i++; __i = i;
  189. waitForCatch(p)
  190. }
  191. }
  192. else return p.state = PromiseClass.REJECTED
  193. }
  194. waitForThen(p);
  195. }
  196. __processchain(r)
  197. }
  198. var create = function(fn){
  199. var p = {
  200. then : function(thenfn){
  201. thenfn.isThen = true
  202. p.chain.push(thenfn)
  203. return p;
  204. }
  205. , pcatch : function(catchfn) {
  206. catchfn.isCatch = true
  207. p.chain.push(catchfn)
  208. return p;
  209. }
  210. , start : function(){
  211. if(this.started) {
  212. console.error('Cannot start more than once...')
  213. return p
  214. };
  215. this.started = true;
  216. this.fn = fn;
  217. try { fn(resolve.bind(p), reject.bind(p)) }
  218. catch(e){ reject(e) }
  219. return p
  220. }
  221. , state : PromiseClass.PENDING
  222. , chain : []
  223. }
  224. p.catch = p.pcatch
  225. p.chain.forEach = forEach;
  226. return p;
  227. }
  228. var PromiseClass = function(fn){
  229. var p = create(fn)
  230. setTimeout(p.start.bind(p), 0)
  231. return p
  232. }
  233. PromiseClass.PENDING = 1
  234. PromiseClass.FULFILLED = 2
  235. PromiseClass.REJECTED = 3
  236. PromiseClass.STARTED = 4
  237. PromiseClass.isSETTLED = function(p){ p.state === PromiseClass.FULFILLED || p.state === PromiseClass.REJECTED }
  238. PromiseClass.resolve = function(v){
  239. if(v && v.then) return v;
  240. var p = create(function(){});
  241. p.result = v;
  242. wait(0, function(){ processchain.bind(p)(v) })
  243. return p;
  244. }
  245. function waitForResult(p, cb){
  246. if(!p) return cb(p)
  247. if(p.state !== PromiseClass.PENDING) cb(p.result)
  248. if(p.runFile && false) {
  249. while(!existsSync(p.runFile) && p.state === PromiseClass.PENDING) {
  250. console.log('Waiting for ResultFle'); wait(500, function(){})
  251. }
  252. cb(p.result)
  253. }
  254. else {
  255. // while(p.state === PromiseClass.PENDING) {
  256. // console.log('Waiting for Result')
  257. function waiter(){
  258. // console.log(p.result)
  259. if(p.state === PromiseClass.PENDING) wait(500, waiter);
  260. else return cb(p.result)
  261. }
  262. wait(500, waiter)
  263. // }
  264. }
  265. }
  266. PromiseClass.all = function(arr){
  267. arr.forEach = forEach;
  268. var resultPs = [];
  269. var results = [];
  270. console.log('All : ' + arr.length)
  271. var pAll = new PromiseClass(function(resolve, reject){
  272. arr.forEach(function(p){
  273. if(!p.then) { var pfn = p; p = new PromiseClass(function(resolve, reject){ pfn() }) }
  274. else {
  275. // !p.start ? p.start = function(){return p} : null
  276. // p.start()
  277. // .then( function(){ waitForResult(p, function(r){ results.push(r) }) })
  278. }
  279. resultPs.push(p)
  280. // waitForResult(p, function(r){ results.push(r) })
  281. })
  282. // PB : TODO -- This is the same as processchain!!!
  283. var allwaitr = function(){
  284. var allResolved = true
  285. for(var rIdx =0; rIdx < resultPs.length; rIdx++ ){
  286. if(resultPs[rIdx]) {
  287. allResolved = false
  288. waitForResult(resultPs[rIdx], function(r){ results[rIdx] = r; resultPs[rIdx] = null; })
  289. break;
  290. }
  291. }
  292. if(allResolved) resolve(results);
  293. else wait(500, allwaitr)
  294. }
  295. wait(500, allwaitr)
  296. })
  297. // pAll.chain = arr;
  298. return pAll
  299. }
  300. // PromiseClass.resolve = overrides.resolve;
  301. Promise = PromiseClass;
  302. return PromiseClass;
  303. }
  304. createPromiseClass(/*ovrrides*/)
  305. // any = function(arr){
  306. // arr.forEach = forEach;
  307. // var results = [];
  308. // console.log('Any : ' + arr.length)
  309. // return new Promise(function(resolve, reject){
  310. // arr.forEach(function(p){
  311. // console.log('P is : ' + p)
  312. // if(!p.then) { var pfn = p; p = new Promise(function(resolve, reject){ try{ resolve(pfn()) } catch(e){reject(e) } }).start() }
  313. // else {
  314. // !p.start ? p.start = function(){return p} : null
  315. // p.start()
  316. // }
  317. // // results.push(waitForResult(p))
  318. // } )
  319. // resolve(results)
  320. // }).start();
  321. // }
  322. // var any = function(iterable, continueOnFailure) {
  323. // var cancelsignal = Symbol()
  324. // return iterable.reduce(
  325. // function(p, tasq, i ,a) {
  326. // var handleError = function(err, pVal){
  327. // if(err !== cancelsignal) {
  328. // // Cancel only once on first failure.
  329. // console.log('Failed : ' + err.message + ' ' + JSON.stringify(pVal))
  330. // console.dir(p)
  331. // console.warn('Possible failure for task with result : ' + JSON.stringify(pVal))
  332. // if(i>0 && a[i-1].info) console.dir(a[i-1].info)
  333. // console.error('Error : ' + err.stack)
  334. // console.error(a[i-1])
  335. // a[i-1] ? console.log("tasq : " + a[i-1].toString()) : null;
  336. // if(!continueOnFailure) {console.log("Cancelling remaining on any one failure ..."); throw cancelsignal}
  337. // else return pVal;
  338. // // tasq ? console.log("tasq : " + tasq.toString()) : null; // Previous task is what failed not the one we are going to run.
  339. // }
  340. // }
  341. // if(Promise.resolve(p) === p ) {
  342. // if(i>0 && a[i-1].info) p.info = a[i-1].info;
  343. // return p.then( function(pVal){
  344. // // Falsy values are no longer treated as task failure exceptions. Specially for Promises.
  345. // // Even tasq function wrappers are required to return promises that eventually either resolve or reject..
  346. // // Failures are known for promises on reject.
  347. // // In future if we support direct sync function execution with a result examination for failure
  348. // // we could examine the result of the function as falsy's... or a result evaluator handler needs to be passed in...
  349. // // if(!pVal) handleError({ error : true, message : 'Failed without result' }, pVal)
  350. // // Truthy values are failures if obj has error=true.
  351. // if(pVal && pVal.error) handleError(pVal, pVal)
  352. // var trycall = function(tasq){
  353. // try {
  354. // var result = tasq() // PB : TODO -- Handle scope for call
  355. // if(tasq.resultHandler) return tasq.resultHandler(result)
  356. // // if(!result) throw result; // Default failure detection for functions is falsy values.
  357. // return result
  358. // } catch (error) {
  359. // console.error(error);
  360. // console.error('Error : ' + error ? error.stack : 'No stack')
  361. // if(!continueOnFailure) throw error; // PB : TODO -- Support array of results for any with or without continueonfailure.
  362. // }
  363. // }
  364. // var handleNext = function(){
  365. // console.log('Task finished with result : ' + JSON.stringify(pVal))
  366. // if(i>0 && a[i-1].info) console.dir(a[i-1].info)
  367. // if(!tasq && !continueOnFailure) { console.log('Error : No task specified.'); throw false;}
  368. // else if(!tasq) { console.log('Error : No task specified.'); return false;}
  369. // return (Promise.resolve(tasq) === tasq ) ? tasq : trycall(tasq) ;
  370. // }
  371. // if(Promise.resolve(pVal) === pVal) {
  372. // // Passed in function retured a promise. We still need to wait for it.
  373. // pVal.then(function(pVal){ return handleNext(); })
  374. // }
  375. // else return handleNext()
  376. // }).pcatch(function(error) {
  377. // if(error !== cancelsignal) {
  378. // console.log(`E3 : i = ${i} `);
  379. // if(error.result) console.error(`error.result`)
  380. // console.error('Error : ' + (error.message || error.messages))
  381. // console.error('Error : ' + error.stack)
  382. // tasq ? console.log("tasq : " + tasq.toString()) : null;
  383. // console.log('debugData 3-------------------------');
  384. // // handleError()
  385. // throw error
  386. // }
  387. // else throw cancelsignal;
  388. // })
  389. // }
  390. // else if(!p) {
  391. // handleError({ error : true, message : 'Failed without result' }, pVal)
  392. // console.log("Bypass remaining on prior failure");
  393. // return false; // All remaining tasks will return false in the any results even if they are promisies still running or functions not initiated.
  394. // }
  395. // else return p; // A truthy value
  396. // }
  397. // , Promise.resolve(true)
  398. // );
  399. // }
  400. // --------------------------------------------
  401. // --------------------------------------------
  402. // Node
  403. var wait = function(ms, cb) {
  404. return new __Promise(function(resolve){ setTimeout(resolve, ms) } ).then(function(){ cb() });
  405. }
  406. const fs = require('fs')
  407. const { existsSync } = require('fs');
  408. const existsSyncFolder = existsSync
  409. const path = require('path');
  410. var cli = require('./cliverse')
  411. var nodeShellExec = cli.nodeShellExec;
  412. var utils = require('bbhverse');
  413. var any = utils.any;
  414. // --------------------------------------------
  415. var callsheltask = function(args) { return function() { return nodeShellExec.apply(null, args) } }
  416. var gitUser = 'guest';
  417. var gitEmail = 'guest@bbh.org.in';
  418. var BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
  419. var runtimestamp = (new Date()).getTime();
  420. function getVersion() { return BUILD_VERSION; }
  421. console.log(getVersion())
  422. function forEach(eachFn){
  423. for(var i=0; i<this.length; i++) eachFn(this[i])
  424. }
  425. var stampedFilePfx = function(date) {
  426. return date.getFullYear() +
  427. ('0' + (date.getMonth() + 1)).slice(-2) +
  428. ('0' + date.getDate()).slice(-2) +
  429. ('0' + date.getHours()).slice(-2) +
  430. ('0' + date.getMinutes()).slice(-2) +
  431. ('0' + date.getSeconds()).slice(-2);
  432. }
  433. // Detect or specify install directory.
  434. var selectedinstance = { root : path.resolve(".") }
  435. var downloadsdir = selectedinstance.root + '/Downloads';
  436. function ensureDirectoryExistence(filePath) {
  437. var dirname = path.dirname(filePath);
  438. if (existsSyncFolder(dirname)) {
  439. return filePath;
  440. }
  441. ensureDirectoryExistence(dirname);
  442. fs.mkdirSync(dirname);
  443. return filePath;
  444. }
  445. var getTaskCheckExists = function(command, options) {
  446. options = options || {}
  447. return function() {
  448. var runFile = path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/' + stampedFilePfx(new Date()) + command + "out.txt");
  449. var p = nodeShellExec.apply(null, ['where', [command]])
  450. p.runFile = runFile;
  451. var catchr = 'pcatch'
  452. if(p['catch']) { catchr = 'catch' } else { catchr = 'pcatch';}
  453. if (options.ignorefailures) {
  454. return p.then(function(v) {
  455. // WScript.Echo('firstThen ' + v);
  456. return v })[catchr]( function(e) {
  457. console.error(e);
  458. // Ignore. Not a major error if where command fails !!!
  459. throw e;
  460. })
  461. }
  462. else return p.then(function() {
  463. // WScript.Echo('firstThen ddd');
  464. return v });
  465. }
  466. }
  467. function verifyAndInstallPrerequisites() {
  468. fs.writeFileSync(ensureDirectoryExistence(downloadsdir + '/readme.txt'), getVersion() + ' Your local downloads for this instance');
  469. var downloadbatch =
  470. "::************************************************************************** \r\n \
  471. :Download_ <url> <File> \r\n \
  472. Powershell.exe ^\r\n \
  473. $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^\r\n \
  474. [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^\r\n \
  475. (New-Object System.Net.WebClient).DownloadFile('%1','%2') \r\n \
  476. exit /b \r\n \
  477. ::**************************************************************************";
  478. ensureDirectoryExistence(path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/readme.txt'))
  479. fs.writeFileSync(path.normalize(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/download.bat'), downloadbatch);
  480. var downloadtasks = [];
  481. var installtasks = [];
  482. prerequisites.forEach(function(preq) {
  483. var p = preq.exists().then(function(exists) {
  484. if (exists && false) console.log( preq.shellcmd + ' exists');
  485. else {
  486. console.log(preq.shellcmd + ' is not installed');
  487. return preq.preinstallsteps().then(function(){
  488. console.log(' task.install : ' + preq.install)
  489. installtasks.push(preq.install.bind(preq));
  490. })
  491. }
  492. })
  493. // .then(function(res){ console.log( 'preinstallsteps ' + res)});
  494. downloadtasks.push( p )
  495. });
  496. return Promise.all(downloadtasks).then(function(){
  497. console.log('calling install tasks')
  498. return any(installtasks) })
  499. }
  500. // var choiceHandler = function(choices, choice) {
  501. // console.log('chosen : ' + choice)
  502. // var decision = choices['d'];
  503. // if (choice && choice === 'd' || !choice) {
  504. // decision = choices['d']
  505. // }
  506. // else if (isNaN((+choice))) {
  507. // decision = choice
  508. // }
  509. // else decision = choices[choice-1]
  510. // if(!decision) throw 'Invalid selection : ' + decision
  511. // return decision
  512. // }
  513. var prerequisites = [
  514. {
  515. shellcmd: 'git',
  516. url: 'https://github.com/git-for-windows/git/releases/download/v2.33.0.windows.2/Git-2.33.0.2-64-bit.exe'
  517. , installer: 'Git-2.33.0.2-64-bit.exe'
  518. , installcmd: ['cmd', ['/c', 'start',
  519. '/WAIT', downloadsdir + '/' + 'Git-2.33.0.2-64-bit.exe'
  520. , '/VERYSILENT'
  521. // , '/MERGETASKS=!runcode' // This is required only for vscode...
  522. ]]
  523. , preinstallsteps: function() {
  524. var self = this;
  525. console.log('Git preinstall steps')
  526. var steps = [];
  527. steps.push(
  528. function(){
  529. var choices = { 0 : 'guest', 1 : 'chessdemo' }
  530. return cli.prompt(choices, 'git user name', gitUser).then(function(choice){ gitUser = choice } )
  531. }
  532. )
  533. steps.push(
  534. function(){
  535. var choices = { 0 : 'guest@bbh.org.in', 1 : 'chessdemo@bbh.org.in' }
  536. return cli.prompt(choices, 'git user email', gitEmail).then(function(choice){ gitEmail = choice })
  537. }
  538. )
  539. steps.push(
  540. function(){
  541. if (!existsSync(downloadsdir + '/' + self.installer)) {
  542. return nodeShellExec(selectedinstance.root + '/.elxr/run-' + runtimestamp + '/download.bat', [self.url, downloadsdir + '/' + self.installer])
  543. }
  544. else {
  545. console.log(self.installer + ' Already exits Download skipped.')
  546. return Promise.resolve(true)
  547. }
  548. }
  549. )
  550. return any(steps)
  551. // return any([any(steps), any(prompts)])
  552. }
  553. , installsteps: function () {
  554. var self = this;
  555. return any([self.installcmd].map(callsheltask))
  556. }
  557. , postinstallsteps: function(){
  558. // PB : TODO -- Detect failure or cancellation before attenpting postinstall steps...
  559. var steps = [];
  560. steps.push(
  561. function(){
  562. var choices = { 0 : 'guest', 1 : 'chessdemo' }
  563. return cli.prompt(choices, 'git user name', gitUser).then(function(choice){ gitUser = choice } )
  564. }
  565. )
  566. steps.push(
  567. function(){
  568. var choices = { 0 : 'guest@bbh.org.in', 1 : 'chessdemo@bbh.org.in' }
  569. return cli.prompt(choices, 'git user email', gitEmail).then(function(choice){ gitEmail = choice })
  570. }
  571. )
  572. return any(steps).then(function(){
  573. var steps = [
  574. ['git', ['config', '--global', '--add', 'user.name', gitUser]]
  575. , ['git', ['config', '--global', '--add', 'user.email', gitEmail]]
  576. ]
  577. return any(steps.map(callsheltask)).then(function(){
  578. })
  579. });
  580. }
  581. , install: function () {
  582. var self = this;
  583. return any([ /*self.preinstallsteps,*/ self.installsteps.bind(self), self.postinstallsteps.bind(self)])
  584. }
  585. , verifyAndInstall : function(){
  586. var self = this;
  587. return self.exists().then( function(exits) {
  588. if(exists) return self.getUser(null, self.postinstallsteps.bind(self))
  589. else return self.install();
  590. })
  591. }
  592. , exists : function(next){
  593. var self = this;
  594. return getTaskCheckExists(self.shellcmd, { ignorefailures: true })().then(function(exists) {
  595. console.log(exists + ' git exists')
  596. if(exists && exists.messages.join(' ').indexOf(self.shellcmd) > -1 ) {
  597. return true;
  598. }
  599. else return false
  600. })
  601. }
  602. , getUser : function(repo, onNoResult){
  603. onNoResult = onNoResult || function(){return false}
  604. var globalOrLocal = '--global';
  605. if(!repo) globalOrLocal = '--global';
  606. else globalOrLocal = '--local'
  607. return any([['git', ['config', globalOrLocal, '--get-all', 'user.name']]].map(callsheltask)).then(function(result){
  608. // not yet configured.
  609. if(!result.success) return onNoResult()
  610. else {
  611. var users = result.messages[0].trim().split('\n');
  612. if(users.length === 0 ||
  613. users.length === 1 && users[0] === 'guest') {
  614. return onNoResult()
  615. }
  616. else return users[0]; // PB : TODO == We should probably prompt with all the users available for selection !
  617. }
  618. })
  619. .pcatch(function(e){
  620. console.log(e)
  621. return onNoResult()
  622. })
  623. }
  624. }
  625. ,
  626. {
  627. shellcmd: 'node',
  628. url: 'https://nodejs.org/dist/v14.16.0/node-v14.17.3-x64.msi'
  629. , installer: 'node-v14.17.3-x64.msi'
  630. , installcmd: ['MSIEXEC.exe', ['/i'
  631. , path.normalize(downloadsdir + '/' + 'node-v14.17.3-x64.msi')
  632. , 'ACCEPT=YES', '/passive']]
  633. , install : function() {
  634. var self = this;
  635. return any([self.installcmd].map(callsheltask))
  636. }
  637. , exists : function(next){
  638. var self = this;
  639. return getTaskCheckExists(self.shellcmd, { ignorefailures: true })().then(function(exists) {
  640. console.log(self.shellcmd + ' ' + exists + ' node exists')
  641. // if(exists && exists.messages.join(' ').indexOf(self.shellcmd) > -1 ) {
  642. // return true
  643. // }
  644. // else {
  645. // // console.log(self.shellcmd + ' ' + exists + ' node doesnt exist')
  646. // return false
  647. // }
  648. })
  649. }
  650. , preinstallsteps : function(){
  651. return Promise.resolve(true);
  652. }
  653. }
  654. ]
  655. prerequisites.forEach = forEach;
  656. // nodeShellExec(selectedinstance.root + '/.elxr/run-' + '1629889572461' + '/download.bat'
  657. // , ['https://github.com/git-for-windows/git/releases/download/v2.31.0.windows.1/Git-2.32.0.2-64-bit.exe'
  658. // , downloadsdir + '/' + 'Git-2.32.0.2-64-bit.exe']).start()
  659. verifyAndInstallPrerequisites()