-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.ts
91 lines (83 loc) · 2.82 KB
/
gulpfile.babel.ts
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
// import gulp from 'gulp'
// import fs from 'fs'
// import path from 'path'
/* eslint-disable @typescript-eslint/no-var-requires */
const gulp = require('gulp')
const fs = require('fs')
const path = require('path')
const readDir = (sourceDir, fn: (files: string[]) => string[]): Promise<string[]> => {
return new Promise((resolve, reject) => {
fs.readdir(sourceDir, (err, files): void => {
if (err) return reject(err)
const filePaths = files.map((f: string): string => path.join(sourceDir, f))
return resolve(fn(filePaths))
})
})
}
const writeFile = (filePath, data): Promise<void> => {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, data, (err): void => {
if (err) return reject(err)
return resolve()
})
})
}
const getImageCategories = (filePaths: string[]): string[] => {
let arr: string[] = []
// skip dot files
arr = filePaths.filter(filePath => !path.basename(filePath).startsWith('.'))
// natural alphanumeric sort
return arr.splice(0).sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }))
}
const isDirectory = (filePath: string): boolean => {
const fileName = path.basename(filePath)
if (fileName.startsWith('.')) {
return false
}
return path.extname(path.basename(filePath)) === ''
}
// generate image categories json
gulp.task('generate:json', async () => {
const json: { [key: string]: string[] } = {}
const keys: string[] = []
const imageDirs = await readDir('public/images/', getImageCategories)
const promises: Promise<string[]>[] = []
imageDirs.forEach(dir => {
if (isDirectory(dir)) {
promises.push(readDir(dir, getImageCategories))
keys.push(path.basename(dir))
}
})
await Promise.all(promises).then(results => {
results.forEach((values, i) => {
const key = keys[i]
// strip 'public/' prefix
const images = values.map(filePath => filePath.replace(/^public\//g, ''))
// sort by YYYY-MM where most date recent is first, otherwise localeCompare
const sortedImages = images.sort((a, b) => {
const aFile = a.substring(a.lastIndexOf('/')+1)
const bFile = b.substring(b.lastIndexOf('/')+1)
const aArr = aFile.split('-')
const bArr = bFile.split('-')
if (aArr.length < 3 && bArr.length < 3) {
return aFile.localeCompare(bFile, 'en-US', {numeric: true})
}
const aDate = aArr[0] + aArr[1]
const bDate = bArr[0] + bArr[1]
const aInt = parseInt(aDate)
const bInt = parseInt(bDate)
const min = 10 * 6
if (aInt > min || bInt > min) {
if (aInt > bInt) {
return -1
} else if (aInt < bInt) {
return 1
}
}
return aFile.localeCompare(bFile, 'en-US', {numeric: true})
});
json[key] = sortedImages
})
})
writeFile('src/generated/imageCategories.json', JSON.stringify(json, null, 2))
})