Skip to content

Commit

Permalink
Parse switches
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Aug 22, 2014
1 parent 915267d commit cc356f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ myTask.test('myArchive.7z', function (err, files) {
Installation
------------

You must have the `7z` executable available in your PATH. In some GNU/Linux
distributions install the `p7zip-full`.

```
npm install -g 7z
```
Expand Down
90 changes: 38 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
// # node-7zip
// A Node.js wrapper for 7-Zip.
// [GitHub](https://github.com/quentinrossetti/node-7zip)
// I chose to use both promises and callbacks in this library. In all cases the
// API is consistent with standard use:
// Promise-style e.g:
// ```js
// var seven = new require('7z');
// seven.test('myArchive.7z')
// .then(fulfillHandler, rejectHandler);
// ```
// Callback-style e.g:
// ```js
// var seven = new require('7z');
// seven.test('myArchive.7z', function (err, files) {
// if (err) errorHandler;
// successHandler;
// });
// ```
'use strict';
var os = require('os');
var path = require('path');
Expand All @@ -26,29 +7,47 @@ var util = require('util');
var spawn = require('win-spawn');
var Promise = require('promise');


// Create an Api class that extends the EventEmitter.
function Api(name) {
this.name = (name) ? name : '7z';
}
util.inherits(Api, events.EventEmitter);

function concatSwitches(switches) {
var r = '';
var c = '';
for (var s in switches) {
c = switches[s];
c = (c === true) ? '' : c;
r += '-' + s + c;
}
}

// Adds files to archive.
Api.prototype.add = Promise.denodeify(function (archive, files, switches, cb) {

// When no switches are speciied use the given function as the callback and
// set switches as an empty object.
if (typeof switches === 'function') {
cb = switches;
switches = {};
}
archive = path.resolve(archive);


});


// ## API: test
// Test integrity of archive.
//
// ### Arguments
// * `archive` The path to the archive you want to analyse.
//
// ### Return values
// * `files` A array of all the files *AND* directories in the archives. The
// `/` character is used as a path separator on every platform.
// * `err` The error as issued by `child_process.exec`.
Api.prototype.test = Promise.denodeify(function (archive, callback) {

archive = path.resolve(archive);
var cmd = '7z t ' + archive;
process.exec(cmd, function (err, stdout) {
if (err) return callback(err, null);
// Parse each line of the stdout and build an array of files (and
// directories) tested. The / is used in the result instead of the default
// OS path separator.
var r = [];
stdout.split(os.EOL).forEach(function (_line) {
if (_line.substr(0, 12) === 'Testing ') {
Expand All @@ -61,21 +60,7 @@ Api.prototype.test = Promise.denodeify(function (archive, callback) {
});


// ## API: extract
// Extract with full paths
//
// ### Arguments
// * `archive` The path to the archive you want to analyse.
// * `dest` Where to extract the archive.
//
// ### Return values
// * `files` A array of all the extracted files *AND* directories. The `/`
// character is used as a path separator on every platform.
// * `err` The error as issued by `child_process.exec`.
//
// ### Events
// * `data` Emitted when files are extracted. Has one parameter `(files)`, an
// array of files and directories processed.
// Extract with full paths.
Api.prototype.extract = Promise.denodeify(function (archive, dest, callback) {

archive = path.resolve(archive);
Expand All @@ -84,19 +69,21 @@ Api.prototype.extract = Promise.denodeify(function (archive, dest, callback) {
var err = null;
var cmd = ('x ' + archive + ' -y -o' + dest).split(' ');
var run = spawn('7z', cmd, { stdio: 'pipe' });

run.on('close', function (code) {
if (err) {
return callback(err);
}
if (err) return callback(err);
return callback(null);
});

run.stdout.on('data', function (data) {
// 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 emit a `data` event with the processed files
// and directories as an array.
var files = [];
var regex = new RegExp('Error:' + os.EOL + '(.*)', 'g');
var res = regex.exec(data.toString());
if (res) {
err = new Error(res[1]);
}
if (res) err = new Error(res[1]);
data.toString().split(os.EOL).forEach(function (_line) {
if (_line.substr(0, 12) === 'Extracting ') {
files.push(_line.substr(12, _line.length).replace(path.sep, '/'));
Expand All @@ -108,5 +95,4 @@ Api.prototype.extract = Promise.denodeify(function (archive, dest, callback) {
});


// Exports the Api object.
module.exports = Api;
module.exports = Api;
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ describe('The Api object', function () {

});

describe('`add` function', function() {

it('should get an error when 7z gets an error', function (done) {
var api = new Api();
api.add('myArchive.7z', 'files', { i:'az', r: true }, function (s) {
console.log(s);
});
api.add('myArchive.7z', 'files', function (s) {
console.log(s);
});
done();
});

});

describe('`test` function', function(){

it('should get an error when 7z gets an error', function (done) {
Expand Down

0 comments on commit cc356f1

Please sign in to comment.