-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·84 lines (78 loc) · 2.44 KB
/
index.js
File metadata and controls
executable file
·84 lines (78 loc) · 2.44 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
const electron = require('electron')
const app = electron.app
const chokidar = require('chokidar')
const Syncer = require('./lib/sync')
const path = require('path')
const config = require(path.join(process.env.HOME, '.syncer.js'))
const tray = require('./lib/tray')
const tunnel = require('./lib/tunnel')
app.on('ready', () => {
tray.init()
config.repos.forEach((repo) => {
tunnel.create(repo)
let scanComplete = false
const syncer = new Syncer({
srcDir: repo.local,
remote: repo.remote,
verbose: config.options.verbose
})
if (config.options.verbose) {
console.log('configuring server')
}
// TODO extract all this watcher stuff into its own file
syncer.configureServer().then(() => {
if (config.options.watch) {
console.log('watching', repo.local)
let ignored = [new RegExp(`${repo.local}/.git/refs/__git-n-sync__/head|${repo.local}/.git/index-git-n-sync`),
new RegExp(`${repo.local}/.git/objects`)
]
if (repo.ignore) {
ignored = ignored.concat(repo.ignore.map(pattern =>
!pattern.startsWith('/') ? path.join(repo.local, pattern) : pattern
))
}
const watcher = chokidar.watch(repo.local, {ignored})
// TODO if a huge number of files/directories are detected
// warn the user, there is probably something not being ignored that
// should be
watcher.on('all', (event, path) => {
if (scanComplete) {
if (process.env.DEBUG) {
console.log('event', event, path)
}
display(syncer.sync())
}
})
watcher.on('ready', () => {
if (config.options.verbose) {
console.log(new Date(), 'WATCHER IS READY')
}
scanComplete = true
display(syncer.sync())
})
} else {
display(syncer.sync())
}
}).catch((e) => {
console.trace('Error configuring server', e)
})
})
})
const display = (results) => {
return results.then((result) => {
if (!result) {
return
}
const {updates, duration} = result
if (updates.length === 0) {
console.log('No changes were synced')
} else {
updates.map((update) => {
console.log(`${update.action} ${update.filename}`)
})
}
console.log(`Sync completed in ${duration / 1000} seconds`)
}).catch((e) => {
console.trace('error syncing', e)
})
}