git ssb

0+

Rômulo Alves / sync



Tree: 3f257b5c381aa9abc81ea117537a756a2b877e84

Files: 3f257b5c381aa9abc81ea117537a756a2b877e84 / index.js

1501 bytesRaw
1const utils = require('./utils');
2
3module.exports = function sync(source, target, crypt, decrypt) {
4 return new Promise(async (resolve, reject) => {
5 try {
6
7 // Get stats of source and target
8 const [ statSource, statTarget ] = await Promise.all([ utils.getStats(source), utils.getStats(target) ]);
9
10 // Source does not exists and target exists, delete target
11 if (statSource === null && statTarget !== null) {
12 await utils.delete(target);
13
14 return resolve();
15 }
16
17 // Dost not exists in the target, copy
18 if (statSource !== null && statTarget === null) {
19 await utils.copy(source, target, crypt, decrypt);
20
21 return resolve();
22 }
23
24 if (statSource.isDirectory() && statTarget.isDirectory()) {
25
26 // Verify if both are directories
27 const nextPaths = await utils.readRecursive(source, target);
28
29 for (let index = 0; index < nextPaths.length; index++) {
30 const [ nextSource, nextTarget ] = nextPaths[index];
31
32 // Start to sync next paths
33 await sync(nextSource, nextTarget, crypt, decrypt);
34 }
35 } else if (statSource.isFile() && statTarget.isFile()) {
36
37 // Verify if both are files
38 if (statSource.mtime <= statTarget.mtime) {
39
40 // Source is newer, copy the new version to the target
41 await utils.copy(source, target, crypt, decrypt);
42 }
43 }
44
45 return resolve();
46 } catch (err) {
47 return reject(err);
48 }
49 });
50};

Built with git-ssb-web