@lvce-editor/search-process 4.2.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/searchProcess.js +1 -1
- package/dist/index.js +495 -0
- package/package.json +12 -54
- package/src/parts/Assert/Assert.js +0 -1
- package/src/parts/Character/Character.js +0 -2
- package/src/parts/CollectTextSearchStdout/CollectTextSearchStdout.js +0 -103
- package/src/parts/CommandMap/CommandMap.js +0 -11
- package/src/parts/EncodingType/EncodingType.js +0 -2
- package/src/parts/Env/Env.js +0 -3
- package/src/parts/ErrorCodes/ErrorCodes.js +0 -2
- package/src/parts/Exec/Exec.js +0 -20
- package/src/parts/GetFirstEvent/GetFirstEvent.js +0 -23
- package/src/parts/GetNewLineIndex/GetNewLineIndex.js +0 -11
- package/src/parts/HandleElectronMessagePort/HandleElectronMessagePort.js +0 -11
- package/src/parts/HandleWebSocket/HandleWebSocket.js +0 -13
- package/src/parts/IpcChild/IpcChild.js +0 -8
- package/src/parts/IpcChildModule/IpcChildModule.js +0 -25
- package/src/parts/IpcChildType/IpcChildType.js +0 -19
- package/src/parts/IsEnoentError/IsEnoentError.js +0 -13
- package/src/parts/IsEnoentErrorWindows/IsEnoentErrorWindows.js +0 -3
- package/src/parts/LimitString/LimitString.js +0 -17
- package/src/parts/Listen/Listen.js +0 -10
- package/src/parts/Logger/Logger.js +0 -38
- package/src/parts/Main/Main.js +0 -5
- package/src/parts/ParseRipGrepLines/ParseRipGrepLines.js +0 -11
- package/src/parts/ProcessExitEventType/ProcessExitEventType.js +0 -2
- package/src/parts/Promises/Promises.js +0 -19
- package/src/parts/RipGrep/RipGrep.js +0 -40
- package/src/parts/RipGrepParsedLineType/RipGrepParsedLineType.js +0 -3
- package/src/parts/RipGrepPath/RipGrepPath.js +0 -1
- package/src/parts/SearchFile/SearchFile.js +0 -35
- package/src/parts/TextSearch/TextSearch.js +0 -31
- package/src/parts/TextSearchError/TextSearchError.js +0 -15
- package/src/parts/TextSearchResultType/TextSearchResultType.js +0 -2
- package/src/parts/ToTextSearchResult/ToTextSearchResult.js +0 -25
- package/src/parts/VError/VError.js +0 -1
- package/src/parts/WaitForProcessToExit/WaitForProcessToExit.js +0 -9
- package/src/searchProcessMain.js +0 -3
package/bin/searchProcess.js
CHANGED
package/dist/index.js
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { object, string, array, number } from '@lvce-editor/assert';
|
|
2
|
+
import { NodeWebSocketRpcClient, ElectronMessagePortRpcClient, ElectronUtilityProcessRpcClient, NodeWorkerRpcClient, NodeForkedProcessRpcClient } from '@lvce-editor/rpc';
|
|
3
|
+
import { Console } from 'node:console';
|
|
4
|
+
import { createWriteStream } from 'node:fs';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import * as NodeChildProcess from 'node:child_process';
|
|
7
|
+
import { execa } from 'execa';
|
|
8
|
+
import { rgPath } from '@lvce-editor/ripgrep';
|
|
9
|
+
import { VError } from '@lvce-editor/verror';
|
|
10
|
+
import { Writable } from 'node:stream';
|
|
11
|
+
import { pipeline } from 'node:stream/promises';
|
|
12
|
+
|
|
13
|
+
const NodeWorker = 1;
|
|
14
|
+
const NodeForkedProcess = 2;
|
|
15
|
+
const ElectronUtilityProcess = 3;
|
|
16
|
+
const ElectronMessagePort = 4;
|
|
17
|
+
const WebSocket = 6;
|
|
18
|
+
const Auto = () => {
|
|
19
|
+
const {
|
|
20
|
+
argv
|
|
21
|
+
} = process;
|
|
22
|
+
if (argv.includes('--ipc-type=node-worker')) {
|
|
23
|
+
return NodeWorker;
|
|
24
|
+
}
|
|
25
|
+
if (argv.includes('--ipc-type=node-forked-process')) {
|
|
26
|
+
return NodeForkedProcess;
|
|
27
|
+
}
|
|
28
|
+
if (argv.includes('--ipc-type=electron-utility-process')) {
|
|
29
|
+
return ElectronUtilityProcess;
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`[shared-process] unknown ipc type`);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const getModule = method => {
|
|
35
|
+
switch (method) {
|
|
36
|
+
case NodeForkedProcess:
|
|
37
|
+
return NodeForkedProcessRpcClient;
|
|
38
|
+
case NodeWorker:
|
|
39
|
+
return NodeWorkerRpcClient;
|
|
40
|
+
case ElectronUtilityProcess:
|
|
41
|
+
return ElectronUtilityProcessRpcClient;
|
|
42
|
+
case ElectronMessagePort:
|
|
43
|
+
return ElectronMessagePortRpcClient;
|
|
44
|
+
case WebSocket:
|
|
45
|
+
return NodeWebSocketRpcClient;
|
|
46
|
+
default:
|
|
47
|
+
throw new Error('unexpected ipc type');
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const listen$1 = async ({
|
|
52
|
+
method,
|
|
53
|
+
...params
|
|
54
|
+
}) => {
|
|
55
|
+
const module = getModule(method);
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
const rpc = await module.create(params);
|
|
58
|
+
return rpc;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const handleElectronMessagePort = async (messagePort, ...params) => {
|
|
62
|
+
object(messagePort);
|
|
63
|
+
await listen$1({
|
|
64
|
+
method: ElectronMessagePort,
|
|
65
|
+
messagePort
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const handleWebSocket = async (handle, request) => {
|
|
70
|
+
object(handle);
|
|
71
|
+
object(request);
|
|
72
|
+
await listen$1({
|
|
73
|
+
method: WebSocket,
|
|
74
|
+
request,
|
|
75
|
+
handle
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const EmptyString = '';
|
|
80
|
+
const NewLine = '\n';
|
|
81
|
+
|
|
82
|
+
const ENOENT = 'ENOENT';
|
|
83
|
+
const E_RIP_GREP_NOT_FOUND = 'E_RIP_GREP_NOT_FOUND';
|
|
84
|
+
|
|
85
|
+
const isEnoentErrorWindows = error => {
|
|
86
|
+
return error && error.message && error.message.includes('The system cannot find the path specified.');
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const isEnoentErrorLinux = error => {
|
|
90
|
+
return error.code === ENOENT;
|
|
91
|
+
};
|
|
92
|
+
const isEnoentError = error => {
|
|
93
|
+
if (!error) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return isEnoentErrorLinux(error) || isEnoentErrorWindows(error);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
100
|
+
return string.indexOf(NewLine, startIndex);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const limitString = (string, limit) => {
|
|
104
|
+
let lines = 0;
|
|
105
|
+
let i = 0;
|
|
106
|
+
while (i !== -1 && lines < limit) {
|
|
107
|
+
i = getNewLineIndex(string, i + 1);
|
|
108
|
+
if (i === -1) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
lines++;
|
|
112
|
+
}
|
|
113
|
+
if (i === -1) {
|
|
114
|
+
return string;
|
|
115
|
+
}
|
|
116
|
+
return string.slice(0, i);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// TODO mock this module when used in unit tests
|
|
120
|
+
|
|
121
|
+
const state = {
|
|
122
|
+
console: undefined
|
|
123
|
+
};
|
|
124
|
+
const createConsole = () => {
|
|
125
|
+
const logFile = `${tmpdir()}/log-shared-process.txt`;
|
|
126
|
+
const writeStream = createWriteStream(logFile);
|
|
127
|
+
const logger = new Console(writeStream);
|
|
128
|
+
return logger;
|
|
129
|
+
};
|
|
130
|
+
const getOrCreateLogger = () => {
|
|
131
|
+
if (!state.console) {
|
|
132
|
+
state.console = createConsole();
|
|
133
|
+
}
|
|
134
|
+
return state.console;
|
|
135
|
+
};
|
|
136
|
+
const info = (...args) => {
|
|
137
|
+
const logger = getOrCreateLogger();
|
|
138
|
+
logger.info(...args);
|
|
139
|
+
console.info(...args);
|
|
140
|
+
};
|
|
141
|
+
const error = (...args) => {
|
|
142
|
+
const logger = getOrCreateLogger();
|
|
143
|
+
logger.error(...args);
|
|
144
|
+
console.error(...args);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const getRipGrepPath = () => {
|
|
148
|
+
return process.env.RIP_GREP_PATH;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* @param {string} command
|
|
154
|
+
* @param {string[]} args
|
|
155
|
+
* @param {import('execa').Options} options
|
|
156
|
+
* @returns
|
|
157
|
+
*/
|
|
158
|
+
const exec$1 = async (command, args, options) => {
|
|
159
|
+
string(command);
|
|
160
|
+
array(args);
|
|
161
|
+
object(options);
|
|
162
|
+
const {
|
|
163
|
+
stdout,
|
|
164
|
+
stderr
|
|
165
|
+
} = await execa(command, args, options);
|
|
166
|
+
return {
|
|
167
|
+
stdout,
|
|
168
|
+
stderr
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const ripGrepPath = getRipGrepPath() || rgPath;
|
|
173
|
+
const spawn = (args, options) => {
|
|
174
|
+
try {
|
|
175
|
+
const childProcess = NodeChildProcess.spawn(rgPath, args, options);
|
|
176
|
+
return {
|
|
177
|
+
childProcess,
|
|
178
|
+
on(event, listener) {
|
|
179
|
+
this.childProcess.on(event, listener);
|
|
180
|
+
},
|
|
181
|
+
off(event, listener) {
|
|
182
|
+
this.childProcess.off(event, listener);
|
|
183
|
+
},
|
|
184
|
+
once(event, listener) {
|
|
185
|
+
this.childProcess.once(event, listener);
|
|
186
|
+
},
|
|
187
|
+
stdout: childProcess.stdout,
|
|
188
|
+
stderr: childProcess.stderr,
|
|
189
|
+
kill() {
|
|
190
|
+
this.childProcess.kill();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw new VError(error, `Failed to spawn ripgrep`);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const exec = async (args, options) => {
|
|
198
|
+
array(args);
|
|
199
|
+
object(options);
|
|
200
|
+
const {
|
|
201
|
+
stdout,
|
|
202
|
+
stderr
|
|
203
|
+
} = await exec$1(ripGrepPath, args, options);
|
|
204
|
+
return {
|
|
205
|
+
stdout,
|
|
206
|
+
stderr
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// TODO don't necessarily need ripgrep to list all the files,
|
|
211
|
+
// maybe also a faster c program can do it
|
|
212
|
+
// another option would be to cache the results in renderer-worker and
|
|
213
|
+
// do a delta comparison, so the first time it would send 100kB
|
|
214
|
+
// but the second time only a few hundred bytes of changes
|
|
215
|
+
|
|
216
|
+
const searchFile = async ({
|
|
217
|
+
searchPath = '',
|
|
218
|
+
limit = 100,
|
|
219
|
+
ripGrepArgs = []
|
|
220
|
+
}) => {
|
|
221
|
+
try {
|
|
222
|
+
string(searchPath);
|
|
223
|
+
array(ripGrepArgs);
|
|
224
|
+
number(limit);
|
|
225
|
+
const {
|
|
226
|
+
stdout,
|
|
227
|
+
stderr
|
|
228
|
+
} = await exec(ripGrepArgs, {
|
|
229
|
+
cwd: searchPath
|
|
230
|
+
});
|
|
231
|
+
return limitString(stdout, limit);
|
|
232
|
+
} catch (error$1) {
|
|
233
|
+
if (isEnoentError(error$1)) {
|
|
234
|
+
info(`[shared-process] ripgrep could not be found at "${ripGrepPath}"`);
|
|
235
|
+
return EmptyString;
|
|
236
|
+
}
|
|
237
|
+
// @ts-ignore
|
|
238
|
+
if (error$1 && error$1.stderr === EmptyString) {
|
|
239
|
+
return EmptyString;
|
|
240
|
+
}
|
|
241
|
+
error(error$1);
|
|
242
|
+
return EmptyString;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const Utf8 = 'utf8';
|
|
247
|
+
const Base64 = 'base64';
|
|
248
|
+
|
|
249
|
+
const Begin = 'begin';
|
|
250
|
+
const Match$1 = 'match';
|
|
251
|
+
const Summary = 'summary';
|
|
252
|
+
|
|
253
|
+
const File = 1;
|
|
254
|
+
const Match = 2;
|
|
255
|
+
|
|
256
|
+
const parseRipGrepLines = parsedLineData => {
|
|
257
|
+
if (parsedLineData.lines.text) {
|
|
258
|
+
return parsedLineData.lines.text;
|
|
259
|
+
}
|
|
260
|
+
if (parsedLineData.lines.bytes) {
|
|
261
|
+
return Buffer.from(parsedLineData.lines.bytes, Base64).toString();
|
|
262
|
+
}
|
|
263
|
+
throw new Error(`unable to parse line data`);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
|
|
267
|
+
const results = [];
|
|
268
|
+
const parsedLineData = parsedLine.data;
|
|
269
|
+
const lines = parseRipGrepLines(parsedLineData);
|
|
270
|
+
const lineNumber = parsedLineData.line_number;
|
|
271
|
+
const {
|
|
272
|
+
submatches
|
|
273
|
+
} = parsedLineData;
|
|
274
|
+
const linesLength = lines.length;
|
|
275
|
+
for (const submatch of submatches) {
|
|
276
|
+
const previewStart = Math.max(submatch.start - charsBefore, 0);
|
|
277
|
+
const actualStart = previewStart;
|
|
278
|
+
const previewEnd = Math.min(submatch.end + charsAfter, linesLength);
|
|
279
|
+
const previewText = lines.slice(actualStart, previewEnd);
|
|
280
|
+
results.push({
|
|
281
|
+
type: Match,
|
|
282
|
+
start: submatch.start - actualStart,
|
|
283
|
+
end: submatch.end - actualStart,
|
|
284
|
+
lineNumber,
|
|
285
|
+
text: previewText
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return results;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// TODO update vscode-ripgrep when https://github.com/mhinz/vim-grepper/issues/244, https://github.com/BurntSushi/ripgrep/issues/1892 is fixed
|
|
292
|
+
|
|
293
|
+
// need to use '.' as last argument for ripgrep
|
|
294
|
+
// issue 1 https://github.com/nvim-telescope/telescope.nvim/pull/908/files
|
|
295
|
+
// issue 2 https://github.com/BurntSushi/ripgrep/issues/1892
|
|
296
|
+
// remove workaround when ripgrep is fixed
|
|
297
|
+
|
|
298
|
+
// TODO stats flag might not be necessary
|
|
299
|
+
// TODO update client
|
|
300
|
+
// TODO not always run nice, maybe configure nice via flag/options
|
|
301
|
+
|
|
302
|
+
const collectStdout = async (childProcess, maxSearchResults, charsBefore, charsAfter) => {
|
|
303
|
+
const allSearchResults = Object.create(null);
|
|
304
|
+
let buffer = '';
|
|
305
|
+
let stats = {};
|
|
306
|
+
let limitHit = false;
|
|
307
|
+
let numberOfResults = 0;
|
|
308
|
+
childProcess.stdout.setEncoding(Utf8);
|
|
309
|
+
const handleLine = line => {
|
|
310
|
+
const parsedLine = JSON.parse(line);
|
|
311
|
+
const {
|
|
312
|
+
type,
|
|
313
|
+
data
|
|
314
|
+
} = parsedLine;
|
|
315
|
+
switch (type) {
|
|
316
|
+
case Begin:
|
|
317
|
+
allSearchResults[data.path.text] = [{
|
|
318
|
+
type: File,
|
|
319
|
+
start: 0,
|
|
320
|
+
end: 0,
|
|
321
|
+
lineNumber: 0,
|
|
322
|
+
text: data.path.text
|
|
323
|
+
}];
|
|
324
|
+
break;
|
|
325
|
+
case Match$1:
|
|
326
|
+
const remaining = maxSearchResults - numberOfResults;
|
|
327
|
+
const matches = toTextSearchResult(parsedLine, remaining, charsBefore, charsAfter);
|
|
328
|
+
numberOfResults += matches.length;
|
|
329
|
+
allSearchResults[data.path.text].push(...matches);
|
|
330
|
+
break;
|
|
331
|
+
case Summary:
|
|
332
|
+
stats = data;
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const handleData = chunk => {
|
|
337
|
+
let newLineIndex = getNewLineIndex(chunk);
|
|
338
|
+
const dataString = buffer + chunk;
|
|
339
|
+
if (newLineIndex === -1) {
|
|
340
|
+
buffer = dataString;
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
newLineIndex += buffer.length;
|
|
344
|
+
let previousIndex = 0;
|
|
345
|
+
while (newLineIndex >= 0) {
|
|
346
|
+
const line = dataString.slice(previousIndex, newLineIndex);
|
|
347
|
+
handleLine(line);
|
|
348
|
+
previousIndex = newLineIndex + 1;
|
|
349
|
+
newLineIndex = getNewLineIndex(dataString, previousIndex);
|
|
350
|
+
}
|
|
351
|
+
buffer = dataString.slice(previousIndex);
|
|
352
|
+
if (numberOfResults > maxSearchResults) {
|
|
353
|
+
limitHit = true;
|
|
354
|
+
childProcess.kill();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
await pipeline(childProcess.stdout, new Writable({
|
|
358
|
+
decodeStrings: false,
|
|
359
|
+
construct(callback) {
|
|
360
|
+
callback();
|
|
361
|
+
},
|
|
362
|
+
write(chunk, encoding, callback) {
|
|
363
|
+
try {
|
|
364
|
+
handleData(chunk);
|
|
365
|
+
callback();
|
|
366
|
+
} catch (error) {
|
|
367
|
+
callback(error);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}));
|
|
371
|
+
const results = Object.values(allSearchResults).flat(1);
|
|
372
|
+
return {
|
|
373
|
+
results,
|
|
374
|
+
stats,
|
|
375
|
+
limitHit
|
|
376
|
+
};
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
const Error$1 = 1;
|
|
380
|
+
const Exit = 2;
|
|
381
|
+
|
|
382
|
+
class TextSearchError extends Error {
|
|
383
|
+
constructor(cause) {
|
|
384
|
+
if (isEnoentError(cause)) {
|
|
385
|
+
super(`ripgrep path not found: ${cause}`);
|
|
386
|
+
this.code = E_RIP_GREP_NOT_FOUND;
|
|
387
|
+
} else {
|
|
388
|
+
super(`ripgrep process error: ${cause}`);
|
|
389
|
+
this.code = cause.code;
|
|
390
|
+
}
|
|
391
|
+
this.name = 'TextSearchError';
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const withResolvers = () => {
|
|
396
|
+
/**
|
|
397
|
+
* @type {any}
|
|
398
|
+
*/
|
|
399
|
+
let _resolve;
|
|
400
|
+
/**
|
|
401
|
+
* @type {any}
|
|
402
|
+
*/
|
|
403
|
+
let _reject;
|
|
404
|
+
const promise = new Promise((resolve, reject) => {
|
|
405
|
+
_resolve = resolve;
|
|
406
|
+
_reject = reject;
|
|
407
|
+
});
|
|
408
|
+
return {
|
|
409
|
+
resolve: _resolve,
|
|
410
|
+
reject: _reject,
|
|
411
|
+
promise
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
416
|
+
const {
|
|
417
|
+
resolve,
|
|
418
|
+
promise
|
|
419
|
+
} = withResolvers();
|
|
420
|
+
const listenerMap = Object.create(null);
|
|
421
|
+
const cleanup = value => {
|
|
422
|
+
for (const event of Object.keys(eventMap)) {
|
|
423
|
+
eventEmitter.off(event, listenerMap[event]);
|
|
424
|
+
}
|
|
425
|
+
resolve(value);
|
|
426
|
+
};
|
|
427
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
428
|
+
const listener = event => {
|
|
429
|
+
cleanup({
|
|
430
|
+
type,
|
|
431
|
+
event
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
eventEmitter.on(event, listener);
|
|
435
|
+
listenerMap[event] = listener;
|
|
436
|
+
}
|
|
437
|
+
return promise;
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const waitForProcessToExit = childProcess => {
|
|
441
|
+
return getFirstEvent(childProcess, {
|
|
442
|
+
error: Error$1,
|
|
443
|
+
close: Exit
|
|
444
|
+
});
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// TODO update vscode-ripgrep when https://github.com/mhinz/vim-grepper/issues/244, https://github.com/BurntSushi/ripgrep/issues/1892 is fixed
|
|
448
|
+
|
|
449
|
+
// need to use '.' as last argument for ripgrep
|
|
450
|
+
// issue 1 https://github.com/nvim-telescope/telescope.nvim/pull/908/files
|
|
451
|
+
// issue 2 https://github.com/BurntSushi/ripgrep/issues/1892
|
|
452
|
+
// remove workaround when ripgrep is fixed
|
|
453
|
+
|
|
454
|
+
// TODO stats flag might not be necessary
|
|
455
|
+
// TODO update client
|
|
456
|
+
// TODO not always run nice, maybe configure nice via flag/options
|
|
457
|
+
|
|
458
|
+
const search = async ({
|
|
459
|
+
searchDir = '',
|
|
460
|
+
maxSearchResults = 20_000,
|
|
461
|
+
ripGrepArgs = []
|
|
462
|
+
} = {}) => {
|
|
463
|
+
const charsBefore = 26;
|
|
464
|
+
const charsAfter = 50;
|
|
465
|
+
const childProcess = spawn(ripGrepArgs, {
|
|
466
|
+
cwd: searchDir
|
|
467
|
+
});
|
|
468
|
+
const pipeLinePromise = collectStdout(childProcess, maxSearchResults, charsBefore, charsAfter);
|
|
469
|
+
const closePromise = waitForProcessToExit(childProcess);
|
|
470
|
+
const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise]);
|
|
471
|
+
if (exitResult.type === Error$1) {
|
|
472
|
+
throw new TextSearchError(exitResult.event);
|
|
473
|
+
}
|
|
474
|
+
return pipeLineResult;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const commandMap = {
|
|
478
|
+
'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
|
|
479
|
+
'SearchFile.searchFile': searchFile,
|
|
480
|
+
'HandleWebSocket.handleWebSocket': handleWebSocket,
|
|
481
|
+
'TextSearch.search': search
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const listen = async () => {
|
|
485
|
+
await listen$1({
|
|
486
|
+
method: Auto(),
|
|
487
|
+
commandMap: commandMap
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
const main = async () => {
|
|
492
|
+
await listen();
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/search-process",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Search Process",
|
|
5
|
-
"main": "src/searchProcessMain.js",
|
|
6
|
-
"bin": "./bin/searchProcess.js",
|
|
7
|
-
"type": "module",
|
|
8
5
|
"keywords": [
|
|
9
|
-
"Lvce Editor"
|
|
6
|
+
"Lvce Editor",
|
|
7
|
+
"Search"
|
|
10
8
|
],
|
|
11
|
-
"author": "Lvce Editor",
|
|
12
|
-
"license": "MIT",
|
|
13
9
|
"repository": {
|
|
14
10
|
"type": "git",
|
|
15
|
-
"url": "https://github.com/lvce-editor/search-process.git"
|
|
11
|
+
"url": "git+https://github.com/lvce-editor/search-process.git"
|
|
16
12
|
},
|
|
17
|
-
"
|
|
18
|
-
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Lvce Editor",
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"bin": {
|
|
18
|
+
"search-process": "bin/searchProcess.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@lvce-editor/assert": "^1.3.0",
|
|
@@ -26,49 +26,7 @@
|
|
|
26
26
|
"optionalDependencies": {
|
|
27
27
|
"@lvce-editor/ripgrep": "^1.6.0"
|
|
28
28
|
},
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"unicorn/filename-case": "off",
|
|
32
|
-
"indent": "off",
|
|
33
|
-
"semi": "off",
|
|
34
|
-
"no-unused-vars": "off",
|
|
35
|
-
"unicorn/numeric-separators-style": "off",
|
|
36
|
-
"no-extra-semi": "off",
|
|
37
|
-
"arrow-body-style": "off",
|
|
38
|
-
"padded-blocks": "off",
|
|
39
|
-
"capitalized-comments": "off",
|
|
40
|
-
"padding-line-between-statements": "off",
|
|
41
|
-
"arrow-parens": "off",
|
|
42
|
-
"no-warning-comments": "off",
|
|
43
|
-
"array-bracket-spacing": "off",
|
|
44
|
-
"comma-spacing": "off",
|
|
45
|
-
"unicorn/no-array-callback-reference": "off",
|
|
46
|
-
"comma-dangle": "off",
|
|
47
|
-
"operator-linebreak": "off",
|
|
48
|
-
"no-case-declarations": "off",
|
|
49
|
-
"no-undef": "off",
|
|
50
|
-
"object-curly-spacing": "off",
|
|
51
|
-
"object-shorthand": "off",
|
|
52
|
-
"complexity": "off",
|
|
53
|
-
"no-labels": "off",
|
|
54
|
-
"no-multi-assign": "off",
|
|
55
|
-
"max-params": "off",
|
|
56
|
-
"no-bitwise": "off",
|
|
57
|
-
"unicorn/prefer-math-trunc": "off",
|
|
58
|
-
"no-await-in-loop": "off",
|
|
59
|
-
"unicorn/prefer-add-event-listener": "off",
|
|
60
|
-
"no-unused-expressions": "off",
|
|
61
|
-
"node/prefer-global/process": "off",
|
|
62
|
-
"unicorn/prevent-abbreviations": "off",
|
|
63
|
-
"unicorn/no-process-exit": "off",
|
|
64
|
-
"quotes": "off",
|
|
65
|
-
"n/prefer-global/process": [
|
|
66
|
-
"error",
|
|
67
|
-
"always"
|
|
68
|
-
]
|
|
69
|
-
},
|
|
70
|
-
"ignores": [
|
|
71
|
-
"distmin"
|
|
72
|
-
]
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
73
31
|
}
|
|
74
32
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@lvce-editor/assert'
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { Writable } from 'node:stream'
|
|
2
|
-
import { pipeline } from 'node:stream/promises'
|
|
3
|
-
import * as EncodingType from '../EncodingType/EncodingType.js'
|
|
4
|
-
import * as GetNewLineIndex from '../GetNewLineIndex/GetNewLineIndex.js'
|
|
5
|
-
import * as RipGrepParsedLineType from '../RipGrepParsedLineType/RipGrepParsedLineType.js'
|
|
6
|
-
import * as TextSearchResultType from '../TextSearchResultType/TextSearchResultType.js'
|
|
7
|
-
import * as ToTextSearchResult from '../ToTextSearchResult/ToTextSearchResult.js'
|
|
8
|
-
// TODO update vscode-ripgrep when https://github.com/mhinz/vim-grepper/issues/244, https://github.com/BurntSushi/ripgrep/issues/1892 is fixed
|
|
9
|
-
|
|
10
|
-
// need to use '.' as last argument for ripgrep
|
|
11
|
-
// issue 1 https://github.com/nvim-telescope/telescope.nvim/pull/908/files
|
|
12
|
-
// issue 2 https://github.com/BurntSushi/ripgrep/issues/1892
|
|
13
|
-
// remove workaround when ripgrep is fixed
|
|
14
|
-
|
|
15
|
-
// TODO stats flag might not be necessary
|
|
16
|
-
// TODO update client
|
|
17
|
-
// TODO not always run nice, maybe configure nice via flag/options
|
|
18
|
-
|
|
19
|
-
export const collectStdout = async (childProcess, maxSearchResults, charsBefore, charsAfter) => {
|
|
20
|
-
const allSearchResults = Object.create(null)
|
|
21
|
-
let buffer = ''
|
|
22
|
-
let stats = {}
|
|
23
|
-
let limitHit = false
|
|
24
|
-
let numberOfResults = 0
|
|
25
|
-
|
|
26
|
-
childProcess.stdout.setEncoding(EncodingType.Utf8)
|
|
27
|
-
|
|
28
|
-
const handleLine = (line) => {
|
|
29
|
-
const parsedLine = JSON.parse(line)
|
|
30
|
-
const { type, data } = parsedLine
|
|
31
|
-
switch (type) {
|
|
32
|
-
case RipGrepParsedLineType.Begin:
|
|
33
|
-
allSearchResults[data.path.text] = [
|
|
34
|
-
{
|
|
35
|
-
type: TextSearchResultType.File,
|
|
36
|
-
start: 0,
|
|
37
|
-
end: 0,
|
|
38
|
-
lineNumber: 0,
|
|
39
|
-
text: data.path.text,
|
|
40
|
-
},
|
|
41
|
-
]
|
|
42
|
-
break
|
|
43
|
-
case RipGrepParsedLineType.Match:
|
|
44
|
-
const remaining = maxSearchResults - numberOfResults
|
|
45
|
-
const matches = ToTextSearchResult.toTextSearchResult(parsedLine, remaining, charsBefore, charsAfter)
|
|
46
|
-
numberOfResults += matches.length
|
|
47
|
-
allSearchResults[data.path.text].push(...matches)
|
|
48
|
-
break
|
|
49
|
-
case RipGrepParsedLineType.Summary:
|
|
50
|
-
stats = data
|
|
51
|
-
break
|
|
52
|
-
default:
|
|
53
|
-
break
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const handleData = (chunk) => {
|
|
58
|
-
let newLineIndex = GetNewLineIndex.getNewLineIndex(chunk)
|
|
59
|
-
const dataString = buffer + chunk
|
|
60
|
-
if (newLineIndex === -1) {
|
|
61
|
-
buffer = dataString
|
|
62
|
-
return
|
|
63
|
-
}
|
|
64
|
-
newLineIndex += buffer.length
|
|
65
|
-
let previousIndex = 0
|
|
66
|
-
while (newLineIndex >= 0) {
|
|
67
|
-
const line = dataString.slice(previousIndex, newLineIndex)
|
|
68
|
-
handleLine(line)
|
|
69
|
-
previousIndex = newLineIndex + 1
|
|
70
|
-
newLineIndex = GetNewLineIndex.getNewLineIndex(dataString, previousIndex)
|
|
71
|
-
}
|
|
72
|
-
buffer = dataString.slice(previousIndex)
|
|
73
|
-
|
|
74
|
-
if (numberOfResults > maxSearchResults) {
|
|
75
|
-
limitHit = true
|
|
76
|
-
childProcess.kill()
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
await pipeline(
|
|
81
|
-
childProcess.stdout,
|
|
82
|
-
new Writable({
|
|
83
|
-
decodeStrings: false,
|
|
84
|
-
construct(callback) {
|
|
85
|
-
callback()
|
|
86
|
-
},
|
|
87
|
-
write(chunk, encoding, callback) {
|
|
88
|
-
try {
|
|
89
|
-
handleData(chunk)
|
|
90
|
-
callback()
|
|
91
|
-
} catch (error) {
|
|
92
|
-
callback(error)
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
}),
|
|
96
|
-
)
|
|
97
|
-
const results = Object.values(allSearchResults).flat(1)
|
|
98
|
-
return {
|
|
99
|
-
results,
|
|
100
|
-
stats,
|
|
101
|
-
limitHit,
|
|
102
|
-
}
|
|
103
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as HandleElectronMessagePort from '../HandleElectronMessagePort/HandleElectronMessagePort.js'
|
|
2
|
-
import * as HandleWebSocket from '../HandleWebSocket/HandleWebSocket.js'
|
|
3
|
-
import * as SearchFile from '../SearchFile/SearchFile.js'
|
|
4
|
-
import * as TextSearch from '../TextSearch/TextSearch.js'
|
|
5
|
-
|
|
6
|
-
export const commandMap = {
|
|
7
|
-
'HandleElectronMessagePort.handleElectronMessagePort': HandleElectronMessagePort.handleElectronMessagePort,
|
|
8
|
-
'SearchFile.searchFile': SearchFile.searchFile,
|
|
9
|
-
'HandleWebSocket.handleWebSocket': HandleWebSocket.handleWebSocket,
|
|
10
|
-
'TextSearch.search': TextSearch.search,
|
|
11
|
-
}
|
package/src/parts/Env/Env.js
DELETED
package/src/parts/Exec/Exec.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { execa } from 'execa'
|
|
2
|
-
import * as Assert from '../Assert/Assert.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @param {string} command
|
|
7
|
-
* @param {string[]} args
|
|
8
|
-
* @param {import('execa').Options} options
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
export const exec = async (command, args, options) => {
|
|
12
|
-
Assert.string(command)
|
|
13
|
-
Assert.array(args)
|
|
14
|
-
Assert.object(options)
|
|
15
|
-
const { stdout, stderr } = await execa(command, args, options)
|
|
16
|
-
return {
|
|
17
|
-
stdout,
|
|
18
|
-
stderr,
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as Promises from '../Promises/Promises.js'
|
|
2
|
-
|
|
3
|
-
export const getFirstEvent = (eventEmitter, eventMap) => {
|
|
4
|
-
const { resolve, promise } = Promises.withResolvers()
|
|
5
|
-
const listenerMap = Object.create(null)
|
|
6
|
-
const cleanup = (value) => {
|
|
7
|
-
for (const event of Object.keys(eventMap)) {
|
|
8
|
-
eventEmitter.off(event, listenerMap[event])
|
|
9
|
-
}
|
|
10
|
-
resolve(value)
|
|
11
|
-
}
|
|
12
|
-
for (const [event, type] of Object.entries(eventMap)) {
|
|
13
|
-
const listener = (event) => {
|
|
14
|
-
cleanup({
|
|
15
|
-
type,
|
|
16
|
-
event,
|
|
17
|
-
})
|
|
18
|
-
}
|
|
19
|
-
eventEmitter.on(event, listener)
|
|
20
|
-
listenerMap[event] = listener
|
|
21
|
-
}
|
|
22
|
-
return promise
|
|
23
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as Character from '../Character/Character.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {string} string
|
|
6
|
-
* @param {number|undefined} startIndex
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
export const getNewLineIndex = (string, startIndex = undefined) => {
|
|
10
|
-
return string.indexOf(Character.NewLine, startIndex)
|
|
11
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as Assert from '../Assert/Assert.js'
|
|
2
|
-
import * as IpcChild from '../IpcChild/IpcChild.js'
|
|
3
|
-
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
4
|
-
|
|
5
|
-
export const handleElectronMessagePort = async (messagePort, ...params) => {
|
|
6
|
-
Assert.object(messagePort)
|
|
7
|
-
await IpcChild.listen({
|
|
8
|
-
method: IpcChildType.ElectronMessagePort,
|
|
9
|
-
messagePort,
|
|
10
|
-
})
|
|
11
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import * as Assert from '../Assert/Assert.js'
|
|
2
|
-
import * as IpcChild from '../IpcChild/IpcChild.js'
|
|
3
|
-
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
4
|
-
|
|
5
|
-
export const handleWebSocket = async (handle, request) => {
|
|
6
|
-
Assert.object(handle)
|
|
7
|
-
Assert.object(request)
|
|
8
|
-
await IpcChild.listen({
|
|
9
|
-
method: IpcChildType.WebSocket,
|
|
10
|
-
request,
|
|
11
|
-
handle,
|
|
12
|
-
})
|
|
13
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ElectronMessagePortRpcClient,
|
|
3
|
-
ElectronUtilityProcessRpcClient,
|
|
4
|
-
NodeForkedProcessRpcClient,
|
|
5
|
-
NodeWebSocketRpcClient,
|
|
6
|
-
NodeWorkerRpcClient,
|
|
7
|
-
} from '@lvce-editor/rpc'
|
|
8
|
-
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
9
|
-
|
|
10
|
-
export const getModule = (method) => {
|
|
11
|
-
switch (method) {
|
|
12
|
-
case IpcChildType.NodeForkedProcess:
|
|
13
|
-
return NodeForkedProcessRpcClient
|
|
14
|
-
case IpcChildType.NodeWorker:
|
|
15
|
-
return NodeWorkerRpcClient
|
|
16
|
-
case IpcChildType.ElectronUtilityProcess:
|
|
17
|
-
return ElectronUtilityProcessRpcClient
|
|
18
|
-
case IpcChildType.ElectronMessagePort:
|
|
19
|
-
return ElectronMessagePortRpcClient
|
|
20
|
-
case IpcChildType.WebSocket:
|
|
21
|
-
return NodeWebSocketRpcClient
|
|
22
|
-
default:
|
|
23
|
-
throw new Error('unexpected ipc type')
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const NodeWorker = 1
|
|
2
|
-
export const NodeForkedProcess = 2
|
|
3
|
-
export const ElectronUtilityProcess = 3
|
|
4
|
-
export const ElectronMessagePort = 4
|
|
5
|
-
export const WebSocket = 6
|
|
6
|
-
|
|
7
|
-
export const Auto = () => {
|
|
8
|
-
const { argv } = process
|
|
9
|
-
if (argv.includes('--ipc-type=node-worker')) {
|
|
10
|
-
return NodeWorker
|
|
11
|
-
}
|
|
12
|
-
if (argv.includes('--ipc-type=node-forked-process')) {
|
|
13
|
-
return NodeForkedProcess
|
|
14
|
-
}
|
|
15
|
-
if (argv.includes('--ipc-type=electron-utility-process')) {
|
|
16
|
-
return ElectronUtilityProcess
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`[shared-process] unknown ipc type`)
|
|
19
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
|
|
2
|
-
import * as IsEnoentErrorWindows from '../IsEnoentErrorWindows/IsEnoentErrorWindows.js'
|
|
3
|
-
|
|
4
|
-
const isEnoentErrorLinux = (error) => {
|
|
5
|
-
return error.code === ErrorCodes.ENOENT
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const isEnoentError = (error) => {
|
|
9
|
-
if (!error) {
|
|
10
|
-
return false
|
|
11
|
-
}
|
|
12
|
-
return isEnoentErrorLinux(error) || IsEnoentErrorWindows.isEnoentErrorWindows(error)
|
|
13
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import * as GetNewLineIndex from '../GetNewLineIndex/GetNewLineIndex.js'
|
|
2
|
-
|
|
3
|
-
export const limitString = (string, limit) => {
|
|
4
|
-
let lines = 0
|
|
5
|
-
let i = 0
|
|
6
|
-
while (i !== -1 && lines < limit) {
|
|
7
|
-
i = GetNewLineIndex.getNewLineIndex(string, i + 1)
|
|
8
|
-
if (i === -1) {
|
|
9
|
-
break
|
|
10
|
-
}
|
|
11
|
-
lines++
|
|
12
|
-
}
|
|
13
|
-
if (i === -1) {
|
|
14
|
-
return string
|
|
15
|
-
}
|
|
16
|
-
return string.slice(0, i)
|
|
17
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import * as CommandMap from '../CommandMap/CommandMap.js'
|
|
2
|
-
import * as IpcChild from '../IpcChild/IpcChild.js'
|
|
3
|
-
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
4
|
-
|
|
5
|
-
export const listen = async () => {
|
|
6
|
-
await IpcChild.listen({
|
|
7
|
-
method: IpcChildType.Auto(),
|
|
8
|
-
commandMap: CommandMap.commandMap,
|
|
9
|
-
})
|
|
10
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Console } from 'node:console'
|
|
2
|
-
import { createWriteStream } from 'node:fs'
|
|
3
|
-
import { tmpdir } from 'node:os'
|
|
4
|
-
|
|
5
|
-
// TODO mock this module when used in unit tests
|
|
6
|
-
|
|
7
|
-
const state = {
|
|
8
|
-
/**
|
|
9
|
-
* @type {Console|undefined}
|
|
10
|
-
*/
|
|
11
|
-
console: undefined,
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const createConsole = () => {
|
|
15
|
-
const logFile = `${tmpdir()}/log-shared-process.txt`
|
|
16
|
-
const writeStream = createWriteStream(logFile)
|
|
17
|
-
const logger = new Console(writeStream)
|
|
18
|
-
return logger
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const getOrCreateLogger = () => {
|
|
22
|
-
if (!state.console) {
|
|
23
|
-
state.console = createConsole()
|
|
24
|
-
}
|
|
25
|
-
return state.console
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const info = (...args) => {
|
|
29
|
-
const logger = getOrCreateLogger()
|
|
30
|
-
logger.info(...args)
|
|
31
|
-
console.info(...args)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const error = (...args) => {
|
|
35
|
-
const logger = getOrCreateLogger()
|
|
36
|
-
logger.error(...args)
|
|
37
|
-
console.error(...args)
|
|
38
|
-
}
|
package/src/parts/Main/Main.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import * as EncodingType from '../EncodingType/EncodingType.js'
|
|
2
|
-
|
|
3
|
-
export const parseRipGrepLines = (parsedLineData) => {
|
|
4
|
-
if (parsedLineData.lines.text) {
|
|
5
|
-
return parsedLineData.lines.text
|
|
6
|
-
}
|
|
7
|
-
if (parsedLineData.lines.bytes) {
|
|
8
|
-
return Buffer.from(parsedLineData.lines.bytes, EncodingType.Base64).toString()
|
|
9
|
-
}
|
|
10
|
-
throw new Error(`unable to parse line data`)
|
|
11
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const withResolvers = () => {
|
|
2
|
-
/**
|
|
3
|
-
* @type {any}
|
|
4
|
-
*/
|
|
5
|
-
let _resolve
|
|
6
|
-
/**
|
|
7
|
-
* @type {any}
|
|
8
|
-
*/
|
|
9
|
-
let _reject
|
|
10
|
-
const promise = new Promise((resolve, reject) => {
|
|
11
|
-
_resolve = resolve
|
|
12
|
-
_reject = reject
|
|
13
|
-
})
|
|
14
|
-
return {
|
|
15
|
-
resolve: _resolve,
|
|
16
|
-
reject: _reject,
|
|
17
|
-
promise,
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import * as NodeChildProcess from 'node:child_process'
|
|
2
|
-
import * as Assert from '../Assert/Assert.js'
|
|
3
|
-
import * as Env from '../Env/Env.js'
|
|
4
|
-
import * as Exec from '../Exec/Exec.js'
|
|
5
|
-
import * as RgPath from '../RipGrepPath/RipGrepPath.js'
|
|
6
|
-
import { VError } from '../VError/VError.js'
|
|
7
|
-
|
|
8
|
-
export const ripGrepPath = Env.getRipGrepPath() || RgPath.rgPath
|
|
9
|
-
|
|
10
|
-
export const spawn = (args, options) => {
|
|
11
|
-
try {
|
|
12
|
-
const childProcess = NodeChildProcess.spawn(RgPath.rgPath, args, options)
|
|
13
|
-
return {
|
|
14
|
-
childProcess,
|
|
15
|
-
on(event, listener) {
|
|
16
|
-
this.childProcess.on(event, listener)
|
|
17
|
-
},
|
|
18
|
-
off(event, listener) {
|
|
19
|
-
this.childProcess.off(event, listener)
|
|
20
|
-
},
|
|
21
|
-
once(event, listener) {
|
|
22
|
-
this.childProcess.once(event, listener)
|
|
23
|
-
},
|
|
24
|
-
stdout: childProcess.stdout,
|
|
25
|
-
stderr: childProcess.stderr,
|
|
26
|
-
kill() {
|
|
27
|
-
this.childProcess.kill()
|
|
28
|
-
},
|
|
29
|
-
}
|
|
30
|
-
} catch (error) {
|
|
31
|
-
throw new VError(error, `Failed to spawn ripgrep`)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const exec = async (args, options) => {
|
|
36
|
-
Assert.array(args)
|
|
37
|
-
Assert.object(options)
|
|
38
|
-
const { stdout, stderr } = await Exec.exec(ripGrepPath, args, options)
|
|
39
|
-
return { stdout, stderr }
|
|
40
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { rgPath } from '@lvce-editor/ripgrep'
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import * as Assert from '../Assert/Assert.js'
|
|
2
|
-
import * as Character from '../Character/Character.js'
|
|
3
|
-
import * as IsEnoentError from '../IsEnoentError/IsEnoentError.js'
|
|
4
|
-
import * as LimitString from '../LimitString/LimitString.js'
|
|
5
|
-
import * as Logger from '../Logger/Logger.js'
|
|
6
|
-
import * as RipGrep from '../RipGrep/RipGrep.js'
|
|
7
|
-
|
|
8
|
-
// TODO don't necessarily need ripgrep to list all the files,
|
|
9
|
-
// maybe also a faster c program can do it
|
|
10
|
-
// another option would be to cache the results in renderer-worker and
|
|
11
|
-
// do a delta comparison, so the first time it would send 100kB
|
|
12
|
-
// but the second time only a few hundred bytes of changes
|
|
13
|
-
|
|
14
|
-
export const searchFile = async ({ searchPath = '', limit = 100, ripGrepArgs = [] }) => {
|
|
15
|
-
try {
|
|
16
|
-
Assert.string(searchPath)
|
|
17
|
-
Assert.array(ripGrepArgs)
|
|
18
|
-
Assert.number(limit)
|
|
19
|
-
const { stdout, stderr } = await RipGrep.exec(ripGrepArgs, {
|
|
20
|
-
cwd: searchPath,
|
|
21
|
-
})
|
|
22
|
-
return LimitString.limitString(stdout, limit)
|
|
23
|
-
} catch (error) {
|
|
24
|
-
if (IsEnoentError.isEnoentError(error)) {
|
|
25
|
-
Logger.info(`[shared-process] ripgrep could not be found at "${RipGrep.ripGrepPath}"`)
|
|
26
|
-
return Character.EmptyString
|
|
27
|
-
}
|
|
28
|
-
// @ts-ignore
|
|
29
|
-
if (error && error.stderr === Character.EmptyString) {
|
|
30
|
-
return Character.EmptyString
|
|
31
|
-
}
|
|
32
|
-
Logger.error(error)
|
|
33
|
-
return Character.EmptyString
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as CollectTextSearchStdout from '../CollectTextSearchStdout/CollectTextSearchStdout.js'
|
|
2
|
-
import * as ProcessExitEventType from '../ProcessExitEventType/ProcessExitEventType.js'
|
|
3
|
-
import * as RipGrep from '../RipGrep/RipGrep.js'
|
|
4
|
-
import { TextSearchError } from '../TextSearchError/TextSearchError.js'
|
|
5
|
-
import * as WaitForProcessToExit from '../WaitForProcessToExit/WaitForProcessToExit.js'
|
|
6
|
-
|
|
7
|
-
// TODO update vscode-ripgrep when https://github.com/mhinz/vim-grepper/issues/244, https://github.com/BurntSushi/ripgrep/issues/1892 is fixed
|
|
8
|
-
|
|
9
|
-
// need to use '.' as last argument for ripgrep
|
|
10
|
-
// issue 1 https://github.com/nvim-telescope/telescope.nvim/pull/908/files
|
|
11
|
-
// issue 2 https://github.com/BurntSushi/ripgrep/issues/1892
|
|
12
|
-
// remove workaround when ripgrep is fixed
|
|
13
|
-
|
|
14
|
-
// TODO stats flag might not be necessary
|
|
15
|
-
// TODO update client
|
|
16
|
-
// TODO not always run nice, maybe configure nice via flag/options
|
|
17
|
-
|
|
18
|
-
export const search = async ({ searchDir = '', maxSearchResults = 20_000, ripGrepArgs = [] } = {}) => {
|
|
19
|
-
const charsBefore = 26
|
|
20
|
-
const charsAfter = 50
|
|
21
|
-
const childProcess = RipGrep.spawn(ripGrepArgs, {
|
|
22
|
-
cwd: searchDir,
|
|
23
|
-
})
|
|
24
|
-
const pipeLinePromise = CollectTextSearchStdout.collectStdout(childProcess, maxSearchResults, charsBefore, charsAfter)
|
|
25
|
-
const closePromise = WaitForProcessToExit.waitForProcessToExit(childProcess)
|
|
26
|
-
const [pipeLineResult, exitResult] = await Promise.all([pipeLinePromise, closePromise])
|
|
27
|
-
if (exitResult.type === ProcessExitEventType.Error) {
|
|
28
|
-
throw new TextSearchError(exitResult.event)
|
|
29
|
-
}
|
|
30
|
-
return pipeLineResult
|
|
31
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import * as ErrorCodes from '../ErrorCodes/ErrorCodes.js'
|
|
2
|
-
import * as IsEnoentError from '../IsEnoentError/IsEnoentError.js'
|
|
3
|
-
|
|
4
|
-
export class TextSearchError extends Error {
|
|
5
|
-
constructor(cause) {
|
|
6
|
-
if (IsEnoentError.isEnoentError(cause)) {
|
|
7
|
-
super(`ripgrep path not found: ${cause}`)
|
|
8
|
-
this.code = ErrorCodes.E_RIP_GREP_NOT_FOUND
|
|
9
|
-
} else {
|
|
10
|
-
super(`ripgrep process error: ${cause}`)
|
|
11
|
-
this.code = cause.code
|
|
12
|
-
}
|
|
13
|
-
this.name = 'TextSearchError'
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import * as ParseRipGrepLines from '../ParseRipGrepLines/ParseRipGrepLines.js'
|
|
2
|
-
import * as TextSearchResultType from '../TextSearchResultType/TextSearchResultType.js'
|
|
3
|
-
|
|
4
|
-
export const toTextSearchResult = (parsedLine, remaining, charsBefore, charsAfter) => {
|
|
5
|
-
const results = []
|
|
6
|
-
const parsedLineData = parsedLine.data
|
|
7
|
-
const lines = ParseRipGrepLines.parseRipGrepLines(parsedLineData)
|
|
8
|
-
const lineNumber = parsedLineData.line_number
|
|
9
|
-
const { submatches } = parsedLineData
|
|
10
|
-
const linesLength = lines.length
|
|
11
|
-
for (const submatch of submatches) {
|
|
12
|
-
const previewStart = Math.max(submatch.start - charsBefore, 0)
|
|
13
|
-
const actualStart = previewStart
|
|
14
|
-
const previewEnd = Math.min(submatch.end + charsAfter, linesLength)
|
|
15
|
-
const previewText = lines.slice(actualStart, previewEnd)
|
|
16
|
-
results.push({
|
|
17
|
-
type: TextSearchResultType.Match,
|
|
18
|
-
start: submatch.start - actualStart,
|
|
19
|
-
end: submatch.end - actualStart,
|
|
20
|
-
lineNumber,
|
|
21
|
-
text: previewText,
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
return results
|
|
25
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@lvce-editor/verror'
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import * as GetFirstEvent from '../GetFirstEvent/GetFirstEvent.js'
|
|
2
|
-
import * as ProcessExitEventType from '../ProcessExitEventType/ProcessExitEventType.js'
|
|
3
|
-
|
|
4
|
-
export const waitForProcessToExit = (childProcess) => {
|
|
5
|
-
return GetFirstEvent.getFirstEvent(childProcess, {
|
|
6
|
-
error: ProcessExitEventType.Error,
|
|
7
|
-
close: ProcessExitEventType.Exit,
|
|
8
|
-
})
|
|
9
|
-
}
|
package/src/searchProcessMain.js
DELETED