|
1 | 1 | const fs = require('fs');
|
| 2 | +const path = require('path'); |
| 3 | +const { spawn } = require('child_process'); |
2 | 4 |
|
3 | 5 | exports.default = async function afterbuild(context) {
|
4 |
| - // do not run when not on macOS or when not in CI (CircleCI or GitHub Actions) |
5 | 6 | const isCI = process.env.CIRCLE_BRANCH || process.env.GITHUB_REF;
|
6 |
| - if (process.platform !== 'darwin' || !isCI) { |
| 7 | + if (!isCI) { |
7 | 8 | return;
|
8 | 9 | }
|
9 | 10 |
|
10 |
| - |
11 | 11 | console.log('AFTER BUILD', context);
|
12 | 12 |
|
| 13 | + // Handle macOS DMG notarization |
| 14 | + if (process.platform === 'darwin') { |
| 15 | + await handleMacOSNotarization(context); |
| 16 | + } |
| 17 | + |
| 18 | + // Handle Windows installer signing |
| 19 | + if (process.platform === 'win32') { |
| 20 | + await handleWindowsSigning(context); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +async function handleMacOSNotarization(context) { |
13 | 25 | const artifactPaths = context.artifactPaths;
|
14 |
| - const newDmgs = artifactPaths.filter((dmg) => dmg.endsWith('.dmg')).map((dmg) => dmg); // Removed unnecessary quotes for consistency |
| 26 | + const newDmgs = artifactPaths.filter((dmg) => dmg.endsWith('.dmg')).map((dmg) => dmg); |
15 | 27 |
|
16 | 28 | let existingDmgs = [];
|
17 | 29 | try {
|
18 |
| - // Attempt to read the existing dmgs.json file |
19 | 30 | const data = fs.readFileSync('dmgs.json', 'utf8');
|
20 | 31 | const parsedData = JSON.parse(data);
|
21 |
| - existingDmgs = parsedData.dmgs || []; // Ensure existingDmgs is an array |
| 32 | + existingDmgs = parsedData.dmgs || []; |
22 | 33 | } catch (error) {
|
23 |
| - // If there's an error reading the file (e.g., file does not exist), proceed with an empty array |
24 | 34 | console.log('No existing dmgs.json or error reading file, creating new one.');
|
25 | 35 | }
|
26 | 36 |
|
27 |
| - // Combine existing and new dmgs, avoiding duplicates |
28 | 37 | const combinedDmgs = [...new Set([...existingDmgs, ...newDmgs])];
|
29 |
| - |
30 |
| - // Write/overwrite the dmgs.json with the combined list of dmgs |
31 | 38 | fs.writeFileSync('dmgs.json', JSON.stringify({ dmgs: combinedDmgs }, null, 2));
|
32 |
| -}; |
| 39 | +} |
| 40 | + |
| 41 | +async function handleWindowsSigning(context) { |
| 42 | + try { |
| 43 | + // Get version from package.json |
| 44 | + const packageJsonPath = path.join(__dirname, 'package.json'); |
| 45 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 46 | + const version = packageJson.version; |
| 47 | + |
| 48 | + // Construct the installer path |
| 49 | + const installerPath = path.join(__dirname, 'release', `Remix-Desktop-Setup-${version}.exe`); |
| 50 | + |
| 51 | + if (!fs.existsSync(installerPath)) { |
| 52 | + console.log(`Windows installer not found at ${installerPath}, skipping signing`); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + console.log(`Signing Windows installer: ${installerPath}`); |
| 57 | + |
| 58 | + // Check if we have the required environment variables for signing |
| 59 | + const requiredEnvVars = ['SM_API_KEY', 'SM_CLIENT_CERT_FILE_B64', 'SM_CODE_SIGNING_CERT_SHA1_HASH']; |
| 60 | + const missingEnvVars = requiredEnvVars.filter(envVar => !process.env[envVar]); |
| 61 | + |
| 62 | + if (missingEnvVars.length > 0) { |
| 63 | + console.log(`Missing required environment variables for signing: ${missingEnvVars.join(', ')}`); |
| 64 | + console.log('Skipping Windows installer signing'); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Use the existing sign-windows.sh script |
| 69 | + await signWithScript(installerPath); |
| 70 | + |
| 71 | + console.log(`Successfully signed Windows installer: ${installerPath}`); |
| 72 | + |
| 73 | + } catch (error) { |
| 74 | + console.error('Error signing Windows installer:', error); |
| 75 | + // Don't fail the build, just log the error |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function signWithScript(filePath) { |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + const child = spawn( |
| 82 | + 'bash', |
| 83 | + [ |
| 84 | + path.resolve(__dirname, 'sign-windows.sh'), |
| 85 | + filePath |
| 86 | + ], |
| 87 | + { stdio: 'inherit' } |
| 88 | + ); |
| 89 | + |
| 90 | + child.on('exit', (code) => { |
| 91 | + if (code === 0) { |
| 92 | + console.log('Windows installer signing completed successfully.'); |
| 93 | + resolve(); |
| 94 | + } else { |
| 95 | + reject(new Error(`Signing script exited with code ${code}`)); |
| 96 | + } |
| 97 | + }); |
| 98 | + }); |
| 99 | +} |
0 commit comments