-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (83 loc) · 2.46 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
const _ = require("lodash");
const wrench = require("wrench");
const fs = require('fs-extra')
let filesTree, mainFolderPath;
module.exports = (_filesTree, source, destination) => {
filesTree = _filesTree;
mainFolderPath = source;
try{
copyFolder("", destination);
return;
}
catch(error){
return error;
}
}
/**
* @param {string} treePath
* @param {string} destination
*/
const copyFolder = (treePath, destination) => {
const folder = treePath ? _.get(filesTree, treePath): filesTree;
// If main dir is fully chosen
if(folder.fullyChosen)
fullFolderCopy(joinPath(mainFolderPath, treePath.replace('.', '/')), destination);
else{
copyFiles(treePath, destination);
// In case this is the main branch
const currentBranch = treePath ? _.get(filesTree, treePath) : filesTree;
_.forEach(currentBranch, (value, folderName) => {
if(folderName !== 'files'){
const innerTreePath = treePath ? treePath+'.'+folderName : folderName;
// In case on unexisted parent folder mkdir parent
fs.ensureDirSync(destination)
copyFolder(innerTreePath, joinPath(destination, folderName));
}
});
}
};
/**
* Copy all tree files
* @param {string} treePath
* @param {string} destination
*/
const copyFiles= (treePath, destination) => {
const filesPaths = getFilesFullPaths(treePath);
_.forEach(filesPaths, (filePath) => {
const fileName = _.last(filePath.split('/'));
const fullDestination = joinPath(destination, fileName);
fs.copySync(filePath, fullDestination);
});
};
// Copy hole folder into destenation
const fullFolderCopy = (fullFolderPath, folderDestination) =>{
wrench.copyDirSyncRecursive(fullFolderPath, folderDestination, {
forceDelete: false
});
};
/**
* Return all existed files in treePath with thier full path
* @param {string} treePath
*/
const getFilesFullPaths = (treePath) => {
const path = treePath ? treePath + '.files' : 'files';
const files = _.get(filesTree, path);
return _.map(files, (file) => {
// Convert to files path format
treePath = treePath.replace('.', '/');
return joinPath(mainFolderPath, treePath, file.name);
});
};
/**
* Combine path with seperator
* @param {Path string} path
* @param {Array} subPaths
*/
const joinPath = (path, ...subPaths) => {
_.forEach(subPaths,(subPath, index) => {
if(path[path.length-1] !== '/')
path += '/';
path += subPath;
});
return path;
};