-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
367 lines (322 loc) · 9.54 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// native
import { writeFileSync, readdirSync } from 'fs';
import { basename, extname, join } from 'path';
// packages
import getPort from 'get-port';
import gulp from 'gulp';
import autoprefixer from 'gulp-autoprefixer';
import changedInPlace from 'gulp-changed-in-place';
import gulpSass from 'gulp-sass';
import gulpIf from 'gulp-if';
import { prepend, append } from 'gulp-insert';
import livereload, { listen } from 'gulp-livereload';
import markdown from 'gulp-markdown';
import toc from 'gulp-markdown-toc';
import revAll from 'gulp-rev-all';
import sourcemaps from 'gulp-sourcemaps';
import open from 'open';
import dartSass from 'sass-embedded';
import webpack from 'webpack';
// local
import getWebpackConfig from './webpack.config.js';
import * as credentials from './scripts/credentials.js';
import * as endrun from './scripts/endrun.js';
import * as examples from './scripts/examples.js';
import * as externaldata from './scripts/externaldata.js';
import * as externalEmbeds from './scripts/external-embeds.js';
import * as github from './scripts/github.js';
import * as google from './scripts/google.js';
import * as includes from './scripts/includes.js';
import server from './scripts/server.js';
import * as setup from './scripts/setup.js';
import * as docs from './scripts/docs.js';
import * as sheets from './scripts/sheets.js';
import * as videos from './scripts/videos.js';
import * as s3 from './scripts/s3.js';
// plugins
import gulpFileSize from './scripts/gulp-plugins/file-size.js';
// utils
import { getLocalConfig } from './scripts/config.js';
import { cleanDir } from './scripts/utils.js';
var serverPort, multiple_graphics;
const { local_markdown, generate_external_embeds, cdn, slug } =
getLocalConfig();
// Pass dart sass to gulp-sass
const sass = gulpSass(dartSass);
async function startServer() {
const port = await getPort({ port: [3000, 3001, 3002, 3003] });
const lrPort = await getPort({ port: 35729 });
server({ port, lrPort });
listen({ port: lrPort });
serverPort = port;
}
function openBrowser() {
return open('http://localhost:' + serverPort);
}
function styles() {
return gulp
.src('src/graphic.scss')
.pipe(sourcemaps.init())
.pipe(
sass({
includePaths: ['templates/'],
}).on('error', sass.logError)
)
.pipe(sourcemaps.write())
.pipe(gulp.dest('build'))
.pipe(livereload());
}
function productionStyles() {
return gulp
.src('src/graphic.scss')
.pipe(sourcemaps.init())
.pipe(
sass
.sync({
outputStyle: 'compressed',
includePaths: ['templates/'],
})
.on('error', sass.logError)
)
.pipe(
autoprefixer({
cascade: false,
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('build'));
}
function graphicsReadme() {
return gulp
.src('templates/charts/README.md')
.pipe(changedInPlace({ firstPass: true }))
.pipe(toc())
.pipe(gulp.dest('templates/charts'))
.pipe(gulp.dest('build/templates/charts'))
.pipe(livereload());
}
function readme() {
return gulp
.src('README.md')
.pipe(changedInPlace({ firstPass: true }))
.pipe(toc())
.pipe(gulp.dest('.'))
.pipe(gulp.dest('build'))
.pipe(livereload());
}
function mustache() {
return gulp.src('src/*.mustache').pipe(gulp.dest('build')).pipe(livereload());
}
function productionMustache() {
return gulp
.src('src/*.mustache')
.pipe(prepend(includes.stylesheetIncludeText()))
.pipe(prepend(includes.javascriptIncludeText()))
.pipe(gulp.dest('build'))
.pipe(livereload());
}
function html() {
return gulp
.src('src/*.html')
.pipe(externaldata.getExternalData())
.pipe(externaldata.renderGraphicHTML())
.pipe(gulp.dest('build'))
.pipe(livereload());
}
function productionHtml() {
if (multiple_graphics) {
writeFileSync(
'./build/includes.html',
includes.stylesheetIncludeText() + includes.javascriptIncludeText()
);
}
return gulp
.src('src/*.html')
.pipe(externaldata.getExternalData())
.pipe(externaldata.renderGraphicHTML())
.pipe(gulpIf(local_markdown, markdown()))
.pipe(gulpIf(singleOrHeader, prepend(includes.stylesheetIncludeText())))
.pipe(gulpIf(singleOrHeader, append(includes.javascriptIncludeText())))
.pipe(gulp.dest('build'))
.pipe(livereload());
}
function singleOrHeader(file) {
if (basename(file.path, extname(file.path)) == 'header') {
return true;
}
if (!multiple_graphics) {
return true;
}
return false;
}
function embedGraphicHtml() {
return gulp
.src('src/*.html')
.pipe(externaldata.getExternalData())
.pipe(externaldata.renderGraphicHTML())
.pipe(gulpIf(local_markdown, markdown()))
.pipe(gulp.dest('build/embed-contents'));
}
function checkGraphicsCount(done) {
const files = readdirSync('./src/', 'utf-8');
let fileCount = 0;
files.forEach(function (filename) {
if (filename.match(/[^_].*\.html$/) || filename == 'header.mustache') {
fileCount++;
}
});
multiple_graphics = fileCount > 1;
done();
}
function productionScripts() {
return new Promise((resolve, reject) => {
const webpackConfig = getWebpackConfig('production');
const bundle = webpack(webpackConfig);
bundle.run((err) => {
if (err) {
reject(err);
}
resolve();
});
});
}
function assets() {
return gulp
.src('src/assets/**', { base: 'src' })
.pipe(gulpFileSize({ fileSizeLimit: 512000 })) // 500kb
.pipe(gulp.dest('build'))
.pipe(livereload());
}
const buildDev = gulp.series(
clean,
gulp.parallel(mustache, html, styles, assets, readme, graphicsReadme)
);
const buildProduction = gulp.series(
clean,
productionStyles,
productionScripts,
assets,
checkGraphicsCount,
productionMustache,
productionHtml
);
const buildEmbed = gulp.series(
embedGraphicHtml,
externalEmbeds.embedLoaderHtml
);
function buildEmbedIfFlagged() {
function skipEmbed(cb) {
cb();
}
if (generate_external_embeds) {
return buildEmbed;
} else {
return skipEmbed;
}
}
function watch() {
gulp.watch(['README.md'], readme);
gulp.watch(['templates/charts/README.md'], graphicsReadme);
gulp.watch(['src/*.scss', 'templates/charts/stylesheets/*.scss'], styles);
gulp.watch(['src/assets/**'], assets);
// Triggers a full refresh (html doesn't actually need to be recompiled)
gulp.watch(['post-templates/**'], html);
gulp.watch(['post-templates/custom-header-data.json'], mustache);
gulp.watch(['src/*.mustache'], mustache);
return gulp.watch(['src/*.html', 'src/template-files'], html);
}
function clean() {
return Promise.all([cleanDir('./dist'), cleanDir('./build')]);
}
function revision() {
return gulp
.src('build/**', { base: 'build' })
.pipe(
revAll.revision({
transformPath: (rev, source, file) => {
return new URL(join(slug, rev), cdn).href;
},
dontGlobal: [/.*\/embed-loaders\/*/],
// If you want an unversioned file. Careful deploying with this, the
// cache times are long.
// dontRenameFile: [/.*.csv/],
includeFilesInManifest: ['.html', '.mustache', '.js', '.css'],
})
)
.pipe(gulp.dest('dist'))
.pipe(revAll.manifestFile())
.pipe(gulp.dest('dist'));
}
const defaultTask = gulp.series(
clean,
buildDev,
endrun.getPostData,
startServer,
openBrowser,
watch
);
// Primary interface
gulp.task(
'setup',
gulp.series(
setup.setup,
setup.resetType,
setup.handleHeaderTemplateFiles,
setup.handleMatchingRepo,
github.ensureUpdatesRemote,
github.updateDependabotSettings,
defaultTask
)
);
gulp.task('default', defaultTask);
gulp.task(
'deploy',
gulp.series(
github.ensureRepoCleanAndPushed,
buildProduction,
buildEmbedIfFlagged(),
revision,
s3.deploy,
endrun.endrunDeploy,
buildDev
)
);
// Asset tasks
gulp.task('sass:production', productionStyles);
gulp.task('scripts:production', productionScripts);
gulp.task('html:production', productionHtml);
gulp.task('clean', clean);
gulp.task('build:production', buildProduction);
gulp.task(
'build:embed',
gulp.series(buildProduction, externalEmbeds.setEmbedConfigFlag, buildEmbed)
);
gulp.task('build:examples', examples.build);
gulp.task('revision', revision);
gulp.task('docs:download', docs.downloadData);
gulp.task('sheets:download', sheets.downloadData);
gulp.task('videos:transcode', videos.transcodeUploadedVideos);
gulp.task('posts:download', endrun.getPostData);
// Deployment
gulp.task('deploy:endrun', endrun.endrunDeploy);
gulp.task('deploy:s3', gulp.series(buildProduction, revision, s3.deploy));
gulp.task('deploy:s3:raw', s3.deploy);
gulp.task('deploy:data', s3.deployData);
// Credential management
gulp.task('credentials', credentials.ensureRequiredCredentialsTask);
gulp.task('credentials:clear', credentials.clearServicePasswords);
gulp.task('credentials:endrun', credentials.resetEndrunKey);
gulp.task('credentials:endrun_local', credentials.resetEndrunLocalKey);
gulp.task('credentials:aws', credentials.resetAWSKeys);
gulp.task('credentials:github', credentials.resetGithubKey);
gulp.task('credentials:google', google.resetGoogleToken);
gulp.task('credentials:google_client', google.resetGoogleClient);
// Configuration management
gulp.task('reset:type', setup.resetType);
gulp.task('dependabot:disable', github.updateDependabotSettings);
// Rig updates management
gulp.task('repo:create', github.createAndSetRepository);
gulp.task('repo:labels', github.setupDefaultLabels);
gulp.task('repo:issues', github.createDefaultIssues);
gulp.task('remote:add', github.ensureUpdatesRemote);
gulp.task('update', github.pullUpdates);