forked from TryGhost/Themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
250 lines (209 loc) · 6.97 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
const {series, parallel, watch, src, dest} = require('gulp');
const pump = require('pump');
const glob = require('glob');
const argv = require('yargs').argv;
const exec = require('child_process').exec;
// gulp plugins and utils
const livereload = require('gulp-livereload');
const gulpStylelint = require('gulp-stylelint');
const postcss = require('gulp-postcss');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const beeper = require('beeper');
const zip = require('gulp-zip');
// postcss plugins
const easyimport = require('postcss-easy-import');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
function serve(done) {
livereload.listen();
done();
}
function handleError(done) {
return function (err) {
if (err) beeper();
return done(err);
};
}
function doHBS(path, done) {
pump([
src([`${path}/*.hbs`, `${path}/partials/**/*.hbs`]),
livereload()
], handleError(done));
}
function doCSS(path, done) {
pump([
src(`${path}/assets/css/screen.css`, {sourcemaps: true}),
postcss([
easyimport,
autoprefixer(),
cssnano()
]),
dest(`${path}/assets/built/`, {sourcemaps: '.'}),
livereload()
], handleError(done));
}
function doJS(path, done) {
pump([
src([
'packages/_shared/assets/js/lib/**/*.js',
'packages/_shared/assets/js/main.js',
`${path}/assets/js/lib/**/*.js`,
`${path}/assets/js/main.js`,
], {sourcemaps: true}),
concat('main.min.js'),
uglify(),
dest(`${path}/assets/built/`, {sourcemaps: '.'}),
livereload()
], handleError(done));
}
function main(done) {
const tasks = glob.sync('packages/*', {ignore: 'packages/_shared'}).map(path => {
const packageName = require(`./${path}/package.json`).name;
function package(taskDone) {
const hbs = (done) => doHBS(path, done);
hbs.displayName = `hbs_${packageName}`;
const css = (done) => doCSS(path, done);
css.displayName = `css_${packageName}`;
const js = (done) => doJS(path, done);
js.displayName = `js_${packageName}`;
const hbsWatcher = () => watch([`${path}/*.hbs`, `${path}/partials/**/*.hbs`], hbs);
hbsWatcher.displayName = `hbsWatcher_${packageName}`;
const cssWatcher = () => watch(`${path}/assets/css/**/*.css`, css);
cssWatcher.displayName = `cssWatcher_${packageName}`;
const jsWatcher = () => watch(`${path}/assets/js/**/*.js`, js);
jsWatcher.displayName = `jsWatcher_${packageName}`;
const watcher = parallel(hbsWatcher, cssWatcher, jsWatcher);
const build = series(css, js);
series(build, serve, watcher)();
taskDone();
}
package.displayName = packageName;
return package;
});
function sharedCSS(done) {
glob.sync('packages/*', {ignore: 'packages/_shared'}).map(path => {
pump([
src(`${path}/assets/css/screen.css`, {sourcemaps: true}),
postcss([
easyimport,
autoprefixer(),
cssnano()
]),
dest(`${path}/assets/built/`, {sourcemaps: '.'}),
livereload()
], handleError(done));
});
}
const sharedCSSWatcher = () => watch('packages/_shared/assets/css/**/*.css', sharedCSS);
function sharedJS(done) {
glob.sync('packages/*', {ignore: 'packages/_shared'}).map(path => {
pump([
src([
'packages/_shared/assets/js/lib/**/*.js',
'packages/_shared/assets/js/main.js',
`${path}/assets/js/lib/**/*.js`,
`${path}/assets/js/main.js`,
], {sourcemaps: true}),
concat('main.min.js'),
uglify(),
dest(`${path}/assets/built/`, {sourcemaps: '.'}),
livereload()
], handleError(done));
});
}
const sharedJSWatcher = () => watch('packages/_shared/assets/js/**/*.js', sharedJS);
const sharedWatcher = parallel(sharedCSSWatcher, sharedJSWatcher);
return series(parallel(...tasks), sharedWatcher, tasksDone => {
tasksDone();
done();
})();
}
function doLint(theme, fix, done) {
let source = ['packages/*/assets/css/**/*.css'];
if (theme) {
source = [`packages/${theme}/assets/css/**/*.css`, 'packages/_shared/assets/css/**/*.css'];
}
pump([
src([...source, '!packages/*/assets/built/*.css'], {base: '.'}),
gulpStylelint({
fix: fix,
reporters: [
{formatter: 'string', console: true}
]
}),
dest('./')
], handleError(done));
}
function lint(done) {
doLint(false, true, done);
}
function symlink(done) {
if (!argv.theme || !argv.site) {
handleError(done('Required parameters [--theme, --site] missing!'));
}
exec(`ln -sfn ${__dirname}/packages/${argv.theme} ${argv.site}/content/themes`);
done();
}
function test(done) {
const testLint = lintDone => {
doLint(false, false, done)
lintDone();
};
const testGScan = gscanDone => {
glob.sync('packages/*', {ignore: 'packages/_shared'}).forEach(path => {
exec(`gscan ${path} --colors`, (error, stdout, _stderr) => {
console.log(stdout);
if (error) process.exit(1);
});
});
gscanDone();
}
return series(testLint, testGScan)();
}
function testCI(done) {
if (!argv.theme) {
handleError(done('Required parameter [--theme] missing!'));
}
const testLint = lintDone => {
doLint(argv.theme, false, done)
lintDone();
};
const testGScan = gscanDone => {
exec(`gscan --fatal --verbose packages/${argv.theme} --colors`, (error, stdout, _stderr) => {
console.log(stdout);
if (error) process.exit(1);
});
gscanDone();
}
return series(testLint, testGScan)();
}
function css(done) {
doCSS(`./packages/${argv.theme}`, done);
}
function js(done) {
doJS(`./packages/${argv.theme}`, done);
}
const build = series(css, js);
function zipper(done) {
if (!argv.theme) {
handleError(done('Required parameter [--theme] missing!'));
}
const filename = require(`./packages/${argv.theme}/package.json`).name + '.zip';
pump([
src([
'**',
'!node_modules', '!node_modules/**',
'!dist', '!dist/**',
'!yarn-error.log'
], {cwd: `./packages/${argv.theme}`}),
zip(filename),
dest(`packages/${argv.theme}/dist/`)
], handleError(done));
}
exports.lint = lint;
exports.symlink = symlink;
exports.test = test;
exports.testCI = testCI;
exports.zip = series(build, zipper);
exports.default = main;