|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawnSync } = require('child_process'); |
| 6 | + |
| 7 | +const MacArtifactTarget = { |
| 8 | + X64: 'mac-x64', |
| 9 | + Arm64: 'mac-arm64', |
| 10 | +}; |
| 11 | + |
| 12 | +const NativeArchToken = { |
| 13 | + [MacArtifactTarget.X64]: 'x86_64', |
| 14 | + [MacArtifactTarget.Arm64]: 'arm64', |
| 15 | +}; |
| 16 | + |
| 17 | +const NativeTargetPathToken = { |
| 18 | + [MacArtifactTarget.X64]: ['x64-darwin', 'darwin-x64'], |
| 19 | + [MacArtifactTarget.Arm64]: ['arm64-darwin', 'darwin-arm64'], |
| 20 | +}; |
| 21 | + |
| 22 | +const rootDir = path.resolve(__dirname, '..'); |
| 23 | +const target = (process.argv[2] || '').trim(); |
| 24 | + |
| 25 | +function fail(message) { |
| 26 | + console.error(`[verify-mac-artifact] ${message}`); |
| 27 | + process.exit(1); |
| 28 | +} |
| 29 | + |
| 30 | +function log(message) { |
| 31 | + console.log(`[verify-mac-artifact] ${message}`); |
| 32 | +} |
| 33 | + |
| 34 | +if (!Object.values(MacArtifactTarget).includes(target)) { |
| 35 | + fail(`Usage: node scripts/verify-mac-artifact.cjs ${MacArtifactTarget.X64}|${MacArtifactTarget.Arm64}`); |
| 36 | +} |
| 37 | + |
| 38 | +function walk(dir, visitor) { |
| 39 | + if (!fs.existsSync(dir)) return; |
| 40 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 41 | + const fullPath = path.join(dir, entry.name); |
| 42 | + if (entry.isDirectory()) { |
| 43 | + visitor(fullPath, entry); |
| 44 | + walk(fullPath, visitor); |
| 45 | + } else { |
| 46 | + visitor(fullPath, entry); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function findPackagedApps() { |
| 52 | + const releaseDir = path.join(rootDir, 'release'); |
| 53 | + const apps = []; |
| 54 | + walk(releaseDir, (candidate, entry) => { |
| 55 | + if (entry.isDirectory() && candidate.endsWith('.app')) { |
| 56 | + apps.push(candidate); |
| 57 | + } |
| 58 | + }); |
| 59 | + return apps.sort(); |
| 60 | +} |
| 61 | + |
| 62 | +function runFile(filePath) { |
| 63 | + const result = spawnSync('file', [filePath], { |
| 64 | + encoding: 'utf8', |
| 65 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 66 | + }); |
| 67 | + if (result.status !== 0) { |
| 68 | + fail(`Could not inspect file architecture for ${filePath}: ${result.stderr || result.error?.message || 'unknown error'}`); |
| 69 | + } |
| 70 | + return (result.stdout || '').trim(); |
| 71 | +} |
| 72 | + |
| 73 | +function selectApp(apps, expectedToken) { |
| 74 | + const matching = apps.find((appPath) => { |
| 75 | + const executablePath = path.join(appPath, 'Contents', 'MacOS', 'WeSight'); |
| 76 | + return fs.existsSync(executablePath) && runFile(executablePath).includes(expectedToken); |
| 77 | + }); |
| 78 | + if (matching) return matching; |
| 79 | + return apps[0] || null; |
| 80 | +} |
| 81 | + |
| 82 | +function assertFileHasArch(filePath, expectedToken) { |
| 83 | + const output = runFile(filePath); |
| 84 | + if (!output.includes(expectedToken)) { |
| 85 | + fail(`Expected ${filePath} to include ${expectedToken}, got: ${output}`); |
| 86 | + } |
| 87 | + log(output); |
| 88 | +} |
| 89 | + |
| 90 | +function shouldInspectNativeModule(filePath) { |
| 91 | + const normalizedPath = filePath.replace(/\\/g, '/'); |
| 92 | + const targetPathTokens = NativeTargetPathToken[target]; |
| 93 | + if (targetPathTokens.some((token) => normalizedPath.includes(`/${token}/`) || normalizedPath.includes(`/${token}.node`))) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + const packagedTargetPattern = /\/(?:x64|arm64)-(?:darwin|linux|win32)\/|\/(?:darwin|linux|win32)-(?:x64|arm64)\//; |
| 98 | + return !packagedTargetPattern.test(normalizedPath); |
| 99 | +} |
| 100 | + |
| 101 | +const apps = findPackagedApps(); |
| 102 | +const expectedArchToken = NativeArchToken[target]; |
| 103 | +const appPath = selectApp(apps, expectedArchToken); |
| 104 | +if (!appPath) { |
| 105 | + fail(`No packaged .app found under ${path.join(rootDir, 'release')}`); |
| 106 | +} |
| 107 | + |
| 108 | +const executablePath = path.join(appPath, 'Contents', 'MacOS', 'WeSight'); |
| 109 | + |
| 110 | +log(`Checking ${appPath}`); |
| 111 | + |
| 112 | +if (!fs.existsSync(executablePath)) { |
| 113 | + fail(`Packaged app executable is missing: ${executablePath}`); |
| 114 | +} |
| 115 | +assertFileHasArch(executablePath, expectedArchToken); |
| 116 | + |
| 117 | +const nativeModules = []; |
| 118 | +const skippedNativeModules = []; |
| 119 | +walk(appPath, (candidate, entry) => { |
| 120 | + if (!entry.isFile() || !candidate.endsWith('.node')) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if (shouldInspectNativeModule(candidate)) { |
| 125 | + nativeModules.push(candidate); |
| 126 | + } else { |
| 127 | + skippedNativeModules.push(candidate); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +if (nativeModules.length === 0) { |
| 132 | + fail('No native .node modules were found in the packaged app.'); |
| 133 | +} |
| 134 | + |
| 135 | +for (const nativeModule of nativeModules.sort()) { |
| 136 | + assertFileHasArch(nativeModule, expectedArchToken); |
| 137 | +} |
| 138 | + |
| 139 | +log(`Verified ${nativeModules.length} native module(s).`); |
| 140 | +if (skippedNativeModules.length > 0) { |
| 141 | + log(`Skipped ${skippedNativeModules.length} non-target vendor native module(s).`); |
| 142 | +} |
0 commit comments