-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
253 lines (214 loc) · 6.64 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
"use strict"
const argv = require("yargs").argv
const fs = require("fs")
const path = require("path")
const mkdirp = require("mkdirp-bluebird")
const Promise = require("bluebird")
const named = require("vinyl-named")
const _ = require("lodash")
const debug = require("debug")("route4me:gulpfile")
const gulp = require("gulp")
const log = require("fancy-log")
const gulpIf = require("gulp-if")
const eslint = require("gulp-eslint-new")
const mocha = require("gulp-mocha")
const size = require("gulp-size")
// const gulpUglify = require("gulp-uglify")
const gulpWebpack = require("webpack-stream")
const babel = require("gulp-babel")
const gulpSequence = require("gulp4-run-sequence")
const cache = require("gulp-cached")
// TODO: disable:
const jsdoc = require("gulp-jsdoc3")
const jsdoc2md = require("jsdoc-to-markdown")
const gitbook = require("gitbook")
const webpack = require("webpack")
const del = require("del")
const jsdocConfig = require("./.jsdocrc.js")
const webpackBrowserConfig = require("./build-config/webpack.browser.js")
const webpackTestConfig = require("./build-config/webpack.test.js")
const babelBrowserConfig = require("./build-config/babel.browser.js")
const babelNodeConfig = require("./build-config/babel.node.js")
const fix = !!argv.fix
const grep = argv.grep
/*
======================================
MONKEY PATCH
======================================
*/
gulp.Gulp.prototype.__runTask = gulp.Gulp.prototype._runTask
gulp.Gulp.prototype._runTask = function monkeyPatchGulpRunTask(task) {
this.currentTask = task
this.__runTask(task)
}
/*
======================================
/MONKEY PATCH
======================================
*/
const PATHS = {
"test": [
"test/**/*.test.js",
"test/**/*.spec.js",
"test/**/*.integration.js",
"examples/**/*.js",
],
"src": [
"src/**/*.js",
],
"entry": {
"browser": "./src/index.js",
},
}
gulp.task("doc:pre", gulp.series(function DG() { // eslint-disable-line prefer-arrow-callback
const docDir = path.join(__dirname, "book", "en", "code")
const parentsTree = {}
return mkdirp(docDir)
.then(() => jsdoc2md.getTemplateData({
files: PATHS.src,
}))
.each((item) => {
parentsTree[item.id] = item
})
.reduce((acc, item) => {
let ancestor = item
while (ancestor && "undefined" !== typeof ancestor.memberof) {
ancestor = parentsTree[ancestor.memberof]
}
let category = ancestor && ancestor.category
if ("undefined" === typeof category) {
category = "Uncategorized"
}
if (!acc.has(category)) {
acc.set(category, [])
}
acc.get(category).push(item)
return acc
}, new Map())
// render all categories:
.map((item) => {
const category = item[0]
const data = item[1]
return Promise.props({
category,
output: jsdoc2md.render({
data,
"name-format": false,
"module-index-format": "none",
"global-index-format": "none",
"member-index-format": "grouped", // grouped, list
"property-list-format": "table", // list, table - works for enums
"param-list-format": "table", // list, table - only table, or replace template!
// "configure": ".jsdocrc.json",
"plugin": "dmd-gitbook",
"no-scope": false,
"example-lang": "javascript",
"private": true
})
})
})
// write to file:
.map((item) => {
const fn = path.join(docDir, `${item.category}.md`)
log("DOC, saving to file", fn)
return fs.writeFile(fn, item.output)
})
}))
gulp.task("doc:install", gulp.series(function DI() { // eslint-disable-line prefer-arrow-callback
const cmd = gitbook.commands.filter(c => c.name.match(/^install\s/i))[0]
return cmd.exec([__dirname], {})
}))
gulp.task("watch:doc",gulp.series( ["doc:pre"], function D() { // eslint-disable-line prefer-arrow-callback
const cmd = gitbook.commands.filter(c => c.name.match(/^serve\s/i))[0]
return cmd.exec([
path.join(__dirname),
path.join(__dirname, "tmp", "gitbook")
], {
log: "info",
format: "website",
open: false,
port: 4000,
live: true,
// lrport: typeof options.livereload === 'object' ? options.livereload.port : undefined,
watch: true, //typeof options.livereload === 'object' ? options.livereload.watch : undefined,
//browser: options.browser
})
}))
// TODO: remove this task
gulp.task("docold", gulp.series(function D() { // eslint-disable-line prefer-arrow-callback
return gulp.src(
"README.md", {
read: false,
})
.pipe(jsdoc(jsdocConfig))
}))
gulp.task("lint", gulp.series(function L() { // eslint-disable-line prefer-arrow-callback
return gulp.src(_.flattenDeep([
PATHS.src,
PATHS.test,
]), { "base": "./" })
.pipe(cache("lint", { optimizeMemory: true }))
.pipe(eslint({
fix,
}))
.pipe(eslint.format())
.pipe(gulpIf(fix, gulp.dest("./")))
.pipe(eslint.failAfterError())
}))
gulp.task("test", gulp.series(function T() { // eslint-disable-line prefer-arrow-callback
return gulp.src(PATHS.test, { read: false })
.pipe(mocha({
// reporter: "list",
ignoreLeaks: false,
ui: "bdd",
require: ["./test/bootstrap"],
grep,
}))
}))
gulp.task("build:node", gulp.series(function BN() { // eslint-disable-line prefer-arrow-callback
const babelConfig = babelNodeConfig
return gulp.src(PATHS.src)
.pipe(babel(babelConfig))
.pipe(size({ title: "build:node", showFiles: true }))
.pipe(gulp.dest("dist/"))
}))
gulp.task("build:browser",function taskBuildBrowser(done) { // eslint-disable-line prefer-arrow-callback
try {
const config = webpackBrowserConfig
debug("WebPack config:", config)
gulp.src(PATHS.entry.browser)
.pipe(named())
.pipe(gulpWebpack(config, webpack))
.pipe(size({ title: "build:browser", showFiles: true }))
.pipe(gulp.dest("dist/browser/"))
return done()
} catch(err) {
console.log(err);
}
})
gulp.task("build:browser:test",function taskBuildBrowserTest(done) { // eslint-disable-line prefer-arrow-callback
const config = webpackTestConfig
debug("WebPack config:", config)
gulp.src(PATHS.entry.browser)
.pipe(named())
.pipe(gulpWebpack(config, webpack))
.pipe(size({ title: "build:browser:test", showFiles: true }))
.pipe(gulp.dest("tmp/browser/"))
return done()
})
gulp.task("clean", gulp.series(function () {
return del([
"./dist/",
"./tmp/",
])
}))
// SUPERTASKS
gulp.task("doc", gulp.series("watch:doc"))
gulp.task("build", (done) => {
gulpSequence("build:node", "build:browser","build:browser:test", done)
return undefined
})
gulp.task("default", gulp.series((done) => {
gulpSequence("lint", "build", "test", done)
return undefined
}))