-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (100 loc) · 3.61 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
const fse = require('fs-extra');
const path = require('path');
async function sync(config, state = undefined)
{
if(!config.source || !config.destination)
throw "Source and destination must be defined";
let sdir = path.normalize(config.source);
let tdir = path.normalize(config.destination);
fse.accessSync(sdir, fse.constants.R_OK)
fse.accessSync(tdir, fse.constants.R_OK | fse.constants.W_OK)
let toCheck = (await syncLevel(config, sdir, tdir)).filter(e => e.state === 'TO_CHECK');
for(tc of toCheck) {
await sync({...config, source: `${sdir}/${tc.value}`, destination: `${tdir}/${tc.value}`})
}
}
async function syncLevel(config, level_s, level_t)
{
let state = []
let del = true; // delete from target if not in source.
let create = true; // creats in target if in source.
let replace = true; // replaces files in target with files in source.
let sourceFilter = () => true;
if(config.delete_in_target && ["boolean", "function"].includes(typeof(config.delete_in_target)))
del = config.delete_in_target;
if(config.create_in_target && ["boolean", "function"].includes(typeof(config.create_in_target)))
create = config.create_in_target;
if(config.replace_in_target && ["boolean", "function"].includes(typeof(config.replace_in_target)))
replace = config.replace_in_target;
if(config.filter_in_source && typeof(config.filter_in_source) === 'function')
sourceFilter = config.filter_in_source;
let sourceFiles = new Set(fse.readdirSync(level_s).filter(e => sourceFilter(path.normalize(`${level_s}/${e}`))));
let targetFiles = new Set(fse.readdirSync(level_t));
for (const f of targetFiles) {
if(!sourceFiles.has(f))
{
let p = path.normalize(`${level_t}/${f}`);
if(typeof del === 'boolean' ? del : del(p))
{
await fse.remove(p);
state.push({state: 'DELETED', value: p});
}
}
else
{
let pT = path.normalize(`${level_t}/${f}`);
let pS = path.normalize(`${level_s}/${f}`);
const lstatT = await fse.lstat(pT);
const lstatS = await fse.lstat(pS);
// there might be egde case here..
if(lstatT.isDirectory())
{
state.push({state: 'TO_CHECK', value: f});
}
else
{
if(typeof replace === 'boolean' ? replace : replace(pT, pS, lstatT, lstatS))
{
await fse.copy(pS, pT);
state.push({state:"REPLACED", value:pT})
}
}
}
}
for (const f of sourceFiles) {
if(!targetFiles.has(f))
{
let pS = path.normalize(`${level_s}/${f}`);
let statS = await fse.lstat(pS);
let pT = path.normalize(`${level_t}/${f}`);
if(typeof create === 'boolean' ? create : create(pT))
{
if(statS.isDirectory())
{
await fse.ensureDir(pT);
state.push({state: 'TO_CHECK', value: f})
}
else
{
await fse.copy(pS, pT);
state.push({state: 'CREATED', value: pT})
}
}
}
}
return state;
}
if(require.main === module)
{
let file = 'config.js';
if(process.argv.length === 3)
file = process.argv[2];
let cfg = JSON.stringify(fse.readFileSync(file));
sync(cfg).catch(console.error)
}
else
{
module.exports = {
sync
}
}