1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict'
|
10 |
|
11 | const nunjucks = require('nunjucks')
|
12 | const chalk = require('chalk')
|
13 | const path = require('path')
|
14 |
|
15 | module.exports = function (grunt) {
|
16 | grunt.registerMultiTask('nunjucks', `Renders Nunjucks' templates to HTML`, function () {
|
17 |
|
18 | const completeTask = this.async()
|
19 |
|
20 |
|
21 | const options = this.options({
|
22 | watch: false,
|
23 | paths: '',
|
24 | configureEnvironment: false,
|
25 | data: false,
|
26 | preprocessData: false,
|
27 | noCache: true
|
28 | })
|
29 |
|
30 |
|
31 | if (!this.files.length) {
|
32 | grunt.log.error('No files specified.')
|
33 |
|
34 |
|
35 | return completeTask()
|
36 | }
|
37 |
|
38 |
|
39 | if (!options.data) {
|
40 | grunt.log.error(`Template's data is empty. Guess you've forget to specify data option.`)
|
41 | }
|
42 |
|
43 |
|
44 | const env = nunjucks.configure(options.paths, options)
|
45 |
|
46 |
|
47 | if (typeof options.configureEnvironment === 'function') {
|
48 | options.configureEnvironment.call(this, env, nunjucks)
|
49 | }
|
50 |
|
51 |
|
52 | const totalFiles = this.files.length
|
53 |
|
54 | let countCompiled = 0
|
55 |
|
56 |
|
57 | const task = new Promise((resolve, reject) => {
|
58 |
|
59 | this.files.forEach(file => {
|
60 |
|
61 | let filedest = file.dest
|
62 |
|
63 |
|
64 | if (!file.src.length) {
|
65 | grunt.log.error(`No source files specified for ${chalk.cyan(filedest)}.`)
|
66 |
|
67 |
|
68 | return reject(new Error('For some destinations were not specified source files.'))
|
69 | }
|
70 |
|
71 |
|
72 | file.src.forEach(src => {
|
73 |
|
74 | let filepath = path.join(process.cwd(), src)
|
75 |
|
76 | let data = {}
|
77 |
|
78 | for (let i in options.data) {
|
79 | if (options.data.hasOwnProperty(i)) {
|
80 | data[i] = options.data[i]
|
81 | }
|
82 | }
|
83 |
|
84 |
|
85 | if (options.data && typeof options.preprocessData === 'function') {
|
86 | data = options.preprocessData.call(file, data)
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 | env.render(filepath, data, (error, result) => {
|
92 |
|
93 | if (error) {
|
94 | grunt.log.error(error)
|
95 | grunt.fail.warn('Failed to compile one of the source files.')
|
96 | grunt.log.writeln()
|
97 |
|
98 |
|
99 | return reject(new Error('Failed to compile some source files.'))
|
100 | }
|
101 |
|
102 |
|
103 | grunt.file.write(filedest, result)
|
104 |
|
105 |
|
106 | grunt.verbose.ok(`File ${chalk.cyan(filedest)} created.`)
|
107 | grunt.verbose.writeln()
|
108 |
|
109 | countCompiled++
|
110 | })
|
111 | })
|
112 | })
|
113 |
|
114 |
|
115 | resolve()
|
116 | })
|
117 |
|
118 |
|
119 | task.catch(error => {
|
120 | if (error) {
|
121 | grunt.log.writeln()
|
122 | grunt.log.error(error)
|
123 | grunt.log.writeln()
|
124 | }
|
125 | })
|
126 |
|
127 |
|
128 | task.then(success => {
|
129 |
|
130 | let logType = (countCompiled === totalFiles) ? 'ok' : 'error'
|
131 | let countCompiledColor = (logType === 'ok') ? 'green' : 'red'
|
132 | grunt.log[logType](`${chalk[countCompiledColor](countCompiled)}/${chalk.cyan(totalFiles)} ${grunt.util.pluralize(totalFiles, 'file/files')} compiled.`)
|
133 | })
|
134 |
|
135 |
|
136 | completeTask()
|
137 | })
|
138 | }
|