-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (60 loc) · 1.66 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
var path = require('path');
var fs = require('fs');
var absolutePath = function (relativePath) {
return path.join(process.cwd(), relativePath);
};
var deleteFolderRecursive = function(path, preserveList) {
if(fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
if (preserveList.indexOf(absolutePath(file)) < 0) {
var curPath = path + '/' + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath, preserveList);
fs.rmdirSync(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
}
});
}
};
function jsonToFs(root, obj) {
Object.keys(obj).forEach(function (path) {
var currPath = root + '/' + path;
if (typeof obj[path] === 'string') {
fs.writeFileSync(currPath, obj[path]);
} else {
fs.mkdirSync(currPath);
jsonToFs(currPath, obj[path]);
}
});
}
function fsToJson(root, ignoreList) {
var obj = {};
fs.readdirSync(root).forEach(function(file){
if (ignoreList.indexOf(absolutePath(file)) < 0) {
var curPath = root + '/' + file;
if(fs.lstatSync(curPath).isDirectory()) {
obj[file] = fsToJson(curPath, ignoreList);
} else {
obj[file] = fs.readFileSync(curPath, 'utf8');
}
}
});
return obj;
}
module.exports = {
jsonToFs: function (root, obj, preserveList) {
preserveList = preserveList || [];
preserveList = preserveList.map(absolutePath);
root = absolutePath(root);
deleteFolderRecursive(root, preserveList);
jsonToFs(root, obj);
},
fsToJson: function (root, ignoreList) {
ignoreList = ignoreList || [];
ignoreList = ignoreList.map(absolutePath);
root = absolutePath(root);
return fsToJson(root, ignoreList);
}
};