-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
165 lines (144 loc) · 3.92 KB
/
background.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
const { ipcRenderer } = require("electron");
const fs = require("file-system");
const fg = require("fast-glob");
const hirestime = require("hirestime");
var lineNumber = require("line-number");
var parse = require("gitignore-globs");
var async = require("async");
var express = require("express");
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var path = require("path");
var IPCStream = require('electron-ipc-stream')
var ipcs = new IPCStream('background-stream')
io.listen(9090);
io.on("connection", client => {
var content;
var flags;
client.on("load-results", path => {
var data = fs.readFileSync(path, "utf8");
var filename = path.replace(/^.*[\\\/]/, "");
var match = lineNumber(data, new RegExp(content, flags));
io.emit("result", {
path: path,
file: filename,
match: match
});
});
client.on("pause", () => {
ipcRenderer.send("console-log", "pause");
globs.pause();
});
client.on("query", args => {
const getElapsed = hirestime();
var searched = 0;
var searchedSize = 0;
var sending = [];
var dirs = args.directory.split(",");
var expression = args.query;
if (/^[A-Za-z]+$/.test(args.query)) {
expression = `*${args.query}*`;
}
content = args.content.content;
flags = args.content.flags;
var ignores = [".git"];
if (args.gitignore) {
ignores = parse(path.join(dirs[0], ".gitignore"));
ignores.push(".git");
}
var options = {
cwd: dirs[0],
absolute: true,
stats: true,
dot: true,
ignore: ignores
};
var q = async.queue(function (file, callback) {
var path = file.path;
var filename = path.replace(/^.*[\\\/]/, "");
var stats = file;
if (!content) {
sending.push({
path: path,
file: filename,
directory: dirs[0],
stats: {
size: stats.size,
atimeMs: stats.atimeMs,
birthtimeMs: stats.birthtimeMs,
mtimeMs: stats.mtimeMs
}
})
callback();
} else {
fs.readFile(path, "utf8", (err, data) => {
if (err) callback();
if (content) {
var match = data.search(new RegExp(content, flags));
if (match > -1) {
//io.emit("match", buf);
sending.push({
path: path,
file: filename,
directory: dirs[0],
stats: {
size: stats.size,
atimeMs: stats.atimeMs,
birthtimeMs: stats.birthtimeMs,
mtimeMs: stats.mtimeMs
}
})
}
}
searched++;
searchedSize += stats.size;
callback();
});
}
}, 200);
// assign a callback
q.drain = function () {
};
const stream = fg.stream(expression, options);
stream.on("data", path => {
q.push(path);
});
stream.once("end", () => {
ipcs.write(sending);
io.emit("stop-loading", getElapsed(hirestime.S));
io.emit("searched-files", {
searched: searched,
searchedSize: searchedSize
});
});
});
});
function splitArrayIntoChunks(arr, chunkLen) {
var chunkList = [];
var chunkCount = Math.ceil(arr.length / chunkLen);
for (var i = 0; i < chunkCount; i++) {
chunkList.push(arr.splice(0, chunkLen));
}
return chunkList;
}
const throttle = (func, limit) => {
let lastFunc
let lastRan
return function () {
const context = this
const args = arguments
if (!lastRan) {
func.apply(context, args)
lastRan = Date.now()
} else {
clearTimeout(lastFunc)
lastFunc = setTimeout(function () {
if ((Date.now() - lastRan) >= limit) {
func.apply(context, args)
lastRan = Date.now()
}
}, limit - (Date.now() - lastRan))
}
}
}