Skip to content

Commit 8b34f50

Browse files
committed
Be possible specify config cwd
1 parent 4ab612b commit 8b34f50

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@
3939
]
4040
},
4141
"standard": {
42-
"globals": [
43-
"describe",
44-
"it"
42+
"env": [
43+
"mocha"
4544
]
4645
},
4746
"version": "2.0.0"

packages/metascraper-date/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = () => {
6565
wrap($ => $('[class*="time"]').text())
6666
]
6767

68-
rules.name = 'date'
68+
rules.propName = 'date'
6969

7070
return rules
7171
}

packages/metascraper/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"sanitize-html": "~1.16.1"
6363
},
6464
"devDependencies": {
65+
"clear-module": "latest",
6566
"coveralls": "latest",
6667
"mocha": "latest",
6768
"nyc": "latest",

packages/metascraper/src/load-rules.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

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

67
const DEFAULT_RULES = [
78
'metascraper-author',
@@ -18,8 +19,10 @@ let singletonConfig
1819

1920
module.exports = () =>
2021
singletonConfig ||
21-
Promise.resolve(config).then(({ config = { rules: DEFAULT_RULES } }) => {
22-
singletonConfig = config.rules.map(rule => {
22+
Promise.resolve(config).then(configFile => {
23+
const rules = get(configFile, 'config.rules', DEFAULT_RULES)
24+
25+
singletonConfig = rules.map(rule => {
2326
let moduleName
2427
let moduleConfig
2528

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

3639
return require(moduleName)(moduleConfig)
3740
})
41+
3842
return singletonConfig
3943
})
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"rules": [
3-
"metascraper-author",
4-
"metascraper-date",
5-
"metascraper-description",
6-
"metascraper-image",
7-
"metascraper-logo",
8-
{"metascraper-clearbit-logo": {
9-
"format": "jpg"
10-
}},
11-
"metascraper-publisher",
12-
"metascraper-title",
13-
"metascraper-url"
3+
"metascraper-author",
4+
"metascraper-date",
5+
"metascraper-description",
6+
"metascraper-image",
7+
"metascraper-logo",
8+
{"metascraper-clearbit-logo": {
9+
"format": "jpg"
10+
}},
11+
"metascraper-publisher",
12+
"metascraper-title",
13+
"metascraper-url"
1414
]
1515
}
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
'use strict'
22

3+
const clearModule = require('clear-module')
34
const should = require('should')
4-
const metascraper = require('../../..')
5-
6-
it('loading custom rules', async () => {
7-
const url = 'https://facebook.com'
8-
const html = '<div></div>'
9-
const meta = await metascraper({ url, html })
10-
should(meta.logo).be.equal(
11-
'https://logo.clearbit.com/facebook.com?size=128&format=jpg'
12-
)
5+
6+
describe('custom rules', () => {
7+
before(clearModule.all)
8+
after(clearModule.all)
9+
10+
it('loading custom rules', async () => {
11+
process.env.METASCRAPER_CONFIG_CWD = __dirname
12+
const metascraper = require('../../..')
13+
14+
const url = 'https://facebook.com'
15+
const html = '<div></div>'
16+
const meta = await metascraper({ url, html })
17+
should(meta.logo).be.equal(
18+
'https://logo.clearbit.com/facebook.com?size=128&format=jpg'
19+
)
20+
})
1321
})

0 commit comments

Comments
 (0)