forked from popcodeorg/popcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
164 lines (145 loc) · 4.38 KB
/
gulpfile.js
File metadata and controls
164 lines (145 loc) · 4.38 KB
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
/* eslint-env node */
/* eslint-disable import/unambiguous */
/* eslint-disable import/no-commonjs */
/* eslint-disable import/no-nodejs-modules */
const fs = require('fs');
const path = require('path');
const https = require('https');
const gulp = require('gulp');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const cssnano = require('cssnano');
const forOwn = require('lodash.forown');
const postcss = require('gulp-postcss');
const postcssPresetEnv = require('postcss-preset-env');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const cloudflare = require('cloudflare');
const BrowserSync = require('browser-sync');
const pify = require('pify');
const isDocker = require('is-docker');
const config = require('./src/config');
const webpackConfiguration = require('./webpack.config');
const browserSync = BrowserSync.create();
const srcDir = 'src';
const distDir = 'dist';
const stylesheetsDir = path.join(srcDir, 'css');
const highlightStylesheetsDir = 'node_modules/highlight.js/styles';
const staticDir = path.join(srcDir, 'static');
const bowerComponents = 'bower_components';
const postcssBrowsers = [];
const supportedBrowsers =
JSON.parse(fs.readFileSync('./config/browsers.json'));
forOwn(supportedBrowsers, (version, browser) => {
let browserForPostcss = browser;
if (browser === 'msie') {
browserForPostcss = 'ie';
} else if (browser === 'chromium') {
return;
}
postcssBrowsers.push(`${browserForPostcss} >= ${version}`);
});
gulp.task('static', () => gulp.
src(path.join(staticDir, '**/*')).
pipe(gulp.dest(distDir)),
);
gulp.task('css', () => {
const processors = [postcssPresetEnv({browsers: postcssBrowsers})];
if (process.env.NODE_ENV === 'production') {
processors.push(cssnano());
}
return gulp.
src([
path.join(bowerComponents, 'normalize-css/normalize.css'),
path.join(highlightStylesheetsDir, 'github.css'),
path.join(stylesheetsDir, '**/*.css'),
]).
pipe(concat('application.css')).
pipe(sourcemaps.init({loadMaps: true})).
pipe(postcss(processors)).
pipe(sourcemaps.write('./')).
pipe(gulp.dest(distDir)).
pipe(browserSync.stream());
});
gulp.task('js', () => new Promise((resolve, reject) => {
webpack(
webpackConfiguration(process.env.NODE_ENV),
(error, stats) => {
if (error) {
reject(error);
return;
}
if (stats.hasErrors()) {
reject(new Error(stats.toJson().errors.join('\n\n')));
return;
}
if (stats.hasWarnings()) {
// eslint-disable-next-line no-console
console.warn(stats.toJson().warnings);
}
resolve(stats);
},
);
}));
gulp.task('build', ['static', 'css', 'js']);
gulp.task('syncFirebase', async() => {
const data = await pify(fs).readFile(
path.resolve(__dirname, 'config/firebase-auth.json'),
);
const firebaseSecret = process.env.FIREBASE_SECRET;
if (!firebaseSecret) {
throw new Error('Missing environment variable FIREBASE_SECRET');
}
return new Promise((resolve, reject) => {
const req = https.request({
hostname: `${config.firebaseApp}.firebaseio.com`,
path: `/.settings/rules.json?auth=${firebaseSecret}`,
method: 'PUT',
}, (res) => {
if (res.statusCode === 200) {
resolve();
} else {
res.on('data', reject);
}
});
req.write(data);
req.end();
});
});
gulp.task('dev', ['browserSync', 'static', 'css'], () => {
gulp.watch(path.join(staticDir, '/**/*'), ['static']);
gulp.watch(path.join(stylesheetsDir, '**/*.css'), ['css']);
gulp.watch(path.join(distDir, '*')).on('change', browserSync.reload);
});
gulp.task('browserSync', ['static'], () => {
const compiler = webpack(webpackConfiguration(process.env.NODE_ENV));
compiler.plugin('invalid', browserSync.reload);
browserSync.init({
open: !isDocker(),
server: {
baseDir: distDir,
middleware: [
webpackDevMiddleware(
compiler,
{
lazy: false,
},
),
],
},
});
});
gulp.task('purgeCache', () =>
cloudflare({
email: process.env.CLOUDFLARE_EMAIL,
key: process.env.CLOUDFLARE_KEY,
}).zones.purgeCache(
process.env.CLOUDFLARE_ZONE,
{
files: [
`https://${process.env.HOSTNAME}/index.html`,
`https://${process.env.HOSTNAME}/application.css`,
],
},
),
);