Skip to content

Commit

Permalink
Be possible specify config cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Dec 14, 2017
1 parent 4ab612b commit 8b34f50
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
]
},
"standard": {
"globals": [
"describe",
"it"
"env": [
"mocha"
]
},
"version": "2.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper-date/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = () => {
wrap($ => $('[class*="time"]').text())
]

rules.name = 'date'
rules.propName = 'date'

return rules
}
1 change: 1 addition & 0 deletions packages/metascraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"sanitize-html": "~1.16.1"
},
"devDependencies": {
"clear-module": "latest",
"coveralls": "latest",
"mocha": "latest",
"nyc": "latest",
Expand Down
12 changes: 8 additions & 4 deletions packages/metascraper/src/load-rules.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const config = require('cosmiconfig')('metascraper').load(process.cwd())
const { isObject, isArray, isString } = require('lodash')
const cwd = process.env.METASCRAPER_CONFIG_CWD || process.cwd()
const config = require('cosmiconfig')('metascraper').load(cwd)
const { isObject, isArray, isString, get } = require('lodash')

const DEFAULT_RULES = [
'metascraper-author',
Expand All @@ -18,8 +19,10 @@ let singletonConfig

module.exports = () =>
singletonConfig ||
Promise.resolve(config).then(({ config = { rules: DEFAULT_RULES } }) => {
singletonConfig = config.rules.map(rule => {
Promise.resolve(config).then(configFile => {
const rules = get(configFile, 'config.rules', DEFAULT_RULES)

singletonConfig = rules.map(rule => {
let moduleName
let moduleConfig

Expand All @@ -35,5 +38,6 @@ module.exports = () =>

return require(moduleName)(moduleConfig)
})

return singletonConfig
})
22 changes: 11 additions & 11 deletions packages/metascraper/test/unit/custom-rules/.metascraperrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"rules": [
"metascraper-author",
"metascraper-date",
"metascraper-description",
"metascraper-image",
"metascraper-logo",
{"metascraper-clearbit-logo": {
"format": "jpg"
}},
"metascraper-publisher",
"metascraper-title",
"metascraper-url"
"metascraper-author",
"metascraper-date",
"metascraper-description",
"metascraper-image",
"metascraper-logo",
{"metascraper-clearbit-logo": {
"format": "jpg"
}},
"metascraper-publisher",
"metascraper-title",
"metascraper-url"
]
}
26 changes: 17 additions & 9 deletions packages/metascraper/test/unit/custom-rules/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
'use strict'

const clearModule = require('clear-module')
const should = require('should')
const metascraper = require('../../..')

it('loading custom rules', async () => {
const url = 'https://facebook.com'
const html = '<div></div>'
const meta = await metascraper({ url, html })
should(meta.logo).be.equal(
'https://logo.clearbit.com/facebook.com?size=128&format=jpg'
)

describe('custom rules', () => {
before(clearModule.all)
after(clearModule.all)

it('loading custom rules', async () => {
process.env.METASCRAPER_CONFIG_CWD = __dirname
const metascraper = require('../../..')

const url = 'https://facebook.com'
const html = '<div></div>'
const meta = await metascraper({ url, html })
should(meta.logo).be.equal(
'https://logo.clearbit.com/facebook.com?size=128&format=jpg'
)
})
})

0 comments on commit 8b34f50

Please sign in to comment.