Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ const { cmdArrayAttr } = require('./common');
const proxyifyCmd = (t, ...cmdStart) => {
// Create the target (or use the one passed in)
t = t || function _t(...args) {
// Wrap all the arguments in quotes
const newArgs = cmdStart
.concat(args)
.map((x) => JSON.stringify(x));
// Run this command in the shell
return origShell.exec.call(this.stdout, newArgs.join(' '));
let newArgs = [];
// The first segment of the command should not be wrapped in quotes
newArgs.push(cmdStart[0]);

// Wrap all subsequent arguments in quotes
newArgs = newArgs
.concat(cmdStart.slice(1))
.concat(args);
// Run this command in the shell with globbing temporarily disabled.
// 'noglob' is part of ShellJS internals, but hopefully this will stay
// around. We cannot achieve the same with `set('-f')` because we need to
// know the previous state in order to reset globbing back the way it was.
const oldGlob = origShell.config.noglob;
try {
origShell.config.noglob = true;
return origShell.cmd.call(this.stdout, newArgs);
} finally {
origShell.config.noglob = oldGlob;
}
};
// Store the list of commands, in case we have a subcommand chain
t[cmdArrayAttr] = cmdStart;
Expand Down
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,26 @@ describe('proxy', function describeproxy() {
done();
});

it('other shelljs commands can still glob', (done) => {
const fa = 'a.txt';
const fglob = '*.txt';
shell.exec('echo hello world').to(fa);
shell.exec('echo hello world').to(fglob);

if (unix()) {
shell.__native.rm(fglob);
} else {
shell.del(fglob);
}
fs.existsSync(fglob).should.equal(false);
fs.existsSync(fa).should.equal(true);

shell.rm('*.txt');
fs.existsSync(fglob).should.equal(false);
fs.existsSync(fa).should.equal(false);
done();
});

it('escapes quotes', (done) => {
if (!unix()) {
// Windows doesn't support `"` as a character in a filename, see
Expand Down
Loading