Skip to content

Commit

Permalink
Allow using callback function. Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
okonet committed Mar 2, 2016
1 parent 13717e5 commit de8ee82
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'isomorphic-fetch'
import util from 'util'
import chalk from 'chalk'
import debug from 'debug'
import { merge, template, isArray, isString } from 'lodash'
import { merge, template, isArray, isString, isFunction } from 'lodash'

const DEBUG_KEY = 'metalsmith:remote-json-to-files' // See https://github.com/mahnunchik/metalsmith-debug
const log = debug(DEBUG_KEY)
Expand Down Expand Up @@ -47,19 +47,21 @@ function transform(json, opts) {
export default (fetchOptions = {}, transformOpts = {}) => {
const { url, ...rest } = fetchOptions
const fetchOpts = merge(defaultFetchOptions, rest)
const transformOptions = merge(defaultTransformOptions, transformOpts)

return (files, metalsmith, done) => {

// Check required options and parameters
if (typeof url === 'undefined') {
return done(new Error(`${nok} 'url' parameter must be specified`))
}
if (!isString(transformOptions.filename)) {
return done(new Error(`${nok} 'filename' option is required and must be a string`))
}
if (!isString(transformOptions.contents)) {
return done(new Error(`${nok} 'contents' option is required and must be a string`))
if (!isFunction(transformOpts)) {
transformOpts = merge(defaultTransformOptions, transformOpts)
if (!isString(transformOpts.filename)) {
return done(new Error(`${nok} 'filename' option is required and must be a string`))
}
if (!isString(transformOpts.contents)) {
return done(new Error(`${nok} 'contents' option is required and must be a string`))
}
}

// Request JSON
Expand All @@ -69,7 +71,9 @@ export default (fetchOptions = {}, transformOpts = {}) => {
log(`${ok} Fetched from ${url}: `, inspect(json))

// Transfor the result to files
const newFiles = transform(json, transformOptions)
const newFiles = isFunction(transformOpts) ?
transformOpts(json, files, metalsmith) :
transform(json, transformOpts, files, metalsmith)

// Add files to pipeline
files = Object.assign(files, newFiles)
Expand Down
52 changes: 52 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,58 @@ describe('metalsmith-remote-json-to-files', () => {

})

it('should support callback function', (done) => {
const fixturesPath = 'test/fixtures/basic'
fetchMock.mock('http://myurl.test', fixtureData)

function cb(json, files, metalsmith) {
expect(json).toBeAn('array')
expect(files).toBeAn('object')
expect(metalsmith).toExist()

return json.reduce((res, item) => {
const filename = `${ item.name }.html`
return {
...res,
[filename]: {
string: `Title ${item.name}`,
date: new Date(parseInt(item.name, 10)),
contents: new Buffer(`Wrapped ${item.body} content\n`),
number: 1,
bool: true,
func: () => null
}
}
}, {})
}

new Metalsmith(fixturesPath)
.use(plugin({
url: 'http://myurl.test'
}, cb))
.use((files) => {
expect(files['1.html'].string).toBeA('string')
expect(files['1.html'].string).toEqual('Title 1')

expect(files['1.html'].number).toBeA('number')
expect(files['1.html'].number).toEqual(1)

expect(files['1.html'].bool).toBeA('boolean')
expect(files['1.html'].bool).toEqual(true)

expect(files['1.html'].func).toBeA('function')

expect(files['1.html'].date).toBeA(Date)
expect(files['1.html'].date.getTime()).toEqual(1)
})
.build(err => {
expect(err).toBe(null)
equal(path.join(fixturesPath, 'expected'), path.join(fixturesPath, 'build'))
done()
})

})

it.skip('should iterate over deeply nested props of JSON', (done) => {
const fixturesPath = 'test/fixtures/no-array'
fetchMock.mock('http://myurl.test', {
Expand Down

0 comments on commit de8ee82

Please sign in to comment.