Skip to content

Commit

Permalink
Correct tets & add CLi script
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthD committed May 26, 2016
1 parent abe5daf commit d80bc3f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
123 changes: 123 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const Tqi = require('./src/tqi'),
fs = require('fs'),
path = require('path'),
kuler = require('kuler'),
args = require('args'),
async = require('async');

var tqi = new Tqi();

const options = args.Options.parse([
{
name: 'help',
shortName: 'h',
help: 'Get Help',
defaultValue : null,
type : "bool",
required : false
},
{
name: 'input',
shortName: 'i',
help: 'Input folder or txt file',
defaultValue : null,
required : true
},
{
name: 'dic',
shortName: 'd',
help: 'Path of a classic dictionnary file',
defaultValue : null,
required : false
},
{
name: 'aff',
shortName: 'a',
help: 'Path of an affix dictionnary',
defaultValue : null,
required : false
},
{
name: 'log',
shortName: 'l',
help: 'Log all results into log file',
defaultValue : 'log.txt',
required : false
}
]);

/* ----------- */
/* CHECK ARGS */
/* ----------- */

const parsed = args.parser(process.argv).parse(options);


if(parsed.help){
// shows help
console.info(options.getHelp());
return;
}

if(!parsed.input){
console.info(kuler('Please indicate Input folder or txt file , see help' , 'red'));
return;
}

/* ----------- */
/* EXECUTION */
/* ----------- */

fs.stat(parsed.input, (err,stats)=>{
if(err){
console.info(kuler('Input file/folder does not exist', 'red'));
return;
}

// If it's an existing file.
if(stats.isFile()){
console.log(kuler('File detected : \r' , 'cyan'));
fs.readFile(path.resolve(parsed.input), 'utf8', (err,data)=>{
tqi.analyze(data).then((result) => {
console.log(parsed.input , "=>", result);
});
});
}
// If it's an existing file.
if(stats.isDirectory()){
const filesResults = {
valid : 0,
error : 0,
rate : 0,
nbOfFiles : 0
};
console.log(kuler('Folder detected ... \r' , 'cyan'));
fs.readdir(parsed.input, (err, files) =>{
async.each(files, (file,next) =>{
var pathFile = path.resolve(parsed.input + '/' + file);
fs.readFile(pathFile, 'utf8', (err,data)=>{
tqi.analyze(data).then((result) => {
filesResults.valid += result.valid;
filesResults.error += result.error;
var subResult = file + '=>' + JSON.stringify(result) + '\n';
fs.appendFile(parsed.log, subResult , (err) =>{
if(!err){
console.log(file , '=>', result);
filesResults.nbOfFiles++;
return next();
}
console.error(err);
});
});
});
},(err) =>{
filesResults.rate = filesResults.valid / (filesResults.error + filesResults.valid) * 100;
fs.appendFile(parsed.log, JSON.stringify(filesResults) + '\n' , (err) =>{
if(!err){
console.info(kuler('Number of files processed : ' + filesResults.nbOfFiles + ', rate : ' + filesResults.rate + '%', 'green'));
}
});
});
});
}
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"test": "test"
},
"dependencies": {
"args": "0.0.3",
"async": "^1.5.2",
"kuler": "0.0.0",
"natural": "^0.4.0",
"nodehun": "^2.0.10"
},
Expand Down
3 changes: 3 additions & 0 deletions test/tqi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ describe(pkg.name + '/src/tqi.js', () => {
});
describe('#Analyze', () => {
it('Analyze must return exact int numbers', function() {
this.timeout = 5500;
return tqi.analyze(enBig).then(function(result){
expect(result.valid).to.be.a("number");
expect(result.error).to.be.a("number");
expect(result.rate).to.be.a("number");
expect(result.valid + result.error).to.be.equal(1454145);
});
});
});
Expand Down

0 comments on commit d80bc3f

Please sign in to comment.