Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 5, 2021
1 parent ec1b544 commit 88c1516
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 181 deletions.
245 changes: 96 additions & 149 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,184 +1,131 @@
import fs from 'fs'
import {promises as fs} from 'fs'
import path from 'path'
import parse from 'parse-author'
import spdx from 'spdx-license-list'
import {read} from 'to-vfile'
import {findUpOne} from 'vfile-find-up'
import {headingRange} from 'mdast-util-heading-range'

var licenseRegexp = /^licen[cs]e(?=$|\.)/i
var licenseHeadingRegexp = /^licen[cs]e$/i
var http = 'http://'
var https = 'https://'
const licenseRegexp = /^licen[cs]e(?=$|\.)/i
const licenseHeadingRegexp = /^licen[cs]e$/i
const http = 'http://'
const https = 'https://'

/* Add a license section. */
export default function remarkLicense(options) {
var settings = options || {}
var finals = settings.ignoreFinalDefinitions
var test = settings.heading || licenseHeadingRegexp

var headingOptions = {
ignoreFinalDefinitions:
finals === undefined || finals === null ? true : finals,
test: test
}

return transformer

function transformer(tree, file, next) {
var cwd = file.cwd
var left = 2 // Two async operations.
var defaultName
var defaultUrl
var defaultLicense
var defaultLicenseFile
export default function remarkLicense(options = {}) {
const ignoreFinalDefinitions =
options.ignoreFinalDefinitions === undefined ||
options.ignoreFinalDefinitions === null
? true
: options.ignoreFinalDefinitions
const test = options.heading || licenseHeadingRegexp

// eslint-disable-next-line complexity
return async (tree, file) => {
// Else is for stdin, typically not used.
/* c8 ignore next */
const base = file.dirname ? path.resolve(file.cwd, file.dirname) : file.cwd
const packageFile = await findUpOne('package.json', base)
let defaultName
let defaultUrl
let defaultLicense
let defaultLicenseFile

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

if (settings.file) {
one()
} else {
fs.readdir(cwd, onfiles)
if (packageFile && (!options.url || !options.name || !options.license)) {
await read(packageFile)
const packageJson = JSON.parse(String(packageFile))
const author =
typeof packageJson.author === 'string'
? parse(packageJson.author)
: packageJson.author || {}
defaultLicense = packageJson.license
defaultName = author.name
defaultUrl = author.url
}

function onpackage(error, buf) {
var pack = {}
var author

if (buf) {
try {
pack = JSON.parse(buf)
} catch (error) {
return one(error)
if (!options.file) {
const files = await fs.readdir(
(packageFile && path.resolve(packageFile.cwd, packageFile.dirname)) ||
file.cwd
)
let index = -1

while (++index < files.length) {
if (licenseRegexp.test(files[index])) {
defaultLicenseFile = files[index]
break
}
}

// Hard to test.
/* c8 ignore next 3 */
if (error && error.code !== 'ENOENT') {
one(error)
} else {
defaultLicense = pack.license
author = pack.author || {}
author = typeof author === 'string' ? parse(author) : author
defaultName = author.name
defaultUrl = author.url

one()
}
}

function onfiles(error, files) {
var length
var index

// Hard to test.
/* c8 ignore next 3 */
if (error) {
one(error)
} else {
length = files.length
index = -1

while (++index < length) {
if (licenseRegexp.test(files[index])) {
defaultLicenseFile = files[index]
break
}
}
const url = options.url || defaultUrl
const name = options.name || defaultName
const license = options.license || defaultLicense
let licenseFile = options.file || defaultLicenseFile

one()
}
/* Ignore the license file itself. */
if (licenseFile && file.path === licenseFile) {
return
}

function one(error) {
if (error) {
next(error)
left = Infinity
} else if (--left === 0) {
done()
}
if (!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`'
)
}

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

/* Ignore the license file itself. */
if (licenseFile && file.path === licenseFile) {
return next()
}
if (!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 (!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`'
)
)
}
if (!licenseFile && license in spdx) {
licenseFile = spdx[license].url
}

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`'
)
)
}
headingRange(tree, {ignoreFinalDefinitions, test}, (start, nodes, end) => {
const children = []
const node = {type: 'paragraph', children}
let parent

if (!licenseFile && license in spdx) {
licenseFile = spdx[license].url
if (licenseFile) {
parent = {type: 'link', title: null, url: licenseFile, children: []}
children.push(parent)
} else {
parent = node
}

headingRange(tree, headingOptions, onheading)
parent.children.push({type: 'text', value: license})

next()
children.push({type: 'text', value: ' © '})

function onheading(start, nodes, end) {
var children = []
var node = {type: 'paragraph', children: children}
var link
var parent
if (url) {
let link

if (licenseFile) {
parent = {type: 'link', title: null, url: licenseFile, children: []}
children.push(parent)
if (
url.slice(0, http.length) !== http &&
url.slice(0, https.length) !== https
) {
link = http + url
} else {
parent = node
link = url
}

parent.children.push({type: 'text', value: license})

children.push({type: 'text', value: ' © '})

if (url) {
if (
url.slice(0, http.length) !== http &&
url.slice(0, https.length) !== https
) {
link = http + url
} else {
link = url
}

parent = {type: 'link', title: null, url: link, children: []}
children.push(parent)
} else {
parent = node
}
parent = {type: 'link', title: null, url: link, children: []}
children.push(parent)
} else {
parent = node
}

parent.children.push({type: 'text', value: name})
parent.children.push({type: 'text', value: name})

return [start, node, end]
}
}
return [start, node, end]
})
}
}
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"dependencies": {
"mdast-util-heading-range": "^3.0.0",
"parse-author": "^2.0.0",
"spdx-license-list": "^6.0.0"
"spdx-license-list": "^6.0.0",
"to-vfile": "^7.0.0",
"vfile-find-up": "^6.0.0"
},
"devDependencies": {
"c8": "^7.0.0",
Expand All @@ -47,7 +49,7 @@
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix",
Expand All @@ -65,13 +67,6 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-optional-catch-binding": "off"
},
"overrides": [
{
"files": [
Expand Down
33 changes: 10 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,23 @@ import {remark} from 'remark'
import {isHidden} from 'is-hidden'
import license from '../index.js'

var root = path.join('test', 'fixtures')
const root = path.join('test', 'fixtures')

fs.writeFileSync(
path.join(root, 'fail-unexpected-end-of-json', 'package.json'),
'{\n'
)

fs.renameSync('package.json', 'package.json.bak')

process.on('exit', () => {
fs.unlinkSync(path.join(root, 'fail-unexpected-end-of-json', 'package.json'))
fs.renameSync('package.json.bak', 'package.json')
})

test('current working directory', function (t) {
t.plan(1)

remark()
.use(license)
.process('# License', function (error, file) {
t.deepEqual(
[error, String(file)],
[
null,
'# License\n\n[MIT](license) © [Titus Wormer](https://wooorm.com)\n'
]
)
})
})

test('Fixtures', async function (t) {
var fixtures = fs.readdirSync(root)
var index = -1
test('Fixtures', async (t) => {
const fixtures = fs.readdirSync(root)
let index = -1

while (++index < fixtures.length) {
const name = fixtures[index]
Expand All @@ -45,7 +32,7 @@ test('Fixtures', async function (t) {

try {
config = JSON.parse(fs.readFileSync(path.join(root, name, 'config.json')))
} catch (_) {
} catch {
try {
config = (
await import(
Expand All @@ -55,12 +42,12 @@ test('Fixtures', async function (t) {
)
)
).default
} catch (_) {}
} catch {}
}

try {
output = String(fs.readFileSync(path.join(root, name, 'output.md')))
} catch (_) {
} catch {
output = ''
}

Expand Down

0 comments on commit 88c1516

Please sign in to comment.