remark-license
Advanced tools
Comparing version 3.0.0 to 4.0.0
116
index.js
@@ -23,74 +23,74 @@ 'use strict'; | ||
function license(options) { | ||
var settings = {}; | ||
var pack = {}; | ||
var entries = []; | ||
var cwd = global.process && global.process.cwd(); | ||
var url; | ||
var length; | ||
var index; | ||
return transformer; | ||
if (!options) { | ||
options = {}; | ||
} | ||
function transformer(tree, file) { | ||
var settings = {}; | ||
var pack = {}; | ||
var entries = []; | ||
var cwd = file.cwd; | ||
var url; | ||
var length; | ||
var index; | ||
try { | ||
pack = require(path.resolve(cwd, 'package.json')); | ||
} catch (err) {} | ||
if (!options) { | ||
options = {}; | ||
} | ||
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; | ||
} | ||
if (options.file) { | ||
settings.file = options.file; | ||
} else { | ||
try { | ||
entries = fs.readdirSync(cwd); | ||
} catch (err) { /* Empty */ } | ||
pack = require(path.resolve(cwd, 'package.json')); | ||
} catch (err) {} | ||
length = entries.length; | ||
index = -1; | ||
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; | ||
} | ||
while (++index < length) { | ||
if (LICENSE.test(entries[index])) { | ||
settings.file = entries[index]; | ||
break; | ||
if (options.file) { | ||
settings.file = options.file; | ||
} else { | ||
try { | ||
entries = fs.readdirSync(cwd); | ||
} catch (err) { /* Empty */ } | ||
length = entries.length; | ||
index = -1; | ||
while (++index < length) { | ||
if (LICENSE.test(entries[index])) { | ||
settings.file = entries[index]; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (options.url) { | ||
settings.url = options.url; | ||
} | ||
if (options.url) { | ||
settings.url = options.url; | ||
} | ||
if (options.name) { | ||
settings.name = options.name; | ||
} | ||
if (options.name) { | ||
settings.name = options.name; | ||
} | ||
settings.license = options.license || pack.license; | ||
settings.license = options.license || pack.license; | ||
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`' | ||
); | ||
} | ||
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`' | ||
); | ||
} | ||
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`' | ||
); | ||
} | ||
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`' | ||
); | ||
} | ||
return transformer; | ||
function transformer(tree) { | ||
heading(tree, /^licen[cs]e$/i, function (start, nodes, end) { | ||
@@ -97,0 +97,0 @@ var children = []; |
{ | ||
"name": "remark-license", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Add a license section to your README", | ||
@@ -36,3 +36,3 @@ "license": "MIT", | ||
"tape": "^4.4.0", | ||
"xo": "^0.17.1" | ||
"xo": "^0.18.0" | ||
}, | ||
@@ -57,2 +57,3 @@ "scripts": { | ||
"space": true, | ||
"esnext": false, | ||
"rules": { | ||
@@ -59,0 +60,0 @@ "import/no-dynamic-require": "off" |
@@ -48,3 +48,3 @@ # remark-license [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat] | ||
* `name` (`string`, optional) | ||
— License holder. Detected from the `package.json` in the | ||
— License holder. [Detected][] from the `package.json` in the | ||
current working directory, supporting both [`object` and | ||
@@ -54,3 +54,3 @@ `string`][author-format] format of `author`. | ||
* `license` (`string`, optional) | ||
— [SPDX][] identifier. Detected from the `license` field in | ||
— [SPDX][] identifier. [Detected][] from the `license` field in | ||
the `package.json` in the current working directory. Deprecated | ||
@@ -60,7 +60,7 @@ license objects are not supported. | ||
* `file` (`string`, optional) | ||
— File-name of license file. Detected from the files in the current | ||
— File-name of license file. [Detected][] from the files in the current | ||
working directory, in which case the first file matching | ||
`/^licen[cs]e(?=$|\.)/i` is used. | ||
* `url` (`string`, optional) | ||
— URL to license holder. Detected from the `package.json` in the | ||
— URL to license holder. [Detected][] from the `package.json` in the | ||
current working directory, supporting both [`object` and | ||
@@ -70,2 +70,30 @@ `string`][author-format] format of `author`. | ||
## Detection | ||
Detection of `package.json` and files in the current working directory is | ||
based on the current working directory as set on the given [`vfile`][vfile]. | ||
If you want to set the cwd yourself (the default is `process.cwd()`), you can | ||
pass in a `vfile` or [`vfile` options][vfile-options] to `.process` like so: | ||
```js | ||
var remark = require('remark'); | ||
var license = require('remark-license'); | ||
var vfile = require('vfile'); | ||
var file = vfile({ | ||
cwd: './some/path/to/a/directory', | ||
contents: [ | ||
'## License', | ||
'', | ||
'Something nondescript.', | ||
'' | ||
].join('\n') | ||
}); | ||
remark().use(license).processSync(file); | ||
console.log(String(file)); | ||
``` | ||
## License | ||
@@ -101,2 +129,8 @@ | ||
[vfile]: https://github.com/vfile/vfile | ||
[vfile-options]: https://github.com/vfile/vfile#vfileoptions | ||
[sec]: #license | ||
[detected]: #detection |
9203
132