feat(theme): support for themes published within a subfolder

If a theme has a build process, it will likely publish a subfolder (e.g. “dist”) to npm.
It can simply set “thumbsup.themeRoot” in its package.json so thumbsup looks in the right place.
pull/118/head
Romain 6 years ago
parent 00dac76983
commit 39591c70a1

@ -18,6 +18,9 @@ class Theme {
// load all theme helpers
// and copy assets into the output folder (static files, CSS...)
prepare (done) {
this.options = this.loadPackageOptions()
this.dir = path.join(this.dir, this.options.themeRoot || '')
// validate that the theme looks well structured
this.validateStructure()
// compiled template
this.template = compileTemplate(path.join(this.dir, 'album.hbs'))
@ -29,6 +32,19 @@ class Theme {
], done)
}
// look for an explicit root in the theme's <package.json>
loadPackageOptions () {
try {
const packagePath = path.join(this.dir, 'package.json')
const contents = fs.readFileSync(packagePath).toString()
const pkg = JSON.parse(contents)
return pkg.thumbsup || {}
} catch (ex) {
debug(`Theme does not have a package.json, using default options`)
return {}
}
}
// make sure the given folder is a valid theme
validateStructure () {
const template = fs.existsSync(path.join(this.dir, 'album.hbs'))

@ -1,4 +1,5 @@
const fs = require('fs-extra')
const path = require('path')
const should = require('should/as-function')
const fixtures = require('../fixtures')
const Theme = require('../../src/website/theme')
@ -14,6 +15,58 @@ describe('Theme', () => {
mock.restore()
})
it('uses the target theme folder by default', () => {
mock({
'theme/album.hbs': '',
'theme/theme.less': ''
})
const theme = new Theme('theme', 'dest', {})
theme.prepare(err => {
should(err).eql(null)
should(theme.dir).eql(path.resolve('theme'))
})
})
it('can use a sub-folder specified in package.json', () => {
// 1. this is useful when a packaged theme uses a build process
// since the build result will be in a subfolder, and npm only allows publishing the root folder
// 2. it's also useful if someone just wants to keep the theme repo clean using folders
mock({
'theme/package.json': `{
"thumbsup": {
"themeRoot": "dist"
}
}`,
'theme/dist/album.hbs': '',
'theme/dist/theme.less': ''
})
const theme = new Theme('theme', 'dest', {})
theme.prepare(err => {
should(err).eql(null)
should(theme.dir).eql(path.resolve('theme/dist'))
})
})
it('can have an empty themeRoot', () => {
// 1. this is useful when a packaged theme uses a build process
// since the build result will be in a subfolder, and npm only allows publishing the root folder
// 2. it's also useful if someone just wants to keep the theme repo clean using folders
mock({
'theme/package.json': `{
"thumbsup": {
"themeRoot": ""
}
}`,
'theme/album.hbs': '',
'theme/theme.less': ''
})
const theme = new Theme('theme', 'dest', {})
theme.prepare(err => {
should(err).eql(null)
should(theme.dir).eql(path.resolve('theme'))
})
})
it('throws an error if it doesnt have the mandatory HBS template', () => {
mock({
'theme/theme.less': ''

Loading…
Cancel
Save