-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfile.js
177 lines (136 loc) · 3.69 KB
/
file.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
/*
Library to work with files and directories
*/
/* global Java */
(function() {
var File = Java.type('java.io.File'),
FileReader = Java.type('java.io.FileReader');
var gRealFiles = [];
// returns the content as an array of lines
function _getContent(file) {
return function() {
var fr = new FileReader(file),
crs = [/*10, */13],
char = fr.read(),
content = [],
lineContent = '',
lines = 0;
while (char !== -1) {
if (crs.indexOf(char) > -1) {
content.push(lineContent);
lineContent = '';
} else {
lineContent = lineContent + String.fromCharCode(char);
}
char = fr.read();
}
content.push(lineContent);
fr.close();
return content;
}
};
// returns a file as an object
function _realFile(file) {
var fileParts = file.split(config.fileSeparator),
fileNameWithExtension = fileParts.pop(),
fileName = fileNameWithExtension.split("."),
extension = fileName.pop(),
path = fileParts.join(config.fileSeparator);
return {
fullPath: file.trim(),
path: path,
name: fileName.toString(),
extension:extension,
fileNameWithExtension: fileNameWithExtension,
getContent: _getContent(file),
fileParts: fileParts
};
};
function getDir(dir) {
return new File(dir);
};
function dirExists(dir) {
return getDir(dir).exists();
};
// list all directories in a directory
function listDirs(dir) {
var currDir = getDir(dir),
fileList = Java.from(currDir.listFiles()),
dirs = [];
// Get directories
fileList.forEach(function(file) {
currFile = new File(file);
if (currFile.isDirectory()) {
dirs.push(currFile.getName());
};
});
return dirs;
};
function filesInDir(options) {
// default settings
var settings = {
dir: "",
subDirectories: false,
recursive: false
};
// extend settings with options
for (obj in settings) {
if (options.hasOwnProperty(obj)) {
settings[obj] = options[obj];
}
}
if(!settings.recursive) {
// Reset files
gRealFiles = [];
}
// We want all real files from dir and subdirs
var dir = getDir(settings.dir),
fileList = Java.from(dir.listFiles()),
currFile;
if (!dir.exists()) {
print("The directory does not exist:", settings.dir);
return false;
}
// Get files
fileList.forEach(function(file) {
currFile = new File(file);
// Get files in subdirectories
if (currFile.isDirectory() && options.subDirectories) {
var filesInSubDir = filesInDir({
dir: currFile.getAbsolutePath(),
subDirectories: settings.subDirectories,
recursive: true
});
} else if (currFile.isFile()) {
gRealFiles.push(_realFile(file.toString()));
};
});
return gRealFiles;
};
function appendText(file, text){
var FileWriter = java.io.FileWriter;
var fw = new FileWriter(file, true);
fw.write(java.lang.System.getProperty( "line.separator" ));
fw.write(text);
fw.close();
};
function createFile(file, text){
var FileWriter = java.io.FileWriter;
var fw = new FileWriter(file, false);
//fw.write(java.lang.System.getProperty( "line.separator" ));
fw.write(text);
fw.close();
};
function getFileContent(file) {
return _getContent(file)();
};
return {
getDir: getDir,
dirExists: dirExists,
listDirs: listDirs,
filesInDir: filesInDir,
appendText: appendText,
createFile: createFile,
getFileContent: getFileContent
};
})();