-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.babel.js
328 lines (281 loc) · 9.01 KB
/
gulpfile.babel.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
325
326
327
328
import gulp from 'gulp';
import colors from 'ansi-colors';
import log from 'fancy-log';
import PluginError from 'plugin-error';
import notifier from 'node-notifier';
import fs from 'fs';
import path from 'path';
import connect from 'connect';
import serveStatic from 'serve-static';
import Mustache from 'mustache';
import through from 'through2';
import browserify from 'browserify';
import source from 'vinyl-source-stream';
import del from 'del';
import figlet from 'figlet';
import browserSync from 'browser-sync';
import gulpSass from 'gulp-sass';
import {fileURLToPath} from 'node:url';
import dartSass from 'dart-sass';
import { createRequire } from "module";
const pkg = createRequire(import.meta.url)("./package.json");
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Create browserSync instance
//
const browserSyncInstance = browserSync.create();
const sass = gulpSass(dartSass);
/**
* @name default
* @description The default gulp task.
* @param { function } done - Callback that signals the task is complete.
*/
gulp.task( 'default', ( done ) => {
logData( `v${ pkg.version }`, pkg.name );
logData( 'default', pkg.description );
logName( 'default', done );
} );
/**
* @name clean
* @desc Deletes the public directory.
* @return { stream } - A stream containing the deleted `public/` directory.
*/
gulp.task( 'clean', () => del( 'public' ) );
/**
* @name copy:fonts
* @desc Copies `uswds` fonts statically to public.
* @return { stream } - A stream of copied font files.
*/
gulp.task( 'copy:fonts', () => {
return gulp.src( ['node_modules/uswds/dist/fonts/**/*', 'node_modules/font-awesome/fonts/*'] )
.pipe( gulp.dest( 'public/fonts' ) );
} );
/**
* @name stylesheets
* @desc Compiles the Sass files under `source/stylesheets`.
* @return { stream } - A stream of compiled CSS files.
*/
gulp.task( 'stylesheets', gulp.series('copy:fonts', () => {
return gulp.src( 'source/styles/render.scss' )
.pipe( sass().on( 'error', sass.logError ) )
.pipe( gulp.dest( 'public' ) )
.pipe( browserSyncInstance.stream() );
}));
/**
* @name javascript
* @desc Bundles and transpiles the JavaScript files under `source/javascript`.
* @return { stream } - A stream of bundled JavaScript files.
*/
gulp.task( 'javascript', () => {
const bundler = browserify('source/javascript/start.js')
.transform('babelify', {
global: true,
sourceType: 'module',
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-modules-commonjs']
})
return bundler.bundle()
.pipe(source('render.js'))
.pipe(gulp.dest('public'));
} );
/**
* @name render
* @desc Creates the final HTML page for rendering the mermaid diagrams.
* @see { @link stylesheets }
* @see { @link javascript }
* @return { stream } - A stream of rendered diagrams in HTML files.
*/
gulp.task( 'render', () => {
const htmlTemplate = fs.readFileSync( 'source/html/render.html', 'utf-8' );
return gulp.src( 'source/diagrams/**.mmd' )
.pipe( renderMermaid( htmlTemplate ) )
.pipe( gulp.dest( 'public' ) );
} );
/**
* @name render:list
* @desc Render a list of all the diagrams available under `source/diagrams`.
* @see { @link render }
* @param { function } done - Callback that signals the task is complete.
*/
gulp.task( 'render:list', gulp.series('render', ( done ) => {
const htmlTemplate = fs.readFileSync( 'source/html/index.html', 'utf-8' );
fs.readdir( 'source/diagrams', ( error, files ) => {
let diagrams = files.filter(file => file.includes('.mmd')).map( ( file ) => {
let content = fs.readFileSync('source/diagrams/' + file, 'utf-8');
let metadata = content.split('\n').filter(function(line) {
return line.match(/^%%/) !== null;
}).reduce(function(memo, line) {
let parts = line.replace(/^%%/, '').split(':').map(function(part) {
return part.trim();
});
memo[parts[0]] = parts[1];
return memo;
}, {});
return {
href: `${ path.basename( file, '.mmd' ) }.html`,
name: metadata.title || "needs a title",
description: metadata.description || "need a description"
};
} );
var listView = Mustache.render( htmlTemplate, {
'diagram-list': diagrams,
} );
fs.writeFile( './public/index.html', listView, ( error ) => {
if ( ! error ) {
browserSyncInstance.reload();
done();
}
} );
} );
}));
/**
* @name server
* @desc Runs a preview server for local development of mermaid diagrams.
* @see { @link render:list }
* @param { function } done - Callback that signals the task is complete.
*/
gulp.task( 'server', gulp.series('stylesheets', 'javascript', 'render:list', ( done ) => {
var port = 1337;
browserSyncInstance.init( {
proxy: `localhost:${ port }`,
} );
gulp.watch( 'source/diagrams/*.mmd', gulp.series('render:list'))
.on( 'change', ( event ) => {
if ( 'deleted' === event.type ) {
let renderedFileName = `${ path.basename( event.path, '.mmd' ) }.html`;
let destFilePath = path.resolve( 'public', renderedFileName);
del.sync( destFilePath );
}
browserSyncInstance.reload();
} );
gulp.watch( 'source/html/*.html', gulp.series('render:list'));
gulp.watch( 'source/styles/**/*.scss', gulp.series('stylesheets'));
gulp.watch( 'source/javascript/**/*.js', gulp.series('javascript'))
.on( 'change', browserSyncInstance.reload );
connect()
.use( serveStatic( path.join( __dirname, '/public' ), { fallthrough: false } ) )
.use( ( error, request, response, next ) => {
if ( error ) {
logError( 'server', `${ error.statusCode } ${ error.path }` );
}
next();
} )
.listen( port, () => {
logName( 'server' );
logMessage( 'server', `Site available at http://localhost:${ port }/` );
} );
}));
/**
* @name build
* @desc Exports the compiled diagrams and pages into the public/ directory.
* @param { function } done - Callback that signals the task is complete.
*/
gulp.task( 'build', gulp.series('stylesheets', 'javascript', 'render:list', ( done ) => {
logMessage( 'build', 'Build complete.' );
done();
}));
/**
* @name notify
* @desc Wrapper around node-notify.
* @see { @link logError }
* @see { @link logMessage }
* @param { string } title - The title of the notification.
* @param { string } message - The message fo the notification.
*/
const notify = ( title, message ) => {
notifier.notify( {
title: title,
message: message,
icon: 'scuttle-icon.jpg',
} );
};
/**
* @name logData
* @desc Wrapper for gulp-util for logging task data.
* @param { string } task - The name of the task.
* @param { string } data - The data for the task.
*/
const logData = ( task, data ) => {
log(
colors.cyan( task ),
colors.white( data )
);
};
/**
* @name logMessage
* @desc Wrapper for gulp-util for logging task messages.
* @param { string } task - The name of the task.
* @param { string } message - The message for the task.
*/
const logMessage = ( task, message ) => {
notify( task, message );
log(
colors.cyan( task ),
colors.yellow( message )
);
};
/**
* @name logError
* @desc Wrapper for gulp-util for logging task errors.
* @param { string } task - The name of the task.
* @param { string } message - The error message for the task.
*/
const logError = ( task, message ) => {
log(
colors.red( task ),
colors.yellow( message )
);
};
/**
* @name logName
* @desc Display the name of the project.
* @param { string } task - The name of the task.
* @param { function } done - A callback to run.
*/
const logName = ( task, done ) => {
figlet( pkg.name, {
font: `Isometric${ Math.floor( Math.random() * ( 4 - 1 + 1 ) ) + 1 }`,
}, ( error, data ) => {
if ( ! error ) {
data.split( '\n' )
.forEach( ( line ) => {
logData( task, line );
} );
logData( task, '' );
}
if ( 'function' === typeof done ) {
done();
}
} );
};
/**
* @name renderMermaid
* @desc Gulp plugin for rendering Mermaid diagrams within a Mustache template.
* @param { string } template - The Mustache template.
* @return { stream } - A node stream wrapped in through2.
*/
const renderMermaid = function ( template ) {
const taskName = 'render-mermaid';
if ( ! template ) {
throw new PluginError( taskName, 'Missing a Mustache template.' );
}
template = new Buffer.from( template );
return through.obj( function ( file, encoding, callback ) {
if ( file.isNull() ) {
return callback( null, file );
}
let fileName = path.basename( file.path, '.mmd' );
logMessage( taskName, `Processing ${ file.path }`);
if ( file.isBuffer() ) {
file.contents = new Buffer.from( Mustache.render( template.toString(), {
'diagram-title': fileName,
'diagram-contents': file.contents.toString(),
} ) );
file.path = path.join( path.dirname( file.path ), `${ fileName }.html` );
}
if ( file.isStream() ) {
this.emit( 'error', new PluginError( taskName, 'Streaming not supported' ) );
return callback( null, file );
}
callback( null, file );
} );
};