Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nfischer committed Oct 27, 2016
1 parent 924c85d commit f8ff395
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 46 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
"rechoir": "^0.6.2"
},
"devDependencies": {
"coffee-script": "^1.10.0",
"eslint": "^2.0.0",
"eslint-config-airbnb-base": "^3.0.0",
"eslint-plugin-import": "^1.11.1",
"coffee-script": "^1.10.0",
"shelljs-changelog": "^0.2.0",
"shelljs-release": "^0.2.0",
"shx": "^0.1.4",
"travis-check-changes": "^0.2.0"
},
"optionalDependencies": {},
Expand Down
20 changes: 13 additions & 7 deletions src/cmd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var common = require('./common');
var _tempDir = require('./tempdir');
var _which = require('./which');
var path = require('path');
var fs = require('fs');
var child = require('child_process');

common.register('cmd', _cmd, {
unix: false,
canReceivePipe: true,
wrapOutput: false,
cmdOptions: null,
});

// Similar to shell.exec(), this is a hack so that we can get concurrent output.
Expand Down Expand Up @@ -63,6 +64,9 @@ function cmdSync(cmd, args, opts, pipe) {
if (code !== 0) {
common.error(stderr, code, true);
}
// common.state.error = stderr;
// common.state.errorCode = code;
// return stdout;
return new common.ShellString(stdout, stderr, code);
} // cmdSync()

Expand Down Expand Up @@ -90,24 +94,23 @@ function cmdSync(cmd, args, opts, pipe) {
//@ By default, this performs globbing on all platforms (but you can disable
//@ this for extra security using `set('-f')`).
function _cmd() {
var args = [].slice.call(arguments, 0);
var args = [].slice.call(arguments, 1); // ignore the options at the start
var command;
var cmdArgs = [];
var options = {};
if (args.length < 1 || typeof args[0] !== 'string') {
common.error('must specify a command to run');
} else if (args.length === 1) {
command = args[0]; // just this command, no args, no options
command = _which('', args[0]); // just this command, no args, no options
} else {
command = args[0];
command = _which('', args[0]);
var lastArg = args[args.length - 1];
cmdArgs = typeof lastArg === 'string' ? args.slice(1) : args.slice(1, args.length - 1);
options = typeof lastArg === 'string' ? {} : lastArg;
}

// Perform globbing, unless it's disabled
if (!common.config.noglob) {
cmdArgs = common.expand(cmdArgs);
if (!command) {
common.error('command not found: ' + args[0], 127);
}

var pipe = common.readFromPipe();
Expand All @@ -124,6 +127,9 @@ function _cmd() {
process.stdout.write(result.stdout);
process.stderr.write(result.stderr);
}
// common.state.stderr = result.stderr;
// common.state.code = result.status;
// return stdout;
return new common.ShellString(result.stdout, result.stderr, result.status);
} else {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function wrap(cmd, fn, options) {

try {
// parse options if options are provided
if (typeof options.cmdOptions === 'object') {
if (options.cmdOptions instanceof Object) {
args[0] = parseOptions(args[0], options.cmdOptions);
}

Expand Down
15 changes: 7 additions & 8 deletions test/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ shell.config.fatal = true;

assert.throws(function () {
shell.cmd('asdfasdf'); // could not find command
}, /cmd: internal error/);
}, 'cmd: must specify command');

shell.config.fatal = oldFatal;

Expand All @@ -37,20 +37,20 @@ shell.config.fatal = oldFatal;

// check if stdout goes to output
result = shell.cmd(process.execPath, '-e', 'console.log(1234);');
assert.equal(shell.error(), null);
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');

// check if stderr goes to output
result = shell.cmd(process.execPath, '-e', 'console.error(1234);');
assert.equal(shell.error(), null);
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '');
assert.equal(result.stderr, '1234\n');

// check if stdout + stderr go to output
result = shell.cmd(process.execPath, '-e', 'console.error(1234); console.log(666);');
assert.equal(shell.error(), null);
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '666\n');
assert.equal(result.stderr, '1234\n');
Expand All @@ -63,15 +63,14 @@ assert.equal(result.code, 12);
// interaction with cd
shell.cd('resources/external');
result = shell.cmd(process.execPath, 'node_script.js');
assert.equal(shell.error(), null);
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, 'node_script_1234\n');
shell.cd('../..');

// set cwd
var cmdString = process.platform === 'win32' ? 'cd' : 'pwd';
result = shell.cmd(cmdString, { cwd: '..' });
assert.equal(shell.error(), null);
result = shell.cmd('shx', 'pwd', { cwd: '..' });
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, path.resolve('..') + os.EOL);

Expand Down
38 changes: 9 additions & 29 deletions test/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,14 @@ assert.equal(shell.error(), null);
assert.equal(result.toString(), shell.cat('resources/uniq/pipeSorted').toString());

// Synchronous exec
// TODO: add windows tests
if (process.platform !== 'win32') {
// unix-specific
if (shell.which('grep').stdout) {
result = shell.cat('resources/grep/file').exec("grep 'alpha*beta'");
assert.equal(shell.error(), null);
assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n');
} else {
console.error('Warning: Cannot verify piped exec');
}
} else {
console.error('Warning: Cannot verify piped exec');
}
// unix-specific
result = shell.cat('resources/grep/file').cmd('shx', 'grep', 'alpha*beta');
assert.equal(shell.error(), null);
assert.equal(result, 'alphaaaaaaabeta\nalphbeta\n');

// Async exec
// TODO: add windows tests
if (process.platform !== 'win32') {
// unix-specific
if (shell.which('grep').stdout) {
shell.cat('resources/grep/file').exec("grep 'alpha*beta'", function (code, stdout) {
assert.equal(code, 0);
assert.equal(stdout, 'alphaaaaaaabeta\nalphbeta\n');
shell.exit(123);
});
} else {
console.error('Warning: Cannot verify piped exec');
}
} else {
console.error('Warning: Cannot verify piped exec');
shell.exit(123);
}
shell.cat('resources/grep/file').exec("shx grep 'alpha*beta'", function (code, stdout) {
assert.equal(code, 0);
assert.equal(stdout, 'alphaaaaaaabeta\nalphbeta\n');
});
shell.exit(123);

0 comments on commit f8ff395

Please sign in to comment.