Skip to content

Commit

Permalink
Share terminal width with child processes
Browse files Browse the repository at this point in the history
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 committed Oct 16, 2015
1 parent e7d3494 commit 0a4b1df
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 98 deletions.
168 changes: 70 additions & 98 deletions lib/windosu.js
@@ -1,112 +1,84 @@
var __when = function (v, n) { function __when(v, n) {
return v && typeof v.then == 'function' ? v.then(n) : n(v); 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'); 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) { module.exports.exec = function (command, options, callback) {
options = options || {}; options = options || {};
var promise = function () { var promise = function () {
{ return __when(Q.nfcall(fs.readFile, path.join(__dirname, '..', options.user ? 'windosu-runas.cmd' : 'windosu-elevate.cmd'), 'ascii'), function (__t0) {
return __when(Q.nfcall(fs.readFile, path.join(__dirname, '..', options.user ? 'windosu-runas.cmd' : 'windosu-elevate.cmd'), 'ascii'), function (__t0) { var bat = __t0;
var bat = __t0; var id = '.windosu.' + new Date().getTime();
var id = '.windosu.' + new Date().getTime(); var temp = path.join(options.user ? 'C:\\temp' : process.env.TEMP, id);
var temp = path.join(options.user ? 'C:\\temp' : process.env.TEMP, id); var inputName, outputName;
var inputName, outputName; var temps = { error: temp + '.err' };
var temps = { error: temp + '.err' }; var replacements = {
var replacements = { dir: process.cwd(),
dir: process.cwd(), temp: temp,
temp: temp, command: command,
command: command, cliWidth: cliWidth(),
pipe: '"' + process.execPath + '" "' + path.join(__dirname, 'pipe.js') + '"', pipe: '"' + process.execPath + '" "' + path.join(__dirname, 'pipe.js') + '"',
input: inputName = id + '-in', input: inputName = id + '-in',
output: outputName = id + '-out', output: outputName = id + '-out',
invisible: path.join(__dirname, '..', 'invisible.vbs'), invisible: path.join(__dirname, '..', 'invisible.vbs'),
stderr_redir: process.stdout.isTTY ? '2>&1' : '2> %ERROR%' stderr_redir: process.stdout.isTTY ? '2>&1' : '2> %ERROR%'
}; };
for (var n in temps) for (var n in temps)
replacements[n] = temps[n]; replacements[n] = temps[n];
for (var n in replacements) { for (var n in replacements) {
bat = bat.replace('{{ ' + n + ' }}', replacements[n]); bat = bat.replace('{{ ' + n + ' }}', replacements[n]);
} }
var batOut = temps.BAT = temp + '-elevate.bat'; var batOut = temps.BAT = temp + '-elevate.bat';
return __when(Q.nfcall(fs.writeFile, batOut, bat), function (__t1) { return __when(Q.nfcall(fs.writeFile, batOut, bat), function (__t1) {
__t1; __t1;
return __when(Q.nfcall(fs.writeFile, temps.error, ''), function (__t2) { 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 () {
__t2; return __when(options.user ? function () {
var inputPromise = pipe(inputName, { serve: true }); return __when(spawn('runas.exe', [
var outputPromise = pipe(outputName, { '/netonly',
serve: true, '/user:' + options.user,
read: true batOut
]), function (__t3) {
__t3;
}); });
var tail = !process.stdout.isTTY && new Tail(temps.error); }.call(this) : function () {
if (tail) { return __when(Q.nfcall(child_process.exec, 'cmd /C ' + batOut), function (__t4) {
tail.on('line', function (line) { __t4;
console.error(line);
}); });
tail.on('error', function (err) { }.call(this), function (__t5) {
console.error(err); __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 inputPromise.then(function (input) {
{ return input.closeAndWait();
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();
});
}); });
}); });
}); });
}); });
} });
}(); }.call(this);
if (callback) { if (callback) {
promise.then(function (r) { promise.then(function (r) {
return callback(null, r); return callback(null, r);
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -24,6 +24,7 @@
}, },
"dependencies": { "dependencies": {
"temp": "~0.6.0", "temp": "~0.6.0",
"cli-width": "^1.1.0",
"q": "~0.9.7", "q": "~0.9.7",
"tail": "~0.3.1" "tail": "~0.3.1"
}, },
Expand Down
2 changes: 2 additions & 0 deletions src/windosu.latte
Expand Up @@ -4,6 +4,7 @@ var child_process = require('child_process'),
fs = require('fs'), fs = require('fs'),
path = require('path'), path = require('path'),
Tail = require('tail').Tail, Tail = require('tail').Tail,
cliWidth = require('cli-width'),
pipe = require('./pipe'); pipe = require('./pipe');




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


set FIND=%SystemRoot%\System32\Find set FIND=%SystemRoot%\System32\Find
Expand Down
1 change: 1 addition & 0 deletions windosu-runas.cmd
Expand Up @@ -6,6 +6,7 @@
Set DIR={{ dir }} Set DIR={{ dir }}
Set INPUT={{ input }} Set INPUT={{ input }}
Set OUTPUT={{ output }} Set OUTPUT={{ output }}
Set CLI_WIDTH={{ cliWidth }}
Set PIPE={{ pipe }} Set PIPE={{ pipe }}


:: Hide the window :: Hide the window
Expand Down

0 comments on commit 0a4b1df

Please sign in to comment.