forked from harthur/replace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.js
More file actions
168 lines (152 loc) · 5.03 KB
/
replace.js
File metadata and controls
168 lines (152 loc) · 5.03 KB
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
var fs = require("fs"),
path = require("path"),
colors = require("colors"),
minimatch = require("minimatch");
module.exports = function(options) {
var lineCount = 0,
limit = 400; // chars per line
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"));
}
for (var i = 0; i < options.paths.length; i++) {
if (options.async) {
replacizeFile(options.paths[i]);
}
else {
replacizeFileSync(options.paths[i]);
}
}
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");
text = replacizeText(text, file);
if (canReplace && text !== null) {
fs.writeFileSync(file, text);
}
}
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) {
var printout = file[options.fileColor] || file;
if (options.count) {
printout += (" (" + match.length + ")").grey;
}
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 || "$&";
line = line.replace(regex, replaceFunc || replacement[options.color]);
console.log(" " + (i + 1) + ": " + line.slice(0, limit));
}
}
}
if (canReplace) {
return text.replace(regex, replaceFunc || options.replacement);
}
}
}