From 21fcdfaff63d8cae3028382a2bd7ac4997c4bb15 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 21 Sep 2015 07:43:17 -0400 Subject: [PATCH] Use Promise.disposer() to make sure temp files are deleted --- build/actions/device.js | 29 ++++++++++++++--------------- lib/actions/device.coffee | 24 ++++++++++-------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 8fc7b747..5ffffb3a 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -143,9 +143,20 @@ } return vcs.getApplicationName(process.cwd()); }).then(resin.models.application.get).then(function(application) { - return tmp.tmpNameAsync().then(function(temporalPath) { - return capitano.runAsync("os download --output " + temporalPath); - }).then(function(temporalPath) { + var download; + download = function() { + return tmp.tmpNameAsync().then(function(temporalPath) { + return capitano.runAsync("os download --output " + temporalPath); + }).disposer(function(temporalPath) { + return fs.statAsync(temporalPath).then(function(stat) { + if (stat.isDirectory()) { + return rimraf(temporalPath); + } + return fs.unlinkAsync(temporalPath); + }); + }); + }; + return Promise.using(download()).then(function(temporalPath) { return capitano.runAsync("device register " + application.app_name).then(resin.models.device.get).tap(function(device) { return capitano.runAsync("os configure " + temporalPath + " " + device.uuid).then(function() { return capitano.runAsync("os initialize " + temporalPath + " " + device.uuid); @@ -153,18 +164,6 @@ }).then(function(device) { console.log('Done'); return device.uuid; - })["finally"](function() { - return fs.statAsync(temporalPath).then(function(stat) { - if (stat.isDirectory()) { - return rimraf(temporalPath); - } - return fs.unlinkAsync(temporalPath); - })["catch"](function(error) { - if (error.code === 'ENOENT') { - return; - } - throw error; - }); }); }); }).nodeify(done); diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index e78b7738..8c6e67f5 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -197,9 +197,16 @@ exports.init = return vcs.getApplicationName(process.cwd()) .then(resin.models.application.get) .then (application) -> - tmp.tmpNameAsync().then (temporalPath) -> - return capitano.runAsync("os download --output #{temporalPath}") - .then (temporalPath) -> + + download = -> + tmp.tmpNameAsync().then (temporalPath) -> + capitano.runAsync("os download --output #{temporalPath}") + .disposer (temporalPath) -> + fs.statAsync(temporalPath).then (stat) -> + return rimraf(temporalPath) if stat.isDirectory() + return fs.unlinkAsync(temporalPath) + + Promise.using(download()).then (temporalPath) -> capitano.runAsync("device register #{application.app_name}") .then(resin.models.device.get) .tap (device) -> @@ -209,15 +216,4 @@ exports.init = console.log('Done') return device.uuid - .finally -> - fs.statAsync(temporalPath).then (stat) -> - return rimraf(temporalPath) if stat.isDirectory() - return fs.unlinkAsync(temporalPath) - .catch (error) -> - - # Ignore errors if temporary file does not exist - return if error.code is 'ENOENT' - - throw error - .nodeify(done)