Files: aeb64ef804d9d89d3664419052ca631bf516c9ec / index.js
2679 bytesRaw
1 | process.on('uncaughtException', function (err) { |
2 | console.log(err) |
3 | process.exit() |
4 | }) |
5 | |
6 | var electron = require('electron') |
7 | var openWindow = require('./lib/window') |
8 | var createSbot = require('./lib/ssb-server') |
9 | var serveBlobs = require('./lib/serve-blobs') |
10 | var makeSingleInstance = require('./lib/make-single-instance') |
11 | var pull = require('pull-stream') |
12 | var pullFile = require('pull-file') |
13 | var Path = require('path') |
14 | var fs = require('fs') |
15 | var defaultMenu = require('electron-default-menu') |
16 | var Menu = electron.Menu |
17 | var dataUriToBuffer = require('data-uri-to-buffer') |
18 | var extend = require('xtend') |
19 | var ssbKeys = require('ssb-keys') |
20 | |
21 | var windows = { |
22 | dialogs: new Set() |
23 | } |
24 | |
25 | var context = null |
26 | if (process.argv.includes('--use-global-ssb') || process.argv.includes('-g')) { |
27 | context = setupContext('ssb', { |
28 | server: false |
29 | }) |
30 | } else { |
31 | makeSingleInstance(windows, openMainWindow) |
32 | context = setupContext('ssb') |
33 | } |
34 | |
35 | electron.ipcMain.on('add-blob', (ev, id, path, cb) => { |
36 | pull( |
37 | path.startsWith('data:') ? pull.values([dataUriToBuffer(path)]) : pullFile(path), |
38 | context.sbot.blobs.add((err, hash) => { |
39 | if (err) return ev.sender.send('response', id, err) |
40 | ev.sender.send('response', id, null, hash) |
41 | }) |
42 | ) |
43 | }) |
44 | |
45 | electron.app.on('ready', function () { |
46 | Menu.setApplicationMenu(Menu.buildFromTemplate(defaultMenu(electron.app, electron.shell))) |
47 | openMainWindow() |
48 | }) |
49 | |
50 | electron.app.on('activate', function (e) { |
51 | openMainWindow() |
52 | }) |
53 | |
54 | function openMainWindow () { |
55 | if (!windows.main) { |
56 | windows.main = openWindow(context, Path.join(__dirname, 'main-window.js'), { |
57 | minWidth: 800, |
58 | width: 1024, |
59 | height: 768, |
60 | titleBarStyle: 'hidden-inset', |
61 | title: 'Ferment', |
62 | show: true, |
63 | backgroundColor: '#444', |
64 | webPreferences: { |
65 | experimentalFeatures: true |
66 | }, |
67 | icon: './ferment-logo.png' |
68 | }) |
69 | windows.main.setSheetOffset(40) |
70 | windows.main.on('closed', function () { |
71 | windows.main = null |
72 | }) |
73 | } |
74 | } |
75 | |
76 | function setupContext (appName, opts) { |
77 | var ssbConfig = require('ssb-config/inject')(appName, extend({ |
78 | port: 8008, |
79 | blobsPort: 7777 |
80 | }, opts)) |
81 | |
82 | if (opts && opts.server === false) { |
83 | return { |
84 | config: ssbConfig |
85 | } |
86 | } else { |
87 | ssbConfig.keys = ssbKeys.loadOrCreateSync(Path.join(ssbConfig.path, 'secret')) |
88 | var context = { |
89 | sbot: createSbot(ssbConfig), |
90 | config: ssbConfig |
91 | } |
92 | ssbConfig.manifest = context.sbot.getManifest() |
93 | serveBlobs(context) |
94 | fs.writeFileSync(Path.join(ssbConfig.path, 'manifest.json'), JSON.stringify(ssbConfig.manifest)) |
95 | console.log(`Address: ${context.sbot.getAddress()}`) |
96 | return context |
97 | } |
98 | |
99 | return ssbConfig |
100 | } |
101 |
Built with git-ssb-web