From 7e80d50c733f8ee42c7b4a79dc3344b3dd330cff Mon Sep 17 00:00:00 2001
From: Tim Oxley
Date: Thu, 4 Dec 2014 15:44:58 +0800
Subject: [PATCH] Add --out flag for writing an output file.
---
bin/license-checker | 14 +++++++++++---
lib/args.js | 1 +
lib/index.js | 6 +++++-
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/bin/license-checker b/bin/license-checker
index a1e385b..9af2271 100755
--- a/bin/license-checker
+++ b/bin/license-checker
@@ -14,8 +14,16 @@ var path = require('path');
var fs = require('fs');
checker.init(args, function(json) {
- if (args.json) return console.log(JSON.stringify(json, null, 2))
- if (args.csv) return console.log(checker.asCSV(json))
+ var formattedOutput = '';
+ if (args.json) formattedOutput = JSON.stringify(json, null, 2);
+ else if (args.csv) formattedOutput = checker.asCSV(json);
+ else formattedOutput = checker.asTree(json);
- checker.print(json);
+ if (args.out) {
+ var dir = path.dirname(args.out);
+ mkdirp.sync(dir);
+ fs.writeFileSync(args.out, formattedOutput, 'utf8');
+ } else {
+ console.log(formattedOutput);
+ }
});
diff --git a/lib/args.js b/lib/args.js
index 2040e4c..78b68a6 100644
--- a/lib/args.js
+++ b/lib/args.js
@@ -9,6 +9,7 @@ var nopt = require('nopt'),
known = {
json: Boolean,
csv: Boolean,
+ out: require('path'),
unknown: Boolean,
version: Boolean,
color: Boolean,
diff --git a/lib/index.js b/lib/index.js
index 121d969..3d49731 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -135,7 +135,11 @@ exports.init = function(options, callback) {
};
exports.print = function(sorted) {
- console.log(treeify.asTree(sorted, true));
+ console.log(exports.asTree(sorted));
+};
+
+exports.asTree = function(sorted) {
+ return treeify.asTree(sorted, true);
};
exports.asCSV = function(sorted) {