Retry every hdiutil operation

On newer macOS version hdiutil fails even more often
This commit is contained in:
Dmitry Vedenko 2024-03-26 12:57:20 +03:00
parent 71e220d61c
commit 5dee4e7a97
No known key found for this signature in database
GPG Key ID: F4C37A6204F983A2
2 changed files with 62 additions and 25 deletions

File diff suppressed because one or more lines are too long

View File

@ -60,6 +60,11 @@ async function detachImage(mountedPath) {
for (let attempt = 1; attempt < maxAttempts; ++attempt) {
try {
if (!fs.existsSync(mountedPath)) {
helpers.log(`Image ${mountedPath} already detached`);
return;
}
return await helpers.execWithLog('hdiutil', [
'detach', mountedPath
]);
@ -68,35 +73,52 @@ async function detachImage(mountedPath) {
await helpers.sleep(2000 * attempt);
}
}
try {
if (!fs.existsSync(mountedPath)) {
helpers.log(`Image ${mountedPath} already detached`);
return;
}
return await helpers.execWithLog('hdiutil', [
'detach', mountedPath, '-force'
]);
} catch (err) {
helpers.error(err.message);
throw new Error(`Failed to detach image ${mountedPath}`);
}
}
async function attachImage(imagePath, finalizers) {
const output = await helpers.getExecOutput('hdiutil', [
'attach', imagePath,
'-nobrowse',
'-noverify',
'-noautoopen'
]);
const maxAttempts = 10;
const match = /Apple_HFS\s+(.*)\s*$/.exec(output.stdout);
const mountedPath = match[1];
for (let attempt = 1; attempt < maxAttempts; ++attempt) {
try {
const output = await helpers.getExecOutput('hdiutil', [
'attach', imagePath,
'-nobrowse',
'-noverify',
'-noautoopen'
]);
finalizers.push(async () => {
if (fs.existsSync(mountedPath)) {
helpers.log(`Detaching image ${imagePath} mounted at ${mountedPath}`);
await detachImage(mountedPath);
const match = /Apple_HFS\s+(.*)\s*$/.exec(output.stdout);
const mountedPath = match[1];
finalizers.push(async () => {
if (fs.existsSync(mountedPath)) {
helpers.log(`Detaching image ${imagePath} mounted at ${mountedPath}`);
await detachImage(mountedPath);
}
});
return mountedPath;
} catch (err) {
helpers.error(err.message);
await helpers.sleep(2000 * attempt);
}
});
}
return mountedPath;
throw new Error(`Failed to attach image ${imagePath}`);
}
async function copyFiles(targetDir, appDir, files) {
@ -151,17 +173,32 @@ async function createDSStore(mountPath, name) {
}
async function convertDMG(tempDmgPath, dmgPath) {
if (fs.existsSync(dmgPath)){
fs.rmSync(dmgPath);
const maxAttempts = 10;
for (let attempt = 1; attempt < maxAttempts; ++attempt) {
try {
if (fs.existsSync(dmgPath)){
fs.rmSync(dmgPath);
}
await helpers.execWithLog('hdiutil', [
'convert', tempDmgPath,
'-format', 'UDZO',
'-imagekey',
'zlib-level=9',
'-o', dmgPath
]);
return;
} catch (err) {
helpers.error(err.message);
await helpers.sleep(2000 * attempt);
}
}
await helpers.execWithLog('hdiutil', [
'convert', tempDmgPath,
'-format', 'UDZO',
'-imagekey',
'zlib-level=9',
'-o', dmgPath
]);
if (!fs.existsSync(dmgPath)) {
throw new Error(`Failed to convert image ${tempDmgPath} to ${dmgPath}`);
}
}
async function packageDMG(dmgPath, appPath) {