|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- const { any } = require('bbhverse');
- const fs = require('fs')
-
- var cli = require('./cliverse')
- var nodeShellExec = cli.nodeShellExec;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var __isElevated = null;
- var shell_verse = {
-
- getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
- , runTask : ( taskToRun ) => {
- if (__isElevated) return shell_verse.runElevated( taskToRun )
- else return shell_verse.runNonElevated( taskToRun )
- }
-
- , elevatedRunner( taskToRun ){
-
- try {
-
- var __runasresult = null;
- return taskToRun().then((r)=>{
-
-
-
- return __runasresult = r;
- })
- .catch((e) => {
-
-
- console.error(e)
- })
- .finally(() => {
-
- })
- }
- catch (e) {
- console.error('Error Invalid command : ' + e)
-
- }
- finally {
- }
- }
- , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
- , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
- , runElevatedInBatch : ( taskToRun ) => {
- if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
- else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
- }
- , runElevated : ( taskToRun ) => {
-
-
-
-
-
- if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
-
- if(__isElevated) {
- return shell_verse.elevatedRunner(taskToRun)
- }
- else {
- console.log('Requesting Elevated Privileges');
-
-
- return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
- }
- }
- , runElevatedBatch( batchToRun ){
-
-
- batchToRun.forEach(element => {
- element.processedArgs = batchToRun[0].processedArgs
- });
- return any(batchToRun.map(shell_verse.runElevated))
- }
-
- , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
- , runNonElevated : ( taskToRun ) => {
-
- if(__isElevated) {
- return Promise.resolve( 'Skipping regular task in elevated shell.' )
- }
- else {
-
-
-
- return taskToRun().then(r=>{
- taskToRun.statuslog.statuslog(null, taskToRun.info )
- return r;
- }).catch((e) => {
- e.info = taskToRun.info;
- if(taskToRun.errHandler) throw taskToRun.errHandler(e)
- taskToRun.statuslog.statuslog(e);
-
- throw e;
- }).finally(()=>{})
- }
- }
-
- , isElevated : ()=>{
- return acquireElevationState().then( ()=>{
- shell_verse.isElevated = () => {
- return Promise.resolve(__isElevated)
- }
- return shell_verse.isElevated()
- })
- }
-
-
-
- , acquireElevationState : () => {
- return Promise.resolve(false).then(( elevationstate ) => {
- __isElevated ? console.log('Elevated') : console.log('Not Elevated')
- __isElevated = elevationstate;
- shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
- shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
- return __isElevated
- }).catch(() => {
- __isElevated = false;
- shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
- shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
- console.log('Not Elevated');
- return __isElevated
- })
-
-
-
-
-
- }
-
- , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'which')
-
- , getbash : ()=>{ return "sh" }
-
- , createJuntionOrLink : (dirOrFile, target, opts)=>{
- return nodeShellExec('ln', ['-s', target, dirOrFile], opts).catch((e) => { console.error(e) })
- }
-
- , removeJuncionOrLink : ( junctionOrLink )=>{
- return nodeShellExec('rm', [junctionOrLink], { inherit: true, shell: true, env: process.env })
- }
-
- , requestElevation(elevatedRunner, taskToRun) {
-
-
-
- return elevatedRunner(taskToRun);
- }
- , iswin(){ return false}
- , islin(){ return true}
- }
-
- module.exports = shell_verse
|