git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 3dfc64e44061b79b9d06b8b067d711ed62c28896

Files: 3dfc64e44061b79b9d06b8b067d711ed62c28896 / index.js

2681 bytesRaw
1process.on('uncaughtException', function (err) {
2 console.log(err)
3 process.exit()
4})
5
6var electron = require('electron')
7var openWindow = require('./lib/window')
8var createSbot = require('./lib/ssb-server')
9var serveBlobs = require('./lib/serve-blobs')
10var makeSingleInstance = require('./lib/make-single-instance')
11var pull = require('pull-stream')
12var pullFile = require('pull-file')
13var Path = require('path')
14var fs = require('fs')
15var defaultMenu = require('electron-default-menu')
16var Menu = electron.Menu
17var dataUriToBuffer = require('data-uri-to-buffer')
18var extend = require('xtend')
19var ssbKeys = require('ssb-keys')
20
21var windows = {
22 dialogs: new Set()
23}
24
25var context = null
26if (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
35electron.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
45electron.app.on('ready', function () {
46 Menu.setApplicationMenu(Menu.buildFromTemplate(defaultMenu(electron.app, electron.shell)))
47 openMainWindow()
48})
49
50electron.app.on('activate', function (e) {
51 openMainWindow()
52})
53
54function 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: 'Patchwork',
62 show: true,
63 backgroundColor: '#EEE',
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
76function 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