This repository has been archived by the owner on May 24, 2022. It is now read-only.
/
nak
executable file
·106 lines (85 loc) · 2.86 KB
/
nak
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
#!/usr/bin/env node
var options = require("../lib/options").parseArgs(),
fs = require('fs'),
path = require('path'),
walkdir = require('../lib/walkdir').walkdir,
ignorer = require('../lib/ignorer');
// arguments
var fpath = path.resolve(options.args.pop()),
replacement = null,
query = null,
fileColor = "", textColor = "", matchColor = "";
if (options.args.length == 1)
query = options.args.pop();
else if (options.args.length == 2) {
replacement = options.args.pop();
query = options.args.pop();
}
if (options.color) {
fileColor = '\n\033[36m%s\033[0m';
textColor = '\033[37;43m\1\033[0;90m';
matchColor = '\033[90m%s';
}
options.filesInclude = options.fileSearch ? ignorer.makeWildcardRegExp(options.fileSearch, true) : { test: function(){ return true }};
setExclusions(fpath);
// set the query up, and present a final summary; also, pump out results as they come.
// "streaming" output like this is slower (because console.log blocks)
// but serves a purpose when finding text
if (query) {
makeQuery(query, replacement);
var matches = 0, filecount = 0;
walkdir(fpath, options, function(file, lines, _matches) {
if (!options.color) {
console.log(file);
console.log(lines);
}
else {
console.log(fileColor, file + ":");
lines = lines.replace(options.query, textColor);
console.log(matchColor, lines);
}
matches += _matches;
filecount++;
}, function() {
if (!options.ackmate)
console.log("Found " + matches + (matches == 1 ? " match" : " matches") + " in " + filecount + (filecount == 1 ? " file " : " files "));
});
}
// if we're listing, callback at the very end
else if (options.list) {
walkdir(fpath, options, function(lines) {
console.log(lines);
});
}
function setExclusions(fpath) {
var exclusions = dirExclusions = [];
// if these ignore files don't exist, don't worry about them
try {
if (options.pathToNakignore) {
exclusions = fs.readFileSync(options.pathToNakignore, "utf-8");
}
}
catch (e) { };
try {
dirExclusions = fs.readFileSync(fpath + path.sep + ".nakignore", "utf8");
} catch (e) { };
var combinedExclusions = exclusions + "\n" + dirExclusions + "\n" + (options.ignore !== undefined ? options.ignore : "");
if (combinedExclusions.length) {
options.exclusions = combinedExclusions.split(/\r?\n/).filter(function (e) { return !!e && /^[^#]/.test(e)});
options.exclusionsLength = options.exclusions.length;
}
};
function makeQuery(query, replacement) {
var flags = "g";
if (options.literal)
query = ignorer.escapeRegExp(query);
if (options.ignoreCase)
flags += "i";
if (options.wordRegexp)
query = "\\b(?:" + query + ")\\b";
options.query = new RegExp(query, flags);
if (replacement) {
options.replacement = replacement;
}
options.queryClean = new RegExp(query, flags.substr(1));
}