Skip to content

Commit

Permalink
Upgrade to work with latest spike version (#12)
Browse files Browse the repository at this point in the history
* upgrade to work with latest spike version
  • Loading branch information
jescalan authored Aug 18, 2016
1 parent 436d12e commit 979acbd
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 52 deletions.
35 changes: 16 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
require('when/es6-shim/Promise')

const contentful = require('contentful')
const Joi = require('joi')
const W = require('when')
const fs = require('fs')
const path = require('path')
const node = require('when/node')
const posthtml = require('posthtml')
const loader = require('posthtml-loader')
const reshape = require('reshape')
const loader = require('reshape-loader')

class Contentful {
constructor (opts) {
Expand Down Expand Up @@ -43,28 +41,25 @@ class Contentful {

run (compiler, compilation, done) {
return W.reduce(this.contentTypes, (m, ct) => {
let id = ct.id
let transformFn = ct.transform
let options = Object.assign({
content_type: ct.id,
include: 1
},
ct.filters)
content_type: ct.id,
include: 1
}, ct.filters)

if (transformFn === true) transformFn = transform
if (transformFn === false) transformFn = (x) => x

return W(this.client.getEntries(options))
.then(response => {
if(ct.ordered) {
if (ct.ordered) {
response.items = response.items[0].fields[Object.keys(response.items[0].fields)[0]]
}

return W.map(response.items, (entry) => transformFn(entry))
})
.tap((res) => { m[ct.name] = res })
.yield(m)

}, {}).done((res) => {
this.addDataTo = Object.assign(this.addDataTo, { contentful: res })
done()
Expand Down Expand Up @@ -133,7 +128,7 @@ function recursiveTransform (obj, key) {
}

return Object.keys(obj).reduce((prev, curr) => {
if(curr === key) {
if (curr === key) {
prev = recursiveTransform(obj[curr])
} else if (curr === 'sys') {
delete obj[curr]
Expand All @@ -153,18 +148,20 @@ function writeTemplate (ct, compiler, compilation, addDataTo, cb) {
.then((template) => {
return data.map((item) => {
addDataTo = Object.assign(addDataTo, { item: item })
compiler.resourcePath = filePath

const options = loader.parseOptions(compiler.options.posthtml, {})
const mockContext = { resourcePath: filePath, addDependency: (x) => x }
const options = loader.parseOptions.call(mockContext, compiler.options.reshape, {})

return posthtml(options.plugins)
return reshape(options)
.process(template)
.then((res) => {
.then(((locals, res) => {
const html = res.output(locals)
compilation.assets[ct.template.output(item)] = {
source: () => res.html,
size: () => res.html.length
source: () => html,
size: () => html.length
}
}, cb)
}).bind(null, Object.assign({}, options.locals)))
.catch(cb)
})
})
}
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
"bugs": "https://github.com/static-dev/spike-contentful/issues",
"dependencies": {
"contentful": "3.5.0",
"when": "^3.7.7",
"posthtml": "^0.9.0",
"posthtml-loader": "^0.11.0"
"reshape": "^0.3.1",
"reshape-loader": "^0.2.3",
"when": "^3.7.7"
},
"devDependencies": {
"ava": "^0.16.0",
"coveralls": "^2.11.12",
"dotenv": "^2.0.0",
"nyc": "^8.1.0",
"posthtml-jade": "^0.8.2",
"posthtml-exp": "^0.9.0",
"rimraf": "^2.5.4",
"spike-core": "^0.10.0",
"spike-core": "^0.11.2",
"spike-html-standards": "^0.2.1",
"standard": "^7.1.2"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/default/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Contentful = require('../../..')
const jade = require('posthtml-jade')
const htmlStandards = require('spike-html-standards')
const locals = {}

module.exports = {
matchers: { html: '**/*.jade' },
posthtml: { plugins: [jade(locals)] },
matchers: { html: '**/*.sml' },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
plugins: [new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/default/index.jade

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/default/index.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
each(loop='blog of contentful.blogs')
p {{ blog.id }}
4 changes: 2 additions & 2 deletions test/fixtures/json/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Contentful = require('../../..')
const jade = require('posthtml-jade')
const htmlStandards = require('spike-html-standards')
const locals = {}

module.exports = {
matchers: { html: '**/*.jade' },
posthtml: { plugins: [jade(locals)] },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
plugins: [new Contentful({
accessToken: process.env.accessToken,
spaceId: process.env.spaceId,
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/json/index.jade

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/json/index.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
each(loop='blog of contentful.blogs')
p {{ blog.name }}
1 change: 0 additions & 1 deletion test/fixtures/template/error.html

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/template/error.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p {{ notItem.title }}
1 change: 0 additions & 1 deletion test/fixtures/template/template.html

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/template/template.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p {{ item.title }}
33 changes: 17 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Spike = require('spike-core')
const path = require('path')
const fs = require('fs')
const rimraf = require('rimraf')
const exp = require('posthtml-exp')
const htmlStandards = require('spike-html-standards')

const compilerMock = { options: { spike: { locals: {} } } }

Expand Down Expand Up @@ -45,21 +45,21 @@ test('initializes with "limit" filter', (t) => {
test('errors with "limit" filter under 1', (t) => {
t.throws(
() => {
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{ // eslint-disable-line
name: 'test', id: 'xxxx', filters: { limit: 0 } }
]})
}, // eslint-disable-line
},
/option "limit" must be larger than or equal to 1/
)
})

test('errors with "limit" filter over 100', (t) => {
t.throws(
() => {
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
new Contentful({ accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{ // eslint-disable-line
name: 'test', id: 'xxxx', filters: { limit: 101 } }
]})
}, // eslint-disable-line
},
/option "limit" must be less than or equal to 1/
)
})
Expand All @@ -68,10 +68,9 @@ test('initializes with "limit" filter', (t) => {
let opts = { accessToken: 'xxx', spaceId: 'xxx', addDataTo: {}, contentTypes: [{
name: 'test', id: 'xxxx', filters: { limit: 50 } }
]}
t.truthy( new Contentful(opts))
t.truthy(new Contentful(opts))
})


test.cb('returns valid content', (t) => {
const locals = {}
const api = new Contentful({
Expand Down Expand Up @@ -116,7 +115,7 @@ test.cb('implements request options', (t) => {

api.run(compilerMock, undefined, () => {
t.is(locals.contentful.blogs.length, 1)
t.is(locals.contentful.blogs[0].title, 'High School Daydreams')
t.is(locals.contentful.blogs[0].title, 'This Webby Saves Elephants ...')
t.end()
})
})
Expand Down Expand Up @@ -172,7 +171,6 @@ test.cb('implements default transform function', (t) => {
})
})


test.cb('can disable transform function', (t) => {
const locals = {}
const api = new Contentful({
Expand Down Expand Up @@ -209,7 +207,7 @@ test.cb('works as a plugin to spike', (t) => {
project.on('warning', t.end)
project.on('compile', () => {
const src = fs.readFileSync(path.join(projectPath, 'public/index.html'), 'utf8')
t.truthy(src === 'fqhi1USjAIuogSS2AKEKu') // IDs listed in output, sans spaces
t.truthy(src === '<p>fqhi1USjAIuogSS2AKEKu</p>') // IDs listed in output, sans spaces
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand Down Expand Up @@ -253,7 +251,7 @@ test.cb('accepts template object and generates html', (t) => {
order: 'sys.createdAt'
},
template: {
path: '../template/template.html',
path: '../template/template.sml',
output: (item) => `blog_posts/${item.title}.html`
}
}
Expand All @@ -263,7 +261,8 @@ test.cb('accepts template object and generates html', (t) => {
const projectPath = path.join(__dirname, 'fixtures/default')
const project = new Spike({
root: projectPath,
posthtml: { plugins: [exp({ locals })] },
matchers: { html: '**/*.sml' },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
entry: { main: [path.join(projectPath, 'main.js')] },
plugins: [contentful]
})
Expand All @@ -274,7 +273,7 @@ test.cb('accepts template object and generates html', (t) => {
const file1 = fs.readFileSync(path.join(projectPath, 'public/blog_posts/Save The Elephants.html'), 'utf8')
const file2 = fs.readFileSync(path.join(projectPath, 'public/blog_posts/Unlocking The Grid.html'), 'utf8')
t.is(file1.trim(), '<p>Save The Elephants</p>')
t.is(file2.trim(), "<p>Unlocking The Grid</p>")
t.is(file2.trim(), '<p>Unlocking The Grid</p>')
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand All @@ -296,7 +295,7 @@ test.cb('generates error if template has an error', (t) => {
limit: 1
},
template: {
path: '../template/error.html',
path: '../template/error.sml',
output: (item) => `blog_posts/${item.title}.html`
}
}
Expand All @@ -306,14 +305,16 @@ test.cb('generates error if template has an error', (t) => {
const projectPath = path.join(__dirname, 'fixtures/default')
const project = new Spike({
root: projectPath,
posthtml: { plugins: [exp({ locals })] },
matchers: { html: '**/*.sml' },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
entry: { main: [path.join(projectPath, 'main.js')] },
plugins: [contentful]
})

project.on('warning', t.end)
project.on('compile', () => t.end('no error'))
project.on('error', (error) => {
t.is(error.message.message, 'notItem is not defined')
t.is(error.message.message, "Cannot read property 'title' of undefined")
rimraf.sync(path.join(projectPath, 'public'))
t.end()
})
Expand Down

0 comments on commit 979acbd

Please sign in to comment.