From 979acbd8f1055c046ba82b3a128199b3bb68832f Mon Sep 17 00:00:00 2001 From: Jeff Escalante Date: Thu, 18 Aug 2016 16:26:06 -0400 Subject: [PATCH] Upgrade to work with latest spike version (#12) * upgrade to work with latest spike version --- lib/index.js | 35 +++++++++++++--------------- package.json | 11 ++++----- test/fixtures/default/app.js | 6 ++--- test/fixtures/default/index.jade | 2 -- test/fixtures/default/index.sml | 2 ++ test/fixtures/json/app.js | 4 ++-- test/fixtures/json/index.jade | 2 -- test/fixtures/json/index.sml | 2 ++ test/fixtures/template/error.html | 1 - test/fixtures/template/error.sml | 1 + test/fixtures/template/template.html | 1 - test/fixtures/template/template.sml | 1 + test/index.js | 33 +++++++++++++------------- 13 files changed, 49 insertions(+), 52 deletions(-) delete mode 100644 test/fixtures/default/index.jade create mode 100644 test/fixtures/default/index.sml delete mode 100644 test/fixtures/json/index.jade create mode 100644 test/fixtures/json/index.sml delete mode 100644 test/fixtures/template/error.html create mode 100644 test/fixtures/template/error.sml delete mode 100644 test/fixtures/template/template.html create mode 100644 test/fixtures/template/template.sml diff --git a/lib/index.js b/lib/index.js index 46d38ce..b8bd07c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -43,20 +41,18 @@ 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]] } @@ -64,7 +60,6 @@ class Contentful { }) .tap((res) => { m[ct.name] = res }) .yield(m) - }, {}).done((res) => { this.addDataTo = Object.assign(this.addDataTo, { contentful: res }) done() @@ -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] @@ -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) }) }) } diff --git a/package.json b/package.json index f7af881..138c3d1 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/fixtures/default/app.js b/test/fixtures/default/app.js index ebd579e..bb365f2 100644 --- a/test/fixtures/default/app.js +++ b/test/fixtures/default/app.js @@ -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, diff --git a/test/fixtures/default/index.jade b/test/fixtures/default/index.jade deleted file mode 100644 index b1f196a..0000000 --- a/test/fixtures/default/index.jade +++ /dev/null @@ -1,2 +0,0 @@ -for blog in contentful.blogs - = blog.id diff --git a/test/fixtures/default/index.sml b/test/fixtures/default/index.sml new file mode 100644 index 0000000..6fee524 --- /dev/null +++ b/test/fixtures/default/index.sml @@ -0,0 +1,2 @@ +each(loop='blog of contentful.blogs') + p {{ blog.id }} diff --git a/test/fixtures/json/app.js b/test/fixtures/json/app.js index 2a62329..be13b8f 100644 --- a/test/fixtures/json/app.js +++ b/test/fixtures/json/app.js @@ -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, diff --git a/test/fixtures/json/index.jade b/test/fixtures/json/index.jade deleted file mode 100644 index dd31d28..0000000 --- a/test/fixtures/json/index.jade +++ /dev/null @@ -1,2 +0,0 @@ -for blog in contentful.blogs - = blog.name diff --git a/test/fixtures/json/index.sml b/test/fixtures/json/index.sml new file mode 100644 index 0000000..c4e932a --- /dev/null +++ b/test/fixtures/json/index.sml @@ -0,0 +1,2 @@ +each(loop='blog of contentful.blogs') + p {{ blog.name }} diff --git a/test/fixtures/template/error.html b/test/fixtures/template/error.html deleted file mode 100644 index 1feb22c..0000000 --- a/test/fixtures/template/error.html +++ /dev/null @@ -1 +0,0 @@ -

{{ notItem.title }}

diff --git a/test/fixtures/template/error.sml b/test/fixtures/template/error.sml new file mode 100644 index 0000000..b278375 --- /dev/null +++ b/test/fixtures/template/error.sml @@ -0,0 +1 @@ +p {{ notItem.title }} diff --git a/test/fixtures/template/template.html b/test/fixtures/template/template.html deleted file mode 100644 index 58218ce..0000000 --- a/test/fixtures/template/template.html +++ /dev/null @@ -1 +0,0 @@ -

{{ item.title }}

diff --git a/test/fixtures/template/template.sml b/test/fixtures/template/template.sml new file mode 100644 index 0000000..db25f28 --- /dev/null +++ b/test/fixtures/template/template.sml @@ -0,0 +1 @@ +p {{ item.title }} diff --git a/test/index.js b/test/index.js index 623b346..e8ea64f 100644 --- a/test/index.js +++ b/test/index.js @@ -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: {} } } } @@ -45,10 +45,10 @@ 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/ ) }) @@ -56,10 +56,10 @@ test('errors with "limit" filter under 1', (t) => { 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/ ) }) @@ -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({ @@ -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() }) }) @@ -172,7 +171,6 @@ test.cb('implements default transform function', (t) => { }) }) - test.cb('can disable transform function', (t) => { const locals = {} const api = new Contentful({ @@ -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 === '

fqhi1USjAIuogSS2AKEKu

') // IDs listed in output, sans spaces rimraf.sync(path.join(projectPath, 'public')) t.end() }) @@ -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` } } @@ -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] }) @@ -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(), '

Save The Elephants

') - t.is(file2.trim(), "

Unlocking The Grid

") + t.is(file2.trim(), '

Unlocking The Grid

') rimraf.sync(path.join(projectPath, 'public')) t.end() }) @@ -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` } } @@ -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() })