-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
177 lines (147 loc) · 4.21 KB
/
index.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
var globby = require("globby");
var async = require("async");
var execFile = require("child_process").execFile;
var phpCmd = "php";
var os = require("os");
var crypto = require("crypto");
var fs = require("fs");
var CacheSwap = require("cache-swap");
var SWAP_CATEGORY = "linted";
var CACHE_DIR = "php-lint";
var CACHE_TMP_DIR = os.tmpdir();
function testPhp() {
execFile(phpCmd, ["-v"], function (err, stdout, stderr) {
if (err) throw new Error(err);
});
}
function lint(path, callback) {
return execFile(
phpCmd,
["-d", "display_errors=1", "-l", path],
{
cwd: process.cwd(),
env: process.env,
},
callback
);
}
function iterate(filePaths, options, callback) {
var swap = null;
if (options.useCache) {
swap = new CacheSwap({
cacheDirName: options.cacheDirName,
tmpDir: options.tmpDir,
});
}
async.eachLimit(
filePaths,
options.limit,
function (filePath, next) {
// If no cache then just lint
if (!swap) {
return lint(filePath, function (err, stdout, stderr) {
if (options.stdout) {
if (options.suppress) {
if (stdout.substr(0, 28) !== "No syntax errors detected in") {
process.stdout.write(stdout);
}
} else {
process.stdout.write(stdout);
}
}
if (options.stderr) process.stderr.write(stderr);
next(err);
});
}
// Cache-enabled only code path
var readFile = function (next) {
fs.readFile(filePath, next);
};
var sha1File = function (fileContent, next) {
var sha1 = crypto.createHash("sha1");
var fileSha1 = sha1.update(fileContent.toString()).digest("hex");
return next(null, fileSha1);
};
async.waterfall([readFile, sha1File], function (err, sha1) {
if (err) return next(err);
swap.hasCached(SWAP_CATEGORY, sha1, function (isCached) {
if (isCached) return next();
lint(filePath, function (err, stdout, stderr) {
if (options.stdout) process.stdout.write(stdout);
if (options.stderr) process.stderr.write(stderr);
if (err || stderr) return next(err, stdout, stderr);
swap.addCached(SWAP_CATEGORY, sha1, "", next);
});
});
});
},
function (err, stdout, stderr) {
// all files are done or there was an error
callback(err);
}
);
}
module.exports = {
cli: function (pathsArgs, opt, cb) {
globby(pathsArgs).then(function (paths) {
var options = {
stdout: true,
stderr: true,
limit: 10,
suppress: opt.suppress,
};
var callback = function (err) {
if (cb) return cb(err);
if (err) throw new Error(err);
};
return iterate(paths, options, callback);
});
},
lint: function (files, options, callback) {
if (typeof options === "function") {
callback = options;
options = {
useCache: true,
cacheDirName: CACHE_DIR,
tmpDir: CACHE_TMP_DIR,
};
}
if (!options.limit) options.limit = 10;
if (options.phpCmd) phpCmd = options.phpCmd;
testPhp();
globby(files).then(function (paths) {
iterate(paths, options, callback);
});
},
clearCache: function (cacheDirName, tmpDir, callback) {
if (typeof cacheDirName === "function") {
callback = cacheDirName;
cacheDirName = CACHE_DIR;
tmpDir = CACHE_TMP_DIR;
}
var cache = new CacheSwap({ cacheDirName: cacheDirName, tmpDir: tmpDir });
cache.clear(null, callback);
},
gruntPlugin: function (grunt) {
grunt.task.registerMultiTask(
"phplint",
"Lint PHP files in parallel.",
function () {
var done = this.async();
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
stdout: true,
stderr: true,
limit: 10,
useCache: true,
phpCmd: "php",
cacheDirName: CACHE_DIR,
tmpDir: CACHE_TMP_DIR,
});
if (options.phpCmd) phpCmd = options.phpCmd;
testPhp();
iterate(this.filesSrc, options, done);
}
);
},
};