|
1 | 1 | import type { ForgeConfig } from '@electron-forge/shared-types';
|
2 | 2 | import { MakerSquirrel } from '@electron-forge/maker-squirrel';
|
3 |
| -import { MakerZIP } from '@electron-forge/maker-zip'; |
| 3 | +import { MakerDMG } from '@electron-forge/maker-dmg'; |
4 | 4 | import { MakerDeb } from '@electron-forge/maker-deb';
|
5 | 5 | import { MakerRpm } from '@electron-forge/maker-rpm';
|
6 | 6 | import { VitePlugin } from '@electron-forge/plugin-vite';
|
7 | 7 | import { FusesPlugin } from '@electron-forge/plugin-fuses';
|
8 | 8 | import { FuseV1Options, FuseVersion } from '@electron/fuses';
|
| 9 | +import { readdirSync, rmdirSync, statSync, existsSync, mkdirSync, cpSync } from 'node:fs'; |
| 10 | +import { join, normalize } from 'node:path'; |
| 11 | +// Use flora-colossus for finding all dependencies of EXTERNAL_DEPENDENCIES |
| 12 | +// flora-colossus is maintained by MarshallOfSound (a top electron-forge contributor) |
| 13 | +// already included as a dependency of electron-packager/galactus (so we do NOT have to add it to package.json) |
| 14 | +// grabs nested dependencies from tree |
| 15 | +import { Walker, DepType, type Module } from 'flora-colossus'; |
| 16 | + |
| 17 | +let nativeModuleDependenciesToPackage: string[] = []; |
| 18 | + |
| 19 | +export const EXTERNAL_DEPENDENCIES = [ |
| 20 | + 'electron-squirrel-startup', |
| 21 | + 'smart-whisper', |
| 22 | + '@libsql/client', |
| 23 | + '@libsql/darwin-arm64', |
| 24 | + '@libsql/darwin-x64', |
| 25 | + '@libsql/linux-x64-gnu', |
| 26 | + '@libsql/linux-x64-musl', |
| 27 | + '@libsql/win32-x64-msvc', |
| 28 | + 'libsql', |
| 29 | + // Add any other native modules you need here |
| 30 | +]; |
9 | 31 |
|
10 | 32 | const config: ForgeConfig = {
|
| 33 | + hooks: { |
| 34 | + prePackage: async () => { |
| 35 | + console.error('prePackage'); |
| 36 | + const projectRoot = normalize(__dirname); |
| 37 | + // In a monorepo, node_modules are typically at the root level |
| 38 | + const monorepoRoot = join(projectRoot, '../../'); // Go up to monorepo root |
| 39 | + |
| 40 | + const getExternalNestedDependencies = async ( |
| 41 | + nodeModuleNames: string[], |
| 42 | + includeNestedDeps = true |
| 43 | + ) => { |
| 44 | + const foundModules = new Set(nodeModuleNames); |
| 45 | + if (includeNestedDeps) { |
| 46 | + for (const external of nodeModuleNames) { |
| 47 | + type MyPublicClass<T> = { |
| 48 | + [P in keyof T]: T[P]; |
| 49 | + }; |
| 50 | + type MyPublicWalker = MyPublicClass<Walker> & { |
| 51 | + modules: Module[]; |
| 52 | + walkDependenciesForModule: ( |
| 53 | + moduleRoot: string, |
| 54 | + depType: DepType |
| 55 | + ) => Promise<void>; |
| 56 | + }; |
| 57 | + const moduleRoot = join(monorepoRoot, 'node_modules', external); |
| 58 | + console.log('moduleRoot', moduleRoot); |
| 59 | + // Initialize Walker with monorepo root as base path |
| 60 | + const walker = new Walker(monorepoRoot) as unknown as MyPublicWalker; |
| 61 | + walker.modules = []; |
| 62 | + await walker.walkDependenciesForModule(moduleRoot, DepType.PROD); |
| 63 | + walker.modules |
| 64 | + .filter((dep) => (dep.nativeModuleType as number) === DepType.PROD) |
| 65 | + // Remove the problematic name splitting that breaks scoped packages |
| 66 | + .map((dep) => dep.name) |
| 67 | + .forEach((name) => foundModules.add(name)); |
| 68 | + } |
| 69 | + } |
| 70 | + return foundModules; |
| 71 | + }; |
| 72 | + |
| 73 | + const nativeModuleDependencies = await getExternalNestedDependencies(EXTERNAL_DEPENDENCIES); |
| 74 | + nativeModuleDependenciesToPackage = Array.from(nativeModuleDependencies); |
| 75 | + |
| 76 | + // Copy external dependencies to local node_modules |
| 77 | + console.error('Copying external dependencies to local node_modules'); |
| 78 | + const localNodeModules = join(projectRoot, 'node_modules'); |
| 79 | + const rootNodeModules = join(monorepoRoot, 'node_modules'); |
| 80 | + |
| 81 | + // Ensure local node_modules directory exists |
| 82 | + if (!existsSync(localNodeModules)) { |
| 83 | + mkdirSync(localNodeModules, { recursive: true }); |
| 84 | + } |
| 85 | + |
| 86 | + console.log(`Found ${nativeModuleDependenciesToPackage.length} dependencies to copy`); |
| 87 | + |
| 88 | + // Copy all required dependencies |
| 89 | + for (const dep of nativeModuleDependenciesToPackage) { |
| 90 | + const rootDepPath = join(rootNodeModules, dep); |
| 91 | + const localDepPath = join(localNodeModules, dep); |
| 92 | + |
| 93 | + try { |
| 94 | + // Skip if source doesn't exist |
| 95 | + if (!existsSync(rootDepPath)) { |
| 96 | + console.log(`Skipping ${dep}: not found in root node_modules`); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + // Skip if target already exists (don't override) |
| 101 | + if (existsSync(localDepPath)) { |
| 102 | + console.log(`Skipping ${dep}: already exists locally`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Copy the package |
| 107 | + console.log(`Copying ${dep}...`); |
| 108 | + cpSync(rootDepPath, localDepPath, { recursive: true }); |
| 109 | + console.log(`✓ Successfully copied ${dep}`); |
| 110 | + |
| 111 | + } catch (error) { |
| 112 | + console.error(`Failed to copy ${dep}:`, error); |
| 113 | + } |
| 114 | + } |
| 115 | + }, |
| 116 | + packageAfterPrune: async (_forgeConfig, buildPath) => { |
| 117 | + try { |
| 118 | + function getItemsFromFolder( |
| 119 | + path: string, |
| 120 | + totalCollection: { |
| 121 | + path: string; |
| 122 | + type: 'directory' | 'file'; |
| 123 | + empty: boolean; |
| 124 | + }[] = [] |
| 125 | + ) { |
| 126 | + try { |
| 127 | + const normalizedPath = normalize(path); |
| 128 | + const childItems = readdirSync(normalizedPath); |
| 129 | + const getItemStats = statSync(normalizedPath); |
| 130 | + if (getItemStats.isDirectory()) { |
| 131 | + totalCollection.push({ |
| 132 | + path: normalizedPath, |
| 133 | + type: 'directory', |
| 134 | + empty: childItems.length === 0, |
| 135 | + }); |
| 136 | + } |
| 137 | + childItems.forEach((childItem) => { |
| 138 | + const childItemNormalizedPath = join(normalizedPath, childItem); |
| 139 | + const childItemStats = statSync(childItemNormalizedPath); |
| 140 | + if (childItemStats.isDirectory()) { |
| 141 | + getItemsFromFolder(childItemNormalizedPath, totalCollection); |
| 142 | + } else { |
| 143 | + totalCollection.push({ |
| 144 | + path: childItemNormalizedPath, |
| 145 | + type: 'file', |
| 146 | + empty: false, |
| 147 | + }); |
| 148 | + } |
| 149 | + }); |
| 150 | + } catch { |
| 151 | + return; |
| 152 | + } |
| 153 | + return totalCollection; |
| 154 | + } |
| 155 | + const getItems = getItemsFromFolder(buildPath) ?? []; |
| 156 | + for (const item of getItems) { |
| 157 | + const DELETE_EMPTY_DIRECTORIES = true; |
| 158 | + if (item.empty === true) { |
| 159 | + if (DELETE_EMPTY_DIRECTORIES) { |
| 160 | + const pathToDelete = normalize(item.path); |
| 161 | + // one last check to make sure it is a directory and is empty |
| 162 | + const stats = statSync(pathToDelete); |
| 163 | + if (!stats.isDirectory()) { |
| 164 | + // SKIPPING DELETION: pathToDelete is not a directory |
| 165 | + return; |
| 166 | + } |
| 167 | + const childItems = readdirSync(pathToDelete); |
| 168 | + if (childItems.length !== 0) { |
| 169 | + // SKIPPING DELETION: pathToDelete is not empty |
| 170 | + return; |
| 171 | + } |
| 172 | + rmdirSync(pathToDelete); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } catch (error) { |
| 177 | + console.error('Error in packageAfterPrune:', error); |
| 178 | + throw error; |
| 179 | + } |
| 180 | + }, |
| 181 | + }, |
11 | 182 | packagerConfig: {
|
12 | 183 | asar: true,
|
13 | 184 | name: 'Amical',
|
14 | 185 | executableName: 'Amical',
|
15 | 186 | icon: './assets/logo', // Path to your icon file (without extension)
|
16 |
| - extraResource: ['../../packages/native-helpers/swift-helper/bin'], |
| 187 | + extraResource: [ |
| 188 | + '../../packages/native-helpers/swift-helper/bin', |
| 189 | + './src/db/migrations', |
| 190 | + ], |
17 | 191 | extendInfo: {
|
18 | 192 | NSMicrophoneUsageDescription:
|
19 | 193 | 'This app needs access to your microphone to record audio for transcription.',
|
20 | 194 | },
|
| 195 | + //! issues with monorepo setup and module resolutions |
| 196 | + //! when forge walks paths via flora-colossus |
| 197 | + prune: false, |
| 198 | + ignore: (file: string) => { |
| 199 | + try { |
| 200 | + |
| 201 | + const filePath = file.toLowerCase(); |
| 202 | + const KEEP_FILE = { |
| 203 | + keep: false, |
| 204 | + log: true, |
| 205 | + }; |
| 206 | + // NOTE: must return false for empty string or nothing will be packaged |
| 207 | + if (filePath === '') KEEP_FILE.keep = true; |
| 208 | + if (!KEEP_FILE.keep && filePath === '/package.json') KEEP_FILE.keep = true; |
| 209 | + if (!KEEP_FILE.keep && filePath === '/node_modules') KEEP_FILE.keep = true; |
| 210 | + if (!KEEP_FILE.keep && filePath === '/.vite') KEEP_FILE.keep = true; |
| 211 | + if (!KEEP_FILE.keep && filePath.startsWith('/.vite/')) KEEP_FILE.keep = true; |
| 212 | + if (!KEEP_FILE.keep && filePath.startsWith('/node_modules/')) { |
| 213 | + // check if matches any of the external dependencies |
| 214 | + for (const dep of nativeModuleDependenciesToPackage) { |
| 215 | + if ( |
| 216 | + filePath === `/node_modules/${dep}/` || |
| 217 | + filePath === `/node_modules/${dep}` |
| 218 | + ) { |
| 219 | + KEEP_FILE.keep = true; |
| 220 | + break; |
| 221 | + } |
| 222 | + if (filePath === `/node_modules/${dep}/package.json`) { |
| 223 | + KEEP_FILE.keep = true; |
| 224 | + break; |
| 225 | + } |
| 226 | + if (filePath.startsWith(`/node_modules/${dep}/`)) { |
| 227 | + KEEP_FILE.keep = true; |
| 228 | + KEEP_FILE.log = false; |
| 229 | + break; |
| 230 | + } |
| 231 | + |
| 232 | + // Handle scoped packages: if dep is @scope/package, also keep @scope/ directory |
| 233 | + if (dep.includes('/') && dep.startsWith('@')) { |
| 234 | + const scopeDir = dep.split('/')[0]; // @libsql/client -> @libsql |
| 235 | + if ( |
| 236 | + filePath === `/node_modules/${scopeDir}/` || |
| 237 | + filePath === `/node_modules/${scopeDir}` || |
| 238 | + filePath.startsWith(`/node_modules/${scopeDir}/`) |
| 239 | + ) { |
| 240 | + KEEP_FILE.keep = true; |
| 241 | + KEEP_FILE.log = filePath === `/node_modules/${scopeDir}/` || filePath === `/node_modules/${scopeDir}`; |
| 242 | + break; |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + if (KEEP_FILE.keep) { |
| 248 | + if (KEEP_FILE.log) console.log('Keeping:', file); |
| 249 | + return false; |
| 250 | + } |
| 251 | + return true; |
| 252 | + } catch (error) { |
| 253 | + console.error('Error in ignore:', error); |
| 254 | + throw error; |
| 255 | + } |
| 256 | + }, |
21 | 257 | },
|
22 | 258 | rebuildConfig: {},
|
23 |
| - makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})], |
| 259 | + makers: [ |
| 260 | + new MakerSquirrel({}), |
| 261 | + new MakerDMG({ |
| 262 | + name: 'Amical', |
| 263 | + icon: './assets/logo.svg' |
| 264 | + }, ['darwin']), |
| 265 | + new MakerRpm({}), |
| 266 | + new MakerDeb({}) |
| 267 | + ], |
24 | 268 | plugins: [
|
25 | 269 | new VitePlugin({
|
26 | 270 | // `build` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.
|
|
0 commit comments