forked from godofredoninja/simply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
271 lines (234 loc) · 7.06 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
const { series, watch, src, dest, parallel } = require('gulp')
const pump = require('pump')
const del = require('del')
const rename = require('gulp-rename')
const replace = require('gulp-replace')
// gulp plugins and utils
const livereload = require('gulp-livereload')
const beeper = require('beeper')
const postcss = require('gulp-postcss')
const zip = require('gulp-zip')
const gulpif = require('gulp-if')
const sourcemaps = require('gulp-sourcemaps')
const header = require('gulp-header')
const svgSymbols = require('gulp-svg-symbols');
// Babel
const browserify = require('browserify')
const source = require('vinyl-source-stream')
const buffer = require('vinyl-buffer')
const uglify = require('gulp-uglify')
//
const merge = require('merge-stream')
// postcss plugins
const cssnano = require('cssnano')
const autoprefixer = require('autoprefixer')
const comments = require('postcss-discard-comments')
const simpleExtend = require('postcss-extend')
const tailwindcss = require('tailwindcss')
// const lol = require('postcss-advanced-variables')
const postImport = require('postcss-import')
const precss = require('precss')
const postNesting = require('tailwindcss/nesting') // postcss-nested
// sass
// const sass = require('gulp-sass')(require('sass'))
// environment
const isProduction = process.argv.includes('--production') || process.env.NODE_ENV === 'production'
// Data collection on the theme
const { name, version, author, license, repository } = require('./package.json')
// Build Comments
const BuildComments = `/*!
* ${name} v${version}
* Copyright ${new Date().getFullYear()} ${author.name} <${author.email}> (${repository.url})
* Licensed under ${license}
*/`
// clean assets
const clean = () => {
return del([
'assets',
'partials/styles',
'dis',
'partials/icons/icons-symbol-defs.hbs'
], { force: true })
}
function serve (done) {
livereload.listen()
done()
}
const handleError = done => {
return function (err) {
if (err) {
beeper()
}
return done(err)
}
}
// hbs
// function hbs (done) {
// pump([
// src(['*.hbs', 'partials/**/*.hbs']),
// livereload()
// ], handleError(done))
// }
const postcssPluginsDev = [
postImport(),
simpleExtend(),
precss(),
postNesting(),
tailwindcss()
]
const postcssPluginsPro = [
autoprefixer(),
cssnano(),
comments({ removeAll: true })
]
// style
function styles (done) {
pump([
src('src/css/*.css'),
gulpif(!isProduction, sourcemaps.init()),
// sass({ outputStyle: 'expanded' }).on('error', sass.logError),
gulpif(!isProduction, postcss(postcssPluginsDev)),
gulpif(isProduction, postcss(postcssPluginsDev.concat(postcssPluginsPro))),
gulpif(isProduction, header(BuildComments)),
gulpif(!isProduction, sourcemaps.write('./map')),
dest('assets/styles'),
livereload()
], handleError(done))
}
// Scripts
function scripts (done) {
const files = ['main', 'post', 'prismjs', 'kusi-doc-post', 'pagination', 'aigan-post']
merge(files.map(function (file) {
return pump([
browserify({
basedir: '.',
debug: true,
entries: `./src/js/${file}.js`,
cache: {},
packageCache: {}
}).transform('babelify', {
presets: ['@babel/env'],
plugins: ['@babel/plugin-transform-runtime']
}).bundle(),
source(`${file}.js`),
buffer(),
gulpif(!isProduction, sourcemaps.init()),
gulpif(isProduction, uglify()),
gulpif(isProduction, header(BuildComments)),
gulpif(!isProduction, sourcemaps.write('./map')),
dest('assets/scripts'),
livereload()
], handleError(done))
}))
}
// Image
function images (done) {
pump([
src('src/img/**/*.*'),
dest('assets/images'),
livereload()
], handleError(done))
}
function svgdefs(done) {
pump([
src(`src/img/icon/*.svg`),
svgSymbols({
templates: [`default-svg`],
slug: name => name.replace(/^/, "icon-"),
}),
rename('svgdefs.hbs'),
dest(`partials/icons`),
], handleError(done))
}
// function svgs (done) {
// pump([
// src('src/svg-icons/symbol-defs.svg'),
// rename('icons-symbol-defs.hbs'),
// dest('partials/icons'),
// livereload()
// ], handleError(done))
// }
function copyAmpStyle (done) {
pump([
src('assets/styles/amp.css'),
replace('@charset "UTF-8";', ''),
postcss([cssnano(), comments({ removeAll: true })]),
rename('amp-styles.hbs'),
dest('partials/amp')
], handleError(done))
}
function copyMainStyle (done) {
pump([
src('assets/styles/main.css'),
replace('@charset "UTF-8";', ''),
postcss([cssnano(), comments({ removeAll: true })]),
rename('main-styles.hbs'),
dest('partials')
], handleError(done))
}
// ZIP
function zipper (done) {
const filename = `${name}-v${version}.zip`
pump([
src([
'assets/**',
'locales/*.json',
'*.hbs',
'partials/**',
'podcast/**',
'LICENSE',
'package.json',
'README.md',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!src', '!src/**'
], { base: '.' }),
zip(filename),
dest('dist')
], handleError(done))
}
// TryGhost Admin
const dotenv = require('dotenv')
const path = require('path')
const GhostAdminApi = require('@tryghost/admin-api')
const ENV_FILE = path.join(__dirname, '.env')
const env = dotenv.config({ path: ENV_FILE })
async function deploy (done) {
try {
const url = process.env.GHOST_API_URL || env.parsed.GHOST_API_URL
console.log(url)
const adminApiKey = process.env.GHOST_ADMIN_API_KEY || env.parsed.GHOST_ADMIN_API_KEY
console.log(adminApiKey)
const themeName = process.env.THEME_NAME || require('./package.json').name
console.log('name =', themeName)
const apiVersion = process.env.API_VERSION || require('./package.json').engines['ghost-api']
console.log(apiVersion)
const zipFile = `./dist/${themeName}-v${version}.zip`
console.log('zip = ', zipFile)
const api = new GhostAdminApi({
url,
key: adminApiKey,
version: apiVersion
})
await api.themes.upload({ file: zipFile }).then(response => console.log(response)).catch(error => console.error(error))
console.log('uploaded')
await api.themes.activate(`${themeName}-v${version}`).then(response => console.log(response)).catch(error => console.error(error))
console.log('activated')
done()
} catch (err) {
console.log('error caught')
handleError(done)
}
}
const cssWatcher = () => watch('src/css/**', styles)
const jsWatcher = () => watch(['src/js/**', '*.js'], scripts)
const imgWatcher = () => watch('src/img/**', images)
// const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], hbs)
const hbsWatcher = () => watch(['*.hbs', 'partials/**/*.hbs'], styles)
const compile = parallel(styles, scripts, images, svgdefs)
const watcher = parallel(cssWatcher, jsWatcher, imgWatcher, hbsWatcher)
const build = series(clean, compile)
const production = series(build, copyAmpStyle, copyMainStyle, zipper)
// const production = series(build)
const development = series(build, serve, watcher)
module.exports = { build, development, production, deploy, svgdefs }