1 | 'use strict'
|
2 |
|
3 | const after = async (files, name) => {
|
4 | const index = await indexOf(files, name)
|
5 |
|
6 | if (index === undefined) {
|
7 | return []
|
8 | }
|
9 |
|
10 | return files.filter((file, i) => i > index)
|
11 | }
|
12 |
|
13 | const before = async (files, name) => {
|
14 | const index = await indexOf(files, name)
|
15 |
|
16 | if (index === undefined) {
|
17 | return []
|
18 | }
|
19 |
|
20 | return files.filter((file, i) => i < index)
|
21 | }
|
22 |
|
23 | const indexOf = async (files, name) => {
|
24 | return files.map((file, i) => [ file.name, i ])
|
25 | .filter(([n, i]) => n === name)
|
26 | .map(([n, i]) => i)
|
27 | .shift()
|
28 | }
|
29 |
|
30 | const synced = async (files) => {
|
31 | return files.filter(file => file.locations.length === 2)
|
32 | }
|
33 |
|
34 | const unsynced = async (files) => {
|
35 | return files.filter(file => file.locations.length === 1)
|
36 | }
|
37 |
|
38 | module.exports = (files) => ({
|
39 | all: () => {
|
40 | return files
|
41 | },
|
42 | after: async (name) => {
|
43 | return after(files, name)
|
44 | },
|
45 | before: async (name) => {
|
46 | return before(files, name)
|
47 | },
|
48 | conflicting: async () => {
|
49 | const unsyncedFiles = await unsynced(files)
|
50 |
|
51 | const withConflicts = await Promise.all(unsyncedFiles.map(async (file) => {
|
52 | const afterFiles = await after(files, file.name)
|
53 | const afterSyncedFiles = await synced(afterFiles)
|
54 | return [file, afterSyncedFiles]
|
55 | }))
|
56 |
|
57 | return withConflicts.filter(([file, conflicts]) => conflicts.length > 0)
|
58 | .map(([file, conflicts]) => file)
|
59 | },
|
60 | synced: async () => {
|
61 | return synced(files)
|
62 | },
|
63 | unsynced: async () => {
|
64 | return unsynced(files)
|
65 | }
|
66 | })
|