-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
220 lines (185 loc) · 5.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
'use strict';
/**
* > Gulp plugin for [ejs](http://ejs.co/). Version for [Wezom](wezom.com.ua) projects.
* The project is inspired by [ejs-locals](https://github.com/RandomEtc/ejs-locals)
* @module
* @author Oleg Dutchenko <dutchenko.o.dev@gmail.com>
* @version 3.1.2
*/
// ----------------------------------------
// Imports
// ----------------------------------------
// modules
const ejs = require('ejs');
const chalk = require('chalk');
const lodash = require('lodash');
const through2 = require('through2');
const PluginError = require('plugin-error');
const notSupportedFile = require('gulp-not-supported-file');
const rewrite = require('rewrite-ext');
// data
const pkg = require('./package.json');
// utils
const setupOptions = require('./utils/setup-options');
const HistoryStorage = require('./utils/history-storage');
const crashed = require('./utils/crashed');
// methods
const createSetLayoutMethod = require('./methods/set-layout');
const createWidgetMethod = require('./methods/widget');
const createRequireNodeModuleMethod = require('./methods/require-node-module');
const createRequireMethod = require('./methods/require');
const createIncludeMethod = require('./methods/include');
const createBlockMethod = require('./methods/block');
// ----------------------------------------
// Private
// ----------------------------------------
/**
* Create new plugin error
* @const {Function}
* @param {Object} data
* @param {Object} [options={}]
* @return {PluginError}
*/
const pluginError = (data, options) => new PluginError(pkg.name, data, options);
/**
* History storage
* @const {DataStorage}
* @private
*/
let storage = new HistoryStorage();
/**
* All gathered options
* @const {Object}
* @private
*/
const configs = {};
// ----------------------------------------
// Public
// ----------------------------------------
/**
* Core plugin method
* @param {Object} [opts={}]
* @returns {DestroyableTransform}
*/
function gulpEjsMonster (opts = {}) {
let config = configs[opts.__UNIQUE_KEY__];
if (config === undefined) {
let configOpts = setupOptions(opts);
let key = configOpts.__UNIQUE_KEY__;
configs[key] = {
options: configOpts,
data: lodash.merge({}, configOpts.locals, {
setLayout: createSetLayoutMethod(configOpts, storage),
widget: createWidgetMethod(configOpts, storage),
include: createIncludeMethod(configOpts, storage),
require: createRequireMethod(configOpts, storage),
requireNodeModule: createRequireNodeModuleMethod(storage),
/**
* List of blocks
* @memberOf locals
*/
blocks: {}
}),
ejs: lodash.merge({}, configOpts.ejs)
};
delete configs[key].options.locals;
delete configs[key].options.ejs;
config = configs[key];
config.data.block = createBlockMethod(config.data.blocks, storage);
opts.__UNIQUE_KEY__ = key;
}
const options = config.options;
const data = config.data;
const ejsOptions = config.ejs;
/**
* Read buffer and transform
* @param {Buffer} file
* @param {...*} args
*/
function readBuffer (file, ...args) {
let cb = args[1];
let notSupported = notSupportedFile(file, pluginError);
if (Array.isArray(notSupported)) {
notSupported.shift();
return cb(notSupported[0], notSupported[1]);
}
createBlockMethod.clearAllBlocks(data.blocks);
storage.reset();
storage.push(chalk.green('Render history:'));
storage.push(chalk.green('Start'));
storage.push('render view', file.path);
data.viewName = file.stem;
data.viewPath = file.path;
renderFile(file.path);
/**
* Render given file
* @param {string} filePath - resolved file path
*/
function renderFile (filePath) {
data.fileChanged = true;
ejs.renderFile(filePath, data, ejsOptions, (error, markup) => {
// if get error - try to detect what's went wrong
if (error) {
if (ejsOptions.compileDebug) {
crashed(error, storage, ejsOptions);
return cb(error);
}
if (!data.layout) {
createBlockMethod.clearAllBlocks(data.blocks);
}
crashed.reRenderLog(filePath, storage);
return ejs.renderFile(filePath, data, lodash.merge(ejsOptions, {compileDebug: true}), (err) => {
crashed(err, storage, ejsOptions);
return cb(err);
});
}
// if file has layout - render
if (data.layout) {
let layoutPath = data.layout;
storage.indent('<<<<');
storage.push('> render layout', layoutPath, '>');
data.body = markup;
delete data.layout;
return renderFile(layoutPath);
}
// after render
if (options.afterRender) {
let postMarkup = options.afterRender(markup, file, storage.paths.concat([]));
if (typeof postMarkup === 'string') {
markup = postMarkup;
}
}
if (options.showHistory) {
console.log(storage.print());
console.log(chalk.green('Done!\n'));
}
// change file data
file.contents = Buffer.from(markup);
if (file.extname) {
file.extname = options.extname;
} else {
file.path = rewrite(file.path, options.extname);
}
// all done - go out
return cb(null, file);
});
}
}
return through2.obj(readBuffer);
}
/**
* Plugin name
* @type {string}
*/
gulpEjsMonster.pluginName = pkg.name;
/**
* Prevention failing process
* @type {Function}
*/
gulpEjsMonster.preventCrash = function () {
this.emit('end');
};
// ----------------------------------------
// Exports
// ----------------------------------------
module.exports = gulpEjsMonster;