-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
341 lines (305 loc) · 11.8 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const fs = require('fs');
const self = module.exports = {
/**
* Will read a file into an array of lines
*
* @param filePath -> the path to the file
*/
readFile: function (filePath) {
return new Promise(function (resolve, reject) {
let lineReader = require('readline').createInterface({
input: fs.createReadStream(filePath)
});
let lines = [];
lineReader.on('line', function (line) {
lines.push(line)
});
lineReader.on('close', function () {
resolve(lines);
});
}.bind())
},
/**
* Will append text to a file
*
* @param filePath -> the path to the file
* @param text -> the text to append
*/
appendToFile: function (filePath, text) {
return new Promise(function (resolve, reject) {
let logStream = fs.createWriteStream(filePath, {flags: 'a'});
logStream.write(text);
logStream.on('close', function() {
resolve()
});
logStream.end();
})
},
/**
* Will read a file with encoding to a string
*
* @param filePath -> the path to the file
* @param encoding -> the desired encoding
*/
readFileWithEncoding: function (filePath, encoding = 'utf8') {
return fs.readFileSync(filePath, encoding);
},
/**
* Will read a file to json
*/
fileToJson: async function (filePath) {
const fileStr = await self.readFileWithEncoding(filePath);
return JSON.parse(fileStr);
},
/**
* Will join the paths of dirs
*/
joinPath: function (...paths) {
const path = require('path');
return path.join(...paths)
},
/**
* Will return the root path of a project
*/
getProjectRoot: function(process) {
return process.mainModule.path
},
/**
* Will save text or, a bunch of lines in an array, into a file
*
* @param text -> optional text to save
* @param filePath -> the path to the file
*/
writeFileSync: function (text = null, filePath) {
const fh = require('os-file-handler');
const parentDir = fh.getParentDirPath(filePath);
fh.createDir(parentDir);
fs.writeFileSync(filePath, text);
},
/**
* Will save a json into a file
*
* @param jsonObj
* @param filePath
* @returns {Promise<void>}
* @constructor
*/
JSONObjectToFile: async function (jsonObj, filePath) {
// stringify JSON Object
const jsonContent = JSON.stringify(jsonObj);
fs.writeFile(filePath, jsonContent, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
});
},
/**
* Will read a range of lines from a file.
* Will start from lineToStart and stop at the first occurrence of lineToStop, as long as lineToStart found.
*
* @param filePath -> the path to the file
* @param lineToStart -> the line to start the reading
* @param lineToStop -> the line to stop the reading (we will start to look for this line only after
* the lineToStart has been found)
* @param includeFirstLine -> set to true to include the first line in the reading
* @param includeLastLine -> set to true to include the last line in the reading
* @param isLineIdentical -> true if the line should hold the exact match. False to if the line contains
* @param caseSensitive -> case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)
*/
readFileInRange: function (filePath,
lineToStart,
lineToStop,
includeFirstLine = true,
includeLastLine = true,
isLineIdentical = true,
caseSensitive = true) {
return new Promise(function (resolve, reject) {
let caseSensitiveFunc = returnSelf;
if (!caseSensitive) {
caseSensitiveFunc = textToLowerCase;
lineToStart = caseSensitiveFunc(lineToStart);
lineToStop = caseSensitiveFunc(lineToStop)
}
let compareFunc = isContains;
if (isLineIdentical) {
compareFunc = isMatch
}
let lineReader = require('readline').createInterface({
input: fs.createReadStream(filePath)
});
let startFound = false;
let lines = [];
let closed = false;
lineReader.on('line', function (line) {
let cLine = caseSensitiveFunc(line);
if (!startFound) {
if (compareFunc(cLine, lineToStart)) {
startFound = true;
if (includeFirstLine) {
lines.push(line)
}
}
} else {
if (compareFunc(cLine, lineToStop)) {
if (includeLastLine) {
lines.push(line)
}
resolve(lines);
closeLineReader(lineReader);
closed = true;
} else {
lines.push(line)
}
}
});
lineReader.on('close', function () {
if (!closed) {
closeLineReader(lineReader);
resolve(undefined)
}
});
}.bind())
},
/**
* Will check if a file contains text. If it does, return true else, false.
*
* @param filePath -> the path to the file
* @param text -> the text which you'd like to look for
* @param isLineIdentical -> true if the line should hold the exact match. False to if the line contains
* @param caseSensitive -> case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)
*/
isFileContainsText: function (filePath, text, isLineIdentical = true, caseSensitive = true) {
return new Promise(function (resolve, reject) {
let caseSensitiveFunc = returnSelf;
if (!caseSensitive) {
caseSensitiveFunc = textToLowerCase;
text = caseSensitiveFunc(text)
}
let compareFunc = isContains;
if (isLineIdentical) {
compareFunc = isMatch
}
let lineReader = require('readline').createInterface({
input: fs.createReadStream(filePath)
});
lineReader.on('line', function (line) {
line = caseSensitiveFunc(line);
if (compareFunc(line, text)) {
resolve(true);
closeLineReader(lineReader)
}
});
lineReader.on('close', function () {
resolve(false)
});
}.bind())
},
/**
* Will return the file paths contains a text from a bunch of files.
*
* @param filePaths -> an array carrying the list of files to look upon
* @param text -> the text which you'd like to look for
* @param isLineIdentical -> true if the line should hold the exact match. False to if the line contains
* @param caseSensitive -> case sensitive toggler (if you set isLineIdentical to true, it of course, doesn't matter)
* @return Promise -> a promise carrying an array to all of the files found (an empty array if nothing's found)
*/
getFilesContainsText: function (filePaths = [], text, isLineIdentical = true, caseSensitive = true) {
return new Promise(async function (resolve, reject) {
let filesFound = [];
for (let i = 0; i < filePaths.length; i++) {
let found = await self.isFileContainsText(filePaths[i], text, isLineIdentical, caseSensitive);
if (found) {
filesFound.push(filePaths[i])
}
}
resolve(filesFound)
}.bind())
},
/**
* Will look for expression in a file (or in array of lines) and return all of the occurrences
* in which the expression exists.
*
* For example, if you have a file contains:
*
* package in.remotify.www.hathwayremote;
* import android.annotation.SuppressLint;
* import android.content.Context;
* import android.content.SharedPreferences.Editor;
* import android.hardware.ConsumerIrManager;
* import android.hardware.Gyroscope;
* import android.os.Bundle;
*
* and you want to scrape all of the lines which has the expression 'hardware', using this
* function will return the array:
* ['import android.hardware.ConsumerIrManager;', 'import android.hardware.Gyroscope;']
*
*
* @param filePath -> optional file path instead of just an array of lines
* @param lines -> optional strings in an array instead of a file
* @param lineToFocusOn -> the line which includes the string you want to scrape
* @param strContainsStart -> a string marking the start of the expression to return
* @param includeAllOfStart -> true to include all of the start expression in the selection,
* false to exclude it and return only the expression after it
* @param strContainsEnd -> a string marking the end of the expression to return
* @param includeAllOfEnd -> true to include all of the end expression in the selection,
* false to exclude it and return only the expression after it
*/
findExpression: function (filePath,
lines = [],
lineToFocusOn,
strContainsStart,
includeAllOfStart = false,
strContainsEnd = null,
includeAllOfEnd = false,) {
return new Promise(async function (resolve, reject) {
let txtLines = lines;
if (lines.length === 0) {
txtLines = self.readFile(filePath)
}
let matches = [];
// run on all of the lines in a java files
for (let j = 0; j < txtLines.length; j++) {
let line = txtLines[j];
if (line.includes(strContainsStart)) {
let startIdx = line.indexOf(strContainsStart);
if (!includeAllOfStart) {
startIdx += strContainsStart.length
}
let endIdx = line.indexOf(strContainsEnd, startIdx + 1);
if (includeAllOfEnd) {
endIdx += strContainsEnd.length
}
let ans = "";
if (strContainsEnd !== null) {
ans = line.substring(startIdx, endIdx);
matches.push(ans);
} else {
ans = line.substring(startIdx, line.length);
matches.push(ans)
}
}
}
resolve(matches)
})
},
};
// will kill the line reader
function closeLineReader(lineReader) {
lineReader.removeAllListeners();
lineReader.close()
}
// assign functions to save compute time in each line running
function isContains(text1, text2) {
return text1.includes(text2)
}
function isMatch(text1, text2) {
return text1 === text2
}
function returnSelf(text) {
return text
}
function textToLowerCase(text) {
return text.toLowerCase()
}