-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgulpfile.js
More file actions
391 lines (345 loc) · 9.88 KB
/
gulpfile.js
File metadata and controls
391 lines (345 loc) · 9.88 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
'use strict'
const path = require('path')
const del = require('del')
const mergeStream = require('merge-stream')
const gulp = require('gulp')
const $ = require('gulp-load-plugins')()
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const magenta = require('ansi-magenta')
const log = require('fancy-log')
const beeper = require('beeper')
const inquirer = require('inquirer')
const bc = require('./build-config')
const { version } = require('./package.json')
let newVersion = false
const bundler = webpack(require('./webpack.config.js'))
log(`environment is`, magenta(bc.env))
if (bc.isRelease) {
log(`release mode to`, magenta(bc.releaseDest))
}
////////
// MISC
////////
// no arrow function: we're using `this`
function onError(err) {
beeper()
if (err.annotated) {
log(err.annotated)
} else if (err.message) {
log(err.message)
} else {
log(err)
}
return this.emit('end')
}
////////
// JS
////////
//----- APPLICATION
const jsApp = done => {
bundler.run((err, stats) => {
// https://webpack.js.org/api/node/#error-handling
if (err) return done(err)
const info = stats.toJson()
if (stats.hasErrors()) return done(info.errors)
done()
})
}
jsApp.description = `bundle the JS front application`
//----- DATA DICTIONNARY
const charType = {
c: `isConsonant`,
tm: `isToneMark`,
vs: `isVowel`,
vl: `isVowel`,
ds: `isDiphtong`,
dl: `isDiphtong`,
gs: `isGlide`,
gl: `isGlide`,
m: `isMiscellanous`,
n: `isNumber`,
}
const mergeData = prefix => data => {
const letters = Object.keys(data)
const result = letters.map((name, index) => {
const letter = data[name]
letter.id = `${prefix}-${index + 1}`
letter[charType[prefix]] = true
// normalize arrays
;[`similar`, `variant`].forEach(key => {
const hasKey = Array.isArray(letter[key]) && letter[key].length
const booleanKey = `has${key.charAt(0).toUpperCase() + key.substr(1)}`
letter[booleanKey] = hasKey > 0
letter[key] = letter[booleanKey] ? letter[key] : []
})
if (/^[vdg]l$/.test(prefix)) letter.isLong = true
if (/^[vdg]s$/.test(prefix)) letter.isShort = true
if (letter.meaning) {
// letters don't have a 2 words rtgs
const secondRtgsPart = letter.rtgs.split(' ')[1]
letter.longMeaning = secondRtgsPart
? `${secondRtgsPart}: ${letter.meaning}`
: ``
}
letter.longId = `${prefix}-${letter.rtgs.replace(' ', '-').toLowerCase()}`
return letter
})
return new Buffer.from(JSON.stringify(result, null, 2))
}
const data = () => {
const cons = gulp
.src('data/consonants/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('01-dico-consonants.json', mergeData('c')))
const toneMarks = gulp
.src('data/tone-marks/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('02-dico-tone-marks.json', mergeData('tm')))
const shortVowels = gulp
.src('data/vowels/short/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('03-dico-short-vowels.json', mergeData('vs')))
const longVowels = gulp
.src('data/vowels/long/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('04-dico-long-vowels.json', mergeData('vl')))
const shortDiphtongs = gulp
.src('data/diphtongs/short/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('05-dico-short-diphtongs.json', mergeData('ds')))
const longDiphtongs = gulp
.src('data/diphtongs/long/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('06-dico-long-diphtongs.json', mergeData('dl')))
const shortGlides = gulp
.src('data/glides/short/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('07-dico-short-glides.json', mergeData('gs')))
const longGlides = gulp
.src('data/glides/long/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('08-dico-long-glides.json', mergeData('gl')))
const miscellaneous = gulp
.src('data/miscellaneous/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('09-dico-miscellaneous.json', mergeData('m')))
const numbers = gulp
.src('data/numbers/*.json')
.pipe($.plumber(onError))
.pipe($.jsoncombine('10-dico-numbers.json', mergeData('n')))
return mergeStream(
cons,
toneMarks,
shortVowels,
longVowels,
shortDiphtongs,
longDiphtongs,
shortGlides,
longGlides,
miscellaneous,
numbers
)
.pipe(gulp.dest('js/models'))
.pipe($.plumber(onError))
.pipe(
$.jsoncombine('dico-all.js', data => {
// fix dictionaries that arrive in random order
const dictionaries = Object.keys(data).sort()
const result = dictionaries.reduce((accumulator, dictionary) => {
return accumulator.concat(data[dictionary])
}, [])
return new Buffer.from(JSON.stringify(result, null, 2))
})
)
.pipe($.defineModule('es6'))
.pipe(gulp.dest('js/models'))
}
data.description = `update Thai dictionary to be consumable by the JS front application`
//----- ALL JS
const js = gulp.series(data, jsApp)
js.description = `build JS files`
////////
// ASSETS
////////
const cleanIcon = require(`gulp-cheerio-clean-svg`)
const svgTemplates = [`default-svg`, `default-css`, `default-demo`]
//----- CHARACTERS
const characters = () => {
return gulp
.src(`characters/*.svg`)
.pipe(
$.cheerio({
run: $ => {
// remove Affinity Designer's rectangle wrapper
const $rects = $(`svg > rect`)
$rects.each((i, el) => {
const $el = $(el)
const style = $el.attr(`style`)
// test if it's the empty background rect or part of the char
if (style !== `fill:none;`) return
const content = $el.html()
$el.replaceWith(content)
})
},
parserOptions: {
xmlMode: true,
},
})
)
.pipe(
$.svgSymbols({
id: `char-%f`,
class: `.char-%f`,
fontSize: 16,
templates: svgTemplates,
svgAttrs: { class: `svg-icon-library` },
})
)
.pipe($.rename({ basename: `svg-chars` }))
.pipe($.if(/[.]svg$/, gulp.dest('html')))
.pipe($.if(/[.]html$/, gulp.dest('.tmp')))
.pipe($.if(/[.]css$/, $.rename({ extname: `.scss` })))
.pipe($.if(/[.]scss$/, gulp.dest(`css`)))
}
characters.description = `bundle SVG characters`
//----- ICONS (not used for now)
const icons = () => {
return gulp
.src(`icons/*.svg`)
.pipe(
$.cheerio({
run: $ => {
// remove Google's background path
$(`path[fill=none]`).remove()
},
parserOptions: {
xmlMode: true,
},
})
)
.pipe(
$.rename(path => {
const { basename } = path
const materialNameReg = /ic_([^\d]*)_black_24px/
const isMaterialIcon = materialNameReg.test(basename)
if (!isMaterialIcon) return
path.basename = materialNameReg.exec(basename)[1].replace(/_/g, `-`)
})
)
.pipe($.cheerio(cleanIcon()))
.pipe(
$.svgSymbols({
id: `icon-%f`,
class: `.icon-%f`,
templates: svgTemplates,
svgAttrs: { class: `svg-icon-library` },
})
)
.pipe($.rename({ basename: `svg-icons` }))
.pipe($.if(/[.]svg$/, gulp.dest('html')))
.pipe($.if(/[.]html$/, gulp.dest('.tmp')))
.pipe($.if(/[.]css$/, $.rename({ extname: `.scss` })))
.pipe($.if(/[.]scss$/, gulp.dest('css')))
}
icons.description = `bundle SVG files`
const assets = gulp.parallel(characters, icons)
assets.description = `build every assets`
////////
// MISC
////////
const clean = () => del(`${bc.buildDir}/*`)
clean.description = `clean everything in the destination folder`
const showBundleSize = () => {
return gulp
.src([
`${bc.buildDir}/*.js`,
`${bc.buildDir}/*.css`,
`${bc.buildDir}/*.html`,
])
.pipe($.size({ gzip: true, showFiles: true }))
}
////////
// DEV
////////
const build = gulp.series(
clean,
// assets first for css to include the right SVG files
assets,
js
)
build.description = `build everything (--prod for prod ^^)`
const buildProd = gulp.series(build, showBundleSize)
buildProd.description = `build everything (prod)`
const webpackServerOptions = {
contentBase: path.join(__dirname, bc.buildDir),
historyApiFallback: true,
stats: {
colors: true,
},
}
// https://github.com/webpack/docs/wiki/usage-with-gulp
const PORT = 3000
const webpackDevServer = done => {
new WebpackDevServer(bundler, webpackServerOptions).listen(
PORT,
`localhost`,
err => {
if (err) return onError(err)
log(`http://localhost:3000`)
}
)
}
let hash
const watch = () => {
gulp.watch(`data/**/*.json`, data)
gulp.watch(`characters/*.svg`, characters)
gulp.watch(`icons/*.svg`, icons)
}
watch.description = `watch & rebuild on change`
const serveAndWatch = () => {
webpackDevServer()
watch()
}
const dev = bc.skipBuild ? serveAndWatch() : gulp.series(build, serveAndWatch)
dev.description = `build, watch & launch a dev server`
////////
// RELEASE
////////
function askVersion() {
return inquirer
.prompt([
{
name: `newVersion`,
message: `version number? (actual is ${version})`,
validate: value => /\d+\.\d+\.\d+/.test(value),
},
])
.then(result => {
newVersion = result.newVersion
return true
})
}
exports.askVersion = askVersion
function bump() {
return gulp
.src([`package.json`, `manifest.json`])
.pipe($.jsonEditor({ version: newVersion }))
.pipe(gulp.dest(`.`))
}
const preRelease = bc.skipBump
? buildProd
: gulp.series(askVersion, bump, buildProd)
gulp.task(`data`, data)
gulp.task(`js`, js)
gulp.task(`js:app`, jsApp)
gulp.task(`characters`, characters)
gulp.task(`icons`, icons)
gulp.task(`assets`, assets)
gulp.task(`clean`, clean)
gulp.task(`build`, build)
gulp.task(`build:prod`, buildProd)
gulp.task(`dev`, dev)
gulp.task(`watch`, watch)
gulp.task(`pre-release`, preRelease)
gulp.task(`ask-version`, askVersion)