This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
forked from unic/gulp-accessibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (57 loc) · 1.84 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
//
// Gulp AccessSniff Task
// ------------------------
// through2 is a thin wrapper around node transform streams
const _ = require('lodash');
const gutil = require('gulp-util');
const through = require('through2');
const accessSniff = require('access-sniff');
const StringDecoder = require('string_decoder').StringDecoder
const PluginError = gutil.PluginError;
// Consts
const PLUGIN_NAME = 'gulp-accessibility';
var accessSniffPlugin = function(options) {
const gulpOptions = options ? options : {};
// Creating a stream through which each file will pass
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
cb(null, file);
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Cannot read streams');
}
if (file.isBuffer()) {
return accessSniff
.default(file.path, gulpOptions)
.then(function(response) {
file.contents = new Buffer(JSON.stringify(response));
return callback(null, file);
})
.catch(function(error) {
var error = new Error(error);
return callback(error, file);
});
}
});
}
accessSniffPlugin.report = function(options) {
const gulpOptions = options ? options : {};
const decoder = new StringDecoder('utf8');
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
cb(null, file);
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Cannot read streams');
}
if (file.isBuffer()) {
const fileContents = decoder.write(file.contents);
const jsonContent = JSON.parse(fileContents);
var reportData = accessSniff.report(jsonContent, gulpOptions);
file.contents = new Buffer(reportData);
return callback(null, file);
}
});
};
// Exporting the plugin main function
module.exports = accessSniffPlugin;