Skip to content

Commit

Permalink
Add support for ignoring final definitions
Browse files Browse the repository at this point in the history
Closes GH-7.
  • Loading branch information
wooorm committed Aug 10, 2018
1 parent 9d689f1 commit 719bcbd
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 9 deletions.
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ var https = 'https://'

/* Add a license section. */
function license(options) {
if (!options) {
options = {}
var opts = options || {}
var finals = opts.ignoreFinalDefinitions
var headingOptions = {
ignoreFinalDefinitions:
finals === undefined || finals === null ? true : finals,
test: licenseHeadingRegexp
}

return transformer
Expand All @@ -36,13 +40,13 @@ function license(options) {
var defaultLicenseFile

// Skip package loading if we have all info in `options`.
if (options.url && options.name && options.license) {
if (opts.url && opts.name && opts.license) {
one()
} else {
fs.readFile(path.resolve(cwd, 'package.json'), onpackage)
}

if (options.file) {
if (opts.file) {
one()
} else {
fs.readdir(cwd, onfiles)
Expand Down Expand Up @@ -106,10 +110,10 @@ function license(options) {
}

function done() {
var url = options.url || defaultUrl
var name = options.name || defaultName
var license = options.license || defaultLicense
var licenseFile = options.file || defaultLicenseFile
var url = opts.url || defaultUrl
var name = opts.name || defaultName
var license = opts.license || defaultLicense
var licenseFile = opts.file || defaultLicenseFile

/* Ignore the license file itself. */
if (licenseFile && file.path === licenseFile) {
Expand Down Expand Up @@ -140,7 +144,7 @@ function license(options) {
licenseFile = spdx[license].url
}

heading(tree, licenseHeadingRegexp, onheading)
heading(tree, headingOptions, onheading)

next()

Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ If there is no given or found license file, but `options.license` is a known
and `string`][author-format] format of `author`. `http://` is prepended if
`url` starts without HTTP or HTTPS protocol.

###### `options.ignoreFinalDefinitions`

Ignore final definitions otherwise in the section (`boolean`, default: true).

## Detection

Detection of `package.json` and files in the current working directory is
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/definitions-ignore-false/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignoreFinalDefinitions": false
}
5 changes: 5 additions & 0 deletions test/fixtures/definitions-ignore-false/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [PI][]

## License

MIX © Alpha Bravo
4 changes: 4 additions & 0 deletions test/fixtures/definitions-ignore-false/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"license": "MIX",
"author": "Alpha Bravo"
}
5 changes: 5 additions & 0 deletions test/fixtures/definitions-ignore-false/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [PI][]

## License

[pi]: https://en.wikipedia.org/wiki/Pi
7 changes: 7 additions & 0 deletions test/fixtures/definitions/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# [PI][]

## License

MIX © Alpha Bravo

[pi]: https://en.wikipedia.org/wiki/Pi
4 changes: 4 additions & 0 deletions test/fixtures/definitions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"license": "MIX",
"author": "Alpha Bravo"
}
5 changes: 5 additions & 0 deletions test/fixtures/definitions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [PI][]

## License

[pi]: https://en.wikipedia.org/wiki/Pi
7 changes: 7 additions & 0 deletions test/fixtures/other-headings/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# PI

## License

MIX © Alpha Bravo

## Other
4 changes: 4 additions & 0 deletions test/fixtures/other-headings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"license": "MIX",
"author": "Alpha Bravo"
}
5 changes: 5 additions & 0 deletions test/fixtures/other-headings/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# PI

## License

## Other

0 comments on commit 719bcbd

Please sign in to comment.