From 49f00be43c9ad7fe500d732d8a837ba7dae53b0d Mon Sep 17 00:00:00 2001 From: David Carley Date: Fri, 9 Sep 2022 18:54:34 +0000 Subject: [PATCH] Some final tweaks to the sd card image script --- scripts/prep-sd-image.js | 96 +++++++++++++++++++++------------------- scripts/util.js | 14 +++++- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/scripts/prep-sd-image.js b/scripts/prep-sd-image.js index ab1e825..a85c6e8 100755 --- a/scripts/prep-sd-image.js +++ b/scripts/prep-sd-image.js @@ -3,12 +3,30 @@ const inquirer = require("inquirer"); const merge = require("lodash.merge"); const { resolve } = require("path"); -const { statSync, rmdirSync, copyFileSync, writeFileSync, readFileSync, existsSync, rmSync } = require("fs"); +const { + statSync, + rmdirSync, + copyFileSync, + writeFileSync, + readFileSync, + existsSync, + rmSync +} = require("fs"); const { exit } = require("process"); const { glob } = require("glob"); const packageJSON = require("../package.json"); const config_defaults = require("../src/resources/onefinity_defaults.json"); -const { info, runCommand, logErrorAndExit, assertOS, assertEffectiveRoot, assertFileExists, assertInstalled, initSignalHandlers, registerSignalHandler } = require("./util"); +const { + info, + runCommand, + logErrorAndExit, + assertOS, + assertEffectiveRoot, + assertFileExists, + assertInstalled, + initSignalHandlers, + doFinally +} = require("./util"); const variant_defaults = { machinist_x35: require("../src/resources/onefinity_machinist_x35_defaults.json"), @@ -67,20 +85,7 @@ main(); async function main() { let meta; - const finallyHandler = () => { - if (meta) { - if (existsSync(meta.imageFilePath)) { - rmSync(meta.imageFilePath); - } - - if (existsSync(meta.compressedImageFilePath)) { - rmSync(meta.compressedImageFilePath); - } - } - }; - const unregister = registerSignalHandler(finallyHandler); - - try { + await doFinally(async () => { assertOS(); assertEffectiveRoot(); assertFileExists(ORIGINAL_IMAGE_FILENAME); @@ -104,12 +109,17 @@ async function main() { await configureAutoExpand(meta); compress(meta); moveImageFiles(meta); - } catch (error) { - finallyHandler(); - unregister(); + }, () => { + if (meta) { + if (existsSync(meta.imageFilePath)) { + rmSync(meta.imageFilePath); + } - console.error(error); - } + if (existsSync(meta.compressedImageFilePath)) { + rmSync(meta.compressedImageFilePath); + } + } + }); } function createImageFileCopy() { @@ -130,18 +140,20 @@ function createImageFileCopy() { async function attachToLoopback(meta, partition, cb) { info("Attaching the image to a loopback device..."); - const start = meta.partitions[partition].start; - const loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`); + let loopback; + await doFinally( + async () => { + const start = meta.partitions[partition].start; + loopback = runCommand(`losetup -f --show -o "${start}" "${meta.imageFilePath}"`); - const finallyHandler = () => runCommand(`losetup -d ${loopback}`); - const unregister = registerSignalHandler(finallyHandler); - - try { - await cb(loopback); - } finally { - finallyHandler(); - unregister(); - } + await cb(loopback); + }, + () => { + if (loopback) { + runCommand(`losetup -d ${loopback}`); + } + } + ); } function prepareImage() { @@ -417,23 +429,17 @@ function getCompressedFilename(target) { async function mountLoopback(loopback, cb) { let mountpoint; - const finallyHandler = () => { - if (mountpoint) { - runCommand(`umount "${mountpoint}"`); - rmdirSync(mountpoint); - } - }; - const unregister = registerSignalHandler(finallyHandler); - - try { + await doFinally(async () => { mountpoint = runCommand("mktemp --tmpdir -d onefinity-raspi-root-XXXXXXXXXX"); runCommand(`mount ${loopback} ${mountpoint}`); await cb(mountpoint); - } finally { - finallyHandler(); - unregister(); - } + }, () => { + if (mountpoint) { + runCommand(`umount "${mountpoint}"`); + rmdirSync(mountpoint); + } + }); } function scrubFiles(mountpoint, patterns) { diff --git a/scripts/util.js b/scripts/util.js index 6c1f1b9..1685b52 100644 --- a/scripts/util.js +++ b/scripts/util.js @@ -11,7 +11,8 @@ module.exports = { assertOS, assertEffectiveRoot, assertFileExists, - assertInstalled + assertInstalled, + doFinally }; let signalHandlers = []; @@ -147,3 +148,14 @@ function assertInstalled(tools) { `); } } + +async function doFinally(doHandler, finallyHandler) { + const unregister = registerSignalHandler(finallyHandler); + + try { + await doHandler(); + } finally { + unregister(); + await finallyHandler(); + } +} \ No newline at end of file