Use Promise.disposer() to make sure temp files are deleted

This commit is contained in:
Juan Cruz Viotti 2015-09-21 07:43:17 -04:00
parent 4072edcced
commit 21fcdfaff6
2 changed files with 24 additions and 29 deletions

View File

@ -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);

View File

@ -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)