-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
84 lines (76 loc) · 1.98 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
/**
* @fileoverview gulpfile for watching and compiling .tex notes.
* @author Alvin Lin (alvin@omgimanerd.tech)
*/
import del from 'del';
import glob from 'glob';
import gulp from 'gulp';
import changed from 'gulp-changed';
import plumber from 'gulp-plumber';
import pdflatex2 from 'gulp-pdflatex2';
import rename from 'gulp-rename';
import path from 'path';
const getOutputFile = texFile => {
texFile = path.parse(texFile)
return path.join(texFile.dir, 'output', texFile.name + '.pdf')
}
const compileChanged = () => {
return gulp.src('./latex/**/*.tex')
.pipe(plumber({
errorHandler: function () {
this.emit('end')
}
}))
.pipe(changed('./latex', { transformPath: getOutputFile }))
.pipe(pdflatex2({
texInputs: ['./cls'],
separator: process.platform === 'win32' ? ';' : ':',
}))
.pipe(rename(path => {
path.dirname += '/output'
path.extname = '.pdf'
}))
.pipe(gulp.dest('./latex'))
}
const compileAll = () => {
return gulp.src('./latex/**/*.tex')
.pipe(pdflatex2({
cliOptions: ['-shell-escape'],
separator: process.platform === 'win32' ? ';' : ':',
}))
.pipe(rename(path => {
path.dirname += '/output'
path.extname = '.pdf'
}))
.pipe(gulp.dest('./latex'))
}
const clean = done => {
glob('./latex/**/*.tex', (error, texFiles) => {
glob('./latex/**/output/*', (error, outputFiles) => {
const correctOutputFiles = texFiles.map(getOutputFile).map(path => {
return `./${path}`
})
for (const file of outputFiles) {
if (!correctOutputFiles.includes(file)) {
console.log(`Deleted ${file}`)
del(file)
}
}
done()
})
})
}
const purge = () => {
return del(['./latex/**/output'])
}
const watchFiles = () => {
gulp.watch('./latex/**/*.tex', compileChanged)
}
export {
compileChanged as default,
compileChanged as latex,
compileAll as latexAll,
clean as clean,
purge as purge,
watchFiles as watch,
}