forked from chibisafe/chibisafe
-
Notifications
You must be signed in to change notification settings - Fork 56
/
gulpfile.js
224 lines (192 loc) · 5.6 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
const { exec } = require('child_process')
const path = require('path')
const gulp = require('gulp')
const cssnano = require('cssnano')
const buble = require('gulp-buble')
const eslint = require('gulp-eslint-new')
const gulpif = require('gulp-if')
const jetpack = require('fs-jetpack')
const nodemon = require('gulp-nodemon')
const postcss = require('gulp-postcss')
const postcssPresetEnv = require('postcss-preset-env')
const replace = require('gulp-replace')
const sass = require('gulp-dart-sass')
const sassCompiler = require('sass')
const sourcemaps = require('gulp-sourcemaps')
const stylelint = require('@ronilaukkarinen/gulp-stylelint')
const terser = require('gulp-terser')
let sassEmbeddedCompiler
try {
sassEmbeddedCompiler = require('sass-embedded')
console.log('Using "sass-embedded" package to compile sass\u2026')
} catch (_) {}
// Put built files for development on a Git-ignored directory.
// This will prevent IDE's Git from unnecessarily
// building diff's during development.
const dist = process.env.NODE_ENV === 'development'
? path.join(__dirname, 'dist-dev')
: path.join(__dirname, 'dist')
// Ensure dist directory exists.
jetpack.dir(dist)
const postcssPlugins = [
postcssPresetEnv()
]
sass.compiler = sassEmbeddedCompiler || sassCompiler
// Minify on production
if (process.env.NODE_ENV !== 'development') {
postcssPlugins.push(cssnano())
}
/** TASKS: LINT */
gulp.task('lint:style', () => {
return gulp.src([
'./src/**/*.css',
'./src/**/*.scss'
])
.pipe(stylelint({
failAfterError: true,
reporters: [{ formatter: 'string', console: true }]
}))
})
gulp.task('lint:js', () => {
return gulp.src([
'./*.js',
'./{controllers,database,routes,scripts,src}/**/*.js'
], {
ignore: [
'./src/libs/**/*'
]
})
.pipe(eslint())
.pipe(eslint.format('stylish'))
.pipe(eslint.failAfterError())
})
// Set _settle to true, so that if one of the parallel tasks fails,
// the other one won't exit prematurely (this is a bit awkward).
// https://github.com/gulpjs/gulp/issues/1487#issuecomment-466621047
gulp._settle = true
gulp.task('lint', gulp.parallel('lint:style', 'lint:js'))
gulp._settle = false
/** TASKS: CLEAN */
gulp.task('clean:style', async () => {
return jetpack.findAsync(dist, {
matching: '**/*.css*(.map)'
}).then(files =>
Promise.all(files.map(file =>
jetpack.removeAsync(file)
))
)
})
gulp.task('clean:js', () => {
return jetpack.findAsync(dist, {
matching: '**/*.js*(.map)'
}).then(files =>
Promise.all(files.map(file =>
jetpack.removeAsync(file)
))
)
})
gulp.task('clean:rest', () => {
// Delete all other files and sub-directories
return jetpack.findAsync(dist, {
directories: true
}).then(files =>
Promise.all(files.map(file =>
jetpack.removeAsync(file)
))
)
})
gulp.task('clean', gulp.series(gulp.parallel('clean:style', 'clean:js'), 'clean:rest'))
/** TASKS: BUILD */
gulp.task('build:sass', function () {
return gulp.src('./src/**/*.scss', {
ignore: '_*.scss'
})
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dist))
})
gulp.task('build:css', () => {
return gulp.src('./src/**/*.css', {
ignore: './src/libs/fontello/fontello.css'
})
.pipe(sourcemaps.init())
.pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dist))
})
gulp.task('build:fontello', () => {
const version = require('./src/versions.json')[5]
return gulp.src('./src/libs/fontello/fontello.css')
.pipe(sourcemaps.init())
.pipe(gulpif(version !== undefined, replace(/(fontello\.(eot|woff2?|woff|ttf|svg))/g, `$1?_=${version}`)))
.pipe(postcss(postcssPlugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(`${dist}/libs/fontello`))
})
gulp.task('build:js', () => {
return gulp.src('./src/**/*.js')
.pipe(sourcemaps.init())
.pipe(buble())
// Minify on production
.pipe(gulpif(process.env.NODE_ENV !== 'development', terser()))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dist))
})
gulp.task('build', gulp.parallel('build:sass', 'build:css', 'build:fontello', 'build:js'))
gulp.task('build-dev', gulp.series('clean', 'build'))
/** TASKS: VERSION STRINGS */
gulp.task('exec:bump-versions', cb => {
exec('node ./scripts/bump-versions.js 1', (error, stdout, stderr) => {
if (stdout) process.stdout.write(stdout)
if (stderr) process.stderr.write(stderr)
cb(error)
})
})
gulp.task('default', gulp.series('lint', 'clean', 'build', 'exec:bump-versions'))
/** TASKS: WATCH (SKIP LINTER) */
gulp.task('watch:scss', () => {
return gulp.watch([
'src/**/*.scss'
], gulp.series('build:sass'))
})
gulp.task('watch:css', () => {
return gulp.watch([
'src/**/*.css'
], {
ignored: [
'src/libs/fontello/fontello.css'
]
}, gulp.series('build:css'))
})
gulp.task('watch:fontello', () => {
return gulp.watch([
'src/libs/fontello/fontello.css'
], gulp.series('build:fontello'))
})
gulp.task('watch:js', () => {
return gulp.watch([
'src/**/*.js'
], gulp.series('build:js'))
})
gulp.task('watch:src', gulp.parallel('watch:css', 'watch:scss', 'watch:fontello', 'watch:js'))
gulp.task('nodemon', cb => {
return nodemon({
script: './lolisafe.js',
env: process.env,
watch: [
'controllers/',
'routes/',
'views/_globals.njk',
'views/_layout.njk',
'views/album.njk',
'config.js',
'logger.js',
'lolisafe.js'
],
ext: 'js',
done: cb
})
})
gulp.task('watch', gulp.series('clean', 'build', gulp.parallel('nodemon', 'watch:src')))