// -------------- // elxr // A cli tool for elixr. const { existsSync } = require('fs'); const { spawn, spawnSync } = require('child_process'); const cliargs = require('../elxr/cliargs'); // Use minimist... const processedArgs = cliargs(process.argv.slice(2)); console.dir(processedArgs) // Serialize a set of functions that will execute to return a promises one after the other. // Will stop when any one fails. function any(iterable) { return iterable.reduce( (p, fn, i ,a) => { // console.log('accumulator :'); // console.log(p); if(Promise.resolve(p) === p ) { return p.then((pVal) => { if(Promise.resolve(pVal) === pVal) { // Passed in function retured a promise. We still need to wait for it. pVal.then((pVal)=>{ // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p); if(!pVal && !continueOnFailure) { console.error("E : pVal :" + pVal); // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]); // a.map((i)=>console.error(i.toString())); console.error('debugData 3 -------------------'); // console.error(debugData); console.error('debugData 3 -------------------'); console.log("Cancelling remaining..."); throw 'Failed in reduce 1 ' return false; } // console.log('calling fn : '+ fn); return (Promise.resolve(fn) === fn ) ? fn : fn() ; }) } else { // console.log('Then --pVal = ' + pVal + ' bContinue = ' + continueOnFailure ); console.log(p); if(!pVal && !continueOnFailure) { console.error("E : pVal :" + pVal); // i==0 ? null : console.error("E :1 a[i-1] " + a[i-1]); // a.map((i)=>console.error(i.toString())); console.error('debugData 3 -------------------'); // console.error(debugData); console.error('debugData 3 -------------------'); console.log("Cancelling remaining..."); throw 'Failed in reduce 1 ' return false; } // console.log('calling fn : '+ fn); return (Promise.resolve(fn) === fn ) ? fn : fn() ; } }).catch((error) => { console.error("E : " + error); fn ? console.error("Fn : " + fn.toString()) : null; // i==0 ? null : console.error("E :2 a[i-1] " + a[i-1]); // a.map((i)=>console.error(i.toString())); console.error('debugData 4-------------------------'); // console.error(debugData); console.error('debugData 4-------------------------'); throw 'Failed in reduce 2 ' return false; }) } else if(!p) { console.log("Bypass on failure"); return false; } } , Promise.resolve(true) ); } var cli = 'elxr'; var ver = '#unversioned'; var help = '#unkown list of commands... please refer dveloper documentation for ' + cli; // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"} var dbForLabel = function(label){ var dbsForLabel = { devmysql : 'mysql' , development : 'mssql' , production : 'mssql' } return dbsForLabel[label] || 'mysql' } var __runcmd = function(label){ var op = { 'h' : ()=>{ console.log(cli + ' ' + ver + ' ' + help); return '-h' } , 'upgrade' : ()=>{ console.log('upgrade.......') var tasks = [ ()=>{ var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], { inherit : true, shell: true , env: process.env }) p.position = 1; console.log('One') return p; } , ()=>{ var p = nodeShellExec('npm', ['cache', 'clean', '-f'], { inherit : true, shell: true , env: process.env }) p.position = 2; console.log('Two') return p; } , ()=>{ var p = nodeShellExec('npm', ['install', '-g', 'n'], { inherit : true, shell: true , env: process.env }) p.position = 3; console.log('Three') return p; } , ()=>{ var p = nodeShellExec('n', ['latest'], { inherit : true, shell: true , env: process.env }) p.position = 4; console.log('Four') return p; } ] any(tasks) console.log('.......done') } , 'pull' : ()=>{ // Clone if not exists var gitclonables = [ 'ember-masonry-grid' , 'bbhverse' , 'clientverse' , 'serververse' , 'client' , 'elixir-client' , 'elixir-client-unlinked' , 'ember-searchable-select' , 'loopback-component-jsonapi' , 'config' , 'loopback-connector-ds' , 'chess-server-lib' , 'elixir-server' , 'chess-server' , 'setup' ] gitclonables.forEach(clonable => { console.log('' + process.cwd()) if(existsSync(clonable)) { console.log('pulling ' + clonable) nodeShellExec('git', ['pull'], { inherit : true, shell: true , cwd : clonable , env: process.env }) } else { console.log('cloning...') nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + clonable + '.git']) } }) console.log('pull') } , 'start' : (label)=>{ console.log('Starting Elixir Server.'); var env = Object.assign({}, process.env); // Shallow clone it. // console.dir(env) env.NODE_ENV = label; env.DEBUG = 'loopback:connector:' + dbForLabel(label) nodeShellExec('node', ['--inspect=9228', 'sage-rw/server.js'], { inherit : true, shell: true, detached: true, cwd : 'loopback', env: env }) nodeShellExec('node', ['--inspect=9227', 'sage-r/server.js'], { inherit : true, shell: true, detached: true, cwd : 'loopback', env: env }) // nodeShellExec('node', ['--inspect=9226', ' bin/www'], { // inherit : true, // shell: true, detached: true, // cwd : 'qms/server', // env: env, // shell : true // }) nodeShellExec('ember', ['s'], { inherit : true, shell: true, detached: true, cwd : 'qms/', env: env, shell : true }) } , 'g' : ()=>{ if(processedArgs.h) { console.log('elxr g [modelname] => generate a model named [modelname]'); console.log('elxr g => regenerate all known models'); return } // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} ); // console.log('Starting directory: ' + process.cwd()); // try { // child = child.on('close', () => { process.chdir(label) } ); // console.log('New directory: ' + process.cwd()); // } // catch (err) { // console.log('chdir: ' + err); // } // child.on('close', function(){ // var options = { // shell : true // , inherit : true // // , cwd : '' + process.cwd // // , env : process.env // }; // nodeShellExec('git', ['init'], { inherit : true}); nodeShellExec('pwd', { inherit : true}); //$ "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --profile-directory="Profile 1" http://localhost:4200/tests/index.html?grep=loopback nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [ "--profile-directory=Profile 1" , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]); // nodeShellExec('npm', ['init', '-y'], options); // nodeShellExec('npm', ['init', '-y'], options); // }) } } return op[label] ? op[label]() : op['start'](label); } // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql // mysql -u root -p db_name < drop_all_tables.sql var mysql = '../xampp/mysql/bin/mysql' var mysqldump = '../xampp/mysql/bin/mysqldump' __runcmd(processedArgs.label || processedArgs._[0] || 'h'); // nodeShellExec('git', ['status']); function nodeShellExec() { const child = spawn(...arguments); return new Promise(function(resolve, reject){ // use child.stdout.setEncoding('utf8'); if you want text chunks child.stdout.setEncoding('utf8'); // console.log('here') child.stdout.on('data', (chunk) => console.log(chunk)); child.on('error', (chunk) => console.error(chunk)); child.stderr.pipe(process.stderr); child.on('close', (code) => { console.log(`child process ${Array.from(arguments)[0]} exited with code ${code}`) if(code !== 0 ) return reject(code) resolve(true) }); }); }