-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
211 lines (175 loc) · 4.53 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
const fs = require('fs')
const { src, dest, parallel, series }
= require('gulp')
const rename = require('gulp-rename')
const del = require('del')
const feather = require('./build/feather.js')
const pageTools = require('./build/compile-page.js')
const { Feed } = require('feed')
const glob = require('glob')
const { rollup } = require('rollup')
const { terser } = require('rollup-plugin-terser')
const sass = require('gulp-dart-sass')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
// Pass CSS through Sass and then PostCSS.
function buildCSS(cb) {
return src(`src/css/main.scss`)
.pipe(sass().on('error', sass.logError))
.pipe(postcss([
autoprefixer(),
cssnano()
]))
.pipe(dest('out/'))
}
async function buildJS(cb) {
const bundle = await rollup({
input: 'src/js/index.js',
plugins: [
terser(),
],
})
return bundle.write({
file: `out/bundle.js`,
format: 'iife',
})
}
function buildRes(cb) {
return src('src/res/**/{*,.*}')
.pipe(rename(path => {
if (path.basename.startsWith('..')) {
path.basename = path.basename.substr(2)
} else {
path.dirname = 'res/'
}
}))
.pipe(dest('out/'))
}
async function buildFeather(cb) {
await feather.build()
cb()
}
function buildPages(cb) {
let eachI18n = []
for (let lang of fs.readdirSync('src/i18n')) {
// Wrapper object to name the function so that Gulp will say which
// language it's working on.
// [10:02:48] Starting 'en'...
// [10:02:48] Starting 'es'...
let wrapper = {
[lang](cb_) {
let base = `src/i18n/${lang}/`
let outDir = `out${pageTools.getBase(lang)}/`
let eunaURL = 'https://eunakria.github.io'
let eunaAuthor = {
name: 'Linus Parker',
link: `${eunaURL}/`,
}
let feed = new Feed({
title: 'Eunakria',
id: `${eunaURL}/`,
link: `${eunaURL}/`,
language: lang,
image: `${eunaURL}/favicon.svg`,
favicon: `${eunaURL}/favicon.ico`,
feedLinks: {
atom: `${eunaURL}/atom.xml`,
},
author: eunaAuthor,
})
let postData = []
for (let postSource of glob.sync(
`${base}/posts/**/*.@(md|html)`
)) {
// Translate the input path
let outPath = toDir(postSource
.replace(base, '')
.replace(/^(?:.*?\/)?(\d+)-\d+-\d+-(.*)$/, (_, p1, p2) => (
`${p1}/${p2}`
))
)
let compiled = pageTools.compilePage(postSource, lang,
`${outDir}/${outPath}`, {
linkPath: outPath,
useMarkdown: postSource.match(/\.md$/),
})
if (!compiled.proof) {
insertSorted(postData, compiled)
}
}
for (let pageSource of glob.sync(
`${base}/**/*.@(md|html)`,
{ ignore: `${base}/posts/**/*` },
)) {
// Translate the input path
let outPath = toDir(pageSource.replace(base, ''))
pageTools.compilePage(pageSource, lang,
`${outDir}/${outPath}`, {
linkPath: outPath,
useMarkdown: pageSource.match(/\.md$/),
passData: {
posts: postData,
},
})
}
let langBase = pageTools.getBase(lang)
for (let datum of postData) {
feed.addItem({
title: datum.title,
id: datum.url,
link: `${eunaURL}${langBase}${datum.path}`,
description: datum.excerpt,
author: [ eunaAuthor ],
date: datum.date,
})
}
fs.writeFileSync(`out${langBase}/feed.xml`, feed.atom1())
cb_()
} }
eachI18n.push( ...Object.values(wrapper) )
}
return parallel( ...eachI18n, function buildPages(cb_) {
for (let pageSource of glob.sync(`src/pages/**/*`)) {
let outPath = toDir(pageSource.replace(/^src\/pages\//, ''))
pageTools.compilePage(pageSource, 'en', 'out/' + outPath, {
linkPath: outPath,
useMarkdown: pageSource.match(/\.md$/),
})
}
cb_()
} )
function toDir(str) {
return str.replace(/(index)?\.(md|html)$/, '/index.html')
}
function insertSorted(array, el) {
if (array.length === 0) {
array.push(el)
return
}
// if (array.length > 1)
if (array[0].date < el.date) {
array.unshift(el)
return
}
for (let idx = 0; idx < array.length - 1; idx++) {
if (array[idx].date > el.date && array[idx+1].date < el.date) {
array.splice(idx+1, 0, el)
return
}
}
// else
array.push(el)
}
}
async function clean(cb) {
await del('out/')
}
let buildPagesC = buildPages()
exports.default = parallel(buildPagesC, buildCSS, buildJS,
series(buildRes, buildFeather) )
exports.clean = clean
exports.pages = buildPagesC
exports.css = buildCSS
exports.js = buildJS
exports.res = buildRes