From 5c8059ac6662c2984e7c3584afbb22decfc353d4 Mon Sep 17 00:00:00 2001 From: Marwan Hilmi Date: Wed, 20 Jan 2016 23:34:05 -0800 Subject: [PATCH 1/2] Overwrite option --- index.js | 20 ++++++++++++++++++-- package.json | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8367535..f86fa7d 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var debug = require('debug')('extract-zip') module.exports = function (zipPath, opts, cb) { debug('creating target directory', opts.dir) + opts.overwrite = opts.overwrite || false mkdirp(opts.dir, function (err) { if (err) return cb(err) @@ -122,11 +123,15 @@ module.exports = function (zipPath, opts, cb) { console.log('read err', err) }) - if (symlink) writeSymlink() + if (symlink) { + if (opts.overwrite) overwriteExistingSymlink() + else writeSymlink() + } else writeStream() function writeStream () { - var writeStream = fs.createWriteStream(dest, {mode: procMode}) + var writeFlag = opts.overwrite ? 'w' : 'wx' + var writeStream = fs.createWriteStream(dest, {mode: procMode, flag: writeFlag}) readStream.pipe(writeStream) writeStream.on('finish', function () { @@ -139,6 +144,17 @@ module.exports = function (zipPath, opts, cb) { return done(err) }) } + // Attempt to delete an existing symlink before attempting to write + function overwriteExistingSymlink () { + fs.unlink(dest, function (err) { + if (err) { + if (err.code === 'ENOENT') return writeSymlink() + cancelled = true + done(err) + } + writeSymlink() + }) + } // AFAICT the content of the symlink file itself is the symlink target filename string function writeSymlink () { diff --git a/package.json b/package.json index 0432504..672cf7a 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "concat-stream": "1.5.0", "debug": "0.7.4", "mkdirp": "0.5.0", + "path-exists": "^1.0.0", "yauzl": "2.4.1" }, "devDependencies": { From 5d7eca63f3e70ee346d33d0f99a194fc6b9f040f Mon Sep 17 00:00:00 2001 From: Marwan Hilmi Date: Thu, 21 Jan 2016 00:24:58 -0800 Subject: [PATCH 2/2] Add some tests --- index.js | 4 ++-- package.json | 1 - test/test.js | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f86fa7d..df4e9b4 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ var debug = require('debug')('extract-zip') module.exports = function (zipPath, opts, cb) { debug('creating target directory', opts.dir) - opts.overwrite = opts.overwrite || false + opts.overwrite = (typeof opts.overwrite === 'undefined') ? true : opts.overwrite mkdirp(opts.dir, function (err) { if (err) return cb(err) @@ -131,7 +131,7 @@ module.exports = function (zipPath, opts, cb) { function writeStream () { var writeFlag = opts.overwrite ? 'w' : 'wx' - var writeStream = fs.createWriteStream(dest, {mode: procMode, flag: writeFlag}) + var writeStream = fs.createWriteStream(dest, {mode: procMode, flags: writeFlag}) readStream.pipe(writeStream) writeStream.on('finish', function () { diff --git a/package.json b/package.json index 672cf7a..0432504 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,6 @@ "concat-stream": "1.5.0", "debug": "0.7.4", "mkdirp": "0.5.0", - "path-exists": "^1.0.0", "yauzl": "2.4.1" }, "devDependencies": { diff --git a/test/test.js b/test/test.js index 4d8032d..3a32d75 100644 --- a/test/test.js +++ b/test/test.js @@ -28,6 +28,25 @@ test('extract cat zip', function (t) { }) }) +test('extract cat zip to same dir without overwrite', function (t) { + console.log('extracting again to', targetA) + + extract(sourceA, {dir: targetA, overwrite: false}, function (err) { + if (err) t.ok(true, 'error passed') + t.end() + }) +}) + +test('extract cat zip to same dir with overwrite', function (t) { + console.log('extracting again to', targetA) + + extract(sourceA, {dir: targetA, overwrite: true}, function (err) { + if (err) throw err + t.false(err, 'no error') + t.end() + }) +}) + test('files', function (t) { t.plan(1) @@ -95,6 +114,16 @@ test('verify extraction worked', function (t) { }) }) +test('extract github zip again without overwrite', function (t) { + console.log('extracting again to', targetB) + t.plan(1) + + extract(sourceB, {dir: targetB, overwrite: false}, function (err) { + if (err) t.ok(true, 'error passed') + t.end() + }) +}) + test('callback called once', function (t) { rimraf.sync(targetC) @@ -106,9 +135,8 @@ test('callback called once', function (t) { if (err) throw err // this triggers an error due to symlink creation - extract(sourceC, {dir: targetC}, function (err) { + extract(sourceC, {dir: targetC, overwrite: false}, function (err) { if (err) t.ok(true, 'error passed') - t.ok(true, 'callback called') }) })