Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Added support for stdin #45
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Jun 4, 2016
1 parent d407d40 commit a11f132
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 119 deletions.
105 changes: 16 additions & 89 deletions lib/cli.js
@@ -1,35 +1,24 @@
'use strict';

var fs = require('fs'),
async = require('async'),
var async = require('async'),
chalk = require('chalk'),
program = require('commander'),
pth = require('path'),
programOptions = require('./options'),
debug = require('./debug'),
dict = require('./dictionary'),
report = require('./report'),
tasks = require('./tasks'),
utils = require('./utils'),
yaspeller = require('./yaspeller'),
_ = require('lodash'),
exitCodes = require('./exit-codes'),
exitCode = 0,
json,
jsonConfig,
jsonDefault = require('../.yaspellerrc.default.json'),
dictionary = [],
settings = {};

programOptions.set({
ignoreTags: jsonDefault.ignoreTags.join(',')
});
programOptions.set({ignoreTags: jsonDefault.ignoreTags.join(',')});

program.parse(process.argv);

if(!program.args.length) {
program.help();
}

jsonConfig = utils.getConfig(program.config);

json = _.assign(jsonDefault, jsonConfig);
Expand All @@ -55,82 +44,20 @@ programOptions.apiOptions.forEach(function(el) {
}
});

dictionary = dict.getDictionary(program.dictionary, json.dictionary);

var tasks = [],
hasData = function(err, data) {
return !err && data && Array.isArray(data.data) && data.data.length;
},
onResource = function(err, data) {
if(hasData(err, data)) {
data.data = dict.removeDictWords(data.data, dictionary);
data.data = yaspeller.removeDuplicates(data.data);
}

if(!exitCode && hasData(err, data)) {
exitCode = exitCodes.HAS_TYPOS;
}

if(err) {
exitCode = exitCodes.ERROR_LOADING;
}

report.oneach(err, data);
};
dict.set(program.dictionary, json.dictionary);

report.addReports(program.report || json.report);

program.args.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();
}
}
});
});
if(process.stdin.isTTY && !program.args.length) {
program.help();
}

async.series(tasks, function() {
report.onend();
process.exit(exitCode);
});
async.series(
process.stdin.isTTY ?
tasks.forResources(program.args, settings) :
tasks.forStdin(settings),
function() {
report.onend();
process.exit();
}
);
22 changes: 15 additions & 7 deletions lib/dictionary.js
Expand Up @@ -11,13 +11,12 @@ var chalk = require('chalk'),

module.exports = {
/**
* Get dictionary.
* Set dictionary.
*
* @param {Array} files
* @param {Array} configDictionary - Dictionary from .yaspellerrc
* @return {Array}
*/
getDictionary: function(files, configDictionary) {
set: function(files, configDictionary) {
var result = [],
count = 0,
commonUniqueWords = [],
Expand Down Expand Up @@ -45,8 +44,17 @@ module.exports = {
this.checkDuplicates(commonUniqueWords, 'Duplicate words in dictionaries:');
}

return this.prepareDictionary(result);
this._dict = this.prepareDictionary(result);
},
/**
* Get dictionary.
*
* @return {Array}
*/
get: function() {
return this._dict;
},
_dict: [],
/**
* Load dictionary.
*
Expand Down Expand Up @@ -116,11 +124,11 @@ module.exports = {
* Remove typos that is in the dictionary.
*
* @param {Object[]} data - Array of typos.
* @param {string[]|RegExp[]} dictionary
* @return {Object[]}
*/
removeDictWords: function(data, dictionary) {
var result = [];
removeDictWords: function(data) {
var result = [],
dictionary = this.get();

data.forEach(function(typo) {
if((typo.code !== 1 && typo.code !== 3) || this.isTypo(typo.word, dictionary)) {
Expand Down
123 changes: 123 additions & 0 deletions lib/tasks.js
@@ -0,0 +1,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);
});
}];
}
};

0 comments on commit a11f132

Please sign in to comment.