Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite Option #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var debug = require('debug')('extract-zip')

module.exports = function (zipPath, opts, cb) {
debug('creating target directory', opts.dir)
opts.overwrite = (typeof opts.overwrite === 'undefined') ? true : opts.overwrite

mkdirp(opts.dir, function (err) {
if (err) return cb(err)
Expand Down Expand Up @@ -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, flags: writeFlag})
readStream.pipe(writeStream)

writeStream.on('finish', function () {
Expand All @@ -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()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing a path exists check before attempting to delete, I just try to delete and if there's a ENOENT, we just proceed to write the symlink.

cancelled = true
done(err)
}
writeSymlink()
})
}

// AFAICT the content of the symlink file itself is the symlink target filename string
function writeSymlink () {
Expand Down
32 changes: 30 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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')
})
})
Expand Down