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