Skip to content

Commit

Permalink
Fix Windows path escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
dmythro committed May 11, 2016
1 parent e3aa169 commit 0920ab0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
22 changes: 10 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ class PackDir {

escapeArg(arg) {
if (isWindows) {
return '"'
+ arg
return arg
.trim()
.replace(/(["])/g, '\\$1')
+ '"';
.replace(' ' , '\ ');
}

return arg
Expand Down Expand Up @@ -119,7 +117,7 @@ class PackDir {
}
}
catch (e) {
console.error(`Error while packaging "${path}": ${e.message}.`);
console.error(`Error while packaging "${path}":\n${e.message.trim()}`);
}

return false;
Expand Down Expand Up @@ -193,14 +191,14 @@ class PackDir {
let fileName = path + this.ZIP,
pathInfo = Path.parse(path),
pathStat = FS.statSync(path),
pathWithMask = this.escapeArg(
pathWithMask = (
pathStat.isDirectory()
? pathInfo.base + Path.sep + '*'
: pathInfo.base
? (pathInfo.base + Path.sep + '*')
: pathInfo.base
),
pathToZipFile = this.escapeArg(pathInfo.base + '.zip'),
pathToZipFile = pathInfo.base + '.zip',
params = {};

if (pathInfo.dir) {
params.cwd = pathInfo.dir;
}
Expand All @@ -209,8 +207,8 @@ class PackDir {

let args = [
'-r',
pathToZipFile,
pathWithMask
this.escapeArg(pathToZipFile),
this.escapeArg(pathWithMask)
];

if (isWindows) {
Expand Down
9 changes: 5 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Pack Dir', () => {
});

it('executed sync/async', () => {
let cmd = 'echo test';
let cmd = 'node -v';

Pack.param('isSync', true);
expect(Pack.exec(cmd) instanceof Buffer).toBe(true);
Expand All @@ -60,13 +60,14 @@ describe('Pack Dir', () => {
});

it('executed file sync/async', () => {
let cmd = 'echo';
let cmd = 'node',
args = ['-v'];

Pack.param('isSync', true);
expect(Pack.execFile(cmd) instanceof Buffer).toBe(true);

Pack.param('isSync', false);
expect(Pack.execFile(cmd) instanceof require('events')).toBe(true);
expect(Pack.execFile(cmd, args) instanceof require('events')).toBe(true);
});

it('logs are silent when off', () => {
Expand Down Expand Up @@ -117,7 +118,7 @@ describe('Pack Dir', () => {

it('escapes args/paths', () => {
if (isWindows) {
expect(Pack.escapeArg(TEST_PATH)).toEqual('"' + TEST_PATH.replace('"', '\\"') + '"');
expect(Pack.escapeArg(TEST_PATH)).toEqual(TEST_PATH.replace(' ' , '\ '));
} else {
expect(Pack.escapeArg(TEST_PATH)).toEqual(TEST_PATH.replace(' ', '\\ '));
}
Expand Down

0 comments on commit 0920ab0

Please sign in to comment.