Skip to content

Commit

Permalink
fix for spike@next
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Feb 13, 2017
1 parent 1948cf3 commit 8749013
Show file tree
Hide file tree
Showing 4 changed files with 870 additions and 864 deletions.
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require('fs')
const path = require('path')
const reshape = require('reshape')
const loader = require('reshape-loader')
const SpikeUtil = require('spike-util')
const bindAll = require('es6bindall')

// this needs to be a function that returns a function
Expand All @@ -16,6 +17,8 @@ module.exports = class Records {
}

apply (compiler) {
this.util = new SpikeUtil(compiler.options)

compiler.plugin('run', run.bind(this, compiler))
compiler.plugin('watch-run', run.bind(this, compiler))

Expand Down Expand Up @@ -50,7 +53,7 @@ function run (compiler, compilation, done) {
// templates need to be ignored as they often contain extra variables
templates.map((t) => {
const ignorePath = path.join(compiler.options.context, t)
compiler.options.spike.ignore.push(ignorePath)
this.util.getSpikeOptions().ignore.push(ignorePath)
})

keys.all(tasks)
Expand Down Expand Up @@ -101,7 +104,7 @@ function writeTemplates (compilation, compiler, _data, k) {
filename: path.join(root, tpl.path)
})

const options = loader.parseOptions.call(this.loaderContext, compiler.options.reshape, {})
const options = loader.parseOptions.call(this.loaderContext, this.util.getSpikeOptions().reshape, {})

return reshape(options)
.process(template)
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
"dependencies": {
"es6bindall": "^0.0.9",
"reshape": "^0.4.0",
"reshape-loader": "^0.4.0",
"reshape-loader": "next",
"rest": "^2.0.0",
"spike-util": "next",
"when": "^3.7.7"
},
"devDependencies": {
"ava": "^0.18.0",
"coveralls": "^2.11.15",
"coveralls": "^2.11.16",
"nyc": "^10.1.2",
"posthtml-exp": "^0.9.0",
"reshape-standard": "next",
"rimraf": "^2.5.4",
"spike-core": "^0.13.3",
"spike-html-standards": "^0.3.0",
"spike-core": "next",
"standard": "^8.0.0"
},
"engines": {
Expand Down
65 changes: 32 additions & 33 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,52 @@ const path = require('path')
const fs = require('fs')
const Records = require('..')
const rimraf = require('rimraf')
const htmlStandards = require('spike-html-standards')
const htmlStandards = require('reshape-standard')

const fixturesPath = path.join(__dirname, 'fixtures')

test.cb('loads data correctly', (t) => {
test('loads data correctly', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'data',
locals: locals,
config: { addDataTo: locals, test: { data: { result: 'true' } } },
verify: (_, publicPath, cb) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>true</p>')
cb()
}
})
})

test.cb('loads a file correctly', (t) => {
test('loads a file correctly', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'data',
locals: locals,
config: { addDataTo: locals, test: { file: '../testFile.json' } },
verify: (_, publicPath, cb) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>true</p>')
cb()
}
})
})

test.cb('loads a url correctly', (t) => {
test('loads a url correctly', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'data',
locals: locals,
config: { addDataTo: locals, test: { url: 'http://api.bycarrot.com/staff' } },
verify: (_, publicPath, cb) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>true</p>')
cb()
}
})
})

test.cb('transform option works', (t) => {
test('transform option works', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'data',
locals: locals,
config: {
Expand All @@ -69,7 +62,6 @@ test.cb('transform option works', (t) => {
verify: (_, publicPath, cb) => {
const out = fs.readFileSync(path.join(publicPath, 'index.html'), 'utf8')
t.is(out.trim(), '<p>false</p>')
cb()
}
})
})
Expand Down Expand Up @@ -137,10 +129,9 @@ test.cb('single template errors with non-array data', (t) => {
project.compile()
})

test.cb('single template works with "path" and "template" params', (t) => {
test('single template works with "path" and "template" params', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'template',
locals: locals,
config: {
Expand All @@ -160,15 +151,13 @@ test.cb('single template works with "path" and "template" params', (t) => {
t.is(index.trim(), '<p>2</p>')
t.is(wow.trim(), '<p>wow</p>')
t.is(amaze.trim(), '<p>amaze</p>')
cb()
}
})
})

test.cb('single template works with "transform" param', (t) => {
test('single template works with "transform" param', (t) => {
const locals = {}
compileAndCheck({
test: t,
return compileAndCheck({
fixture: 'template',
locals: locals,
config: {
Expand All @@ -189,7 +178,6 @@ test.cb('single template works with "transform" param', (t) => {
t.is(index.trim(), '<p>undefined</p>') // bc the transform is not global
t.is(wow.trim(), '<p>wow</p>')
t.is(amaze.trim(), '<p>amaze</p>')
cb()
}
})
})
Expand All @@ -204,24 +192,35 @@ function configProject (fixturePath, recordsConfig, locals) {
root: projectPath,
entry: { main: path.join(projectPath, 'app.js') },
matchers: { html: '*(**/)*.sgr' },
reshape: (ctx) => htmlStandards({ webpack: ctx, locals }),
reshape: htmlStandards({ locals: () => { return locals } }),
ignore: ['template.sgr'],
plugins: [new Records(recordsConfig)]
})
return { projectPath, project }
}

function compileProject (project, t, cb) {
project.on('error', t.end)
project.on('warning', t.end)
project.on('compile', cb)
project.compile()
function compileProject (project) {
return new Promise((resolve, reject) => {
project.on('error', reject)
project.on('warning', reject)
project.on('compile', resolve)
project.compile()
})
}

function compileAndCheck (opts) {
const {projectPath, project} = configProject(opts.fixture, opts.config, opts.locals)
const publicPath = path.join(projectPath, 'public')
compileProject(project, opts.test, (data) => {
opts.verify(data, publicPath, () => { rimraf(publicPath, opts.test.end) })
return compileProject(project)
.then((data) => opts.verify(data, publicPath))
.then(() => rimrafPromise(publicPath))
}

function rimrafPromise (dir) {
return new Promise((resolve, reject) => {
rimraf(dir, (err) => {
if (err) return reject(err)
resolve()
})
})
}
Loading

0 comments on commit 8749013

Please sign in to comment.