mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 19:26:33 +00:00
Replace promise chains with async/await in child-writer
Change-type: patch
This commit is contained in:
parent
ecc500907c
commit
da548f59d1
@ -82,12 +82,10 @@ const terminate = (code) => {
|
||||
* @example
|
||||
* handleError(new Error('Something bad happened!'))
|
||||
*/
|
||||
const handleError = (error) => {
|
||||
const handleError = async (error) => {
|
||||
ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error))
|
||||
Bluebird.delay(DISCONNECT_DELAY)
|
||||
.then(() => {
|
||||
terminate(EXIT_CODES.GENERAL_ERROR)
|
||||
})
|
||||
await Bluebird.delay(DISCONNECT_DELAY)
|
||||
terminate(EXIT_CODES.GENERAL_ERROR)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,40 +95,33 @@ const handleError = (error) => {
|
||||
* @param {Boolean} verify - whether to validate the writes or not
|
||||
* @param {Function} onProgress - function to call on progress
|
||||
* @param {Function} onFail - function to call on fail
|
||||
* @param {Function} onFinish - function to call on finish
|
||||
* @param {Function} onError - function to call on error
|
||||
* @returns {Promise<void>}
|
||||
* @returns {Promise<{ bytesWritten, devices, errors} >}
|
||||
*
|
||||
* @example
|
||||
* writeAndValidate(source, destinations, verify, onProgress, onFail, onFinish, onError)
|
||||
*/
|
||||
const writeAndValidate = (source, destinations, verify, onProgress, onFail, onFinish, onError) => {
|
||||
return source.getInnerSource()
|
||||
.then((innerSource) => {
|
||||
return sdk.multiWrite.pipeSourceToDestinations(
|
||||
innerSource,
|
||||
destinations,
|
||||
onFail,
|
||||
onProgress,
|
||||
verify
|
||||
)
|
||||
})
|
||||
.then(({ failures, bytesWritten }) => {
|
||||
const result = {
|
||||
bytesWritten,
|
||||
devices: {
|
||||
failed: failures.size,
|
||||
successful: destinations.length - failures.size
|
||||
},
|
||||
errors: []
|
||||
}
|
||||
for (const [ destination, error ] of failures) {
|
||||
error.device = destination.drive.device
|
||||
result.errors.push(error)
|
||||
}
|
||||
onFinish(result)
|
||||
})
|
||||
.catch(onError)
|
||||
const writeAndValidate = async (source, destinations, verify, onProgress, onFail) => {
|
||||
const innerSource = await source.getInnerSource()
|
||||
const { failures, bytesWritten } = await sdk.multiWrite.pipeSourceToDestinations(
|
||||
innerSource,
|
||||
destinations,
|
||||
onFail,
|
||||
onProgress,
|
||||
verify
|
||||
)
|
||||
const result = {
|
||||
bytesWritten,
|
||||
devices: {
|
||||
failed: failures.size,
|
||||
successful: destinations.length - failures.size
|
||||
},
|
||||
errors: []
|
||||
}
|
||||
for (const [ destination, error ] of failures) {
|
||||
error.device = destination.drive.device
|
||||
result.errors.push(error)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
ipc.connectTo(IPC_SERVER_ID, () => {
|
||||
@ -159,7 +150,7 @@ ipc.connectTo(IPC_SERVER_ID, () => {
|
||||
terminate(EXIT_CODES.SUCCESS)
|
||||
})
|
||||
|
||||
ipc.of[IPC_SERVER_ID].on('write', (options) => {
|
||||
ipc.of[IPC_SERVER_ID].on('write', async (options) => {
|
||||
/**
|
||||
* @summary Progress handler
|
||||
* @param {Object} state - progress state
|
||||
@ -172,52 +163,20 @@ ipc.connectTo(IPC_SERVER_ID, () => {
|
||||
|
||||
let exitCode = EXIT_CODES.SUCCESS
|
||||
|
||||
/**
|
||||
* @summary Finish handler
|
||||
* @param {Object} results - Flash results
|
||||
* @example
|
||||
* writer.on('finish', onFinish)
|
||||
*/
|
||||
const onFinish = (results) => {
|
||||
log(`Finish: ${results.bytesWritten}`)
|
||||
results.errors = _.map(results.errors, (error) => {
|
||||
return errors.toJSON(error)
|
||||
})
|
||||
ipc.of[IPC_SERVER_ID].emit('done', { results })
|
||||
Bluebird.delay(DISCONNECT_DELAY)
|
||||
.then(() => {
|
||||
terminate(exitCode)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Abort handler
|
||||
* @example
|
||||
* writer.on('abort', onAbort)
|
||||
*/
|
||||
const onAbort = () => {
|
||||
const onAbort = async () => {
|
||||
log('Abort')
|
||||
ipc.of[IPC_SERVER_ID].emit('abort')
|
||||
Bluebird.delay(DISCONNECT_DELAY)
|
||||
.then(() => {
|
||||
terminate(exitCode)
|
||||
})
|
||||
await Bluebird.delay(DISCONNECT_DELAY)
|
||||
terminate(exitCode)
|
||||
}
|
||||
|
||||
ipc.of[IPC_SERVER_ID].on('cancel', onAbort)
|
||||
|
||||
/**
|
||||
* @summary Error handler
|
||||
* @param {Error} error - error
|
||||
* @example
|
||||
* writer.on('error', onError)
|
||||
*/
|
||||
const onError = (error) => {
|
||||
log(`Error: ${error.message}`)
|
||||
exitCode = EXIT_CODES.GENERAL_ERROR
|
||||
ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error))
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Failure handler (non-fatal errors)
|
||||
* @param {SourceDestination} destination - destination
|
||||
@ -234,24 +193,34 @@ ipc.connectTo(IPC_SERVER_ID, () => {
|
||||
}
|
||||
|
||||
const destinations = _.map(options.destinations, 'device')
|
||||
const dests = _.map(options.destinations, (destination) => {
|
||||
return new sdk.sourceDestination.BlockDevice(destination, options.unmountOnSuccess)
|
||||
})
|
||||
const source = new sdk.sourceDestination.File(options.imagePath, sdk.sourceDestination.File.OpenFlags.Read)
|
||||
writeAndValidate(
|
||||
source,
|
||||
dests,
|
||||
options.validateWriteOnSuccess,
|
||||
onProgress,
|
||||
onFail,
|
||||
onFinish,
|
||||
onError
|
||||
)
|
||||
|
||||
log(`Image: ${options.imagePath}`)
|
||||
log(`Devices: ${destinations.join(', ')}`)
|
||||
log(`Umount on success: ${options.unmountOnSuccess}`)
|
||||
log(`Validate on success: ${options.validateWriteOnSuccess}`)
|
||||
const dests = _.map(options.destinations, (destination) => {
|
||||
return new sdk.sourceDestination.BlockDevice(destination, options.unmountOnSuccess)
|
||||
})
|
||||
const source = new sdk.sourceDestination.File(options.imagePath, sdk.sourceDestination.File.OpenFlags.Read)
|
||||
try {
|
||||
const results = await writeAndValidate(
|
||||
source,
|
||||
dests,
|
||||
options.validateWriteOnSuccess,
|
||||
onProgress,
|
||||
onFail
|
||||
)
|
||||
log(`Finish: ${results.bytesWritten}`)
|
||||
results.errors = _.map(results.errors, (error) => {
|
||||
return errors.toJSON(error)
|
||||
})
|
||||
ipc.of[IPC_SERVER_ID].emit('done', { results })
|
||||
await Bluebird.delay(DISCONNECT_DELAY)
|
||||
terminate(exitCode)
|
||||
} catch (error) {
|
||||
log(`Error: ${error.message}`)
|
||||
exitCode = EXIT_CODES.GENERAL_ERROR
|
||||
ipc.of[IPC_SERVER_ID].emit('error', errors.toJSON(error))
|
||||
}
|
||||
})
|
||||
|
||||
ipc.of[IPC_SERVER_ID].on('connect', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user