Browse Source

fixed elxr use

production
Pradeep Bhaskaran 4 years ago
parent
commit
ad05f20506
2 changed files with 70 additions and 49 deletions
  1. 57
    42
      index.js
  2. 13
    7
      windowselevate.hta

+ 57
- 42
index.js View File

// -------------- // --------------
// elxr // elxr
// A cli tool for elixr. // A cli tool for elixr.

// PB : TODO --
// runas bypass non elevated tasks when we request privelege
// runas message is always error needs to be fixed.
// runas wait needs to be parallelized.
// suppress elevation check error messages
// support runas lauched directly from shell.
// pass in environment in hta to shellexecute.

const { existsSync } = require('fs'); const { existsSync } = require('fs');
const fs = require('fs') const fs = require('fs')
const { spawn, spawnSync } = require('child_process'); const { spawn, spawnSync } = require('child_process');
] ]


var productionRepos = [ var productionRepos = [
'elixir-config-production'
'elixir-config-production'
] ]
var productionIsAllowed = true;
if(productionIsAllowed) gitRepos = gitRepos.concat(productionRepos)


var env = Object.assign({}, process.env); // Shallow clone it. var env = Object.assign({}, process.env); // Shallow clone it.
var __runcmd = function(label){ var __runcmd = function(label){
// PB : TODO -- Convert all the cli args back to string. // PB : TODO -- Convert all the cli args back to string.
var namedArgs = []; var namedArgs = [];
Object.keys(processedArgs).forEach((v)=>{ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; }) Object.keys(processedArgs).forEach((v)=>{ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; })
var cargs = `${processedArgs.debug ? '--inspect-brk' : ''} --inspect-brk elxr ${processedArgs._.join(' ')} ${namedArgs.join(' ') } --runas=true`
console.log('Requesting Elevated Privileges for : ' + `node ${cargs}`)
//console.log(' namedArgs : ' + namedArgs)
process.env.NODE_ENV = process.env.NODE_ENV || 'development'; process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var args = [__dirname + '/windowselevate.hta'].concat(processedArgs._); args.push('--runas')
var args = [__dirname + '/windowselevate.hta'].concat(processedArgs._).concat(namedArgs.join(' ')); args.push('--runas=self');
nodeShellExec('MSHTA', [`"${args.join('" "')}"`] nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
, { , {
inherit : true
,
shell: true
// ,
inherit : true
, shell: true
, env: process.env , env: process.env
, runas : true
, runas : 'self'
, title : `runas` , title : `runas`
} }
).then(()=>{ ).then(()=>{
, 'use' : ()=>{ , 'use' : ()=>{


var runconfig = { NODE_ENV : process.env.NODE_ENV } var runconfig = { NODE_ENV : process.env.NODE_ENV }
try {
runconfig = Object.assign(runconfig, require('../run.js'))
} catch(e) { }
if(runconfig.NODE_ENV === process.env.NODE_ENV && processedArgs._[1] && runconfig.use === processedArgs._[1]) return;
try { runconfig = Object.assign(runconfig, require('../run.js')) } catch(e) { }
if((!processedArgs.runas || processedArgs.runas !== 'self') &&
runconfig.NODE_ENV && runconfig.NODE_ENV === (process.env.NODE_ENV || runconfig.NODE_ENV) &&
processedArgs._[1] && runconfig.use === processedArgs._[1]) {
console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
if(processedArgs.runas) { fs.writeFileSync('run.done', 'success') }
return
}


var tasks = [ var tasks = [
()=>{ ()=>{
else return Promise.resolve(true); else return Promise.resolve(true);
} }
]; ];
any(tasks)
if(!processedArgs.runas) return op['runas']()

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
runconfig.NODE_ENV = process.env.NODE_ENV = process.env.NODE_ENV || runconfig.NODE_ENV || 'development';
if(processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1]; if(processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
if(!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' } if(!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }

// console.log(process.env.cwd) // console.log(process.env.cwd)
fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig)) fs.writeFileSync('./run.js', 'module.exports = ' + JSON.stringify(runconfig))
tasks = [
()=>{
// Use junctions to avoid npm package issues
var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-' + process.env.NODE_ENV + '-config'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
return p;
}
];
if(processedArgs._[1]) {
tasks.concat(
[
()=>{
var p = nodeShellExec('mklink', ['/D', 'data', runconfig.use + '-data'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
return p;
}
]
)
}
any(tasks).then(()=>{ any(tasks).then(()=>{
fs.writeFileSync('run.done', 'success')
if(!processedArgs.runas) return op['runas']()

tasks = [
()=>{
// Use junctions to avoid npm package issues
var p = nodeShellExec('mklink', ['/J', 'config', runconfig.use + '-config' + '-' + process.env.NODE_ENV ], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
return p;
}
];
if(processedArgs._[1]) {
tasks = tasks.concat(
[
()=>{
var p = nodeShellExec('mklink', ['/D', 'data', runconfig.use + '-data'], {
inherit : true, shell: true
, env: process.env
}).catch((e)=>{ console.error(e) })
return p;
}
]
)
}
return any(tasks).then(()=>{
fs.writeFileSync('run.done', 'success')
}).catch(()=>{
fs.writeFileSync('run.done', 'error')
})
}).catch(()=>{ }).catch(()=>{
fs.writeFileSync('run.done', 'error') fs.writeFileSync('run.done', 'error')
}) })

+ 13
- 7
windowselevate.hta View File

var args = windowselevate.commandLine.split('"').slice(3); var args = windowselevate.commandLine.split('"').slice(3);
// alert(args) // alert(args)
var processedArgs = { _ : [] } var processedArgs = { _ : [] }
var namedArgs = [];
for(var item in args){ for(var item in args){
// alert(args[item].charAt(0))
args[item] ? args[item].charAt(0) === '-' ? processedArgs[args[item].slice(2)] = args[item].split('=')[1] || true : processedArgs._.push(args[item]) : null
if(args[item].charAt(0) === '-'){
namedArgs.push(args[item])
var split = args[item].split('=');
processedArgs[split[0].slice(2)] = split[1] || true;
}
else processedArgs._.push(args[item]);
} }
// args = args.forEach(function(item){ }) // args = args.forEach(function(item){ })
// alert('processedArgs._ : ' + processedArgs._); // alert('processedArgs._ : ' + processedArgs._);
// alert(processedArgs.runas); // alert(processedArgs.runas);
var namedArgs = [];
// Object.keys(processedArgs).forEach( function(v){ v!='_' ? namedArgs.push('--'+v+'='+processedArgs[v]) : null; })
var cargs = (processedArgs.debug ? '--inspect-brk' : '') + ' elxr ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ') + ' ' + ' --runas=true';
// PB : TODO -- Convert all the cli args back to string.
var cargs = (processedArgs.debug ? '--inspect-brk=9228' : '') + ' elxr ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ');
// alert(cargs)
var shell = new ActiveXObject('shell.application'); var shell = new ActiveXObject('shell.application');
shell.ShellExecute('node', cargs, '', 'runas', 1); shell.ShellExecute('node', cargs, '', 'runas', 1);
var fso = new ActiveXObject('Scripting.FileSystemObject'); var fso = new ActiveXObject('Scripting.FileSystemObject');
document.body.style.backgroundColor = 'black'; document.body.style.backgroundColor = 'black';
document.body.style.fontFamily = 'arial'; document.body.style.fontFamily = 'arial';
var log = document.createElement('div'); var log = document.createElement('div');
log.innerHTML='Please Wait';
function l(msg){ log.innerHTML+= msg; }; function l(msg){ log.innerHTML+= msg; };
log.style.color = 'blue'; log.style.color = 'blue';
log.style.width = '95%';
log.id = 'log'; log.id = 'log';
document.body.appendChild(log); document.body.appendChild(log);
log.innerHTML='Please Wait';
l('<Br/>Current config : ') l('<Br/>Current config : ')
l('<Br/>NODE_ENV = ' + NODE_ENV) l('<Br/>NODE_ENV = ' + NODE_ENV)
l('<Br/>cmd = ' + processedArgs._[0]) l('<Br/>cmd = ' + processedArgs._[0])
processedArgs._[1] === 'use' ? l('<Br/>using = ' + processedArgs._[2]) : null; processedArgs._[1] === 'use' ? l('<Br/>using = ' + processedArgs._[2]) : null;
l('<Br/><Br/>') l('<Br/><Br/>')
var timer = function(){ var timer = function(){
l('.'); l('.');
if(fso.FileExists("run.done")) close(); if(fso.FileExists("run.done")) close();

Loading…
Cancel
Save