mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 05:37:51 +00:00
Use Promise.disposer() to make sure temp files are deleted
This commit is contained in:
parent
4072edcced
commit
21fcdfaff6
@ -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);
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user