forked from harthur/replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace.js
173 lines (159 loc) · 5.14 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
var fs = require("fs"),
path = require("path"),
colors = require("colors");
module.exports = function(opts) {
var excludes = [],
includes,
regex,
canReplace,
replaceFunc,
lineCount = 0,
limit = 400, // chars per line
options;
options = opts;
if (!options.color) options.color = "cyan";
var flags = "g"; // global multiline
if (options.ignoreCase) {
flags += "i";
}
if (options.multiline) {
flags += "m";
}
regex = new RegExp(options.regex, flags);
canReplace = !options.preview && options.replacement !== undefined;
if (options.include) {
includes = options.include.split(",").map(patternToRegex);
}
if (options.exclude) {
excludes = options.exclude.split(",");
}
var listFile = options.excludeList || path.join(__dirname, '/defaultignore');
var list = fs.readFileSync(listFile, "utf-8").split("\n");
excludes = excludes.concat(list)
.filter(function(line) {
return line && line.indexOf("#");
})
.map(patternToRegex);
if (options.funcFile) {
eval('replaceFunc = ' + fs.readFileSync(options.funcFile, "utf-8"));
}
for (var i = 0; i < options.path.length; i++) {
if(options.async) {
replacizeFile(options.path[i]);
}
else {
replacizeFileSync(options.path[i]);
}
}
function patternToRegex(pattern) {
return new RegExp("^" + pattern.replace(/\./g, '\\.').replace(/\*/g, '.*').trim() + "$");
}
function includeFile(file) {
if (includes) {
for (var i = 0; i < includes.length; i++) {
if (file.match(includes[i]))
return true;
}
return false;
}
else {
for (var i = 0; i < excludes.length; i++) {
if (file.match(excludes[i]))
return false;
}
return true;
}
}
function replacizeFile(file) {
fs.lstat(file, function(err, stats) {
if (err) throw err;
if (stats.isSymbolicLink()) {
// don't follow symbolic links for now
return;
}
if (stats.isFile()) {
if (!includeFile(file)) {
return;
}
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) {
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;
}
if (stats.isFile()) {
if (!includeFile(file)) {
return;
}
var text = fs.readFileSync(file, "utf-8");
text = replacizeText(text, file);
if (canReplace) {
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 text;
}
if (!options.silent) {
var printout = " " + 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);
}
}
}