Browse Source

Updates for nodeshellexec and privileged execution

production
Pradeep Bhaskaran 4 years ago
parent
commit
b7d0f32425
1 changed files with 209 additions and 17 deletions
  1. 209
    17
      index.js

+ 209
- 17
index.js View File

@@ -2,12 +2,88 @@
// --------------
// elxr
// A cli tool for elixr.
const { spawn } = require('child_process');
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)

var path = require('path')
// 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';
@@ -33,6 +109,48 @@ 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
}).catch((e)=>{ console.error(e) })
p.position = 1;
console.log('One')
return p;
}
, ()=>{
var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
p.position = 2;
console.log('Two')
return p;
}
, ()=>{
var p = nodeShellExec('npm', ['install', '-g', 'n'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
p.position = 3;
console.log('Three')
return p;
}
, ()=>{
var p = nodeShellExec('n', ['latest'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
p.position = 4;
console.log('Four')
return p;
}
]
any(tasks)
console.log('.......done')
console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
console.info('Uninstalling existing ember globally') ;
@@ -68,13 +186,82 @@ var __runcmd = function(label){
var env = Object.assign({}, process.env); // Shallow clone it.
// console.dir(env)
console.log('Running exlr pull : ' + path.dirname(__dirname))
nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
// nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
stdio: ['pipe', process.stdout, process.stderr],
inherit : true,
shell: true,
cwd : path.dirname(__dirname),
env: env
// nodeShellExec('cmd', ['/c', 'setup\\utility\\chess.bat', 'pull'], {
// // nodeShellExec('cmd', ['/c', '..\\setup\\utility\\chess.bat', 'pull'], {
// stdio: ['pipe', process.stdout, process.stderr],
// inherit : true,
// shell: true,
// cwd : path.dirname(__dirname),
// env: env
// })

var isElevated;
try {
nodeShellExec( "fsutil", ["dirty", "query", "C:"], {
inherit : true, shell: true
, env: process.env
}).then((exitcode)=>{
console.log('Elevated')
}).catch(()=>{
console.log('Not Elevated')
console.log('Requesting Elevated Privileges')
nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]
, {
inherit : true
// , shell: true
// , env: process.env
,
title : `runsAs`
}
);
});
// isElevated = true;
}
catch ( e ) {
// isElevated = false;
}
// nodeShellExec('MSHTA', ["javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('node', 'elxr pull', '', 'runas', 1);close();"]);

// 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 => {
if(existsSync(clonable)) {
console.log('pulling ' + clonable)
nodeShellExec('git', ['pull'], {
inherit : true, shell: true
, cwd : clonable
, env: process.env
, title : `git pull ${clonable}`
}).catch((e)=>{ console.error(e) })
}
else {
console.log('cloning ' + clonable)
nodeShellExec('git', ['clone', '//172.16.0.27/repos/' + clonable + '.git'],
{
inherit : true, shell: true
, cwd : clonable
, env: process.env
, title : `git pull ${clonable}`
}).catch((e)=>{ console.error(e) })
}
})
}
, 'start' : (label)=>{
@@ -178,18 +365,23 @@ __runcmd(processedArgs.label || processedArgs._[0] || 'h');

function nodeShellExec() {

var args = Array.from(arguments);
const child = spawn(...arguments);

// 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));
return new Promise(function(resolve, reject){

child.on('error', (chunk) => console.error(chunk));
// child.stderr.pipe(process.stderr);
// use child.stdout.setEncoding('utf8'); if you want text chunks
child.stdout.setEncoding('utf8');
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}`));
return child;
child.on('close', (code) => {
console.log(` ${args[2].title} exited with code ${code}`)
if(code !== 0 ) return reject(code)
resolve(true)
});
});
}


Loading…
Cancel
Save