remove zip/unzip from shell

This commit is contained in:
Alan K 2014-09-02 15:48:54 -04:00
parent 08f4b7e528
commit 4126321996
6 changed files with 28 additions and 9162 deletions

8761
dist/filer.js vendored

File diff suppressed because it is too large Load Diff

10
dist/filer.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -31,7 +31,6 @@
"bower": "~1.3.8",
"request": "^2.36.0",
"browser-request": "git://github.com/humphd/browser-request.git#959ea95bf200d64939ed76897d3b06bb684f3a0d",
"jszip": "git://github.com/humphd/jszip.git#ad3f356bb165aba1cafeabe1bb3e49293803f975",
"base64-arraybuffer": "^0.1.2"
},
"devDependencies": {

View File

@ -4,7 +4,6 @@ var Environment = require('./environment.js');
var async = require('../../lib/async.js');
var Network = require('./network.js');
var Encoding = require('../encoding.js');
var JSZip = require('jszip');
function Shell(fs, options) {
options = options || {};
@ -476,135 +475,4 @@ Shell.prototype.wget = function(url, options, callback) {
});
};
Shell.prototype.unzip = function(zipfile, options, callback) {
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!zipfile) {
callback(new Errors.EINVAL('Missing zipfile argument'));
return;
}
var path = Path.resolve(sh.pwd(), zipfile);
var destination = Path.resolve(options.destination || sh.pwd());
fs.readFile(path, function(err, data) {
if(err) return callback(err);
var zip = new JSZip(data);
var filenames = [];
zip.filter(function(relPath, file) {
var isDir = file.options.dir;
var data = isDir ? null : file.asNodeBuffer();
filenames.push({
absPath: Path.join(destination, file.name),
isDirectory: isDir,
data: data
});
});
function decompress(path, callback) {
if(path.isDirectory) {
sh.mkdirp(path.absPath, callback);
} else {
fs.writeFile(path.absPath, path.data, callback);
}
}
async.eachSeries(filenames, decompress, callback);
});
};
Shell.prototype.zip = function(zipfile, paths, options, callback) {
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!zipfile) {
callback(new Errors.EINVAL('Missing zipfile argument'));
return;
}
if(!paths) {
callback(new Errors.EINVAL('Missing paths argument'));
return;
}
if(typeof paths === 'string') {
paths = [ paths ];
}
zipfile = Path.resolve(sh.pwd(), zipfile);
function toRelPath(path) {
// Make path relative within the zip
return path.replace(/^\//, '');
}
function addFile(path, callback) {
fs.readFile(path, function(err, data) {
if(err) return callback(err);
zip.file(toRelPath(path), data, {binary: true});
callback();
});
}
function addDir(path, callback) {
fs.readdir(path, function(err, list) {
// Add the directory itself
zip.folder(toRelPath(path));
if(!options.recursive) {
callback();
}
// Add all children of this dir, too
async.eachSeries(list, function(entry, callback) {
add(Path.join(path, entry), callback);
}, callback);
});
}
function add(path, callback) {
path = Path.resolve(sh.pwd(), path);
fs.stat(path, function(err, stats) {
if(err) return callback(err);
if(stats.isDirectory()) {
addDir(path, callback);
} else {
addFile(path, callback);
}
});
}
var zip = new JSZip();
// Make sure the zipfile doesn't already exist.
fs.exists(zipfile, function(exists) {
if(exists) {
return callback(new Errors.EEXIST('zipfile already exists', zipfile));
}
async.eachSeries(paths, add, function(err) {
if(err) return callback(err);
var compressed;
compressed = zip.generate({type: 'nodebuffer'});
fs.writeFile(zipfile, compressed, callback);
});
});
};
module.exports = Shell;

View File

@ -55,7 +55,6 @@ require("./spec/shell/rm.spec");
require("./spec/shell/env.spec");
require("./spec/shell/mkdirp.spec");
require("./spec/shell/wget.spec");
require("./spec/shell/zip-unzip.spec");
require("./spec/shell/network.spec");
// Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test)

View File

