remark-license
Advanced tools
Comparing version 4.0.1 to 5.0.0
247
index.js
@@ -1,143 +0,190 @@ | ||
'use strict'; | ||
'use strict' | ||
var heading = require('mdast-util-heading-range'); | ||
var parse = require('parse-author') | ||
var spdx = require('spdx-license-list') | ||
var heading = require('mdast-util-heading-range') | ||
module.exports = license; | ||
module.exports = license | ||
var fs; | ||
var path; | ||
var fs | ||
var path | ||
try { | ||
fs = require('fs'); | ||
path = require('path'); | ||
fs = require('fs') | ||
path = require('path') | ||
} catch (err) {} | ||
var EXPRESSION = /^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)/; | ||
var LICENSE = /^licen[cs]e(?=$|\.)/i; | ||
var licenseRegexp = /^licen[cs]e(?=$|\.)/i | ||
var licenseHeadingRegexp = /^licen[cs]e$/i | ||
var http = 'http://' | ||
var https = 'https://' | ||
var http = 'http://'; | ||
var https = 'https://'; | ||
/* Add a license section. */ | ||
function license(options) { | ||
return transformer; | ||
var opts = options || {} | ||
var finals = opts.ignoreFinalDefinitions | ||
var headingOptions = { | ||
ignoreFinalDefinitions: | ||
finals === undefined || finals === null ? true : finals, | ||
test: licenseHeadingRegexp | ||
} | ||
function transformer(tree, file) { | ||
var settings = {}; | ||
var pack = {}; | ||
var entries = []; | ||
var cwd = file.cwd; | ||
var url; | ||
var length; | ||
var index; | ||
return transformer | ||
if (!options) { | ||
options = {}; | ||
} | ||
function transformer(tree, file, next) { | ||
var cwd = file.cwd | ||
var left = 2 // Two async operations. | ||
var defaultName | ||
var defaultUrl | ||
var defaultLicense | ||
var defaultLicenseFile | ||
try { | ||
pack = require(path.resolve(cwd, 'package.json')); | ||
} catch (err) {} | ||
if (typeof pack.author === 'string') { | ||
url = EXPRESSION.exec(pack.author); | ||
settings.name = url[1]; | ||
settings.url = url[3]; | ||
} else if (pack.author && pack.author.name) { | ||
settings.name = pack.author.name; | ||
settings.url = pack.author.url; | ||
// Skip package loading if we have all info in `options`. | ||
if (opts.url && opts.name && opts.license) { | ||
one() | ||
} else { | ||
fs.readFile(path.resolve(cwd, 'package.json'), onpackage) | ||
} | ||
if (options.file) { | ||
settings.file = options.file; | ||
if (opts.file) { | ||
one() | ||
} else { | ||
try { | ||
entries = fs.readdirSync(cwd); | ||
} catch (err) { /* Empty */ } | ||
fs.readdir(cwd, onfiles) | ||
} | ||
length = entries.length; | ||
index = -1; | ||
function onpackage(err, buf) { | ||
var pack = {} | ||
var author | ||
while (++index < length) { | ||
if (LICENSE.test(entries[index])) { | ||
settings.file = entries[index]; | ||
break; | ||
if (buf) { | ||
try { | ||
pack = JSON.parse(buf) | ||
} catch (err) { | ||
return one(err) | ||
} | ||
} | ||
} | ||
if (options.url) { | ||
settings.url = options.url; | ||
} | ||
/* istanbul ignore if - hard to test. */ | ||
if (err && err.code !== 'ENOENT') { | ||
one(err) | ||
} else { | ||
defaultLicense = pack.license | ||
author = pack.author || {} | ||
author = typeof author === 'string' ? parse(author) : author | ||
defaultName = author.name | ||
defaultUrl = author.url | ||
if (options.name) { | ||
settings.name = options.name; | ||
one() | ||
} | ||
} | ||
settings.license = options.license || pack.license; | ||
function onfiles(err, files) { | ||
var length | ||
var index | ||
if (!settings.license) { | ||
throw new Error( | ||
'Missing required `license` in settings.\n' + | ||
'Either add a `license` to a `package.json` file\n' + | ||
'or pass it into `remark-license`' | ||
); | ||
/* istanbul ignore if - hard to test. */ | ||
if (err) { | ||
one(err) | ||
} else { | ||
length = files.length | ||
index = -1 | ||
while (++index < length) { | ||
if (licenseRegexp.test(files[index])) { | ||
defaultLicenseFile = files[index] | ||
break | ||
} | ||
} | ||
one() | ||
} | ||
} | ||
if (!settings.name) { | ||
throw new Error( | ||
'Missing required `name` in settings.\n' + | ||
'Either add an `author` to a `package.json` file\n' + | ||
'or pass it into `remark-license`' | ||
); | ||
function one(err) { | ||
if (err) { | ||
next(err) | ||
left = Infinity | ||
} else if (--left === 0) { | ||
done() | ||
} | ||
} | ||
heading(tree, /^licen[cs]e$/i, function (start, nodes, end) { | ||
var children = []; | ||
var node = {type: 'paragraph', children: children}; | ||
var url; | ||
var parent; | ||
function done() { | ||
var url = opts.url || defaultUrl | ||
var name = opts.name || defaultName | ||
var license = opts.license || defaultLicense | ||
var licenseFile = opts.file || defaultLicenseFile | ||
if (settings.file) { | ||
parent = { | ||
type: 'link', | ||
url: settings.file, | ||
children: [] | ||
}; | ||
/* Ignore the license file itself. */ | ||
if (licenseFile && file.path === licenseFile) { | ||
return next() | ||
} | ||
children.push(parent); | ||
} else { | ||
parent = node; | ||
if (!license) { | ||
return next( | ||
new Error( | ||
'Missing required `license` in settings.\n' + | ||
'Either add a `license` to a `package.json` file\n' + | ||
'or pass it into `remark-license`' | ||
) | ||
) | ||
} | ||
parent.children.push({type: 'text', value: settings.license}); | ||
if (!name) { | ||
return next( | ||
new Error( | ||
'Missing required `name` in settings.\n' + | ||
'Either add an `author` to a `package.json` file\n' + | ||
'or pass it into `remark-license`' | ||
) | ||
) | ||
} | ||
children.push({type: 'text', value: ' © '}); | ||
if (!licenseFile && license in spdx) { | ||
licenseFile = spdx[license].url | ||
} | ||
if (settings.url) { | ||
url = settings.url; | ||
heading(tree, headingOptions, onheading) | ||
if ( | ||
url.slice(0, http.length) !== http && | ||
url.slice(0, https.length) !== https | ||
) { | ||
url = http + url; | ||
next() | ||
function onheading(start, nodes, end) { | ||
var children = [] | ||
var node = {type: 'paragraph', children: children} | ||
var link | ||
var parent | ||
if (licenseFile) { | ||
parent = {type: 'link', title: null, url: licenseFile, children: []} | ||
children.push(parent) | ||
} else { | ||
parent = node | ||
} | ||
parent = { | ||
type: 'link', | ||
url: url, | ||
children: [] | ||
}; | ||
parent.children.push({type: 'text', value: license}) | ||
children.push(parent); | ||
} else { | ||
parent = node; | ||
} | ||
children.push({type: 'text', value: ' © '}) | ||
parent.children.push({type: 'text', value: settings.name}); | ||
if (url) { | ||
if ( | ||
url.slice(0, http.length) !== http && | ||
url.slice(0, https.length) !== https | ||
) { | ||
link = http + url | ||
} else { | ||
link = url | ||
} | ||
return [start, node, end]; | ||
}); | ||
parent = {type: 'link', title: null, url: link, children: []} | ||
children.push(parent) | ||
} else { | ||
parent = node | ||
} | ||
parent.children.push({type: 'text', value: name}) | ||
return [start, node, end] | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "remark-license", | ||
"version": "4.0.1", | ||
"version": "5.0.0", | ||
"description": "Add a license section to your README", | ||
@@ -16,4 +16,4 @@ "license": "MIT", | ||
], | ||
"repository": "https://github.com/wooorm/remark-license", | ||
"bugs": "https://github.com/wooorm/remark-license/issues", | ||
"repository": "remarkjs/remark-license", | ||
"bugs": "https://github.com/remarkjs/remark-license/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -23,27 +23,29 @@ "contributors": [ | ||
], | ||
"dependencies": { | ||
"mdast-util-heading-range": "^2.0.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"mdast-util-heading-range": "^2.0.0", | ||
"parse-author": "^2.0.0", | ||
"spdx-license-list": "^4.1.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^14.0.0", | ||
"esmangle": "^1.0.0", | ||
"nyc": "^11.0.0", | ||
"remark": "^7.0.0", | ||
"remark-cli": "^3.0.0", | ||
"remark-preset-wooorm": "^3.0.0", | ||
"browserify": "^16.0.0", | ||
"nyc": "^12.0.0", | ||
"prettier": "^1.14.2", | ||
"remark": "^9.0.0", | ||
"remark-cli": "^5.0.0", | ||
"remark-preset-wooorm": "^4.0.0", | ||
"tape": "^4.4.0", | ||
"xo": "^0.18.0" | ||
"tinyify": "^2.4.3", | ||
"xo": "^0.22.0" | ||
}, | ||
"scripts": { | ||
"build-md": "remark *.md -qfo", | ||
"build-bundle": "browserify index.js -s remarkLicense > remark-license.js", | ||
"build-mangle": "esmangle remark-license.js > remark-license.min.js", | ||
"build": "npm run build-md && npm run build-bundle && npm run build-mangle", | ||
"lint": "xo", | ||
"format": "remark *.md -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify . -s remarkLicense > remark-license.js", | ||
"build-mangle": "browserify . -s remarkLicense -p tinyify > remark-license.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test/index.js", | ||
"test": "npm run build && npm run lint && npm run test-coverage" | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
}, | ||
@@ -56,4 +58,12 @@ "nyc": { | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
}, | ||
"xo": { | ||
"space": true, | ||
"prettier": true, | ||
"esnext": false, | ||
@@ -60,0 +70,0 @@ "rules": { |
# remark-license [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] | ||
Add a [license][sec] section to a README with [**remark**][remark]. | ||
Add a [license][section] section to a README with [**remark**][remark]. | ||
@@ -26,12 +26,12 @@ ## Installation | ||
```javascript | ||
var fs = require('fs'); | ||
var remark = require('remark'); | ||
var license = require('remark-license'); | ||
var fs = require('fs') | ||
var remark = require('remark') | ||
var license = require('remark-license') | ||
remark() | ||
.use(license) | ||
.process(fs.readFileSync('example.md'), function (err, file) { | ||
if (err) throw err; | ||
console.log(String(file)); | ||
}); | ||
.process(fs.readFileSync('example.md'), function(err, file) { | ||
if (err) throw err | ||
console.log(String(file)) | ||
}) | ||
``` | ||
@@ -54,2 +54,4 @@ | ||
Does nothing when no heading is found. | ||
Does nothing when the processed file is the license file (such for a license | ||
heading in `license.md`). | ||
@@ -76,2 +78,4 @@ ##### `options` | ||
`/^licen[cs]e(?=$|\.)/i` is used. | ||
If there is no given or found license file, but `options.license` is a known | ||
[SPDX identifier][spdx], the URL to the license on `spdx.org` is used. | ||
@@ -85,2 +89,6 @@ ###### `options.url` | ||
###### `options.ignoreFinalDefinitions` | ||
Ignore final definitions otherwise in the section (`boolean`, default: true). | ||
## Detection | ||
@@ -95,16 +103,19 @@ | ||
```js | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var remark = require('remark'); | ||
var license = require('remark-license'); | ||
var fs = require('fs') | ||
var path = require('path') | ||
var remark = require('remark') | ||
var license = require('remark-license') | ||
remark() | ||
.use(license) | ||
.process({ | ||
cwd: path.join('.', 'some', 'path', 'to', 'a', 'directory'), | ||
contents: fs.readFileSync('example.md') | ||
}, function (err, file) { | ||
if (err) throw err; | ||
console.log(String(file)); | ||
}); | ||
.process( | ||
{ | ||
cwd: path.join('.', 'some', 'path', 'to', 'a', 'directory'), | ||
contents: fs.readFileSync('example.md') | ||
}, | ||
function(err, file) { | ||
if (err) throw err | ||
console.log(String(file)) | ||
} | ||
) | ||
``` | ||
@@ -118,7 +129,15 @@ | ||
— Add a list of contributors | ||
* [`remark-toc`](https://github.com/wooorm/remark-toc) | ||
* [`remark-toc`](https://github.com/remarkjs/remark-toc) | ||
— Add a table of contents | ||
* [`remark-usage`](https://github.com/wooorm/remark-usage) | ||
* [`remark-usage`](https://github.com/remarkjs/remark-usage) | ||
— Add a usage example | ||
## Contribute | ||
See [`contributing.md` in `remarkjs/remark`][contributing] for ways to get | ||
started. | ||
This organisation has a [Code of Conduct][coc]. By interacting with this | ||
repository, organisation, or community you agree to abide by its terms. | ||
## License | ||
@@ -130,13 +149,13 @@ | ||
[build-badge]: https://img.shields.io/travis/wooorm/remark-license.svg | ||
[build-badge]: https://img.shields.io/travis/remarkjs/remark-license.svg | ||
[build-status]: https://travis-ci.org/wooorm/remark-license | ||
[build-status]: https://travis-ci.org/remarkjs/remark-license | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/remark-license.svg | ||
[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-license.svg | ||
[coverage-status]: https://codecov.io/github/wooorm/remark-license | ||
[coverage-status]: https://codecov.io/github/remarkjs/remark-license | ||
[chat-badge]: https://img.shields.io/gitter/room/wooorm/remark.svg | ||
[chat-badge]: https://img.shields.io/gitter/room/remarkjs/Lobby.svg | ||
[chat]: https://gitter.im/wooorm/remark | ||
[chat]: https://gitter.im/remarkjs/Lobby | ||
@@ -149,3 +168,3 @@ [license]: LICENSE | ||
[remark]: https://github.com/wooorm/remark | ||
[remark]: https://github.com/remarkjs/remark | ||
@@ -160,4 +179,8 @@ [author-format]: https://docs.npmjs.com/files/package.json#people-fields-author-contributors | ||
[sec]: #license | ||
[section]: #license | ||
[detected]: #detection | ||
[contributing]: https://github.com/remarkjs/remark/blob/master/contributing.md | ||
[coc]: https://github.com/remarkjs/remark/blob/master/code-of-conduct.md |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
12174
156
178
1
3
9
1
+ Addedparse-author@^2.0.0
+ Addedspdx-license-list@^4.1.0
+ Addedauthor-regex@1.0.0(transitive)
+ Addedparse-author@2.0.0(transitive)
+ Addedspdx-license-list@4.1.0(transitive)