Use rezip-electron when build macOS zip files
This commit is contained in:
parent
e1a12d2b99
commit
d2b99213da
3 changed files with 102 additions and 0 deletions
57
ts/scripts/artifact-build-completed.ts
Normal file
57
ts/scripts/artifact-build-completed.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { tmpdir } from 'os';
|
||||
import { mkdtemp, rm, rename, stat } from 'fs/promises';
|
||||
import { createReadStream } from 'fs';
|
||||
import { pipeline } from 'stream/promises';
|
||||
import { createHash } from 'crypto';
|
||||
import path from 'path';
|
||||
import type { ArtifactCreated } from 'electron-builder';
|
||||
|
||||
export async function artifactBuildCompleted({
|
||||
target,
|
||||
file,
|
||||
packager,
|
||||
updateInfo,
|
||||
}: ArtifactCreated): Promise<void> {
|
||||
if (packager.platform.name !== 'mac') {
|
||||
return;
|
||||
}
|
||||
if (target?.name !== 'zip') {
|
||||
return;
|
||||
}
|
||||
if (!file.endsWith('.zip')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ESM module
|
||||
const { optimize } = await import('@indutny/rezip-electron');
|
||||
|
||||
const tmpFolder = await mkdtemp(path.join(tmpdir(), 'rezip'));
|
||||
const optimizedPath = path.join(tmpFolder, path.basename(file));
|
||||
|
||||
try {
|
||||
console.log(`Optimizing ${file} => ${optimizedPath}`);
|
||||
|
||||
await optimize({
|
||||
inputPath: file,
|
||||
outputPath: optimizedPath,
|
||||
blockMapPath: `${file}.blockmap`,
|
||||
});
|
||||
|
||||
console.log(`Replacing ${file}`);
|
||||
await rename(optimizedPath, file);
|
||||
} finally {
|
||||
await rm(tmpFolder, { recursive: true });
|
||||
}
|
||||
|
||||
console.log('Updating hash and size');
|
||||
const sha512 = createHash('sha512');
|
||||
await pipeline(createReadStream(file), sha512);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
updateInfo.sha512 = sha512.digest('base64');
|
||||
const { size } = await stat(file);
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
updateInfo.size = size;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue