-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
158 lines (130 loc) · 3.39 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
'use strict'
/**
* Dependencies
*/
const $ = require('gulp-load-plugins')()
const bs = require('browser-sync')
const cp = require('child_process')
const gulp = require('gulp')
const {promises} = require('fs')
const {compileTemplate} = require('statil')
const {md} = require('./md')
const sass = require('sass')
/**
* Globals
*/
const SRC_DOC_TEMPLATE_DIR = 'docs/templates'
const SRC_DOC_TEMPLATE_FILES = 'docs/templates/**/*'
const SRC_DOC_STATIC_FILES = 'docs/static/**/*'
const SRC_DOC_STYLE_FILES = 'docs/styles/**/*.scss'
const SRC_DOC_STYLE_ENTRY = 'docs/styles/docs.scss'
const OUT_DOC_DIR = 'gh-pages'
const COMMIT = cp.execSync('git rev-parse --short HEAD').toString().trim()
const PAGES = [
{
md: 'index.md',
template: 'index.html',
lang: 'en',
},
{
md: 'index_ru.md',
template: 'index.html',
outPath: 'ru',
lang: 'ru',
},
]
/**
* Clear
*/
gulp.task('clear', () => (
// Skips dotfiles like `.git` and `.gitignore`
import('del').then(({deleteAsync}) => deleteAsync(`${OUT_DOC_DIR}/*`).catch(console.error.bind(console)))
))
/**
* Static
*/
gulp.task('docs:static:copy', () => (
gulp.src(SRC_DOC_STATIC_FILES).pipe(gulp.dest(OUT_DOC_DIR))
))
gulp.task('docs:static:watch', () => {
$.watch(SRC_DOC_STATIC_FILES, gulp.series('docs:static:copy'))
})
/**
* Templates
*/
gulp.task('docs:templates:build', async () => {
for (const page of PAGES) {
const mdInput = await promises.readFile(`${SRC_DOC_TEMPLATE_DIR}/${page.md}`, 'utf8')
const mdOut = md(compileTemplate(mdInput)())
const pageInput = await promises.readFile(`${SRC_DOC_TEMPLATE_DIR}/${page.template}`, 'utf8')
const pageOutput = compileTemplate(pageInput)({
COMMIT,
CONTENT: mdOut,
LANG: page.lang,
})
const outPath = `${OUT_DOC_DIR}/${page.outPath || ''}`
await promises.mkdir(outPath, {recursive: true})
await promises.writeFile(`${outPath}/${page.template}`, pageOutput)
}
})
gulp.task('docs:templates:watch', () => {
$.watch(SRC_DOC_TEMPLATE_FILES, gulp.series('docs:templates:build'))
})
/**
* Styles
*/
gulp.task('docs:styles:build', () => (
gulp.src(SRC_DOC_STYLE_ENTRY)
.pipe($.sass(sass)())
.pipe($.autoprefixer())
.pipe($.cleanCss({
keepSpecialComments: 0,
aggressiveMerging: false,
advanced: false,
compatibility: {properties: {colors: false}},
}))
.pipe(gulp.dest(OUT_DOC_DIR))
))
gulp.task('docs:styles:watch', () => {
$.watch(SRC_DOC_STYLE_FILES, gulp.series('docs:styles:build'))
})
/**
* Server
*/
gulp.task('docs:server', () => (
bs.init({
startPath: '/stylebox/',
server: {
baseDir: 'gh-pages',
middleware: [
(req, res, next) => {
req.url = req.url.replace(/^\/stylebox\//, '').replace(/^[/]*/, '/')
next()
},
],
},
port: 36463,
files: 'gh-pages',
open: false,
online: false,
ui: false,
ghostMode: false,
notify: false,
})
))
/**
* Default
*/
gulp.task('buildup', gulp.parallel(
'docs:static:copy',
'docs:templates:build',
'docs:styles:build'
))
gulp.task('watch', gulp.parallel(
'docs:static:watch',
'docs:templates:watch',
'docs:styles:watch',
'docs:server'
))
gulp.task('build', gulp.series('clear', 'buildup'))
gulp.task('default', gulp.series('clear', 'buildup', 'watch'))