blob: 4ea84a30c4220b894d91e5efc82d87771e99a1e5 [file]
import { spawn } from 'node:child_process';
import { access, readFile, rm } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { npmSpawnOptions } from './npm-spawn.mjs';
const repoRoot = dirname(dirname(fileURLToPath(import.meta.url)));
const desktopRoot = join(repoRoot, 'apps', 'desktop');
const releaseDirectory = join(desktopRoot, 'release');
const electronDistributionDirectory = join(repoRoot, 'node_modules', 'electron', 'dist');
const requiredElectronLicensePaths = [
join(electronDistributionDirectory, 'LICENSE'),
join(electronDistributionDirectory, 'LICENSES.chromium.html'),
];
export function runCommand(
command,
args,
{ spawnProcess = spawn, platform = process.platform } = {},
) {
return new Promise((resolve, reject) => {
// Every command here is a repository constant, so the shell that Windows
// needs to reach npm.cmd introduces no quoting concern.
const child = spawnProcess(
command,
args,
npmSpawnOptions({ cwd: repoRoot, env: process.env, stdio: 'inherit' }, platform),
);
child.once('error', reject);
child.once('exit', (code, signal) => {
if (code === 0) {
resolve();
return;
}
reject(
new Error(
`${command} ${args.join(' ')} failed with ${
signal ? `signal ${signal}` : `exit code ${code}`
}`,
),
);
});
});
}
export async function packageWindowsX64({
platform = process.platform,
arch = process.arch,
run = runCommand,
remove = rm,
assertFile = access,
} = {}) {
if (platform !== 'win32' || arch !== 'x64') {
throw new Error('Release packaging requires a Windows x64 host.');
}
const manifest = JSON.parse(await readFile(join(desktopRoot, 'package.json'), 'utf8'));
const exePath = join(releaseDirectory, `Maka-${manifest.version}-win-x64.exe`);
const zipPath = join(releaseDirectory, `Maka-${manifest.version}-win-x64.zip`);
const updateMetadataPath = join(releaseDirectory, 'latest.yml');
const unpackedDirectory = join(releaseDirectory, 'win-unpacked');
for (const path of requiredElectronLicensePaths) {
await assertFile(path);
}
await run('npm', ['run', 'clean']);
await run('npm', ['run', 'build']);
await run('npm', ['run', 'prepare:bundled-git']);
await run('npm', ['run', 'check:release']);
await remove(releaseDirectory, { recursive: true, force: true });
await run('npm', ['--workspace', '@maka/desktop', 'run', 'package:windows-x64']);
await assertFile(exePath);
await assertFile(zipPath);
await assertFile(updateMetadataPath);
// win-unpacked stays: the ZIP is an archive of exactly this directory, so it
// is what the verifier inspects. Extracting the ZIP would only rebuild a copy
// of it, and writing tens of thousands of small files on Windows costs more
// than the entire packaging step. It is not a release asset — the upload
// globs match artifacts by name — and the release directory is rebuilt from
// scratch on the next run.
await assertFile(unpackedDirectory);
return { exePath, zipPath, unpackedDirectory };
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
const { exePath } = await packageWindowsX64();
console.log(`Created ${exePath}`);
}