-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
324 lines (281 loc) · 8.43 KB
/
gulpfile.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const del = require('del');
const fs = require('fs');
const path = require('path');
// General
const concat = require('gulp-concat');
const gulpIf = require('gulp-if');
const lazypipe = require('lazypipe');
const merge = require('merge-stream');
const wrap = require('gulp-wrap');
const { src, dest, series, task, watch } = require('gulp');
const browserSync = require('browser-sync');
const nodemon = require('nodemon');
// HTML
const gulpHtmlmin = require('gulp-htmlmin');
// Scripts
const terser = require('gulp-terser');
// Styles
const minify = require('cssnano');
const postcss = require('gulp-postcss');
const prefix = require('autoprefixer');
const sass = require('gulp-sass')(require('sass'));
// Markdown-It
const cheerio = require('gulp-cheerio');
const markdownIt = require('gulp-markdownit');
const markdownitContainer = require('markdown-it-container');
const markdownitInlineComments = require('markdown-it-inline-comments');
const md = require('markdown-it')();
// Constants
const docsUrl = 'https://github.com/KevinBonnoron/node-red-contrib-freebox';
const editorFilePath = 'src/nodes';
const uiCssWrap = '<style><%= contents %></style>';
const uiJsWrap = '<script type="text/javascript"><%= contents %></script>';
const uiFormWrap = '<script type="text/x-red" data-template-name="<%= data.type %>"><%= data.contents %></script>';
const uiHelpWrap = '<script type="text/x-red" data-help-name="<%= data.type %>"><%= data.contents %></script>';
const nodeMap = {
'freebox-server': { doc: 'freebox-server', type: 'freebox-server' },
connection: { doc: 'connection', type: 'connection' },
'lan-browser': { doc: 'lan-browser', type: 'lan-browser' },
api: { doc: 'api', type: 'api' },
poll: { doc: 'poll', type: 'poll' },
};
let nodemonInstance;
let browserSyncInstance;
let currentFolder;
function getFolders(dir) {
return fs.readdirSync(dir).filter((file) => fs.statSync(path.join(dir, file)).isDirectory());
}
// Compile sass and wrap it
const buildSass = lazypipe()
.pipe(sass, {
outputStyle: 'expanded',
sourceComments: true,
})
.pipe(postcss, [
prefix({
cascade: true,
remove: true,
}),
minify({
discardComments: {
removeAll: true,
},
}),
])
.pipe(wrap, uiCssWrap);
// Shrink js and wrap it
const buildJs = lazypipe().pipe(terser).pipe(wrap, uiJsWrap);
const buildForm = lazypipe()
.pipe(gulpHtmlmin, {
collapseWhitespace: true,
minifyCSS: true,
})
.pipe(() =>
wrap(
uiFormWrap,
{ type: nodeMap[currentFolder].type },
{ variable: 'data' }
)
);
// Covert markdown documentation to html and modify it to look more like Node-RED
// help files.
const buildHelp = lazypipe()
.pipe(markdownIt, {
options: { html: true },
plugins: [
[
markdownitContainer,
'vuepress-custom-container',
{
validate: function (params) {
return params
.trim()
.match(/^(?:tip|warning|danger)\s?(.*)$/);
},
render: function (tokens, idx) {
const m = tokens[idx].info
.trim()
.match(/^(tip|warning|danger)\s?(.*)$/);
if (tokens[idx].nesting === 1) {
let title = 'INFO';
switch (m[1]) {
case 'warning':
case 'danger':
title = 'WARNING';
break;
}
// opening tag
return `<div class="custom-block ${
m[1]
}">\n<p class="custom-block-title">${md.utils.escapeHtml(
m[2] || title
)}</p>\n`;
} else {
// closing tag
return '</div>\n';
}
},
},
],
markdownitInlineComments,
],
})
.pipe(cheerio, ($) => {
$.prototype.wrapAll = function (wrapper) {
const $container = $(wrapper).clone();
$(this).eq(0).before($container);
for (const i in this) {
const clone = $(this).eq(i).clone();
$container.append($('<div>' + clone + '</div>').html());
$(this).eq(i).remove();
}
};
// Make things look more like Node-RED docs.
// Handle <badge />
$('badge').each((i, item) => {
item.tagName = 'span';
const $item = $(item)
.addClass('badge')
.text(item.attribs.text || item.attribs.type);
if (item.attribs.type) {
$item.addClass(item.attribs.type);
}
return item;
});
// Remove H1 title
$('h1').remove();
// increase header element by one to conform to Node-RED titles being <h3></h3>
$('h2,h3,h4,h5').each(
(i, item) => (item.tagName = `h${Number(item.tagName[1]) + 1}`)
);
// Change relative links to the full address of docs
$('a').each((i, item) => {
const href = $(item).attr('href');
if (href.startsWith('.') || href.startsWith('/')) {
$(item)
.attr('href', `${docsUrl}${href.replace('.md', '.html')}`)
.attr('rel', 'noopener noreferrer');
}
});
$('h3').each((i, item) => {
const h3 = $(item);
const items = h3.nextUntil('h3');
// Remove Changelog from the end of the file
if (h3.text().toLowerCase() !== 'Changelog'.toLowerCase()) {
items.wrapAll('<dl class="message-properties" />');
} else {
h3.remove();
items.remove();
}
});
// Convert <p> inside <dl> to <dd>
$('dl > p').each((i, item) => (item.tagName = 'dd'));
// h4 is the property name and the first li will contain the type
// Pattern: h4 > ul > li
$('h4').each((i, item) => {
const li = $(item).next().find('li').eq(0);
const property = li.find('code').text();
$(item).append(`<span class="property-type">${property}</span>`);
li.remove();
item.tagName = 'dt';
return item;
});
// Remove empty <ul></ul>
$('ul').each((i, item) => {
if ($(item).find('li').length === 0) {
$(item).remove();
}
});
})
.pipe(gulpHtmlmin, {
collapseWhitespace: true,
minifyCSS: true,
})
.pipe(() =>
wrap(
uiHelpWrap,
{ type: nodeMap[currentFolder].type },
{ variable: 'data' }
)
);
task('buildEditorFiles', (done) => {
const folders = getFolders(editorFilePath);
if (folders.length === 0) return done();
const tasks = folders.map((folder) => {
currentFolder = folder;
return src([
`src/nodes/${folder}/ui-*.+(js|html)`,
`docs/nodes/${nodeMap[folder].doc}.md`,
])
.pipe(gulpIf((file) => file.extname === '.scss', buildSass()))
.pipe(gulpIf((file) => file.extname === '.js', buildJs()))
.pipe(gulpIf((file) => file.extname === '.html', buildForm()))
.pipe(gulpIf((file) => file.extname === '.md', buildHelp()))
.pipe(concat(folder + '.html'))
.pipe(dest(editorFilePath + '/' + folder));
});
return merge(tasks);
});
// Clean generated files
task('cleanFiles', (done) => {
del.sync(['nodes/*/*.html', '!nodes/*/ui-*.html'], { onlyFiles: true });
return done();
});
// nodemon and browser-sync code modified from
// https://github.com/connio/node-red-contrib-connio/blob/master/gulpfile.js
function runNodemonAndBrowserSync(done) {
nodemonInstance = nodemon(`
nodemon
--ignore **/*
--exec node-red -u ~/.node-red
`);
nodemon
.once('start', () => {
browserSyncInstance = browserSync.create();
browserSyncInstance.init({
ui: false,
proxy: {
target: 'http://localhost:1880',
ws: true,
},
ghostMode: false,
open: false,
reloadDelay: 3000,
});
})
.on('quit', () => process.exit(0));
done();
}
function restartNodemon(done) {
nodemonInstance.restart();
done();
}
function restartNodemonAndBrowserSync(done) {
nodemonInstance.restart();
browserSyncInstance.reload();
done();
}
module.exports = {
build: series('cleanFiles', 'buildEditorFiles'),
start: series(
'cleanFiles',
'buildEditorFiles',
runNodemonAndBrowserSync,
function watcher(done) {
watch(
['src/common/*', 'src/nodes/*/ui-*', 'src/nodes/*/*.json', 'docs/node/*.md'],
series(
'cleanFiles',
'buildEditorFiles',
restartNodemonAndBrowserSync
)
);
// only server side files modified restart node-red only
watch(
['src/common/*', 'src/nodes/*/*.js', '!src/nodes/*/ui-*.js'],
restartNodemon
);
done();
}
),
};