diff --git a/README.md b/README.md index b790f62..047f592 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ - [All options in alphabetical order:](#all-options-in-alphabetical-order) - [Exclusions](#exclusions) - [Examples](#examples) + - [Clarifications](#clarifications) - [Custom format](#custom-format) - [Requiring](#requiring) - [Debugging](#debugging) @@ -183,6 +184,7 @@ before. - `--start [filepath]` path of the initial json to look for - `--summary` output a summary of the license usage', - `--unknown` report guessed licenses as unknown licenses. +- `--clarificationsFile` A file that describe the license clarifications for each package, see clarificationExample.json, any field available to the customFormat option can be clarified. The clarifications file can also be used to specify a subregion of a package's license file (instead reading the entire file). - `--version` The current version - `--help` The text you are reading right now :) @@ -212,6 +214,28 @@ license-checker-rseidelsohn --excludePackages 'internal-1;internal-2' license-checker-rseidelsohn --onlyunknown ``` + + +## Clarifications + +The `--clarificationsFile` option can be used to provide custom processing instructions on a per-package basis. The format is as so: + +```json +{ + "package_name@version": { + // Any field available in customFormat can be clarified + "licenses": "MIT", + "licenseFile": "some/path", + "licenseText": "The full text of the license to include if you need" + // You can optionally add a SH-256 checksum of the license file contents that will be checked on each run. Intended to help detect when projects change their license. + "checksum": "deadbeef...", + // Add a licenseStart and optional licenseEnd to snip out a substring of the licenseText. The licenseStart will be included in the licenseText, the licenseEnd will not be. + "licenseStart": "# MIT License", + "licenseEnd": "=========", + } +} +``` + ## Custom format diff --git a/bin/license-checker-rseidelsohn b/bin/license-checker-rseidelsohn index 746882e..250b9c2 100755 --- a/bin/license-checker-rseidelsohn +++ b/bin/license-checker-rseidelsohn @@ -47,7 +47,7 @@ const usageMessage = [ ' --start [filepath] path of the initial json to look for', ' --summary output a summary of the license usage', ' --unknown report guessed licenses as unknown licenses.', - ' --clarificationsFile A file that describe the license clarifications for each package, see clarificationExample.json, any field available to the customFormat option can be clarified.', + ' --clarificationsFile A file that describe the license clarifications for each package, see clarificationExample.json, any field available to the customFormat option can be clarified. Can also be used to specify a subregion of a package\'s license file (instead reading the entire file).', '', ' --version The current version', ' --help The text you are reading right now :)', diff --git a/lib/index.js b/lib/index.js index 4972ca8..fbe72a2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -248,6 +248,20 @@ const flatten = function flatten(options) { .trim(); } } + + let start_index; + let end_index; + if (clarification?.licenseStart) { + start_index = moduleInfo.licenseText.indexOf(clarification.licenseStart); + + if (clarification?.licenseEnd) { + end_index = moduleInfo.licenseText.indexOf(clarification.licenseEnd, start_index); + } else { + end_index = moduleInfo.licenseText.length; + } + + moduleInfo.licenseText = moduleInfo.licenseText.substring(start_index, end_index); + } } if (mustInclude('copyright') && options.customFormat) { diff --git a/tests/clarificationFile-test.js b/tests/clarificationFile-test.js index 75cc3ae..559f272 100644 --- a/tests/clarificationFile-test.js +++ b/tests/clarificationFile-test.js @@ -45,7 +45,7 @@ describe('clarifications', function() { it('should exit 1 if the checksum does not match', function(done) { let data = ""; - let license_checker = spawn('node', [path.join(__dirname, '../bin/license-checker-rseidelsohn'), '--start', path.join(__dirname, clarifications_path), '--clarificationsFile', path.join(__dirname, clarifications_path, 'mismatchFile.json')], { + let license_checker = spawn('node', [path.join(__dirname, '../bin/license-checker-rseidelsohn'), '--start', path.join(__dirname, clarifications_path), '--clarificationsFile', path.join(__dirname, clarifications_path, 'mismatch/clarification.json')], { cwd: path.join(__dirname, '../'), }); @@ -64,7 +64,7 @@ describe('clarifications', function() { it('should succeed if no checksum is specified', function(done) { let data = ""; - let license_checker = spawn('node', [path.join(__dirname, '../bin/license-checker-rseidelsohn'), '--start', path.join(__dirname, clarifications_path), '--clarificationsFile', path.join(__dirname, clarifications_path, 'noChecksum.json')], { + let license_checker = spawn('node', [path.join(__dirname, '../bin/license-checker-rseidelsohn'), '--start', path.join(__dirname, clarifications_path), '--clarificationsFile', path.join(__dirname, clarifications_path, 'example/noChecksum.json')], { cwd: path.join(__dirname, '../'), }); @@ -79,4 +79,64 @@ describe('clarifications', function() { done(); }); }) + + it('should sniip the embedded license out of the README', function(done) { + let data = ""; + + let license_checker = spawn( + 'node', + [ + path.join(__dirname, '../bin/license-checker-rseidelsohn'), + '--start', path.join(__dirname, clarifications_path), + '--clarificationsFile', path.join(__dirname, clarifications_path, 'weirdStart/clarification.json'), + '--customPath', path.join(__dirname, clarifications_path, 'weirdStart/customFormat.json') + ], { + cwd: path.join(__dirname, '../'), + }); + + license_checker.stdout.on("data", function(stdout) { + data += stdout.toString(); + }) + + license_checker.on('exit', function(code) { + assert.equal(code, 0); + assert.equal(data.includes("README"), true) + assert.equal(data.includes("text text text describing the project"), false) + assert.equal(data.includes("# LICENSE"), true) + assert.equal(data.includes("Standard MIT license"), true) + assert.equal(data.includes("# And one more thing..."), false) + assert.equal(data.includes("More text AFTER the license because the real world is difficult :("), false) + done(); + }); + }) + + it('should snip the embedded license in the README to the end.', function(done) { + let data = ""; + + let license_checker = spawn( + 'node', + [ + path.join(__dirname, '../bin/license-checker-rseidelsohn'), + '--start', path.join(__dirname, clarifications_path), + '--clarificationsFile', path.join(__dirname, clarifications_path, 'weirdStart/startOnlyClarification.json'), + '--customPath', path.join(__dirname, clarifications_path, 'weirdStart/customFormat.json') + ], { + cwd: path.join(__dirname, '../'), + }); + + license_checker.stdout.on("data", function(stdout) { + data += stdout.toString(); + }) + + license_checker.on('exit', function(code) { + assert.equal(code, 0); + assert.equal(data.includes("README"), true) + assert.equal(data.includes("text text text describing the project"), false) + assert.equal(data.includes("# LICENSE"), true) + assert.equal(data.includes("Standard MIT license"), true) + assert.equal(data.includes("# And one more thing..."), true) + assert.equal(data.includes("More text AFTER the license because the real world is difficult :("), true) + done(); + }); + }) }); diff --git a/tests/fixtures/clarifications/README b/tests/fixtures/clarifications/README new file mode 100644 index 0000000..8fc5abe --- /dev/null +++ b/tests/fixtures/clarifications/README @@ -0,0 +1,9 @@ +text text text describing the project + +# LICENSE + +Standard MIT license + +# And one more thing... + +More text AFTER the license because the real world is difficult :( \ No newline at end of file diff --git a/tests/fixtures/clarifications/noChecksum.json b/tests/fixtures/clarifications/example/noChecksum.json similarity index 100% rename from tests/fixtures/clarifications/noChecksum.json rename to tests/fixtures/clarifications/example/noChecksum.json diff --git a/tests/fixtures/clarifications/MISMATCH_LICENSE b/tests/fixtures/clarifications/mismatch/MISMATCH_LICENSE similarity index 100% rename from tests/fixtures/clarifications/MISMATCH_LICENSE rename to tests/fixtures/clarifications/mismatch/MISMATCH_LICENSE diff --git a/tests/fixtures/clarifications/mismatchFile.json b/tests/fixtures/clarifications/mismatch/clarification.json similarity index 76% rename from tests/fixtures/clarifications/mismatchFile.json rename to tests/fixtures/clarifications/mismatch/clarification.json index bedeb8b..cab5e08 100644 --- a/tests/fixtures/clarifications/mismatchFile.json +++ b/tests/fixtures/clarifications/mismatch/clarification.json @@ -1,7 +1,7 @@ { "license-checker-rseidelsohn@0.0.0": { "licenses": "MIT", - "licenseFile": "MISMATCH_LICENSE", + "licenseFile": "mismatch/MISMATCH_LICENSE", "checksum": "71eb00f862028a6d940c742dfa96e8f5116823cc5be0b7fb4a640072d2080186" } } \ No newline at end of file diff --git a/tests/fixtures/clarifications/weirdStart/clarification.json b/tests/fixtures/clarifications/weirdStart/clarification.json new file mode 100644 index 0000000..9e949b2 --- /dev/null +++ b/tests/fixtures/clarifications/weirdStart/clarification.json @@ -0,0 +1,7 @@ +{ + "license-checker-rseidelsohn@0.0.0": { + "licenses": "MIT", + "licenseStart": "# LICENSE", + "licenseEnd": "# And one more thing..." + } +} \ No newline at end of file diff --git a/tests/fixtures/clarifications/weirdStart/customFormat.json b/tests/fixtures/clarifications/weirdStart/customFormat.json new file mode 100644 index 0000000..938708b --- /dev/null +++ b/tests/fixtures/clarifications/weirdStart/customFormat.json @@ -0,0 +1,13 @@ +{ + "licenses": true, + "licenseText": true, + "name": false, + "version": false, + "description": false, + "copyright": false, + "licenseFile": true, + "licenseModified": false, + "publisher": false, + "email": false, + "path": false +} diff --git a/tests/fixtures/clarifications/weirdStart/startOnlyClarification.json b/tests/fixtures/clarifications/weirdStart/startOnlyClarification.json new file mode 100644 index 0000000..be4c6d3 --- /dev/null +++ b/tests/fixtures/clarifications/weirdStart/startOnlyClarification.json @@ -0,0 +1,6 @@ +{ + "license-checker-rseidelsohn@0.0.0": { + "licenses": "MIT", + "licenseStart": "# LICENSE" + } +} \ No newline at end of file