Skip to content

Commit

Permalink
Extract with full paths tested and working
Browse files Browse the repository at this point in the history
All needed utilities (run and switches) are also tested.
  • Loading branch information
q2s2t committed Aug 25, 2014
1 parent 170bc30 commit f7d2b47
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 227 deletions.
98 changes: 0 additions & 98 deletions index.js

This file was deleted.

28 changes: 19 additions & 9 deletions lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@
var path = require('path');
var Q = require('q');
var u = {
run: require('../util/run')
run: require('../util/run'),
switches : require('../util/switches')
};

/**
* Extract an archive with full paths.
* @promise ExtractFull
* @progress {array} Extracted files and directories.
* @reject {Error} The error as issued by 7-Zip.
*/
module.exports = function (archive, dest, options, cb) {
return Q.Promise(function (resolve, reject, notify) {

//TODO: parse options, -y always true?
var opts = '';
return Q.Promise(function (fulfill, reject, progress) {

if (options === undefined) {
options = {};
}

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

// When a stdout is emitted, parse each line and search for a pattern. When
// the pattern is found, extract the file (or directory) name from it and
// pass it to an array. Finally returns this array.
Expand All @@ -23,16 +33,16 @@ module.exports = function (archive, dest, options, cb) {
entries.push(line.substr(12, line.length).replace(path.sep, '/'));
}
});
notify(entries);
progress(entries);
})

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

.catch(function (err) {
reject(err);
});

}).nodeify(cb);
};
112 changes: 0 additions & 112 deletions test/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions test/lib/extractFull.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ describe('Module: `extractFull`', function () {
done();
});
});

it('should return an error on output duplticate', function (done) {
extractFull('test/zip.7z', '.tmp/test', { o: '.tmp/test/duplicate' })
.catch(function (err) {
expect(err).to.be.an.instanceof(Error);
done();
});
});

it('should return entries on progress', function (done) {
extractFull('test/zip.7z', '.tmp/test')
Expand Down
18 changes: 10 additions & 8 deletions util/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ var spawn = require('win-spawn');
var Q = require('q');

/**
* @function run
* @param {string} command The command to run
* @promise Run
* @param {string} command The command to run.
* @progress {string} stdout message.
* @reject {Error} The error issued by 7-Zip.
* @reject {number} Exit code issued by 7-Zip.
*/
module.exports = function (command, cb) {
return Q.Promise(function (resolve, reject, notify) {
return Q.Promise(function (fulfill, reject, progress) {

// Parse the command variable. If the command is not a string reject the
// Promise. Otherwise transform the command into two variables: the command
Expand All @@ -22,8 +25,7 @@ module.exports = function (command, cb) {

// 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
// error's message. Otherwise progress with the processed files and
// directories as an array.
// error's message. Otherwise progress with stdout message.
var err;
var reg = new RegExp('Error:' + os.EOL + '(.*)', 'g');
var run = spawn(cmd, args, { stdio: 'pipe' });
Expand All @@ -32,11 +34,11 @@ module.exports = function (command, cb) {
if (res) {
err = new Error(res[1]);
}
notify(data.toString());
progress(data.toString());
});
run.on('close', function (code) {
if (code === 0) return resolve();
reject(err);
if (code === 0) return fulfill();
reject(err, code);
});

}).nodeify(cb);
Expand Down

0 comments on commit f7d2b47

Please sign in to comment.