diff --git a/customFormatExample.json b/customFormatExample.json index 5f216fb..47d35d3 100644 --- a/customFormatExample.json +++ b/customFormatExample.json @@ -3,6 +3,7 @@ "version": "", "description": "", "licenses": "", + "copyright": "", "licenseFile": "none", "licenseText": "none", "licenseModified": "no" diff --git a/lib/index.js b/lib/index.js index 66924f6..1ac3cb5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -178,7 +178,7 @@ var flatten = function(options) { } if (index === 0) { - // Treat the file with the highest precedense as licenseFile + // Treat the file with the highest precedence as licenseFile /*istanbul ignore else*/ if (include("licenseFile")) { moduleInfo.licenseFile = options.basePath ? path.relative(options.basePath, licenseFile) : licenseFile; @@ -195,6 +195,36 @@ var flatten = function(options) { moduleInfo.licenseText = content.replace(/"/g, '\'').replace(/\r?\n|\r/g, " ").trim(); } } + + if(include('copyright') && options.customFormat) { + if (!content) { + content = fs.readFileSync(licenseFile, { encoding: 'utf8' }); + } + + var linesWithCopyright = content + .replace(/\r\n/g, '\n') + .split('\n\n') + .filter(function selectCopyRightStatements(value) { + return value.startsWith('opyright', 1) && // include copyright statements + !value.startsWith('opyright notice', 1) && // exclude lines from from license text + !value.startsWith('opyright and related rights', 1); + }) + .filter(function removeDuplicates(value, index, list) { + return index === 0 || value !== list[0]; + }); + + if(linesWithCopyright.length > 0) { + moduleInfo.copyright = linesWithCopyright[0] + .replace(/\n/g, '. ') + .trim(); + } + + // Mark files with multiple copyright statements. This might be + // an indicator to take a closer look at the LICENSE file. + if(linesWithCopyright.length > 1) { + moduleInfo.copyright += '*'; + } + } } } }); diff --git a/tests/test.js b/tests/test.js index 6df80d9..deae151 100644 --- a/tests/test.js +++ b/tests/test.js @@ -502,6 +502,28 @@ describe('main tests', function() { }); }); + describe('handle copytight statement', function(){ + + it('should output copyright statements when configured in custom format', function(done) { + checker.init({ + start: path.join(__dirname, '../'), + customFormat: { + copyright: '', // specify custom format + email: false, + licenseFile: false, + licenseText: false, + publisher: false + } + }, function(err, output) { + assert(output.hasOwnProperty('abbrev@1.0.9'), 'Check if the expected package still exists.'); + assert.equal(output['abbrev@1.0.9'].copyright, 'Copyright (c) Isaac Z. Schlueter and Contributors'); + done(); + }); + + }); + + }); + describe('should only list UNKNOWN or guessed licenses successful', function() { var output; before(function(done) {