'use strict';

var fs = require('fs');
var path = require('path');
var dir = require('node-dir');
var q = require('q');
var cli = require('commander');

var getLicenseType = function (info) {
    if (info.licenses) {
        if (typeof info.licenses[0] === 'object') {
            return info.licenses[0].type;
        }

        return info.licenses[0];
    }

    if (info.license) {
        if (typeof info.license === 'object') {
            return info.license.type;
        }

        return info.license;
    }

    return '';
};

var findLicenses = function () {
    var deferred = q.defer();

    var modulesDir = path.join(process.cwd(), 'node_modules');

    dir.files(modulesDir, function (err, files) {
        var licenses = [];

        files.forEach(function (file) {
            if (/license/gi.test(file)) {
                var currPath = file.split(path.sep);
                var currModule = currPath[currPath.lastIndexOf('node_modules') + 1];

                var moduleInfo = {};

                try {
                    moduleInfo = require(path.join(path.dirname(file), 'package.json'));
                } catch (e) {
                    console.log('No package.json found for ' + currModule + '.');
                }

                var licenseText = fs.readFileSync(file, 'utf8');

                licenses.push({
                    'module': currModule,
                    'license': getLicenseType(moduleInfo),
                    'license_file': file,
                    'content': licenseText
                });
            }
        });

        deferred.resolve(licenses);
    });

    return deferred.promise;
};

var createReadable = function (licenses) {
    var text = '';

    licenses.forEach(function (license) {
        text += 'Module: ' + license.module + '\n';
        text += 'License: ' + license.license + '\n';
        text += 'License Text:\n' + license.content + '\n\n';
    });

    return text;
};

cli
    .version('0.0.1')
    .option('-t, --text', 'Output readable text')
    .parse(process.argv);

if (cli.text) {
    findLicenses().then(function (data) {
        fs.writeFile(path.join(process.cwd(), 'licensure.txt'), createReadable(data), function (err) {
            if (err) {
                throw err;
            }

            console.log('License information store in licensure.txt.');
        });
    });
} else {
    findLicenses().then(function (data) {
        fs.writeFile(path.join(process.cwd(), 'licensure.json'), JSON.stringify(data, null, 4), function (err) {
            if (err) {
                throw err;
            }

            console.log('License information stored in licensure.json.');
        });
    });
}

@@ -560,16 +560,34 @@
 nction (
+includeLicenseText
 ) %7B%0A    
@@ -1248,32 +1248,118 @@
              %7D%0A%0A
+                var licenseText = '';%0A%0A                if (!includeLicenseText) %7B%0A    
                 
@@ -1406,16 +1406,34 @@
 'utf8');
+%0A                %7D
 %0A%0A      
@@ -1972,32 +1972,75 @@
 license + '%5Cn';%0A
+%0A        if (license.content !== '') %7B%0A    
         text += 
@@ -2080,16 +2080,47 @@
 nt + '%5Cn
+';%0A        %7D%0A%0A        text += '
 %5Cn';%0A   
@@ -2222,16 +2222,83 @@
  text')%0A
+    .option('-l, --license-text', 'Include license file contents')%0A
     .par
@@ -2341,32 +2341,47 @@
    findLicenses(
+cli.licenseText
 ).then(function 
@@ -2675,16 +2675,31 @@
 icenses(
+cli.licenseText
 ).then(f

Add ability to optionally exclude license contents