@ -1,285 +0,0 @@
var Filer = require('../../..');
var util = require('../../lib/test-utils.js');
var expect = require('chai').expect;
describe('FileSystemShell.zip() and unzip()', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var shell = util.shell();
expect(shell.zip).to.be.a('function');
expect(shell.unzip).to.be.a('function');
});
it('should fail when path argument is absent', function(done) {
var fs = util.fs();
var shell = fs.Shell();
shell.unzip(null, function(err) {
expect(err).to.exist;
done();
});
});
it('should download and unzip the contents of a zip file', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt.zip" : "/tests/test-file.txt.zip";
var filename = "test-file.txt.zip";
var contents = "This is a test file used in some of the tests.\n";
fs.writeFile('/original', contents, function(err) {
if(err) throw err;
shell.wget(url, {filename: filename}, function(err, path) {
if(err) throw err;
shell.unzip(path, function(err) {
if(err) throw err;
fs.readFile('/original', function(err, originalData) {
if(err) throw err;
fs.readFile('/test-file.txt', function(err, data) {
if(err) throw err;
expect(util.typedArrayEqual(data, originalData)).to.be.true;
done();
});
});
});
});
});
});
it('should download and unzip the contents of a zip file with a specified destination', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var Path = Filer.Path;
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt.zip" : "/tests/test-file.txt.zip";
var filename = "test-file.txt.zip";
var contents = "This is a test file used in some of the tests.\n";
fs.writeFile('/original', contents, function(err) {
if(err) throw err;
shell.wget(url, {filename: filename}, function(err, path) {
if(err) throw err;
shell.tempDir(function(err, tmp) {
if (err) throw err;
shell.unzip(path, { destination: tmp }, function(err) {
if(err) throw err;
fs.readFile('/original', function(err, originalData) {
if(err) throw err;
fs.readFile(Path.join(tmp, 'test-file.txt'), function(err, data) {
if(err) throw err;
expect(util.typedArrayEqual(data, originalData)).to.be.true;
done();
});
});
});
});
});
});
});
it('should be able to zip and unzip a file', function(done) {
var fs = util.fs();
var file = '/test-file.txt';
var zipfile = file + '.zip';
var shell = fs.Shell();
var Path = Filer.Path;
var contents = "This is a test file used in some of the tests.\n";
fs.writeFile(file, contents, function(err) {
if(err) throw err;
shell.zip(zipfile, file, function(err) {
if(err) throw err;
fs.stat(zipfile, function(err, stats) {
if(err) throw err;
expect(stats.isFile()).to.be.true;
shell.rm(file, function(err) {
if(err) throw err;
shell.unzip(zipfile, function(err) {
if(err) throw err;
fs.stat(file, function(err, stats) {
if(err) throw err;
expect(stats.isFile()).to.be.true;
fs.readFile(Path.join('/', file), 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
});
});
});
});
});
it('should be able to handle a deep tree structure in a zip', function(done) {
// test-dir.zip has the following structure:
//
// test-dir/
// ├── test-dir2
// │ └── test-file.txt
// ├── test-file.txt
// └── test-file2.txt
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-dir.zip" : "/tests/test-dir.zip";
var filename = "test-dir.zip";
var Path = Filer.Path;
var contents = "This is a test file used in some of the tests.\n";
function confirmFile(filename, callback) {
filename = Path.join('/', filename);
fs.readFile(filename, 'utf8', function(err, data) {
if(err) {
console.error('Expected ' + filename + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(data).to.equal(contents);
callback();
});
}
function confirmDir(dirname, callback) {
dirname = Path.join('/', dirname);
fs.stat(dirname, function(err, stats) {
if(err) {
console.error('Expected ' + dirname + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(stats.isDirectory()).to.be.true;
callback();
});
}
shell.wget(url, {filename: filename}, function(err, path) {
if(err) throw err;
shell.unzip(path, function(err) {
if(err) throw err;
// Forgive the crazy indenting, trying to match tree structure ;)
confirmDir('test-dir', function() {
confirmDir('test-dir/test-dir2', function() {
confirmFile('test-dir/test-dir2/test-file.txt', function() {
confirmFile('test-dir/test-file.txt', function() {
confirmFile('test-dir/test-file2.txt', function() {
done();
});});});});});
});
});
});
it('should be able to re-create (unzip/zip) a deep tree structure in a zip', function(done) {
// test-dir.zip has the following structure:
//
// test-dir/
// ├── test-dir2
// │ └── test-file.txt
// ├── test-file.txt
// └── test-file2.txt
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-dir.zip" : "/tests/test-dir.zip";
var filename = "test-dir.zip";
var Path = Filer.Path;
var contents = "This is a test file used in some of the tests.\n";
function confirmFile(filename, callback) {
filename = Path.join('/unzipped', filename);
fs.readFile(filename, 'utf8', function(err, data) {
if(err) {
console.error('Expected ' + filename + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(data).to.equal(contents);
callback();
});
}
function confirmDir(dirname, callback) {
dirname = Path.join('/unzipped', dirname);
fs.stat(dirname, function(err, stats) {
if(err) {
console.error('Expected ' + dirname + ' to exist.');
expect(false).to.be.true;
return callback(err);
}
expect(stats.isDirectory()).to.be.true;
callback();
});
}
shell.wget(url, {filename: filename}, function(err, path) {
if(err) throw err;
shell.unzip(path, function(err) {
if(err) throw err;
shell.zip('/test-dir2.zip', '/test-dir', { recursive: true }, function(err) {
if(err) throw err;
shell.mkdirp('/unzipped', function(err) {
if(err) throw err;
shell.unzip('/test-dir2.zip', { destination: '/unzipped' }, function(err) {
if(err) throw err;
// Forgive the crazy indenting, trying to match tree structure ;)
confirmDir('test-dir', function() {
confirmDir('test-dir/test-dir2', function() {
confirmFile('test-dir/test-dir2/test-file.txt', function() {
confirmFile('test-dir/test-file.txt', function() {
confirmFile('test-dir/test-file2.txt', function() {
done();
});});});});});
});
});
});
});
});
});
it('should fail if the zipfile already exists', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var file = "/file";
var zipfile = "/zipfile.zip";
var contents = "This is a test file used in some of the tests.\n";
fs.writeFile(file, contents, function(err) {
if(err) throw err;
shell.zip(zipfile, file, function(err) {
if(err) throw err;
shell.zip(zipfile, file, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EEXIST');
done();
});
});
});
});
});