forked from rogeruiz/scuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
317 lines (272 loc) · 8.21 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
import gulp from 'gulp';
import gutil from 'gulp-util';
import notifier from 'node-notifier';
import http from 'http';
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 buffer from 'vinyl-buffer';
import babelify from 'babelify';
import sass from 'gulp-sass';
import del from 'del';
import figlet from 'figlet';
import pkg from './package';
import browserSync from 'browser-sync';
// Create browserSync instance
//
const browserSyncInstance = browserSync.create();
/**
* @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/**/*' )
.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', [ '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( {
entries: 'source/javascript/start.js',
debug: false,
transform: [
[
babelify,
{
presets: [
'es2015',
'es2016'
],
}
],
]
} );
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', [ 'render' ], ( done ) => {
const htmlTemplate = fs.readFileSync( 'source/html/index.html', 'utf-8' );
fs.readdir( 'source/diagrams', ( error, files ) => {
let diagrams = files.map( ( file ) => {
return {
href: `${ path.basename( file, '.mmd' ) }.html`,
name: file,
};
} );
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', [ 'stylesheets', 'javascript', 'render:list' ], ( done ) => {
var port = 1337;
browserSyncInstance.init( {
proxy: `localhost:${ port }`,
} );
gulp.watch( 'source/diagrams/*.mmd', [ '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', [ 'render:list' ] );
gulp.watch( 'source/styles/**/*.scss', [ 'stylesheets' ] );
gulp.watch( 'source/javascript/**/*.js', [ '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 export
* @desc Exports a PNG using PhantomJS of the mermaid diagrams.
* @param { function } done - Callback that signals the task is complete.
*/
gulp.task( 'export', ( done ) => { 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 ) => {
gutil.log(
gutil.colors.cyan( task ),
gutil.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 );
gutil.log(
gutil.colors.cyan( task ),
gutil.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 ) => {
gutil.log(
gutil.colors.red( task ),
gutil.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';
const PluginError = gutil.PluginError;
if ( ! template ) {
throw new PluginError( taskName, 'Missing a Mustache template.' );
}
template = new Buffer( 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( 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 );
} );
};