This repository has been archived by the owner on Jul 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (117 loc) · 4.26 KB
/
index.js
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
const commander = require('commander')
const { compose, evolve, tap, uncurryN } = require('ramda')
const VError = require('verror')
const createMissingPathsOn = require('./lib/createMissingPathsOn')
const getFilesFrom = require('./lib/getFilesFrom')
const getMissingFilesFrom = require('./lib/getMissingFilesFrom')
const getUnsyncedFilesIn = require('./lib/getUnsyncedFilesIn')
const removeFilesFrom = require('./lib/removeFilesFrom')
const serializeFilesFrom = require('./lib/serializeFilesFrom')
const setPerimissionsFor = require('./lib/setPerimissionsFor')
const { loadSnapshot, saveSnapshot } = require('./lib/snapshot')
const symlinkFilesTo = require('./lib/symlinkFilesTo')
const syncFilesTo = require('./lib/syncFilesTo')
const parseNumber = value => {
const parsedValue = parseInt(value, 10)
if (isNaN(parsedValue)) {
throw new commander.InvalidOptionArgumentError('Not a number.')
}
return parsedValue
}
const parseOpts = evolve({
safeDelete: parseNumber,
puid: parseNumber,
pgid: parseNumber,
})
const getFilesAndSerializeFrom = dir =>
compose(
serializeFilesFrom(dir),
getFilesFrom,
)(dir)
const removeFilesThatDontExistOnFrom = uncurryN(4, (opts, getFrom, removeFrom) =>
compose(
removeFilesFrom(opts, removeFrom),
getMissingFilesFrom(getFrom),
),
)
const syncUnsycnedFilesTo = uncurryN(2, dir =>
compose(
syncFilesTo(dir),
tap(createMissingPathsOn(dir)),
getUnsyncedFilesIn,
),
)
const symlinkMissingFilesTo = uncurryN(2, dir =>
compose(
symlinkFilesTo(dir),
tap(createMissingPathsOn(dir)),
getMissingFilesFrom(dir),
),
)
const filebot = opts => {
const {
permissions,
pgid,
primary,
puid,
replica,
safeDelete = 10,
snapshot,
} = opts
console.log(`------------------------------`)
console.log(`\n[Starting filebot - ${new Date().toLocaleString()}]:`)
console.log(`Opts: ${JSON.stringify(opts)}`)
try {
// grab previous snapshot
console.log('\n[Loading previous snapshot]:')
const replicaSnapshotFiles = loadSnapshot(snapshot)
// remove any files that were deleted on the replica directory from the primary directory
console.log('\n[Removing deleted files]:')
removeFilesThatDontExistOnFrom({ safeDelete }, replica, primary, replicaSnapshotFiles)
// move any non-symlinked files that exist on the replica directory to the primary directory
// and symlink back to the replica directory
console.log('\n[Syncing unsynced files]:')
const replicaFiles = getFilesAndSerializeFrom(replica)
syncUnsycnedFilesTo(primary, replicaFiles)
// symlink any files that exist on the primary directory to the replica directory
console.log('\n[Symlinking missing files]:')
const primaryFiles = getFilesAndSerializeFrom(primary)
symlinkMissingFilesTo(replica, primaryFiles)
// remove any files that were deleted on the primary directory from the replica directory.
console.log('\n[Removing files that no longer exist]:')
removeFilesThatDontExistOnFrom({ safeDelete }, primary, replica, replicaFiles)
// save latest snapshot
console.log('\n[Saving latest snapshot]:')
saveSnapshot(snapshot, getFilesAndSerializeFrom(replica))
// set replica permissions
console.log('\n[Setting permissions]:')
setPerimissionsFor(replica, { permissions, pgid, puid })
console.log(`\n[Filebot complete - ${new Date().toLocaleString()}]`)
} catch (e) {
const error = new VError(e, `Filebot failed`)
console.log(`\n[Filebot failed - ${new Date().toLocaleString()}]:`)
console.error(VError.fullStack(error))
throw error
} finally {
console.log(`\n------------------------------\n\n`)
}
}
if (require.main === module) {
const opts = commander.program
.requiredOption('--primary <path>', 'Primary path')
.requiredOption('--replica <path>', 'Replica path')
.requiredOption('--snapshot <path>', 'Snapshot path')
.option('--safeDelete <number>', 'Number of items to safely delete')
.option('--permissions <string>', 'Replica permissions')
.option('--puid <string>', 'Replica owner user')
.option('--pgid <string>', 'Replica owner group')
.parse(process.argv)
.opts()
try {
filebot(parseOpts(opts))
process.exit(0)
} catch (e) {
process.exit(1)
}
}
module.exports = filebot