Skip to content
Permalink
Browse files

Share terminal width with child processes

Currently, `cli-width` reports `0` (the default value) within scripts
called by `windosu`.

Test:

```js
// test.js
var cliWidth = require('cli-width');
console.log(cliWidth());
```

***

```sh
> node bin\sudo node test.js
120
```

Fixes: #4
  • Loading branch information...
Juan Cruz Viotti
Juan Cruz Viotti committed Oct 16, 2015
1 parent e7d3494 commit 0a4b1df806b4acc3b83b6d0ea1f1141ee57cbffe
Showing with 75 additions and 98 deletions.
  1. +70 −98 lib/windosu.js
  2. +1 −0 package.json
  3. +2 −0 src/windosu.latte
  4. +1 −0 windosu-elevate.cmd
  5. +1 −0 windosu-runas.cmd
@@ -1,112 +1,84 @@
var __when = function (v, n) {
return v && typeof v.then == 'function' ? v.then(n) : n(v);
};
var child_process = require('child_process'), Q = require('q'), fs = require('fs'), path = require('path'), Tail = require('tail').Tail, pipe = require('./pipe');
function __when(v, n) {
return v && typeof v.then === 'function' ? v.then(n) : n(v);
}
var child_process = require('child_process'), Q = require('q'), fs = require('fs'), path = require('path'), Tail = require('tail').Tail, cliWidth = require('cli-width'), pipe = require('./pipe');
module.exports.exec = function (command, options, callback) {
options = options || {};
var promise = function () {
{
return __when(Q.nfcall(fs.readFile, path.join(__dirname, '..', options.user ? 'windosu-runas.cmd' : 'windosu-elevate.cmd'), 'ascii'), function (__t0) {
var bat = __t0;
var id = '.windosu.' + new Date().getTime();
var temp = path.join(options.user ? 'C:\\temp' : process.env.TEMP, id);
var inputName, outputName;
var temps = { error: temp + '.err' };
var replacements = {
dir: process.cwd(),
temp: temp,
command: command,
pipe: '"' + process.execPath + '" "' + path.join(__dirname, 'pipe.js') + '"',
input: inputName = id + '-in',
output: outputName = id + '-out',
invisible: path.join(__dirname, '..', 'invisible.vbs'),
stderr_redir: process.stdout.isTTY ? '2>&1' : '2> %ERROR%'
};
for (var n in temps)
replacements[n] = temps[n];
for (var n in replacements) {
bat = bat.replace('{{ ' + n + ' }}', replacements[n]);
}
var batOut = temps.BAT = temp + '-elevate.bat';
return __when(Q.nfcall(fs.writeFile, batOut, bat), function (__t1) {
__t1;
return __when(Q.nfcall(fs.writeFile, temps.error, ''), function (__t2) {
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
__t2;
var inputPromise = pipe(inputName, { serve: true });
var outputPromise = pipe(outputName, {
serve: true,
read: true
return __when(Q.nfcall(fs.readFile, path.join(__dirname, '..', options.user ? 'windosu-runas.cmd' : 'windosu-elevate.cmd'), 'ascii'), function (__t0) {
var bat = __t0;
var id = '.windosu.' + new Date().getTime();
var temp = path.join(options.user ? 'C:\\temp' : process.env.TEMP, id);
var inputName, outputName;
var temps = { error: temp + '.err' };
var replacements = {
dir: process.cwd(),
temp: temp,
command: command,
cliWidth: cliWidth(),
pipe: '"' + process.execPath + '" "' + path.join(__dirname, 'pipe.js') + '"',
input: inputName = id + '-in',
output: outputName = id + '-out',
invisible: path.join(__dirname, '..', 'invisible.vbs'),
stderr_redir: process.stdout.isTTY ? '2>&1' : '2> %ERROR%'
};
for (var n in temps)
replacements[n] = temps[n];
for (var n in replacements) {
bat = bat.replace('{{ ' + n + ' }}', replacements[n]);
}
var batOut = temps.BAT = temp + '-elevate.bat';
return __when(Q.nfcall(fs.writeFile, batOut, bat), function (__t1) {
__t1;
return __when(Q.nfcall(fs.writeFile, temps.error, ''), function (__t2) {
__t2;
var inputPromise = pipe(inputName, { serve: true });
var outputPromise = pipe(outputName, {
serve: true,
read: true
});
var tail = !process.stdout.isTTY && new Tail(temps.error);
if (tail) {
tail.on('line', function (line) {
console.error(line);
});
tail.on('error', function (err) {
console.error(err);
});
}
return function () {
return __when(options.user ? function () {
return __when(spawn('runas.exe', [
'/netonly',
'/user:' + options.user,
batOut
]), function (__t3) {
__t3;
});
var tail = !process.stdout.isTTY && new Tail(temps.error);
if (tail) {
tail.on('line', function (line) {
console.error(line);
}.call(this) : function () {
return __when(Q.nfcall(child_process.exec, 'cmd /C ' + batOut), function (__t4) {
__t4;
});
tail.on('error', function (err) {
console.error(err);
}.call(this), function (__t5) {
__t5;
return __when(outputPromise, function (__t6) {
__t6;
});
});
}.call(this).fin(function () {
if (tail)
tail.unwatch();
for (var n in temps) {
fs.unlink(temps[n]);
}
return function () {
{
return __when(options.user ? function () {
{
return __when(spawn('runas.exe', [
'/netonly',
'/user:' + options.user,
batOut
]), function (__t3) {
;
__t3;
});
}
}() : function () {
{
return __when(Q.nfcall(child_process.exec, 'cmd /C ' + batOut), function (__t4) {
;
__t4;
});
}
}(), function (__t5) {
__t5;
;
return __when(outputPromise, function (__t6) {
;
;
__t6;
});
});
}
}().fin(function () {
if (tail)
tail.unwatch();
for (var n in temps) {
fs.unlink(temps[n]);
}
return inputPromise.then(function (input) {
return input.closeAndWait();
});
return inputPromise.then(function (input) {
return input.closeAndWait();
});
});
});
});
}
}();
});
}.call(this);
if (callback) {
promise.then(function (r) {
return callback(null, r);
@@ -24,6 +24,7 @@
},
"dependencies": {
"temp": "~0.6.0",
"cli-width": "^1.1.0",
"q": "~0.9.7",
"tail": "~0.3.1"
},
@@ -4,6 +4,7 @@ var child_process = require('child_process'),
fs = require('fs'),
path = require('path'),
Tail = require('tail').Tail,
cliWidth = require('cli-width'),
pipe = require('./pipe');


@@ -27,6 +28,7 @@ module.exports.exec = function (command, options, callback) {
dir: process.cwd(),
temp: temp,
command: command,
cliWidth: cliWidth(),
pipe: "\""+process.execPath+"\" \"" + path.join(__dirname, "pipe.js") + '"',
input: inputName = id + '-in',
output: outputName = id + '-out',
@@ -10,6 +10,7 @@ Set DIR={{ dir }}
Set INPUT={{ input }}
Set OUTPUT={{ output }}
Set ERROR={{ error }}
Set CLI_WIDTH={{ cliWidth }}
Set PIPE={{ pipe }}

set FIND=%SystemRoot%\System32\Find
@@ -6,6 +6,7 @@
Set DIR={{ dir }}
Set INPUT={{ input }}
Set OUTPUT={{ output }}
Set CLI_WIDTH={{ cliWidth }}
Set PIPE={{ pipe }}

:: Hide the window

0 comments on commit 0a4b1df

Please sign in to comment.
You can’t perform that action at this time.