Skip to content

Commit

Permalink
Added licenseStart and licenseEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
mikayla-maki committed Feb 3, 2023
1 parent e67b6fc commit fe3182e
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 4 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 :)

Expand Down Expand Up @@ -212,6 +214,28 @@ license-checker-rseidelsohn --excludePackages 'internal-1;internal-2'
license-checker-rseidelsohn --onlyunknown
```

<a name="clarifications"/>

## 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": "=========",
}
}
```

<a name="custom_format"/>

## Custom format
Expand Down
2 changes: 1 addition & 1 deletion bin/license-checker-rseidelsohn
Original file line number Diff line number Diff line change
Expand Up @@ -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 :)',
Expand Down
14 changes: 14 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
64 changes: 62 additions & 2 deletions tests/clarificationFile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '../'),
});

Expand All @@ -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, '../'),
});

Expand All @@ -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();
});
})
});
9 changes: 9 additions & 0 deletions tests/fixtures/clarifications/README
Original file line number Diff line number Diff line change
@@ -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 :(
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"license-checker-rseidelsohn@0.0.0": {
"licenses": "MIT",
"licenseFile": "MISMATCH_LICENSE",
"licenseFile": "mismatch/MISMATCH_LICENSE",
"checksum": "71eb00f862028a6d940c742dfa96e8f5116823cc5be0b7fb4a640072d2080186"
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/clarifications/weirdStart/clarification.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"license-checker-rseidelsohn@0.0.0": {
"licenses": "MIT",
"licenseStart": "# LICENSE",
"licenseEnd": "# And one more thing..."
}
}
13 changes: 13 additions & 0 deletions tests/fixtures/clarifications/weirdStart/customFormat.json
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"license-checker-rseidelsohn@0.0.0": {
"licenses": "MIT",
"licenseStart": "# LICENSE"
}
}

0 comments on commit fe3182e

Please sign in to comment.