-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge.config.ts
More file actions
407 lines (363 loc) · 15.1 KB
/
Copy pathforge.config.ts
File metadata and controls
407 lines (363 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import type { ForgeConfig } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
import { MakerDeb } from '@electron-forge/maker-deb';
import { MakerRpm } from '@electron-forge/maker-rpm';
import { VitePlugin } from '@electron-forge/plugin-vite';
import { FusesPlugin } from '@electron-forge/plugin-fuses';
import { FuseV1Options, FuseVersion } from '@electron/fuses';
import path from 'node:path';
// @ts-ignore
import fs from 'fs-extra';
// const modulesToExternalize = ["axios"];
// List of modules to place outside ASAR
const modulesToExternalize = [
'@babel/runtime', // this is needed for loading modules at runtime
// 'danfojs',
// 'long',
// '@tensorflow',
// 'seedrandom',
// TODO: preload mathjs
// 'mathjs',
// 'typed-function',
// 'decimal.js',
// 'complex.js',
// 'fraction.js',
// 'javascript-natural-sort',
// 'lodash',
'axios',
'form-data',
'node-fetch',
// 'escape-latex',
// 'tiny-emitter',
// 'table',
// 'papaparse', // Now preloaded
// 'plotly.js-dist-min',
// 'xlsx', // Now preloaded
// 'string-width',
// 'strip-ansi',
// 'is-fullwidth-code-point',
// 'astral-regex',
// 'ansi-styles',
// 'lodash.clonedeep',
// 'fast-deep-equal',
// 'lodash.truncate',
'd3',
'd3-array',
'd3-axis',
'd3-brush',
'd3-chord',
'd3-color',
'd3-contour',
'd3-delaunay',
'd3-dispatch',
'd3-drag',
'd3-dsv',
'd3-ease',
'd3-fetch',
'd3-force',
'd3-format',
'd3-geo',
'd3-hierarchy',
'd3-interpolate',
'd3-path',
'd3-polygon',
'd3-quadtree',
'd3-random',
'd3-scale',
'd3-scale-chromatic',
'd3-selection',
'd3-shape',
'd3-time',
'd3-time-format',
'd3-timer',
'd3-transition',
'd3-zoom',
'internmap',
'delaunator',
'robust-predicates',
// 'ansi-regex',
// 'emoji-regex'
'zx'
];
const config: ForgeConfig = {
packagerConfig: {
asar: true,
icon: './build-resources/icons/icon', // no file extension required - Forge auto-detects
// File associations for the packaged app
protocols: [
{
name: 'nodebook',
schemes: ['nodebook']
}
],
// Platform-specific configurations
...(process.platform === 'darwin' && {
extendInfo: './build-resources/Info.plist'
}),
extraResource: [
// "./node_modules/danfojs/",
// "./node_modules/danfojs-node/",
// "./node_modules/@tensorflow/",
"./examples/",
"./docs/",
// File association resources
"./build-resources/application-x-nodebook.xml",
"./build-resources/nodebook.desktop"
],
// Support for different architectures
...(process.env.npm_config_target_arch && {
arch: process.env.npm_config_target_arch
}),
...(process.env.npm_config_target_platform && {
platform: process.env.npm_config_target_platform
})
},
rebuildConfig: {},
makers: [
new MakerSquirrel({
// Windows installer icons and file associations
iconUrl: 'https://raw.githubusercontent.com/your-username/nodebook.js/main/build-resources/icons/icon.png', // Change this URL
setupIcon: './build-resources/icons/icon.ico',
// Windows file associations - handled by the main process
setupExe: 'nodebook-setup.exe'
}, ['win32']),
new MakerZIP({}, ['darwin', 'linux']),
new MakerRpm({
options: {
icon: './build-resources/icons/icon.png',
// Categories and metadata for better integration
categories: ['Development', 'Science', 'Education'],
productDescription: 'Interactive notebook application for reactive computing'
}
}, ['linux']),
new MakerDeb({
options: {
icon: './build-resources/icons/icon.png',
// Categories and metadata for better integration
categories: ['Development', 'Science', 'Education'],
productDescription: 'Interactive notebook application for reactive computing'
}
}, ['linux'])
],
plugins: [
new VitePlugin({
// `build` can specify multiple entry builds, which can be Main process, Preload scripts, Worker process, etc.
// If you are familiar with Vite configuration, it will look really familiar.
build: [
{
// `entry` is just an alias for `build.lib.entry` in the corresponding file of `config`.
entry: 'src/main.ts',
config: 'vite.main.config.mts',
target: 'main',
},
{
entry: 'src/preload.ts',
config: 'vite.preload.config.mts',
target: 'preload',
},
],
renderer: [
{
name: 'main_window',
config: 'vite.renderer.config.mts',
},
],
}),
// Fuses are used to enable/disable various Electron functionality
// at package time, before code signing the application
new FusesPlugin({
version: FuseVersion.V1,
[FuseV1Options.RunAsNode]: false,
[FuseV1Options.EnableCookieEncryption]: true,
[FuseV1Options.EnableNodeOptionsEnvironmentVariable]: false,
[FuseV1Options.EnableNodeCliInspectArguments]: false,
[FuseV1Options.EnableEmbeddedAsarIntegrityValidation]: true,
[FuseV1Options.OnlyLoadAppFromAsar]: true,
}),
],
hooks: {
packageAfterCopy: async (config, buildPath, electronVersion, platform, arch) => {
const fs = require('fs-extra');
const path = require('path');
// Create directory for external modules
// This will be at the same level as app.asar
const resourcesDir = path.join(buildPath, '..');
const externalModulesDir = path.join(resourcesDir, 'node_modules');
await fs.ensureDir(externalModulesDir);
for (const moduleName of modulesToExternalize) {
const src = path.join(__dirname, 'node_modules', moduleName);
const dest = path.join(externalModulesDir, moduleName);
if (await fs.pathExists(src)) {
await fs.copy(src, dest);
console.log(`✅ Copied ${moduleName} to: ${dest}`);
} else {
console.warn(`⚠️ Module not found: ${moduleName}`);
}
}
// macOS-specific setup for file associations
if (platform === 'darwin') {
console.log('Setting up macOS file association resources...');
// The buildPath for macOS points to: Nodebook.js.app/Contents/Resources/app
// So we need to go to: Nodebook.js.app/Contents/Resources/
const appResourcesDir = path.join(buildPath, '..');
console.log('macOS buildPath:', buildPath);
console.log('macOS appResourcesDir:', appResourcesDir);
// Ensure the Resources directory exists
await fs.ensureDir(appResourcesDir);
// Copy main app icon
const appIconSrc = path.join(__dirname, 'build-resources', 'icons', 'icon.icns');
const appIconDest = path.join(appResourcesDir, 'icon.icns');
if (await fs.pathExists(appIconSrc)) {
await fs.copy(appIconSrc, appIconDest);
console.log('✅ Copied icon.icns to app bundle');
} else {
console.warn('⚠️ icon.icns not found in build resources');
}
// Copy document icon to app bundle resources
const documentIconSrc = path.join(__dirname, 'build-resources', 'icons', 'document.icns');
const documentIconDest = path.join(appResourcesDir, 'document.icns');
if (await fs.pathExists(documentIconSrc)) {
await fs.copy(documentIconSrc, documentIconDest);
console.log('✅ Copied document.icns to app bundle');
} else {
console.warn('⚠️ document.icns not found in build resources');
// Fallback: copy app icon as document icon
if (await fs.pathExists(appIconSrc)) {
await fs.copy(appIconSrc, documentIconDest);
console.log('📄 Using app icon as document icon fallback');
}
}
// Copy Info.plist to app bundle - buildPath is app/Contents/Resources/app, so Info.plist is at ../Info.plist
const infoPlistSrc = path.join(__dirname, 'build-resources', 'Info.plist');
const infoPlistDest = path.join(buildPath, '..', 'Info.plist');
console.log('Info.plist source:', infoPlistSrc);
console.log('Info.plist dest:', infoPlistDest);
if (await fs.pathExists(infoPlistSrc) && await fs.pathExists(infoPlistDest)) {
try {
// Read the existing Info.plist
const existingPlist = await fs.readFile(infoPlistDest, 'utf8');
const customPlist = await fs.readFile(infoPlistSrc, 'utf8');
// Extract document types and UTI declarations from custom plist
const docTypesMatch = customPlist.match(/<key>CFBundleDocumentTypes<\/key>\s*<array>.*?<\/array>/s);
const utiMatch = customPlist.match(/<key>UTExportedTypeDeclarations<\/key>\s*<array>.*?<\/array>/s);
if (docTypesMatch && utiMatch) {
let updatedPlist = existingPlist;
// Add document types if not present
if (!updatedPlist.includes('CFBundleDocumentTypes')) {
updatedPlist = updatedPlist.replace(
'</dict>\n</plist>',
` ${docTypesMatch[0]}\n ${utiMatch[0]}\n</dict>\n</plist>`
);
await fs.writeFile(infoPlistDest, updatedPlist);
console.log('✅ Updated Info.plist with file associations');
} else {
console.log('📄 Info.plist already contains file associations');
}
} else {
console.warn('⚠️ Could not extract file associations from custom Info.plist');
}
} catch (error) {
console.warn('⚠️ Error updating Info.plist:', error.message);
}
} else {
console.warn('⚠️ Info.plist source or destination not found');
console.warn('Source exists:', await fs.pathExists(infoPlistSrc));
console.warn('Dest exists:', await fs.pathExists(infoPlistDest));
}
}
},
// Post-package hook for Linux file associations and macOS verification
postPackage: async (config, packageResult) => {
const { platform, outputPaths } = packageResult;
const path = require('path');
const fs = require('fs-extra');
if (platform === 'linux') {
console.log('Setting up Linux file associations...');
for (const outputPath of outputPaths) {
try {
// Copy desktop file
const desktopSrc = path.join(__dirname, 'build-resources', 'nodebook.desktop');
const desktopDest = path.join(outputPath, 'nodebook.desktop');
if (await fs.pathExists(desktopSrc)) {
await fs.copy(desktopSrc, desktopDest);
console.log('✅ Copied desktop file to package');
}
// Copy MIME type definition
const mimeSrc = path.join(__dirname, 'build-resources', 'application-x-nodebook.xml');
const mimeDest = path.join(outputPath, 'mime', 'application-x-nodebook.xml');
if (await fs.pathExists(mimeSrc)) {
await fs.ensureDir(path.dirname(mimeDest));
await fs.copy(mimeSrc, mimeDest);
console.log('✅ Copied MIME type definition to package');
}
// Create post-install script
const postInstallScript = `#!/bin/bash
# Nodebook.js post-install script for file associations
# Install MIME type
if [ -f "/usr/share/mime/packages/application-x-nodebook.xml" ]; then
xdg-mime install --mode system /usr/share/mime/packages/application-x-nodebook.xml 2>/dev/null || true
fi
# Install desktop file
if [ -f "/usr/share/applications/nodebook.desktop" ]; then
desktop-file-install --mode 644 /usr/share/applications/nodebook.desktop 2>/dev/null || true
fi
# Update databases
update-mime-database /usr/share/mime 2>/dev/null || true
update-desktop-database /usr/share/applications 2>/dev/null || true
# Set as default application for .nbjs files
xdg-mime default nodebook.desktop application/x-nodebook 2>/dev/null || true
echo "Nodebook.js file associations installed successfully!"
`;
const scriptPath = path.join(outputPath, 'post-install.sh');
await fs.writeFile(scriptPath, postInstallScript);
await fs.chmod(scriptPath, '755');
console.log('✅ Created post-install script for Linux file associations');
} catch (error) {
console.warn('⚠️ Failed to setup Linux file associations:', error);
}
}
}
if (platform === 'darwin') {
console.log('Verifying macOS file association setup...');
for (const outputPath of outputPaths) {
try {
const appPath = path.join(outputPath, 'Nodebook.js.app');
const resourcesPath = path.join(appPath, 'Contents', 'Resources');
// Check for required icons
const requiredIcons = ['icon.icns', 'document.icns'];
for (const iconFile of requiredIcons) {
const iconPath = path.join(resourcesPath, iconFile);
if (await fs.pathExists(iconPath)) {
console.log(`✅ Found ${iconFile} in app bundle`);
} else {
console.warn(`⚠️ Missing ${iconFile} in app bundle`);
}
}
// Verify Info.plist contains file associations
const infoPlistPath = path.join(appPath, 'Contents', 'Info.plist');
if (await fs.pathExists(infoPlistPath)) {
const plistContent = await fs.readFile(infoPlistPath, 'utf8');
if (plistContent.includes('CFBundleDocumentTypes') && plistContent.includes('UTExportedTypeDeclarations')) {
console.log('✅ Info.plist contains file association declarations');
} else {
console.warn('⚠️ Info.plist missing file association declarations');
}
} else {
console.warn('⚠️ Info.plist missing from app bundle');
}
console.log('');
console.log('📋 macOS Installation Instructions:');
console.log('1. Move Nodebook.js.app to /Applications/ folder');
console.log('2. Run: sudo /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f /Applications/Nodebook.js.app');
console.log('3. Right-click any .nbjs file → Get Info → Change default app to Nodebook.js');
console.log('4. See docs/macos-file-association-troubleshooting.md for detailed instructions');
} catch (error) {
console.warn('⚠️ Failed to verify macOS file associations:', error);
}
}
}
},
}
};
export default config;