Skip to content

Commit

Permalink
Add METASCRAPER_CWD tests and documentation
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
Kikobeats committed Jan 22, 2018
1 parent ab03d97 commit 73be24f
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ It is used for resolve relative links that can be present in the HTML markup.

it can be used as fallback field for different rules as well.

## Environment Variables

### METASCRAPER_CWD

Type: `String` <br/>
Default: `process.cwd()`

This variable is used to determine where starting search for a configuration object.

## Comparison

To give you an idea of how accurate **metascraper** is, here is a comparison of similar libraries:
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-amazon/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ let metascraper
describe('metascraper-amazon', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CONFIG_CWD = __dirname
process.env.METASCRAPER_CWD = __dirname
metascraper = require('metascraper')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CONFIG_CWD
delete process.env.METASCRAPER_CWD
})

describe('amazon.co.uk', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-clearbit-logo/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const snapshot = require('snap-shot')
describe('metascraper clearbit logo', () => {
before(() => {
clearModule.all()
process.env.METASCRAPER_CONFIG_CWD = __dirname
process.env.METASCRAPER_CWD = __dirname
})

after(() => {
clearModule.all()
delete process.env.METASCRAPER_CONFIG_CWD
delete process.env.METASCRAPER_CWD
})

it('if logo is not present, fallback to clearbit logo API', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-logo-favicon/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const readFile = promisify(fs.readFile)
describe('metascraper amazon integration', () => {
before(() => {
clearModule.all()
process.env.METASCRAPER_CONFIG_CWD = __dirname
process.env.METASCRAPER_CWD = __dirname
})

after(() => {
clearModule.all()
delete process.env.METASCRAPER_CONFIG_CWD
delete process.env.METASCRAPER_CWD
})

describe('metascraper logo favicon', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-soundcloud/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ let metascraper
describe('metascraper-soundcloud', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CONFIG_CWD = __dirname
process.env.METASCRAPER_CWD = __dirname
metascraper = require('metascraper')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CONFIG_CWD
delete process.env.METASCRAPER_CWD
})

it('song', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/metascraper-youtube/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ let metascraper
describe('metascraper-youtube', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CONFIG_CWD = __dirname
process.env.METASCRAPER_CWD = __dirname
metascraper = require('metascraper')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CONFIG_CWD
delete process.env.METASCRAPER_CWD
})

it('youtube video', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metascraper/src/load-rules.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const cwd = process.env.METASCRAPER_CONFIG_CWD || process.cwd()
const cwd = process.env.METASCRAPER_CWD || process.cwd()
const config = require('cosmiconfig')('metascraper').load(cwd)
const resolveFrom = require('resolve-from')

Expand Down
29 changes: 29 additions & 0 deletions packages/metascraper/test/unit/load-rules/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const clearModule = require('clear-module')
const should = require('should')

let metascraper

describe('load rules', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CWD = __dirname
metascraper = require('../../../..')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CWD
})

it('based on a configuration file', 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'
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
rules: [
'metascraper-author',
'metascraper-date',
'metascraper-description',
'metascraper-image',
'metascraper-logo',
{
'metascraper-clearbit-logo': {
format: 'jpg'
}
},
'metascraper-publisher',
'metascraper-title',
'metascraper-url'
]
}
29 changes: 29 additions & 0 deletions packages/metascraper/test/unit/load-rules/pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const clearModule = require('clear-module')
const should = require('should')

let metascraper

describe('load rules', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CWD = __dirname
metascraper = require('../../../..')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CWD
})

it('based on a package file', 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'
)
})
})
19 changes: 19 additions & 0 deletions packages/metascraper/test/unit/load-rules/pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"metascraper": {
"rules": [
"metascraper-author",
"metascraper-date",
"metascraper-description",
"metascraper-image",
"metascraper-logo",
{
"metascraper-clearbit-logo": {
"format": "jpg"
}
},
"metascraper-publisher",
"metascraper-title",
"metascraper-url"
]
}
}
15 changes: 15 additions & 0 deletions packages/metascraper/test/unit/load-rules/rc/.metascraperrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"rules": [
"metascraper-author",
"metascraper-date",
"metascraper-description",
"metascraper-image",
"metascraper-logo",
{"metascraper-clearbit-logo": {
"format": "jpg"
}},
"metascraper-publisher",
"metascraper-title",
"metascraper-url"
]
}
29 changes: 29 additions & 0 deletions packages/metascraper/test/unit/load-rules/rc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const clearModule = require('clear-module')
const should = require('should')

let metascraper

describe('load rules', () => {
before(() => {
clearModule('metascraper')
process.env.METASCRAPER_CWD = __dirname
metascraper = require('../../../..')
})

after(() => {
clearModule('metascraper')
delete process.env.METASCRAPER_CWD
})

it('based on a rc file', 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'
)
})
})

0 comments on commit 73be24f

Please sign in to comment.