Skip to content

Commit 84446b4

Browse files
committed
Support for loading plugins
1 parent dac01c6 commit 84446b4

File tree

21 files changed

+198
-2
lines changed

21 files changed

+198
-2
lines changed

.thought/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: [
3+
require('thought-plugin-jsdoc')
4+
]
5+
}

.thought/partials/usage.md.hbs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ Thought implicitly uses the [promised-handlebars](https://github.com/nknapp/prom
116116
to allow helpers that return promises. So, in your helpers, you will never receive a promise as parameter or
117117
as context data, but if you return promises, they will be resolved seamlessly.
118118

119+
## Plugin modules
120+
121+
The customizations described above can be extracted into reusable modules. A thought-plugin is just a
122+
{{npm 'customize'}}-module that is loaded using the [customize.load()](https://github.com/bootprint/customize#module_customize..Customize+load)
123+
function.
124+
125+
### Authoring modules
126+
127+
Have a look at {{npm 'customize-engine-handlebars'}} for an example on how to write a module. For
128+
a real example have a look at {{npm 'thought-plugin-jsdoc'}}.
129+
130+
### Using modules
131+
132+
* Include you favorite plugins in the `devDependencies` of your `package.json`
133+
* Place a file `config.js` into the `.thought`-folder in your project:
134+
135+
{{include '.thought/snippets/config.js'}}
136+
137+
This file loads multiple plugins that are applied to the `customize`-instance one after another.
138+
139+
You can also load plugins from other-plugins. The following example uses plugins that do not yet exist, but the
140+
application is thinkable:
141+
142+
{{include '.thought/snippets/plugin-loading-plugins.js'}}
143+
144+
This allows you to create building-blocks that can then be assembled to a single plugin which can be included in your
145+
projects...
119146

120147

121148

.thought/snippets/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
plugins: [
3+
// JsDoc-Support
4+
require('thought-plugin-jsdoc'),
5+
// Some other feature plugin
6+
require('thought-plugin-something-else'),
7+
// Plugin containing my (nknapp's) personal preferences
8+
require('thought-plugin-nknapp-preferences')
9+
]
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = function (customize) {
2+
3+
return customize
4+
// jsdoc-support
5+
.load(require('thought-plugin-jsdoc'))
6+
// include open-open-source disclaimer in CONTRIBUTING.md
7+
.load(require('thought-plugin-open-open-source'))
8+
// include standardjs-disclaimer in CONTRIBUTING.md
9+
.load(require('thought-plugin-standardjs'))
10+
}

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,59 @@ Thought implicitly uses the [promised-handlebars](https://github.com/nknapp/prom
222222
to allow helpers that return promises. So, in your helpers, you will never receive a promise as parameter or
223223
as context data, but if you return promises, they will be resolved seamlessly.
224224

225+
## Plugin modules
226+
227+
The customizations described above can be extracted into reusable modules. A thought-plugin is just a
228+
[customize](https://npmjs.com/package/customize)-module that is loaded using the [customize.load()](https://github.com/bootprint/customize#module_customize..Customize+load)
229+
function.
230+
231+
### Authoring modules
232+
233+
Have a look at [customize-engine-handlebars](https://npmjs.com/package/customize-engine-handlebars) for an example on how to write a module. For
234+
a real example have a look at [thought-plugin-jsdoc](https://npmjs.com/package/thought-plugin-jsdoc).
235+
236+
### Using modules
237+
238+
* Include you favorite plugins in the `devDependencies` of your `package.json`
239+
* Place a file `config.js` into the `.thought`-folder in your project:
240+
241+
```js
242+
module.exports = {
243+
plugins: [
244+
// JsDoc-Support
245+
require('thought-plugin-jsdoc'),
246+
// Some other feature plugin
247+
require('thought-plugin-something-else'),
248+
// Plugin containing my (nknapp's) personal preferences
249+
require('thought-plugin-nknapp-preferences')
250+
]
251+
}
252+
253+
```
254+
255+
256+
This file loads multiple plugins that are applied to the `customize`-instance one after another.
257+
258+
You can also load plugins from other-plugins. The following example uses plugins that do not yet exist, but the
259+
application is thinkable:
260+
261+
```js
262+
module.exports = function (customize) {
263+
264+
return customize
265+
// jsdoc-support
266+
.load(require('thought-plugin-jsdoc'))
267+
// include open-open-source disclaimer in CONTRIBUTING.md
268+
.load(require('thought-plugin-open-open-source'))
269+
// include standardjs-disclaimer in CONTRIBUTING.md
270+
.load(require('thought-plugin-standardjs'))
271+
}
272+
273+
```
274+
275+
276+
This allows you to create building-blocks that can then be assembled to a single plugin which can be included in your
277+
projects...
225278

226279

227280

customize.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
'use strict'
99

10-
var path = require('path')
10+
const path = require('path')
11+
const fs = require('fs')
12+
13+
// Default config if the file `.thought/config.js` does not exist
14+
const defaultConfig = {
15+
plugins: []
16+
}
1117

1218
/**
1319
*
@@ -18,6 +24,8 @@ var path = require('path')
1824
*/
1925
module.exports = function createSpec (workingDir) {
2026
return function thoughtSpec (customize) {
27+
const configFile = path.resolve('.thought', 'config.js')
28+
var config = fs.existsSync(configFile) ? require(configFile) : defaultConfig
2129
return customize
2230
.registerEngine('handlebars', require('customize-engine-handlebars'))
2331
.merge({
@@ -35,6 +43,13 @@ module.exports = function createSpec (workingDir) {
3543
}
3644
}
3745
})
46+
// Apply any customization from the config-files (such as loading modules)
47+
.load(function (customize) {
48+
const result = config.plugins.reduce((prev, plugin) => {
49+
return prev.load(plugin)
50+
}, customize)
51+
return result
52+
})
3853
.merge({
3954
handlebars: {
4055
partials: path.join(workingDir, '.thought', 'partials'),

test/fixtures/plugin-module/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path')
2+
3+
module.exports = function plugin (customize) {
4+
return customize.merge({
5+
handlebars: {
6+
partials: path.resolve(__dirname, 'partials'),
7+
templates: path.resolve(__dirname, 'templates'),
8+
helpers: {
9+
npm: function (name) {
10+
return `[${name.toUpperCase()}](https://npmjs.com/package/${name})`
11+
}
12+
}
13+
}
14+
})
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Overridden partial license.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is overridden in the plugin-module
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is overridden in the plugin-module

0 commit comments

Comments
 (0)