-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
146 lines (123 loc) · 2.91 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
const browerSync = require('browser-sync');
const del = require('del');
const gulp = require('gulp');
const gulpHtmlmin = require('gulp-htmlmin');
const gulpIf = require('gulp-if');
const gulpTerser = require('gulp-terser');
const path = require('path');
const nodemon = require('nodemon');
const browserSync = require('browser-sync');
const gulpNodeRedNodes = require('./gulp-node-red-nodes');
let nodemonInstance;
let browserSyncInstance;
let config = {
nodeRedUrl: 'http://localhost:1880',
dist: path.resolve(__dirname, 'nodes'),
src: path.resolve(__dirname, 'src'),
nodeNamePrefix: 'connio',
};
function copyAssets() {
let srcGlobs = [
`${config.src}/**/*`,
`!${config.src}/**/ui/**`,
`!${config.src}/**/backend/**`,
];
return gulp.src(srcGlobs).pipe(gulp.dest(config.dist));
}
function copyBackendNode() {
let srcGlobs = [
`${config.src}/**/backend/**/*.js`,
`!${config.src}/shared/**/*`,
];
return gulp.src(srcGlobs).pipe(
gulp.dest((file) => {
let nodeName = file.path.split(config.src)[1].split(path.sep)[1];
file.path = path.join(file.base, nodeName, file.basename);
return config.dist;
}),
);
}
function buildUINode() {
let srcGlobs = [
`${config.src}/**/ui/**/*.js`,
`${config.src}/**/ui/**/*.html`,
`!${config.src}/shared/**/*`,
];
return gulp
.src(srcGlobs)
.pipe(
gulpIf(
(file) => file.extname === '.js',
gulpTerser({
compress: {
drop_debugger: false,
},
}),
),
)
.pipe(gulpNodeRedNodes(config.nodeNamePrefix))
.pipe(
gulpHtmlmin({
processScripts: ['text/x-red'],
collapseWhitespace: true,
minifyCSS: true,
}),
)
.pipe(gulp.dest(config.dist));
}
function runNodemonAndBrowserSync(cb) {
nodemonInstance = nodemon(`
nodemon
--ignore **/*
--exec node-red -u .node-red
`);
nodemon
.on('start', () => {
if (!browserSyncInstance) {
return;
}
browserSyncInstance.reload();
})
.once('start', () => {
browserSyncInstance = browserSync.create();
browserSyncInstance.init({
ui: false,
proxy: {
target: config.nodeRedUrl,
ws: true,
},
ghostMode: false,
open: false,
reloadDelay: 5000,
});
})
.on('quit', () => process.exit(0));
cb();
}
async function cleanDist() {
await del(config.dist);
}
function restartNodemon(cb) {
nodemonInstance.restart();
cb();
}
const buildNodeRedNodes = gulp.parallel(
copyAssets,
copyBackendNode,
buildUINode,
);
module.exports = {
build: gulp.series(cleanDist, buildNodeRedNodes),
start: gulp.series(
cleanDist,
buildNodeRedNodes,
runNodemonAndBrowserSync,
function watcher(cb) {
gulp.watch(
`${config.src}/**/*`,
gulp.series(buildNodeRedNodes, restartNodemon),
);
cb();
},
),
};