@lvce-editor/search-process 0.28.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/LICENSE +21 -0
- package/dist/searchProcessMain.js +3093 -0
- package/dist/searchProcessMain.js.map +1 -0
- package/index.js +6 -0
- package/package.json +29 -0
- package/src/parts/Assert/Assert.js +1 -0
- package/src/parts/Callback/Callback.js +1 -0
- package/src/parts/Character/Character.js +12 -0
- package/src/parts/CollectTextSearchStdout/CollectTextSearchStdout.js +103 -0
- package/src/parts/Command/Command.js +9 -0
- package/src/parts/CommandMap/CommandMap.js +11 -0
- package/src/parts/CommandState/CommandState.js +17 -0
- package/src/parts/EncodingType/EncodingType.js +2 -0
- package/src/parts/Env/Env.js +21 -0
- package/src/parts/ErrorCodes/ErrorCodes.js +22 -0
- package/src/parts/Exec/Exec.js +20 -0
- package/src/parts/GetFirstEvent/GetFirstEvent.js +23 -0
- package/src/parts/GetNewLineIndex/GetNewLineIndex.js +11 -0
- package/src/parts/HandleElectronMessagePort/HandleElectronMessagePort.js +13 -0
- package/src/parts/HandleIpc/HandleIpc.js +10 -0
- package/src/parts/HandleMessage/HandleMessage.js +19 -0
- package/src/parts/HandleWebSocket/HandleWebSocket.js +15 -0
- package/src/parts/IpcChild/IpcChild.js +15 -0
- package/src/parts/IpcChildModule/IpcChildModule.js +25 -0
- package/src/parts/IpcChildType/IpcChildType.js +20 -0
- package/src/parts/IsEnoentError/IsEnoentError.js +13 -0
- package/src/parts/IsEnoentErrorWindows/IsEnoentErrorWindows.js +3 -0
- package/src/parts/JsonRpc/JsonRpc.js +1 -0
- package/src/parts/LimitString/LimitString.js +17 -0
- package/src/parts/Listen/Listen.js +8 -0
- package/src/parts/Logger/Logger.js +50 -0
- package/src/parts/Main/Main.js +8 -0
- package/src/parts/ParseRipGrepLines/ParseRipGrepLines.js +11 -0
- package/src/parts/ProcessExitEventType/ProcessExitEventType.js +2 -0
- package/src/parts/Promises/Promises.js +19 -0
- package/src/parts/RipGrep/RipGrep.js +40 -0
- package/src/parts/RipGrepParsedLineType/RipGrepParsedLineType.js +4 -0
- package/src/parts/RipGrepPath/RipGrepPath.js +1 -0
- package/src/parts/SearchFile/SearchFile.js +35 -0
- package/src/parts/TextSearch/TextSearch.js +31 -0
- package/src/parts/TextSearchError/TextSearchError.js +15 -0
- package/src/parts/TextSearchResultType/TextSearchResultType.js +2 -0
- package/src/parts/ToTextSearchResult/ToTextSearchResult.js +25 -0
- package/src/parts/VError/VError.js +1 -0
- package/src/parts/WaitForProcessToExit/WaitForProcessToExit.js +9 -0
- package/src/searchProcessMain.js +3 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const NodeWorker = 1
|
|
2
|
+
export const NodeForkedProcess = 2
|
|
3
|
+
export const ElectronUtilityProcess = 3
|
|
4
|
+
export const ElectronMessagePort = 4
|
|
5
|
+
export const NodeMessagePort = 5
|
|
6
|
+
export const WebSocket = 6
|
|
7
|
+
|
|
8
|
+
export const Auto = () => {
|
|
9
|
+
const { argv } = process
|
|
10
|
+
if (argv.includes('--ipc-type=node-worker')) {
|
|
11
|
+
return NodeWorker
|
|
12
|
+
}
|
|
13
|
+
if (argv.includes('--ipc-type=node-forked-process')) {
|
|
14
|
+
return NodeForkedProcess
|
|
15
|
+
}
|
|
16
|
+
if (argv.includes('--ipc-type=electron-utility-process')) {
|
|
17
|
+
return ElectronUtilityProcess
|
|
18
|
+
}
|
|
19
|
+
throw new Error(`[shared-process] unknown ipc type`)
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@lvce-editor/json-rpc'
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as HandleIpc from '../HandleIpc/HandleIpc.js'
|
|
2
|
+
import * as IpcChild from '../IpcChild/IpcChild.js'
|
|
3
|
+
import * as IpcChildType from '../IpcChildType/IpcChildType.js'
|
|
4
|
+
|
|
5
|
+
export const listen = async () => {
|
|
6
|
+
const ipc = await IpcChild.listen({ method: IpcChildType.Auto() })
|
|
7
|
+
HandleIpc.handleIpc(ipc)
|
|
8
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 log = (...args) => {
|
|
29
|
+
const logger = getOrCreateLogger()
|
|
30
|
+
logger.log(...args)
|
|
31
|
+
console.log(...args)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const info = (...args) => {
|
|
35
|
+
const logger = getOrCreateLogger()
|
|
36
|
+
logger.info(...args)
|
|
37
|
+
console.info(...args)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const warn = (...args) => {
|
|
41
|
+
const logger = getOrCreateLogger()
|
|
42
|
+
logger.warn(...args)
|
|
43
|
+
console.warn(...args)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const error = (...args) => {
|
|
47
|
+
const logger = getOrCreateLogger()
|
|
48
|
+
logger.error(...args)
|
|
49
|
+
console.error(...args)
|
|
50
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as CommandMap from '../CommandMap/CommandMap.js'
|
|
2
|
+
import * as CommandState from '../CommandState/CommandState.js'
|
|
3
|
+
import * as Listen from '../Listen/Listen.js'
|
|
4
|
+
|
|
5
|
+
export const main = async () => {
|
|
6
|
+
CommandState.registerCommands(CommandMap.commandMap)
|
|
7
|
+
await Listen.listen()
|
|
8
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { rgPath } from '@lvce-editor/ripgrep'
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@lvce-editor/verror'
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|