-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileOrganizer.js
206 lines (167 loc) · 5.29 KB
/
FileOrganizer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var exec = require('child_process').exec;
var Promise = require("es6-promise").Promise;
var fs = require('fs');
var FileOrganizer = function(){
var storagePath = "/srv/LAHelper/logs/";
var MARKERS_FILENAME = ".markers.json";
// Files is array of JSON objects
var store = function(files,userId){
_ensureUserFolderCreated(userId);
_processFilesAndFolders(files,userId);
_performCommit(userId);
};
var _ensureUserFolderCreated = function(userId){
if(!fs.existsSync(storagePath+userId)){
createFileStorage(userId);
}
};
var _processFilesAndFolders = function(files,clientId){
files.map(function(file){
//file = JSON.parse(file);
if(file.type === 'folder'){
_processFolder(file,clientId);
}else if( file.type === 'file' ){
_processFile(file,clientId);
}
});
};
var _processFolder = function(folder,clientId){
if(folder.typeOfChange==="removed"){
_deleteFolder(folder,clientId);
return;
}
if(folder.typeOfChange==="added" || folder.typeOfChange ==="content"){
_getAndCreatePath(folder,clientId);
}
};
var _deleteFolder = function(folder,clientId){
var path = _getAndCreatePath(folder,clientId);
var result = "FAILURE";
if(fs.existsSync(path)){
//fs.rmdirSync(path);
retuslt = "SUCCESS";
}
console.log(" "+result+": deleting: ",path);
};
var _processFile = function(file,clientId){
if(file.typeOfChange==="removed"){
_deleteFile(file,clientId);
return;
}
if(file.typeOfChange==="added" || file.typeOfChange ==="content"){
var folder = _getFolderName(file);
saveFile(file,clientId);
}
};
var _deleteFile = function(file,clientId){
var path = _getAndCreatePath(file,clientId);
var filePath = path+'/'+file.name;
var result = "FAILURE";
if(fs.existsSync(filePath)){
result = "SUCCESS";
fs.unlinkSync(filePath);
}
console.log(" "+result+": deleting: ",filePath);
};
var _getAndCreatePath = function(file,clientId){
var allowedSymbols = /[^A-z0-9\.\_\t//]/g;
//Replace unwanted characters with _
var sanitizedPath = file.path.replace(allowedSymbols,"_");
var segments = sanitizedPath.split("/");
//Remove filename from path if file
if(file.type === 'file'){
segments.pop();
}
//console.log(" Trying to create path",file.path,sanitizedPath);
_createFolderTree(segments,clientId);
var path = segments.join("/");
//console.log(" Returned path: ",storagePath+clientId+path);
return storagePath+clientId+path;
};
var _createFolderTree = function(segments,clientId){
var currentPath = storagePath+clientId;
segments.map(function(segment){
if(segment === ""){return;}
//Add next segment to pah each iteration
currentPath = currentPath+"/"+segment;
if(!fs.existsSync(currentPath)){
console.log(" Created folder",currentPath);
fs.mkdirSync(currentPath);
}
});
};
var _performCommit= function(clientId){
saveState(clientId);
};
var _containsMarkersFile = function(files){
var containsMarkers = false;
files.map(function(file){
if(file.name == MARKERS_FILENAME){
containsMarkers = true;
}
});
return containsMarkers;
};
var _getFolderName = function(file){
var packageRegex = /^\t*package ([A-z]+) +;/;
var packageName = file.fileContents.match(packageRegex);
if(packageName !== null && packageName.length >0 && packageName[0] !== ""){
return packageName[0];
}
//Get name from path
var parts = file.path.split("/");
var parentFolder = parts[parts.length-2];
return parentFolder;
};
var saveFile = function(file,userid){
var directoryPath = _getAndCreatePath(file,userid);
//TODO: Check directory exists
//
var filePath = directoryPath+"/"+file.name;
// Use regex to check filename.
fs.writeFile(filePath, file.fileContents, function(err) {
if(err) {
console.log(" ERROR: file not saved: ",filePath,err);
} else {
console.log(" File saved: ",filePath);
}
});
};
var createFileStorage = function(clientId){
var directory = storagePath+clientId;
fs.mkdirSync(directory);
child = exec("cd "+directory+"; git init; cd "+storagePath,
function (error, stdout, stderr) {
console.log(" Initializing directory ("+clientId,') - stdout: ' + stdout.replace(/\n/g," / "),' - stderr: ' + stderr.replace(/\n/g," / "));
if (error !== null) {
console.log(' exec error: ' + error);
}
});
};
var saveState = function(clientId){
var directory = storagePath+clientId;
child = exec("cd "+directory+"; git add --all .; git commit -m \" Auto commit: "+Date.now()+"\";cd "+directory,
function (error, stdout, stderr) {
console.log(" Commit ("+clientId.substring(0,7),') - stdout: ' + stdout.replace(/\n/g," / "),' - stderr: ' + stderr.replace(/\n/g," / "));
if (error !== null) {
console.log(' exec error: ' + error);
}
});
};
var getGitFilesListOfClient = function(clientId){
return new Promise(function(resolve,reject){
var options = {cwd:storagePath+clientId+"/"};
child = exec("git ls-files",options,
function (error, stdout, stderr) {
console.log(" Ran git ls-files :",options,error,stdout,stderr);
resolve(stdout);
});
});
};
return{
store:store,
getGitFilesListOfClient:getGitFilesListOfClient,
createFileStorage:createFileStorage
};
};
module.exports = FileOrganizer;