-
Notifications
You must be signed in to change notification settings - Fork 67
/
util.js
86 lines (69 loc) · 1.75 KB
/
util.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
var isString = require('lodash/isString');
var jshint = require('../');
var Vinyl = require('vinyl');
var join = require('path').join;
var extname = require('path').extname;
var fs = require('fs');
var ROOT = join(__dirname, '..');
var array = function (obj) {
return Array.isArray(obj) ? obj : [obj];
};
function File(opts) {
if (!(this instanceof File)) return new File(opts);
opts = opts || {};
if (isString(opts)) {
opts = { path: opts.replace(/^fixture\//, 'test/fixtures/') };
}
var path = join(ROOT, opts.path);
var contents = opts.contents
? new Buffer(opts.contents, 'utf8')
: fs.readFileSync(path);
return new Vinyl({
cwd: opts.cwd || ROOT,
base: opts.base || ROOT,
path: path,
contents: contents
});
}
function RcFile(path) {
return JSON.parse(new File(path).contents.toString('utf8'));
}
function Fixture(name) {
if (!extname(name)) name += '.js';
return new File('fixture/' + name);
}
function RcFixture(name) {
return new RcFile('fixture/' + name);
}
function lint(opts, cb) {
var dataEvents = 0;
opts.files = array(opts.file || opts.files);
opts.args = array(opts.config || opts.args);
var done = function (err) {
stream.removeAllListeners('end');
stream.removeAllListeners('error');
cb(err);
};
var stream = jshint.apply(null, opts.args);
stream.on('data', function (file) {
dataEvents++;
return opts.eachFile(file);
});
stream.once('end', function () {
dataEvents.should.equal(opts.files.length);
done();
});
stream.once('error', done);
opts.files.forEach(function (file) {
stream.write(file);
});
stream.end();
return stream;
}
module.exports = {
File: File,
RcFile: RcFile,
Fixture: Fixture,
RcFixture: RcFixture,
lint: lint
};