-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathgulpfile.js
214 lines (187 loc) · 5.42 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
var path = require('path');
var gulp = require('gulp');
var rimraf = require('rimraf');
var fs = require('fs');
var pygmentize = require('pygmentize-bundled');
var opn = require('opn');
var chalk = require('chalk');
var osTmpdir = require('os-tmpdir');
var replace = require('gulp-replace');
var gutil = require('gulp-util');
var concat = require('gulp-concat');
var pandoc = require('gulp-pandoc');
var sass = require('gulp-ruby-sass');
var markdown = require('gulp-markdown');
var markdownpdf = require('gulp-markdown-pdf');
var livereload = require('gulp-livereload');
var layoutize = require('gulp-layoutize');
var deploy = require('gulp-gh-pages');
var srcFromToc = require('./gulp-plugin/gulp-parse-toc');
const CHAPTERS_DIR = 'chapters';
const TEMPLATE_DIR = 'template';
const TEMPLATE_VIEWS_DIR = path.join(TEMPLATE_DIR, 'views');
const TEMPLATE_SASS_DIR = path.join(TEMPLATE_DIR, 'sass');
const DEST_DIR = 'dist';
const REPO_NAME = 'book-of-modern-frontend-tooling';
const BASE_DIR = path.join(DEST_DIR, 'site');
const SITE_DIR = path.join(BASE_DIR, REPO_NAME);
const SITE_ASSETS = path.join(SITE_DIR, 'assets');
const SITE_JS_DIR = path.join(SITE_ASSETS, 'js');
const SITE_CSS_DIR = path.join(SITE_ASSETS, 'css');
const SITE_VENDOR_DIR = path.join(SITE_ASSETS, 'vendor');
const TMP_DIR = osTmpdir();
/*
* List all tasks
**/
gulp.task('default', function () {
gutil.log('Available Tasks:');
Object.keys(gulp.tasks).forEach(function (taskName) {
if (taskName.indexOf('generate') > -1) {
gutil.log('\t', chalk.yellow(taskName));
}
});
});
/*
* Removes the distribution (dist/) directory.
**/
gulp.task('clean', function () {
return rimraf.sync(DEST_DIR);
});
/*
* Copy assets from chapters to distrubtion directory.
**/
gulp.task('copy-assets', function () {
return gulp.src('chapters/assets/**/*')
.pipe(gulp.dest(SITE_ASSETS));
});
/*
* Concatenates all the markdowns into a single index.md markdown
**/
gulp.task('concat', function () {
var concatFileName = 'index.md';
return gulp.src(['chapters/**/*.md'])
.pipe(srcFromToc('chapters/toc.md'))
.pipe(concat(concatFileName))
.pipe(gulp.dest(DEST_DIR));
});
/*
* Converts the concatenated markdown (index.md) to
* index.pdf
**/
gulp.task('generate:pdf', ['concat'], function () {
const CSS_PATH = __dirname + '/build-assets/pdf.css';
return gulp.src([
path.join(DEST_DIR, 'index.md')
])
.pipe(markdownpdf({
cssPath: CSS_PATH
}))
.pipe(gulp.dest(DEST_DIR));
});
/*
* Converts the concatenated markdown (index.md) to
* index.epub
**/
gulp.task('generate:epub', ['concat'], function () {
return gulp.src([
path.join(DEST_DIR, 'index.md')
])
.pipe(pandoc({
from: 'markdown',
to: 'epub',
ext: '.epub',
args: ['-o', './dist/index.epub', '--latex-engine', 'xelatex']
}));
});
/*
* Deploy the contents of the site folder to github pages (gh-pages)
**/
gulp.task('deploy', ['generate:site'], function (cb) {
return gulp.src(DEST_DIR + '/site/**/*')
.pipe(deploy('[email protected]:tooling/book-of-modern-frontend-tooling.git', 'origin'));
});
/*
* Gulp tasks specific to help generate HTML site
* ==============================================
**/
/*
* Generate css from sass files in the template folder
**/
gulp.task('site:sass', function () {
const CSS_PATH = 'style.css';
return gulp.src(TEMPLATE_SASS_DIR + '/*.scss')
.pipe(sass())
.pipe(concat(CSS_PATH))
.pipe(gulp.dest(SITE_CSS_DIR));
});
/*
* Generates TOC html to OS's tmp directory
**/
gulp.task('site:toc', function () {
return gulp.src(['chapters/toc.md'])
.pipe(markdown())
.pipe(replace(/md/g, 'html'))
.pipe(gulp.dest(TMP_DIR));
});
/*
* Generates site
**/
gulp.task('generate:site', ['site:sass', 'site:toc', 'copy-assets'], function () {
var tocFile = fs.readFileSync(TMP_DIR + '/toc.html', 'utf8');
var templatePath = path.join(TEMPLATE_VIEWS_DIR, 'index.jade');
return gulp.src(['chapters/**/*.md'])
.pipe(srcFromToc('chapters/toc.md'))
.pipe(markdown({
highlight: function (code, lang, callback) {;
pygmentize({
lang: lang,
format: 'html'
}, code, function (err, result) {
callback(err, result.toString());
});
}
}))
.pipe(layoutize({
templatePath: templatePath,
engine: 'jade',
locals: {
tocContent: tocFile
}
}))
.pipe(gulp.dest(SITE_DIR));
});
/*
* Runs a live-reload server while listening on all files in the dist
* folder
**/
gulp.task('watch', ['generate:site', 'serve'], function() {
var server = livereload();
opn('http://localhost:3000/' + REPO_NAME);
return gulp.watch([
CHAPTERS_DIR + '/**/*',
TEMPLATE_DIR + '/**/*',
SITE_DIR + '/**/*'
])
.on('change', function (file) {
var filePath = path.relative(__dirname, file.path);
if (filePath.indexOf(TEMPLATE_VIEWS_DIR) === 0 ||
filePath.indexOf(TEMPLATE_SASS_DIR) === 0) {
gulp.run('generate:site');
} else {
server.changed(file.path);
}
});
});
/*
* Runs a static server on port 3000
**/
gulp.task('serve', function () {
const PORT = 3000;
var fileServer = new (require('node-static')).Server(BASE_DIR);
require('http').createServer(function (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
}).resume();
}).listen(PORT);
gutil.log(chalk.blue('HTTP server listening on port', PORT));
});