Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 5, 2021
1 parent 3af091d commit 4e03097
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 60 deletions.
23 changes: 6 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
'use strict'

var parse = require('parse-author')
var spdx = require('spdx-license-list')
var heading = require('mdast-util-heading-range')

module.exports = license

var fs
var path

/* c8 ignore next 4 */
try {
fs = require('fs')
path = require('path')
} catch (_) {}
import fs from 'fs'
import path from 'path'
import parse from 'parse-author'
import spdx from 'spdx-license-list'
import heading from 'mdast-util-heading-range'

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

/* Add a license section. */
function license(options) {
export default function remarkLicense(options) {
var settings = options || {}
var finals = settings.ignoreFinalDefinitions
var test = settings.heading || licenseHeadingRegexp
Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"Titus Wormer <[email protected]> (https://wooorm.com)",
"Ben Briggs <[email protected]> (http://beneb.info)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
Expand Down Expand Up @@ -65,9 +68,21 @@
"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": [
"test/**/*.js"
],
"rules": {
"no-await-in-loop": "off"
}
}
]
},
"remarkConfig": {
"plugins": [
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/match-heading-regex/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = {
const config = {
license: 'MIX',
name: 'Alpha Bravo',
heading: /^lizenz$/i
}

export default config
81 changes: 40 additions & 41 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict'
import fs from 'fs'
import path from 'path'
import test from 'tape'
import remark from 'remark'
import hidden from 'is-hidden'
import negate from 'negate'
import license from '../index.js'

var fs = require('fs')
var path = require('path')
var test = require('tape')
var remark = require('remark')
var hidden = require('is-hidden')
var negate = require('negate')
var license = require('..')

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

fs.writeFileSync(
path.join(root, 'fail-unexpected-end-of-json', 'package.json'),
Expand All @@ -35,25 +33,27 @@ test('current working directory', function (t) {
})
})

test('Fixtures', function (t) {
test('Fixtures', async function (t) {
var fixtures = fs.readdirSync(root).filter(negate(hidden))
var index = -1

t.plan(fixtures.length)

while (++index < fixtures.length) {
one(fixtures[index])
}

function one(name) {
var config
var output
const name = fixtures[index]
let config
let output

try {
config = JSON.parse(fs.readFileSync(path.join(root, name, 'config.json')))
} catch (_) {
try {
config = require(path.join(root, name, 'config.js'))
config = (
await import(
new URL(
path.join('.', 'fixtures', name, 'config.js'),
import.meta.url
)
)
).default
} catch (_) {}
}

Expand All @@ -63,31 +63,30 @@ test('Fixtures', function (t) {
output = ''
}

remark()
.use(license, config)
.process(
{
try {
const file = await remark()
.use(license, config)
.process({
contents: fs.readFileSync(path.join(root, name, 'readme.md')),
cwd: path.join(root, name),
path: 'readme.md'
},
function (error, file) {
var expression
})

if (name.indexOf('fail-') === 0) {
expression = new RegExp(name.slice(5).replace(/-/g, ' '), 'i')
t.equal(String(file), output, 'should work on `' + name + '`')
} catch (error) {
if (name.indexOf('fail-') === 0) {
const expression = new RegExp(name.slice(5).replace(/-/g, ' '), 'i')

t.equal(
expression.test(String(error).replace(/`/g, '')),
true,
'should fail on `' + name + '` matching `' + expression + '`'
)
} else if (error) {
throw error
} else {
t.equal(String(file), output, 'should work on `' + name + '`')
}
}
)
t.equal(
expression.test(String(error).replace(/`/g, '')),
true,
'should fail on `' + name + '` matching `' + expression + '`'
)
} else {
t.ifError(error, name)
}
}
}

t.end()
})

0 comments on commit 4e03097

Please sign in to comment.