Skip to content

Commit

Permalink
extract() and execute() should return File instance
Browse files Browse the repository at this point in the history
extract() and execute() should only execute once
extract() and execute() should work when chaining is not used
  • Loading branch information
dogancelik committed Aug 15, 2015
1 parent 6108a29 commit 8ddd250
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.9.2

* *extract()* and *execute()* now returns *File* instance. This means you can extract or execute more than once.
* You can now use *extract()* and *execute()* even when no method chaining is done.
* *extract()* and *execute()* will only execute *once per download()*.

**Explanation for the last bullet:** In previous versions, if you did: `download(...).extract(...).download(...)`, this would trigger *extract()* twice because we did `.on` bindings to the download stream instead of `.once`. Now if you ever download the file again within the same *File* instance, you have to use *extract()* again to extract it.

## v0.9.1

* *endl* should now resolve URLs without domains better in *download()* (e.g. If `href` is `/LatestSetup.exe` not `http://example.com/LatestSetup.exe`)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "endl",
"version": "0.9.1",
"version": "0.9.2",
"description": "Link extractor, downloader, executer, unzipper",
"author": {
"name": "Doğan Çelik",
Expand Down
18 changes: 15 additions & 3 deletions src/file.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class File
options.directory = path.resolve options.directory

thisClass._file = join options.directory, filename
thisClass._downloadFinished = false # for extract and execute

callbackData = {
url: downloadUrl
Expand All @@ -120,7 +121,8 @@ class File
startDownload = ->
thisClass._stream = createWriteStream thisClass._file
thisClass._ee.emit 'create'
File::_bindCallback(response, callback, callbackData)
File::_bindCallback response, callback, callbackData
File::_bindCallback response, -> thisClass._downloadFinished = true
response.pipe thisClass._stream

stat options.directory, (err, stats) ->
Expand Down Expand Up @@ -187,7 +189,12 @@ class File

extract: (options, callback) ->
thisClass = @
thisClass._ee.on 'create', -> thisClass._stream.on('finish', thisClass._extractOnFinish.bind(thisClass, options, callback))
extractOnFinish = thisClass._extractOnFinish.bind(thisClass, options, callback)
if @_downloadFinished == true
extractOnFinish()
else
thisClass._ee.once 'create', -> thisClass._stream.once('finish', extractOnFinish)
@

_executeOnFinish: (options) ->
if options instanceof Array
Expand All @@ -202,6 +209,11 @@ class File

execute: (options) ->
thisClass = @
thisClass._ee.on 'create', -> thisClass._stream.on('finish', thisClass._executeOnFinish.bind(thisClass, options))
executeOnFinish = thisClass._executeOnFinish.bind(thisClass, options)
if @_downloadFinished == true
executeOnFinish()
else
thisClass._ee.once 'create', -> thisClass._stream.once('finish', executeOnFinish)
@

module.exports = File

0 comments on commit 8ddd250

Please sign in to comment.