From d80bc3f7834b3d637d666b71f44a38df836f8882 Mon Sep 17 00:00:00 2001 From: mattd Date: Thu, 26 May 2016 11:46:00 +0200 Subject: [PATCH] Correct tets & add CLi script --- cli.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + test/tqi-test.js | 3 ++ 3 files changed, 128 insertions(+) create mode 100644 cli.js diff --git a/cli.js b/cli.js new file mode 100644 index 0000000..4c5b914 --- /dev/null +++ b/cli.js @@ -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')); + } + }); + }); + }); + } +}); \ No newline at end of file diff --git a/package.json b/package.json index 5ecf5c7..b9c7320 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/tqi-test.js b/test/tqi-test.js index 78176aa..3ddc444 100644 --- a/test/tqi-test.js +++ b/test/tqi-test.js @@ -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); }); }); });