Skip to content

Commit

Permalink
Now support paths with spaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Oct 20, 2014
1 parent c698fb1 commit 30d38e3
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 29 deletions.
10 changes: 7 additions & 3 deletions lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ module.exports = function (archive, files, options) {
options = {};
}

var opts = u.switches(options);
archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za a ' + archive + ' ' + files + ' ' + opts;

u.run(command)

// When a stdout is emitted, parse each line and search for a pattern. When
Expand All @@ -38,11 +42,11 @@ module.exports = function (archive, files, options) {
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
7 changes: 5 additions & 2 deletions lib/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ module.exports = function (archive, files, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za d ' + archive + ' ' + files + ' ' + opts;
u.run(command)

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
6 changes: 4 additions & 2 deletions lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = function (archive, dest, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');
dest = dest.replace(/\ /g, '\\ ');
var opts = u.switches(options);
var command = '7za e ' + archive + ' -o' + dest + ' ' + opts;
u.run(command)
Expand All @@ -38,11 +40,11 @@ module.exports = function (archive, dest, options) {
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
10 changes: 7 additions & 3 deletions lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ module.exports = function (archive, dest, options) {
options = {};
}

var opts = u.switches(options);
dest = dest.replace(/\ /g, '\\ ');
archive = archive.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za x ' + archive + ' -o' + dest + ' ' + opts;

u.run(command)

// When a stdout is emitted, parse each line and search for a pattern. When
Expand All @@ -38,11 +42,11 @@ module.exports = function (archive, dest, options) {
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
10 changes: 6 additions & 4 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ module.exports = function (archive, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var spec = {};
var regex = /(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ([\.DA]+) +(\d+)[ \d]+ (.+)/;
Expand All @@ -34,8 +36,8 @@ module.exports = function (archive, options) {
.progress(function (data) {
var entries = [];
data.split('\n').forEach(function (line) {
// Populate the tech specs of the archive that are passed to the

// Populate the tech specs of the archive that are passed to the
// fulfill handler.
if (line.substr(0, 7) === 'Path = ') {
spec.path = line.substr(7, line.length);
Expand Down Expand Up @@ -65,11 +67,11 @@ module.exports = function (archive, options) {
});
progress(entries);
})

.then(function () {
fulfill(spec);
})

.catch(function (err) {
reject(err);
});
Expand Down
6 changes: 4 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = function (archive, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za t ' + archive + ' ' + opts;
u.run(command)
Expand All @@ -37,11 +39,11 @@ module.exports = function (archive, options) {
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
7 changes: 5 additions & 2 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module.exports = function (archive, files, options) {
options = {};
}

archive = archive.replace(/\ /g, '\\ ');
files = files.replace(/\ /g, '\\ ');

var opts = u.switches(options);
var command = '7za u ' + archive + ' ' + files + ' ' + opts;
u.run(command)
Expand All @@ -38,11 +41,11 @@ module.exports = function (archive, files, options) {
});
progress(entries);
})

.then(function () {
fulfill();
})

.catch(function (err) {
reject(err);
});
Expand Down
20 changes: 14 additions & 6 deletions test/method.Zip.extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var fs = require('fs-extra');
var extractFull = require('../lib/extractFull');

describe('Method: `Zip.extractFull`', function () {

afterEach(function () { fs.removeSync('.tmp/test'); });

it('should return an error on 7z error', function (done) {
extractFull('test/nothere.7z', '.tmp/test')
.catch(function (err) {
Expand All @@ -22,21 +22,29 @@ describe('Method: `Zip.extractFull`', function () {
done();
});
});

it('should return entries on progress', function (done) {
extractFull('test/zip.7z', '.tmp/test')
.progress(function (entries) {
expect(entries.length).to.be.at.least(1);
done();
});
});

it('should extract on the right path', function (done) {
extractFull('test/zip.7z', '.tmp/test')
.then(function () {
expect(fs.existsSync('.tmp/test/zip')).to.be.eql(true);
done();
});
});

});

it('should work with spaces too', function (done) {
extractFull('test/zip.7z', '.tmp/test spaces agai n')
.then(function () {
expect(fs.existsSync('.tmp/test spaces agai n/zip')).to.be.eql(true);
done();
});
});

});
29 changes: 24 additions & 5 deletions util/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var when = require('when');
*/
module.exports = function (command) {
return when.promise(function (fulfill, reject, progress) {
// Parse the command variable. If the command is not a string reject the

// Parse the command variable. If the command is not a string reject the
// Promise. Otherwise transform the command into two variables: the command
// name and the arguments.
if (typeof command !== 'string') {
Expand All @@ -22,9 +22,28 @@ module.exports = function (command) {
var args = command.split(' ');
var cmd = args[0];
args.shift();


// Recover pathes when the have spaces. The split process above didn't care
// if the string contains pathes with spaces. The loop bellow look for each
// item to have a escape char at the end, if is present concat with the next
// item to restore the original path.
var filterSpaces = function (elem, index, array) {
if (elem[elem.length - 1] === '\\') {
var firstPart = elem.substr(0, elem.length - 1);
var separator = ' ';
var lastPart = args[index + 1];
args[index] = firstPart + separator + lastPart;
args.splice(index + 1, 1);
}
};

// Run the filter twice. By splicing the array in the function above the
// filter does not run on the item just after one that is being removed.
args.forEach(filterSpaces);
args.forEach(filterSpaces);

// When an stdout is emitted, parse it. If an error is detected in the body
// of the stdout create an new error with the 7-Zip error message as the
// of the stdout create an new error with the 7-Zip error message as the
// error's message. Otherwise progress with stdout message.
var err;
var reg = new RegExp('Error:' + os.EOL + '?(.*)', 'g');
Expand All @@ -40,6 +59,6 @@ module.exports = function (command) {
if (code === 0) return fulfill();
reject(err, code);
});

});
};

0 comments on commit 30d38e3

Please sign in to comment.