@modernpoacher/nunjucks 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +4 -0
- package/README.md +88 -0
- package/babel.config.js +41 -0
- package/build/gulp/index.js +78 -0
- package/index.js +50 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# @modernpoacher/nunjucks
|
|
2
|
+
|
|
3
|
+
[Nunjucks](http://mozilla.github.io/nunjucks/api.html) for [Hapi](https://hapi.dev/api).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
The example assumes:
|
|
8
|
+
|
|
9
|
+
* templates are in a directory named "views"
|
|
10
|
+
* templates have the extension "html"
|
|
11
|
+
* there is a template named "mytemplate"
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const Hapi = require('hapi')
|
|
15
|
+
const path = require('path')
|
|
16
|
+
const Vision = require('vision')
|
|
17
|
+
const Nunjucks = require('@modernpoacher/nunjucks')
|
|
18
|
+
|
|
19
|
+
const server = Hapi.server({ host, port })
|
|
20
|
+
|
|
21
|
+
server.register(Vision)
|
|
22
|
+
.then(() => {
|
|
23
|
+
server.views({
|
|
24
|
+
relativeTo: currentDir,
|
|
25
|
+
path: path.join(__dirname, 'views'),
|
|
26
|
+
engines: {
|
|
27
|
+
html: {
|
|
28
|
+
module: Nunjucks
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
server.route({
|
|
35
|
+
method: 'GET',
|
|
36
|
+
path: '/test',
|
|
37
|
+
handler: (request, h) => (
|
|
38
|
+
h.view('mytemplate', {
|
|
39
|
+
myvariable: 'myvalue'
|
|
40
|
+
})
|
|
41
|
+
)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
.then(() => server.start())
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### With Nunjucks filters, etc
|
|
48
|
+
|
|
49
|
+
To go beyond the default configuration of Nunjucks, you must configure an `environment`. (A `viewPath` is _required_.)
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const Nunjucks = require('@modernpoacher/nunjucks');
|
|
53
|
+
|
|
54
|
+
// set the view path
|
|
55
|
+
const viewPath = path.join(__dirname, 'views')
|
|
56
|
+
|
|
57
|
+
const env = Nunjucks.configure(viewPath)
|
|
58
|
+
|
|
59
|
+
// do anything you want to the env here
|
|
60
|
+
env.addFilter('somefilter', (str, count) => {
|
|
61
|
+
// return some string
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
server.register(Vision)
|
|
65
|
+
.then(() => {
|
|
66
|
+
server.views({
|
|
67
|
+
relativeTo: currentDir,
|
|
68
|
+
path: viewPath,
|
|
69
|
+
engines: {
|
|
70
|
+
html: {
|
|
71
|
+
module: Nunjucks
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
server.route({
|
|
78
|
+
method: 'GET',
|
|
79
|
+
path: '/test',
|
|
80
|
+
handler: (request, h) => (
|
|
81
|
+
h.view('mytemplate', {
|
|
82
|
+
myvariable: 'myvalue'
|
|
83
|
+
})
|
|
84
|
+
)
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
.then(() => server.start())
|
|
88
|
+
```
|
package/babel.config.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const debug = require('debug')
|
|
2
|
+
|
|
3
|
+
const log = debug('@modernpoacher/nunjucks')
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
env: {
|
|
7
|
+
NODE_ENV = 'development'
|
|
8
|
+
}
|
|
9
|
+
} = process
|
|
10
|
+
|
|
11
|
+
log('`modernpoacher/nunjucks` is awake')
|
|
12
|
+
|
|
13
|
+
function env () {
|
|
14
|
+
log({ NODE_ENV })
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
NODE_ENV === 'production'
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const presets = [
|
|
22
|
+
[
|
|
23
|
+
'@babel/env', {
|
|
24
|
+
targets: {
|
|
25
|
+
node: '12.19.0'
|
|
26
|
+
},
|
|
27
|
+
useBuiltIns: 'usage',
|
|
28
|
+
corejs: 3
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
module.exports = (api) => {
|
|
34
|
+
if (api) api.cache.using(env)
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
compact: true,
|
|
38
|
+
comments: false,
|
|
39
|
+
presets
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import debug from 'debug'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
exec
|
|
5
|
+
} from 'child_process'
|
|
6
|
+
|
|
7
|
+
const log = debug('@modernpoacher/nunjucks:build:gulp:pre-commit')
|
|
8
|
+
|
|
9
|
+
log('`@modernpoacher/nunjucks` is awake')
|
|
10
|
+
|
|
11
|
+
const PACKAGE_VERSION_CHANGES = /-+\s+"version":\s"(\d+\.\d+\.\d+)",\s+\++\s+"version":\s"(\d+\.\d+\.\d+)",\s+/s
|
|
12
|
+
|
|
13
|
+
const HAS_STAGED_CHANGES = /Changes to be committed/s
|
|
14
|
+
|
|
15
|
+
const NOT_STAGED_CHANGES = /Changes not staged for commit/s
|
|
16
|
+
|
|
17
|
+
const OPTIONS = {
|
|
18
|
+
maxBuffer: 1024 * 500
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const hasPackageVersionChanges = () => (
|
|
22
|
+
new Promise((resolve, reject) => {
|
|
23
|
+
exec('git diff HEAD origin/master package.json', OPTIONS, (e, v) => (!e) ? resolve(PACKAGE_VERSION_CHANGES.test(v)) : reject(e))
|
|
24
|
+
})
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
export const notPackageVersionChanges = () => (
|
|
28
|
+
new Promise((resolve, reject) => {
|
|
29
|
+
exec('git diff HEAD origin/master package.json', OPTIONS, (e, v) => (!e) ? resolve(PACKAGE_VERSION_CHANGES.test(v) !== true) : reject(e))
|
|
30
|
+
})
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
export const hasStagedChanges = () => (
|
|
34
|
+
new Promise((resolve, reject) => {
|
|
35
|
+
exec('git status', OPTIONS, (e, v) => (!e) ? resolve(HAS_STAGED_CHANGES.test(v)) : reject(e))
|
|
36
|
+
})
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
export const notStagedChanges = () => (
|
|
40
|
+
new Promise((resolve, reject) => {
|
|
41
|
+
exec('git status', OPTIONS, (e, v) => (!e) ? resolve(NOT_STAGED_CHANGES.test(v)) : reject(e))
|
|
42
|
+
})
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
export const notPushedChanges = () => (
|
|
46
|
+
new Promise((resolve, reject) => {
|
|
47
|
+
exec('git log origin/master..HEAD', OPTIONS, (e, v) => (!e) ? resolve(!!v) : reject(e))
|
|
48
|
+
})
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
export const patchPackageVersion = () => (
|
|
52
|
+
new Promise((resolve, reject) => {
|
|
53
|
+
exec('npm version patch -m %s -n --no-git-tag-version --no-verify', OPTIONS, (e) => (!e) ? resolve() : reject(e))
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
export const addPackageVersionChanges = () => (
|
|
58
|
+
new Promise((resolve, reject) => {
|
|
59
|
+
exec('git add package.json package-lock.json', OPTIONS, (e) => (!e) ? resolve() : reject(e))
|
|
60
|
+
})
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
export default async function preCommit () {
|
|
64
|
+
log('pre-commit')
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
if (await notStagedChanges()) return
|
|
68
|
+
|
|
69
|
+
if (await hasStagedChanges()) {
|
|
70
|
+
if (await notPackageVersionChanges()) {
|
|
71
|
+
await patchPackageVersion()
|
|
72
|
+
await addPackageVersionChanges()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch ({ code = 'NONE', message = 'No error message defined' }) {
|
|
76
|
+
log({ code, message })
|
|
77
|
+
}
|
|
78
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const Nunjucks = require('nunjucks')
|
|
2
|
+
|
|
3
|
+
const debug = require('debug')
|
|
4
|
+
|
|
5
|
+
const log = debug('@modernpoacher/nunjucks')
|
|
6
|
+
|
|
7
|
+
log('`modernpoacher/nunjucks` is awake')
|
|
8
|
+
|
|
9
|
+
let ENVIRONMENT
|
|
10
|
+
|
|
11
|
+
function compile (src, options = {}, next = null) {
|
|
12
|
+
log('compile')
|
|
13
|
+
|
|
14
|
+
const template = Nunjucks.compile(src, ENVIRONMENT || (ENVIRONMENT = options.environment), (Reflect.has(options, 'filename') ? Reflect.get(options, 'filename') : null))
|
|
15
|
+
|
|
16
|
+
return (next instanceof Function)
|
|
17
|
+
? next(null, (context, options, next) => template.render(context, next))
|
|
18
|
+
: (context) => template.render(context)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function prepare (options = {}, next = () => {}) {
|
|
22
|
+
log('prepare')
|
|
23
|
+
|
|
24
|
+
const {
|
|
25
|
+
path,
|
|
26
|
+
compileOptions = {}
|
|
27
|
+
} = options
|
|
28
|
+
|
|
29
|
+
options.compileOptions = {
|
|
30
|
+
...compileOptions,
|
|
31
|
+
environment: ENVIRONMENT || configure(path, { watch: false })
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return next()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function configure (path, options = { watch: false }) {
|
|
38
|
+
log('configure')
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
ENVIRONMENT = Nunjucks.configure(path, options)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
...Nunjucks,
|
|
47
|
+
compile,
|
|
48
|
+
prepare,
|
|
49
|
+
configure
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@modernpoacher/nunjucks",
|
|
3
|
+
"version": "3.0.8",
|
|
4
|
+
"description": "Nunjucks for Hapi",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hapi",
|
|
7
|
+
"nunjucks"
|
|
8
|
+
],
|
|
9
|
+
"main": "./index.js",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=12.19.0"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/modernpoacher/nunjucks.git"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"lint": "eslint .",
|
|
20
|
+
"lint:fix": "npm run lint -- --fix",
|
|
21
|
+
"gulp": "gulp --color",
|
|
22
|
+
"pre-commit": "npm run lint && git add *.js && npm run gulp -- pre-commit"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"debug": "^4.3.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@babel/cli": "^7.15.7",
|
|
29
|
+
"@babel/core": "^7.15.5",
|
|
30
|
+
"@babel/eslint-parser": "^7.15.7",
|
|
31
|
+
"@babel/preset-env": "^7.15.6",
|
|
32
|
+
"@babel/register": "^7.15.3",
|
|
33
|
+
"babel-loader": "^8.2.2",
|
|
34
|
+
"core-js": "^3.18.0",
|
|
35
|
+
"eslint": "^7.32.0",
|
|
36
|
+
"eslint-config-standard": "^16.0.3",
|
|
37
|
+
"eslint-plugin-import": "^2.24.2",
|
|
38
|
+
"eslint-plugin-node": "^11.1.0",
|
|
39
|
+
"eslint-plugin-promise": "^5.1.0",
|
|
40
|
+
"gulp": "^4.0.2",
|
|
41
|
+
"husky": "^7.0.2"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"nunjucks": "^3.2.0"
|
|
45
|
+
}
|
|
46
|
+
}
|