Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent mode #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Options
* `--csv /path/to/save.csv`: export the data as comma-separated values to the given file.
The path will be created if it does not exist.

* `--silent`: No console output

Example
-------

Expand Down
43 changes: 25 additions & 18 deletions lib/directoryreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ var fs = require('fs'),
module.exports = DirectoryReader;


function DirectoryReader(dirs, excludeDirs) {
function DirectoryReader({ start, exclude, silent }) {
var self = this;

events.EventEmitter.call(self);
self.dirStack = [];
self.excludeDirs = excludeDirs || [];
self.exclude = exclude || [];
self.silent = silent;

process.nextTick(function () {
try {
if (typeof self.excludeDirs === "string" || self.excludeDirs instanceof String) {
self.excludeDirs = [ path.resolve(self.excludeDirs) ]
if (typeof self.exclude === "string" || self.exclude instanceof String) {
self.exclude = [ path.resolve(self.exclude) ]
}
for (var i=0; i < self.excludeDirs.length; ++i) {
self.excludeDirs[i] = path.resolve(self.excludeDirs[i]);
var stat = fs.lstatSync(self.excludeDirs[i]);
for (var i=0; i < self.exclude.length; ++i) {
self.exclude[i] = path.resolve(self.exclude[i]);
var stat = fs.lstatSync(self.exclude[i]);
if (! stat.isDirectory()) {
throw new Error("Path " + self.excludeDirs[i] + " is not a directory");
throw new Error("Path " + self.exclude[i] + " is not a directory");
}
}
}
Expand All @@ -36,18 +37,22 @@ function DirectoryReader(dirs, excludeDirs) {
}


if (typeof dirs === "string" || dirs instanceof String) {
dirs = [ path.resolve(dirs ||'.') ]
if (typeof start === "string" || start instanceof String) {
start = [ path.resolve(start ||'.') ]
}
async.eachSeries(dirs, function(dir, dirIterator) {
async.eachSeries(start, function(dir, dirIterator) {
self.currentFilePath = path.resolve(dir);

if (! self._isExcludedDir()) {
console.log("include dir", self.currentFilePath);
if (!self.silent) {
console.log("include dir", self.currentFilePath);
}
self._pushDir(dirIterator, dirIterator);
}
else {
console.log("exclude dir", self.currentFilePath);
if (!self.silent) {
console.log("exclude dir", self.currentFilePath);
}
dirIterator();
}
},
Expand Down Expand Up @@ -99,11 +104,11 @@ DirectoryReader.prototype._cwd = function() {


DirectoryReader.prototype._isExcludedDir = function() {
var excludeDirs = this.excludeDirs,
var exclude = this.exclude,
dir = path.resolve(this.currentFilePath);

for (var i=0; i<excludeDirs.length;++i) {
if (dir.indexOf(excludeDirs[i]) === 0) {
for (var i=0; i<exclude.length;++i) {
if (dir.indexOf(exclude[i]) === 0) {
return true;
}
}
Expand Down Expand Up @@ -137,7 +142,9 @@ DirectoryReader.prototype.next = function() {
self.emit("dir", file, stat, self.currentFilePath);
}
else {
console.log("exclude dir", self.currentFilePath);
if (!self.silent) {
console.log("exclude dir", self.currentFilePath);
}
self.next();
}
}
Expand Down
16 changes: 11 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var path = require('path'),


exports.dumpLicenses = function(args, callback) {
var reader = new DirectoryReader(args.start, args.exclude),
var reader = new DirectoryReader({ start: args.start, exclude: args.exclude , silent: args.silent}),
licenses = {},
filePaths = [],
atSign = '@';
Expand Down Expand Up @@ -66,7 +66,7 @@ exports.dumpLicenses = function(args, callback) {

}, function (error) {
if (error) {
console.log(error);
console.log(error); // throw ? reject promise ?
}
else {
var result = {};
Expand All @@ -92,13 +92,17 @@ exports.dumpLicenses = function(args, callback) {
dir = path.dirname(args.json);
mkdirp.sync(dir);
fs.writeFileSync(args.json, JSON.stringify(result, null, 4) + '\n', 'utf8');
console.log('file written', args.json);
if (!args.silent) {
console.log('file written', args.json);
}
}
if (args.csv) {
dir = path.dirname(args.csv);
mkdirp.sync(dir);
fs.writeFileSync(args.csv, checker.asCSV(result, args), 'utf8');
console.log('file written', args.csv);
if (!args.silent) {
console.log('file written', args.csv);
}
}
}
else if (! args.gulp) {
Expand All @@ -122,7 +126,9 @@ exports.dumpLicenses = function(args, callback) {
}
checker.print(result);
}
console.log("Number of entries found:", Object.keys(result).length);
if (!args.silent) {
console.log("Number of entries found:", Object.keys(result).length);
}
}
if (! error && args.gulp) {
callback(error, treeify.asTree(result, true));
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var vows = require('vows'),
},
reader: {
topic: function() {
var reader = new DirectoryReader(".."),
var reader = new DirectoryReader({ start: ".." }),
result = {
found: false,
dirs: 0,
Expand Down Expand Up @@ -49,4 +49,4 @@ var vows = require('vows'),
}
};

vows.describe('dumplicenses').addBatch(tests).export(module);
vows.describe('dumplicenses').addBatch(tests).export(module);