This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
tasks.js
123 lines (110 loc) 路 3.93 KB
/
tasks.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
'use strict';
var async = require('async'),
fs = require('fs'),
pth = require('path'),
dict = require('./dictionary'),
exitCodes = require('./exit-codes'),
report = require('./report'),
utils = require('./utils'),
yaspeller = require('./yaspeller');
function hasData(err, data) {
return !err && data && Array.isArray(data.data) && data.data.length;
}
function onResource(err, data) {
if(hasData(err, data)) {
data.data = dict.removeDictWords(data.data);
data.data = yaspeller.removeDuplicates(data.data);
}
if(!process.exitCode && hasData(err, data)) {
process.exitCode = exitCodes.HAS_TYPOS;
}
if(err) {
process.exitCode = exitCodes.ERROR_LOADING;
}
report.oneach(err, data);
}
module.exports = {
/**
* Prepare tasks for resources.
*
* @param {Array} resources
* @param {Object} settings
* @return {Array}
*/
forResources: function(resources, settings) {
var tasks = [];
resources.forEach(function(resource) {
tasks.push(function(cb) {
var subTasks = [];
if(utils.isUrl(resource)) {
if(utils.isSitemap(resource)) {
yaspeller.checkSitemap(resource, function() {
cb();
}, settings, onResource);
} else {
yaspeller.checkUrl(resource, function(err, data) {
onResource(err, data);
cb();
}, settings);
}
} else {
if(fs.existsSync(resource)) {
if(utils.isDir(resource)) {
utils
.findFiles(resource, settings.fileExtensions, settings.excludeFiles)
.forEach(function(file) {
subTasks.push(function(subcb) {
yaspeller.checkFile(file, function(err, data) {
onResource(err, data);
subcb();
}, settings);
});
});
async.parallelLimit(subTasks, settings.maxRequests, function() {
cb();
});
} else {
var file = pth.resolve(resource);
if(utils.isExcludedFile(file, settings.excludeFiles)) {
cb();
} else {
yaspeller.checkFile(file, function(err, data) {
onResource(err, data);
cb();
}, settings);
}
}
} else {
onResource(true, Error(resource + ': is not exists'));
cb();
}
}
});
});
return tasks;
},
/**
* Prepare task for stdin.
*
* @param {Object} settings
* @return {Array}
*/
forStdin: function(settings) {
return [function(cb) {
var text = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if(chunk !== null) {
text += chunk;
}
});
process.stdin.on('end', function() {
yaspeller.checkText(text, function(err, data) {
onResource(err, data);
cb();
}, settings);
});
}];
}
};