forked from harthur/replace
-
Notifications
You must be signed in to change notification settings - Fork 9
/
replace.js
209 lines (188 loc) · 6.39 KB
/
replace.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
var fs = require("fs"),
path = require("path"),
chalk = require("chalk"),
minimatch = require("minimatch"),
sharedOptions = require("./bin/shared-options");
module.exports = function(options) {
var matched = [];
// If the path is the same as the default and the recursive option was not
// specified, search recursively under the current directory as a
// convenience.
if (options.paths.length === 1 &&
options.paths[0] === sharedOptions.paths.default[0] &&
!options.hasOwnProperty('recursive')) {
options.paths = ['.'];
}
var lineCount = 0,
limit = 400; // chars per line
if (options.r) {
options.recursive = true;
}
if (!options.color) {
options.color = "cyan";
}
var flags = "g"; // global multiline
if (options.ignoreCase) {
flags += "i";
}
if (options.multiline) {
flags += "m";
}
var regex;
if (options.regex instanceof RegExp) {
regex = options.regex;
}
else {
regex = new RegExp(options.regex, flags);
}
var canReplace = !options.preview && options.replacement !== undefined;
var includes;
if (options.include) {
includes = options.include.split(",");
}
var excludes = [];
if (options.exclude) {
excludes = options.exclude.split(",");
}
var ignoreFile = options.excludeList || path.join(__dirname, '/defaultignore');
var ignores = fs.readFileSync(ignoreFile, "utf-8").split("\n");
excludes = excludes.concat(ignores);
var replaceFunc;
if (options.funcFile) {
eval('replaceFunc = ' + fs.readFileSync(options.funcFile, "utf-8"));
}
if(options.z) {
process.stdin.resume();
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
const text = replacizeText(_input);
process.stdout.write(text);
});
} else {
for (var i = 0; i < options.paths.length; i++) {
if (options.async) {
replacizeFile(options.paths[i]);
}
else {
replacizeFileSync(options.paths[i]);
}
}
}
return matched;
function canSearch(file, isFile) {
var inIncludes = includes && includes.some(function(include) {
return minimatch(file, include, { matchBase: true });
})
var inExcludes = excludes.some(function(exclude) {
return minimatch(file, exclude, { matchBase: true });
})
return ((!includes || !isFile || inIncludes) && (!excludes || !inExcludes));
}
function replacizeFile(file) {
fs.lstat(file, function(err, stats) {
if (err) throw err;
if (stats.isSymbolicLink()) {
// don't follow symbolic links for now
return;
}
var isFile = stats.isFile();
if (!canSearch(file, isFile)) {
return;
}
if (isFile) {
fs.readFile(file, "utf-8", function(err, text) {
if (err) {
if (err.code == 'EMFILE') {
console.log('Too many files, try running `replace` without --async');
process.exit(1);
}
throw err;
}
text = replacizeText(text, file);
if (canReplace && text !== null) {
fs.writeFile(file, text, function(err) {
if (err) throw err;
});
}
});
}
else if (stats.isDirectory() && options.recursive) {
fs.readdir(file, function(err, files) {
if (err) throw err;
for (var i = 0; i < files.length; i++) {
replacizeFile(path.join(file, files[i]));
}
});
}
});
}
function replacizeFileSync(file) {
var stats = fs.lstatSync(file);
if (stats.isSymbolicLink()) {
// don't follow symbolic links for now
return;
}
var isFile = stats.isFile();
if (!canSearch(file, isFile)) {
return;
}
if (isFile) {
var text = fs.readFileSync(file, "utf-8");
var replacizedText = replacizeText(text, file);
if (replacizedText !== null) {
if (canReplace) {
fs.writeFileSync(file, replacizedText);
} else {
matched.push({ text, path: file });
}
}
}
else if (stats.isDirectory() && options.recursive) {
var files = fs.readdirSync(file);
for (var i = 0; i < files.length; i++) {
replacizeFileSync(path.join(file, files[i]));
}
}
}
function replacizeText(text, file) {
var match = text.match(regex);
if (!match) {
return null;
}
if (!options.silent && file) {
var printout = options.noColor ? file : options.fileColor ? chalk[options.fileColor](file) : file;
if (options.count) {
var count = " (" + match.length + ")";
count = options.noColor ? count : chalk.grey(count);
printout += count;
}
console.log(printout);
}
if (!options.silent && !options.quiet
&& !(lineCount > options.maxLines)
&& options.multiline) {
var lines = text.split("\n");
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (line.match(regex)) {
if (++lineCount > options.maxLines) {
break;
}
var replacement = options.replacement || "$&";
if (!options.noColor) {
replacement = chalk[options.color](replacement);
}
line = line.replace(regex, replaceFunc || replacement);
// only console log if file not stdin
file && console.log(" " + (i + 1) + ": " + line.slice(0, limit));
}
}
}
if (canReplace) {
return text.replace(regex, replaceFunc || options.replacement);
}
}
}