forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
407 lines (316 loc) · 11.5 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const fs = require('fs')
const path = require('path')
const globby = require('globby')
const handlebars = require('handlebars')
const { src, dest, parallel, series } = require('gulp')
const { readFile, writeFile, watch } = require('./scripts/lib/util')
const replace = require('gulp-replace')
const exec = require('./scripts/lib/shell').sync.withOptions({ // always SYNC!
live: true,
exitOnError: true
// TODO: flag for echoing command?
})
const concurrently = require('concurrently')
const { minifyBundleJs, minifyBundleCss } = require('./scripts/lib/minify')
const modify = require('gulp-modify-file')
const { allStructs, publicPackageStructs } = require('./scripts/lib/package-index')
const semver = require('semver')
const { eslintAll } = require('./scripts/eslint-dir')
exports.archive = require('./scripts/lib/archive')
/*
copy over the vdom files that were externalized by rollup.
we externalize these for two reasons:
- when a consumer build system sees `import './vdom'` it's more likely to treat it with side effects.
- rollup-plugin-dts was choking on the namespace declarations in the tsc-generated vdom.d.ts files.
*/
const VDOM_FILE_MAP = {
'packages/core-preact/tsc/vdom.d.ts': 'packages/core',
'packages/common/tsc/vdom.d.ts': 'packages/common'
}
const copyVDomMisc = exports.copyVDomMisc = parallelMap(
VDOM_FILE_MAP,
(srcGlob, destDir) => src(srcGlob)
.pipe(replace(/\/\/.*/g, '')) // remove sourcemap comments and ///<reference> don in rollup too
.pipe(dest(destDir))
)
function parallelMap(map, execute) {
return parallel.apply(null, Object.keys(map).map((key) => {
let task = () => execute(key, map[key])
task.displayName = key
return task
}))
}
const localesDts = exports.localesDts = parallel(localesAllDts, localesEachDts)
function localesAllDts() { // needs tsc
return src('packages/core/tsc/locales-all.d.ts')
.pipe(removeSimpleComments())
.pipe(dest('packages/core'))
}
function localesEachDts() { // needs tsc
return src('packages/core/tsc/locales/*.d.ts')
.pipe(removeSimpleComments())
.pipe(dest('packages/core/locales')) // TODO: remove sourcemap comment
}
function removeSimpleComments() { // like a gulp plugin
return modify(function(code) { // TODO: use gulp-replace instead
return code.replace(/\/\/.*/g, '') // TODO: make a general util for this
})
}
exports.build = series(
series(removeTscDevLinks, writeTscDevLinks), // for tsc
localesAllSrc, // before tsc
execTask('tsc -b --verbose'),
localesDts,
removeTscDevLinks,
execTask('webpack --config webpack.bundles.js --env NO_SOURCE_MAPS'), // always compile from SRC
execTask('rollup -c rollup.locales.js'),
execTask('rollup -c rollup.bundles.js'),
execTask('rollup -c rollup.packages.js'),
copyVDomMisc,
minifyBundleJs,
minifyBundleCss
)
exports.watch = series(
series(removeTscDevLinks, writeTscDevLinks), // for tsc
localesAllSrc, // before tsc
execTask('tsc -b --verbose'), // initial run
localesDts, // won't watch :(
parallel(
localesAllSrcWatch,
execParallel({
tsc: 'tsc -b --watch --preserveWatchOutput --pretty', // wont do pretty bc of piping
bundles: 'webpack --config webpack.bundles.js --watch',
locales: 'rollup -c rollup.locales.js --watch' // operates on src files. fyi: tests will need this instantly, if compiled together
})
)
)
exports.testsIndex = testsIndex
exports.test = series(
testsIndex,
parallel(
testsIndexWatch,
execParallel({
webpack: 'webpack --config webpack.tests.js --watch --env PACKAGES_FROM_SOURCE',
karma: 'karma start karma.config.js'
})
)
)
exports.testCi = series(
testsIndex,
execTask('webpack --config webpack.tests.js'),
execTask('karma start karma.config.js ci')
)
const LOCALES_SRC_DIR = 'packages/core/src/locales'
const LOCALES_ALL_TPL = 'packages/core/src/locales-all.ts.tpl'
const LOCALES_ALL_DEST = 'packages/core/src/locales-all.ts'
exports.localesAllSrc = localesAllSrc
exports.localesAllSrcWatch = localesAllSrcWatch
async function localesAllSrc() {
let localeFileNames = await globby('*.ts', { cwd: LOCALES_SRC_DIR })
let localeCodes = localeFileNames.map((fileName) => path.basename(fileName, '.ts'))
let localeImportPaths = localeCodes.map((localeCode) => `./locales/${localeCode}`)
let templateText = await readFile(LOCALES_ALL_TPL)
let template = handlebars.compile(templateText)
let jsText = template({
localeImportPaths
})
await writeFile(LOCALES_ALL_DEST, jsText)
}
function localesAllSrcWatch() {
return watch([ LOCALES_SRC_DIR, LOCALES_ALL_TPL ], localesAllSrc)
}
exports.writeTscDevLinks = series(removeTscDevLinks, writeTscDevLinks)
exports.removeTscDevLinks = removeTscDevLinks
async function writeTscDevLinks() { // bad name. does js AND .d.ts. is it necessary to do the js?
for (let struct of publicPackageStructs) {
let jsOut = path.join(struct.dir, struct.mainDistJs)
let dtsOut = path.join(struct.dir, struct.mainDistDts)
exec([
'mkdir',
'-p',
path.dirname(jsOut),
path.dirname(dtsOut),
])
exec([ 'ln', '-s', struct.mainTscJs, jsOut ])
exec([ 'ln', '-s', struct.mainTscDts, dtsOut ])
}
}
async function removeTscDevLinks() {
for (let struct of publicPackageStructs) {
let jsLink = path.join(struct.dir, struct.mainDistJs)
let dtsLink = path.join(struct.dir, struct.mainDistDts)
exec([ 'rm', '-f', jsLink, dtsLink ])
}
}
const exec2 = require('./scripts/lib/shell').sync
exports.testsIndex = testsIndex
exports.testsIndexWatch = testsIndexWatch
async function testsIndex() {
let res = exec2(
"find packages*/__tests__/src -mindepth 2 -type f \\( -name '*.ts' -or -name '*.tsx' \\) -print0 | " +
'xargs -0 grep -E "(fdescribe|fit)\\("'
)
if (!res.success && res.stderr) { // means there was a real error
throw new Error(res.stderr)
}
let files
if (!res.success) { // means there were no files that matched
let { stdout } = exec2("find packages*/__tests__/src -mindepth 2 -type f \\( -name '*.ts' -or -name '*.tsx' \\)")
files = stdout.trim()
files = !files ? [] : files.split('\n')
files = uniqStrs(files)
files.sort() // work around OS-dependent sorting ... TODO: better sorting that knows about filename slashes
console.log(`[test-index] All ${files.length} test files.`) // TODO: use gulp log util?
} else {
files = res.stdout.trim()
files = !files ? [] : files.split('\n')
files = files.map((line) => line.trim().split(':')[0]) // TODO: do a max split of 1
files = uniqStrs(files)
files.sort() // work around OS-dependent sorting
console.log(
'[test-index] Only test files that have fdescribe/fit:\n' + // TODO: use gulp log util?
files.map((file) => ` - ${file}`).join('\n')
)
}
let mainFiles = globby.sync('packages*/__tests__/src/main.{js,ts}')
files = mainFiles.concat(files)
// need 'contrib:ci' to have already been run
if (process.env.FULLCALENDAR_FORCE_REACT) {
files = [ 'packages-contrib/react/dist/vdom.js' ].concat(files)
}
let code =
files.map(
(file) => `import ${JSON.stringify('../../' + file)}`
).join('\n') +
'\n'
await writeFile('tmp/tests/index.js', code)
}
function testsIndexWatch() {
return watch(
[ 'packages/__tests__/src', 'packages-premium/__tests__/src' ], // wtf won't globs work for this?
exports.testsIndex
)
}
/*
TODO: make unnecessary. have grep do this instead with the -l option:
https://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux
*/
function uniqStrs(a) {
let hash = {}
for (let item of a) {
hash[item] = true
}
return Object.keys(hash)
}
function execTask(args) {
const exec = require('./scripts/lib/shell').promise.withOptions({ live: true })
let name = Array.isArray(args) ? args[0] : args.match(/\w+/)[0]
let taskFunc = () => exec(args)
taskFunc.displayName = name
return taskFunc
}
function execParallel(map) {
let taskArray = []
for (let taskName in map) {
taskArray.push({ name: taskName, command: map[taskName] })
}
let func = () => concurrently(taskArray, { killOthers: ['failure'] })
func.displayName = 'concurrently'
return func
}
const exec3 = require('./scripts/lib/shell').sync.withOptions({
live: true,
exitOnError: false
})
exports.lintBuiltCss = function() {
let anyFailures = false
for (let struct of publicPackageStructs) {
let builtCssFile = path.join(struct.dir, 'main.css')
if (fs.existsSync(builtCssFile)) {
let cmd = [
'stylelint', '--config', 'stylelint.config.js',
builtCssFile
]
console.log('Running stylelint on', struct.name, '...')
console.log(cmd.join(' '))
console.log()
let { success } = exec3(cmd)
if (!success) {
anyFailures = true
}
}
}
if (anyFailures) {
return Promise.reject(new Error('At least one linting job failed'))
}
return Promise.resolve()
}
exports.lintBuiltDts = function() {
let anyFailures = false
for (let struct of publicPackageStructs) {
let dtsFile = path.join(struct.dir, 'main.d.ts')
console.log(`Checking ${dtsFile}`)
// look for bad module declarations (when relative, assumed NOT to be ambient, so BAD)
// look for references to react/preact (should always use vdom instead)
let { stdout } = require('./scripts/lib/shell').sync([
'grep', '-iEe', '(declare module [\'"]\\.|p?react)', dtsFile
])
stdout = stdout.trim()
if (stdout) { // don't worry about failure. grep gives failure if no results
console.log(' BAD: ' + stdout)
anyFailures = true
}
if (struct.isPremium && struct.name !== '@fullcalendar/premium-common') {
let { stdout: stdout2 } = require('./scripts/lib/shell').sync([
'grep', '-e', '@fullcalendar/premium-common', dtsFile
])
stdout2 = stdout2.trim()
if (!stdout2) {
console.warn(`The premium package ${struct.name} does not have @fullcalendar/premium-common reference in .d.ts`)
anyFailures = true
}
}
console.log()
}
if (anyFailures) {
return Promise.reject(new Error('At least one dts linting job failed'))
}
return Promise.resolve()
}
const REQUIRED_TSLIB_SEMVER = '2'
exports.lintPackageMeta = function() {
let success = true
for (let struct of publicPackageStructs) {
let { meta } = struct
if (!meta.main) {
console.warn(`${struct.name} should have a 'main' entry`)
success = false
}
if (!meta.module) {
console.warn(`${struct.name} should have a 'module' entry`)
success = false
}
if (meta.dependencies && meta.dependencies['@fullcalendar/core']) {
console.warn(`${struct.name} should have @fullcalendar/common as a dep, NOT @fullcalendar/core`)
success = false
}
let tslibSemver = (meta.dependencies || {}).tslib || ''
if (!tslibSemver || !semver.intersects(tslibSemver, REQUIRED_TSLIB_SEMVER)) {
console.warn(`${struct.name} has a tslib version ('${tslibSemver}') that does not satisfy '${REQUIRED_TSLIB_SEMVER}'`)
success = false
}
if (!fs.existsSync(path.join(struct.dir, '.npmignore'))) {
console.warn(`${struct.name} needs a .npmignore file`)
success = false
}
}
if (success) {
return Promise.resolve()
} else {
return Promise.reject(new Error('At least one package.json has an error'))
}
}
exports.lint = series(exports.lintPackageMeta, () => {
return eslintAll() ? Promise.resolve() : Promise.reject(new Error('One or more lint tasks failed'))
})
exports.lintBuilt = series(exports.lintBuiltCss, exports.lintBuiltDts)