forked from Gruntfuggly/todo-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripgrep.js
186 lines (159 loc) · 4.82 KB
/
ripgrep.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
/* jshint esversion:6, node: true */
/* eslint-env node */
/**
* This is a slightly modified version of the ripgrep-js module from npm
* written by alexlafroscia (github.com/alexlafroscia/ripgrep-js)
* Instead of assuming that ripgrep is in the users path, it uses the
* ripgrep binary downloaded via vscode-ripgrep.
*/
'use strict';
const child_process = require( 'child_process' );
const fs = require( 'fs' );
var currentProcess;
function RipgrepError( error, stderr )
{
this.message = error;
this.stderr = stderr;
}
function formatResults( stdout )
{
stdout = stdout.trim();
if( !stdout )
{
return [];
}
return stdout
.split( '\n' )
.map( ( line ) => new Match( line ) );
}
/**
* @method ripGrep
* @param {string} cwd
* @param {object|string} options if a string is provided, it will be used as the `searchTerm`
* @param {string} options.regex a regex pattern to search for. See `-e` option
* @param {string} options.string a fixed string to search for. See `-F` option
* @param {Array<string>} option.globs a set of globs to include/exclude. See `-g` option
* @param {string} [searchTerm]
*/
module.exports.search = function ripGrep( cwd, options, searchTerm )
{
// If you're invoking the function with two arguments, just the `cwd` and `searchTerm`
if( arguments.length === 2 && typeof options === 'string' )
{
searchTerm = options;
options = {};
}
if( !cwd )
{
return Promise.reject( { error: 'No `cwd` provided' } );
}
if( arguments.length === 1 )
{
return Promise.reject( { error: 'No search term provided' } );
}
options.regex = options.regex || '';
options.globs = options.globs || [];
options.string = searchTerm || options.string || '';
var rgPath = options.rgPath;
var isWin = /^win/.test( process.platform );
if( !fs.existsSync( rgPath ) )
{
return Promise.reject( { error: "ripgrep executable not found (" + rgPath + ")" } );
}
if( !fs.existsSync( cwd ) )
{
return Promise.reject( { error: "root folder not found (" + cwd + ")" } );
}
if( isWin )
{
rgPath = '"' + rgPath + '"';
}
else
{
rgPath = rgPath.replace( / /g, '\\ ' );
}
let execString = rgPath + ' --no-messages --vimgrep -H --column --line-number --color never ' + options.additional;
if( options.regex )
{
execString = `${execString} -e ${options.regex}`;
}
else if( options.string )
{
execString = `${execString} -F ${options.string}`;
}
execString = options.globs.reduce( ( command, glob ) =>
{
return `${command} -g \"${glob}\"`;
}, execString );
if( options.filename )
{
var filename = options.filename;
if( isWin && filename.slice( -1 ) === "\\" )
{
filename = filename.substr( 0, filename.length - 1 );
}
execString += " \"" + filename + "\"";
}
else
{
execString += " .";
}
if( options.outputChannel )
{
options.outputChannel.appendLine( "Command: " + execString );
}
return new Promise( function( resolve, reject )
{
// The default for omitting maxBuffer, according to Node docs, is 200kB.
// We'll explicitly give that here if a custom value is not provided.
// Note that our options value is in KB, so we have to convert to bytes.
const maxBuffer = ( options.maxBuffer || 200 ) * 1024;
var currentProcess = child_process.exec( execString, { cwd, maxBuffer } );
var results = "";
currentProcess.stdout.on( 'data', function( data )
{
if( options.outputChannel )
{
options.outputChannel.appendLine( data );
}
results += data;
} );
currentProcess.stderr.on( 'data', function( data )
{
if( options.outputChannel )
{
options.outputChannel.appendLine( data );
}
reject( new RipgrepError( data, "" ) );
} );
currentProcess.on( 'close', function( code )
{
resolve( formatResults( results ) );
} );
} );
};
module.exports.kill = function()
{
if( currentProcess !== undefined )
{
currentProcess.kill( 'SIGINT' );
}
};
class Match
{
constructor( matchText )
{
this.file = "";
if( matchText.length > 1 && matchText[ 1 ] === ':' )
{
this.file = matchText.substr( 0, 2 );
matchText = matchText.substr( 2 );
}
matchText = matchText.split( ':' );
this.file += matchText.shift();
this.line = parseInt( matchText.shift() );
this.column = parseInt( matchText.shift() );
this.match = matchText.join( ':' );
}
}
module.exports.Match = Match;