Stackedit/Gulpfile.js

347 lines
7.9 KiB
JavaScript
Raw Permalink Normal View History

2014-08-31 19:51:13 +00:00
/* jshint -W015 */
var gulp = require('gulp');
var util = require('gulp-util');
var clean = require('gulp-clean');
var jshint = require('gulp-jshint');
var requirejs = require('gulp-requirejs');
var bowerRequirejs = require('bower-requirejs');
var uglify = require('gulp-uglify');
var less = require('gulp-less');
var inject = require('gulp-inject');
var replace = require('gulp-replace');
var bump = require('gulp-bump');
var git = require('gulp-git');
var runSequence = require('run-sequence');
2014-09-01 21:27:15 +00:00
var es = require('event-stream');
2014-08-31 19:51:13 +00:00
var fs = require('fs');
2014-09-01 21:27:15 +00:00
var knox = require('knox');
var zlib = require('zlib');
var mime = require('mime');
var async = require("async");
2014-08-31 19:51:13 +00:00
/** __________________________________________
* constants.js
*/
function getVersion() {
var packageJson = JSON.parse(fs.readFileSync(__dirname + '/package.json', {
encoding: 'utf8'
}));
return packageJson.version;
}
gulp.task('constants', function() {
return gulp.src('./public/res/constants.js')
.pipe(replace(/constants\.VERSION = .*/, 'constants.VERSION = "' + getVersion() + '";'))
.pipe(gulp.dest('./public/res/'));
});
/** __________________________________________
* JSHint
*/
gulp.task('jshint', function() {
return gulp.src([
'./**/*.js',
'!./node_modules/**/*.js',
'!./public/libs/**/*.js',
'!./public/res/libs/**/*.js',
'!./public/res/bower-libs/**/*.js',
'!./public/res-min/**/*.js'
])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
});
/** __________________________________________
* RequireJS
*/
gulp.task('clean-requirejs', function() {
return gulp.src([
'./public/res-min/main.js',
'./public/res-min/require.js'
])
.pipe(clean());
});
gulp.task('copy-requirejs', ['clean-requirejs'], function() {
return gulp.src('./public/res/bower-libs/requirejs/require.js')
.pipe(gulp.dest('./public/res-min/'));
});
gulp.task('requirejs', [
'copy-requirejs',
'constants'
], function() {
return requirejs({
baseUrl: 'public/res',
name: 'main',
out: 'main.js',
mainConfigFile: 'public/res/main.js',
optimize: 'uglify2',
inlineText: true,
2014-09-01 21:27:15 +00:00
paths: {
mathjax: 'empty:'
},
2014-08-31 19:51:13 +00:00
excludeShallow: [
'css/css-builder',
'less/lessc-server',
'less/lessc'
]
})
.pipe(uglify({
output: {
beautify: true,
indent_level: 1
}
}))
.pipe(gulp.dest('./public/res-min/'));
});
2014-09-01 21:27:15 +00:00
gulp.task('bower-requirejs', function(cb) {
bowerRequirejs({
config: './public/res/main.js'
}, function() {
cb();
});
});
2014-08-31 19:51:13 +00:00
/** __________________________________________
* Less
*/
gulp.task('clean-less', function() {
return gulp.src('./public/res-min/themes')
.pipe(clean());
});
gulp.task('less', ['clean-less'], function() {
return gulp.src([
'./public/res/styles/base.less',
'./public/res/themes/*.less'
])
.pipe(less({
compress: true
}))
.pipe(gulp.dest('./public/res-min/themes/'));
});
/** __________________________________________
* Fonts
*/
gulp.task('clean-font', function() {
return gulp.src('./public/res-min/font')
.pipe(clean());
});
gulp.task('copy-font', ['clean-font'], function() {
return gulp.src('./public/res/font/*')
.pipe(gulp.dest('./public/res-min/font/'));
});
/** __________________________________________
* Images
*/
gulp.task('clean-img', function() {
return gulp.src('./public/res-min/img')
.pipe(clean());
});
gulp.task('copy-img', ['clean-img'], function() {
return gulp.src('./public/res/img/*')
.pipe(gulp.dest('./public/res-min/img/'));
});
/** __________________________________________
* cache.manifest
*/
function makeCacheManifest(dest, cdn) {
cdn = cdn || '';
return gulp.src('./public/cache.manifest')
.pipe(replace(/(#Date ).*/, '$1' + Date()))
2014-09-01 21:27:15 +00:00
.pipe(inject(gulp.src('./res-min/**/*.*', {
2014-08-31 19:51:13 +00:00
read: false,
cwd: './public'
}),
{
starttag: '# start_inject_resources',
endtag: '# end_inject_resources',
ignoreExtensions: true,
transform: function(filepath) {
2014-09-01 21:27:15 +00:00
return cdn + filepath.substring(1);
}
}))
.pipe(inject(gulp.src([
'./MathJax.js',
'./config/Safe.js',
'./config/TeX-AMS_HTML.js',
'./images/CloseX-31.png',
'./images/MenuArrow-15.png',
'./jax/output/HTML-CSS/jax.js',
'./extensions/**/*.*',
'./fonts/HTML-CSS/TeX/woff/**/*.*',
'./jax/element/**/*.*',
'./jax/output/HTML-CSS/autoload/**/*.*',
'./jax/output/HTML-CSS/fonts/TeX/**/*.*',
'./jax/output/HTML-CSS/fonts/STIX/**/*.*'
], {
read: false,
cwd: './public/res/bower-libs/MathJax'
}),
{
starttag: '# start_inject_mathjax',
endtag: '# end_inject_mathjax',
ignoreExtensions: true,
transform: function(filepath) {
if(filepath == '/MathJax.js') {
2014-08-31 19:51:13 +00:00
filepath += '?config=TeX-AMS_HTML';
}
2014-09-01 21:27:15 +00:00
else {
filepath += '?rev=2.4-beta-2';
}
return '//cdn.mathjax.org/mathjax/2.4-latest' + filepath;
2014-08-31 19:51:13 +00:00
}
}))
.pipe(gulp.dest(dest));
}
gulp.task('cache-manifest', function() {
return makeCacheManifest('./public/');
});
gulp.task('cache-manifest-stackedit-io', function() {
2014-09-01 21:27:15 +00:00
return makeCacheManifest('./public-stackedit.io/', '//cdn.stackedit.io/' + getVersion() + '/');
2014-08-31 19:51:13 +00:00
});
gulp.task('clean', [
'clean-requirejs',
'clean-less',
'clean-font',
'clean-img'
]);
2014-08-31 20:35:18 +00:00
gulp.task('default', function(cb) {
runSequence([
2014-08-31 19:51:13 +00:00
'jshint',
'requirejs',
'less',
'copy-font',
'copy-img'
],
2014-08-31 20:35:18 +00:00
'cache-manifest',
2014-09-01 22:37:46 +00:00
'cache-manifest-stackedit-io',
2014-08-31 20:35:18 +00:00
cb);
2014-08-31 19:51:13 +00:00
});
function bumpTask(importance) {
return function() {
return gulp.src([
'./package.json',
'./bower.json'
])
.pipe(bump({type: importance}))
.pipe(gulp.dest('./'));
};
}
gulp.task('bump-patch', bumpTask('patch'));
gulp.task('bump-minor', bumpTask('minor'));
gulp.task('bump-major', bumpTask('major'));
2014-08-31 20:05:34 +00:00
gulp.task('git-add', function() {
2014-08-31 19:51:13 +00:00
return gulp.src('./public/res-min/**/*')
2014-08-31 20:05:34 +00:00
.pipe(git.add());
2014-08-31 19:51:13 +00:00
});
2014-08-31 20:05:34 +00:00
gulp.task('git-tag', function() {
2014-09-01 23:20:47 +00:00
var version = getVersion();
var message = 'Version ' + version;
var tag = 'v' + version;
git.commit(message, { args: '-a' }).end();
2014-08-31 19:51:13 +00:00
util.log('Tagging as: ' + util.colors.cyan(tag));
2014-09-01 23:20:47 +00:00
git.tag(tag, message);
git.push('origin', 'master', { args: ' --tags' });
2014-08-31 19:51:13 +00:00
});
function releaseTask(importance) {
2014-08-31 20:35:18 +00:00
return function(cb) {
runSequence(
2014-08-31 19:51:13 +00:00
'bump-' + importance,
'default',
2014-08-31 20:05:34 +00:00
'git-add',
'git-tag',
2014-08-31 20:35:18 +00:00
cb);
2014-08-31 19:51:13 +00:00
};
}
2014-08-31 20:35:18 +00:00
gulp.task('patch', releaseTask('patch'));
gulp.task('minor', releaseTask('minor'));
2014-09-01 21:27:15 +00:00
gulp.task('major', releaseTask('major'));
gulp.task('deploy-cdn', function() {
var s3Client = knox.createClient({
key: process.env.STACKEDIT_AWS_ACCESS_KEY_ID,
secret: process.env.STACKEDIT_AWS_SECRET_KEY,
bucket: 'cdn.stackedit.io'
});
var zippedFormat = {
'text/plain': true,
'text/html': true,
'text/css': true,
'text/cache-manifest': true,
'application/javascript': true,
'image/svg+xml': true
};
function upload(file, cb) {
var headers = {
'x-amz-acl': 'public-read',
'Content-Length': file.contents.length,
'Content-Type': file.contentType,
'Cache-Control': 'max-age=' + file.maxAge
};
file.contentEncoding && (headers['Content-Encoding'] = file.contentEncoding);
s3Client.putBuffer(file.contents, file.dest + file.relative, headers, cb);
}
var queue = async.queue(upload, 16).push;
var version = getVersion();
return gulp.src([
'./**/*',
'!./res/bower-libs/**/*'
], {
cwd: './public',
buffer: false
})
.pipe(es.map(function(file, cb) {
if(!file.contents) {
return cb(null, file);
}
file.contentType = mime.lookup(file.path);
var stream = file.contents;
if(zippedFormat.hasOwnProperty(file.contentType)) {
var gzip = zlib.createGzip();
stream = stream.pipe(gzip);
file.contentEncoding = 'gzip';
}
var bufs = [];
stream.on('data', function(d) {
bufs.push(d);
});
stream.on('error', function(err) {
cb(err);
});
stream.on('end', function() {
file.contents = Buffer.concat(bufs);
file.dest = 'latest/';
file.maxAge = 86400;
queue(file, function(err) {
file.dest = version + '/';
file.maxAge = 31536000;
err ? cb(err) : queue(file, cb);
});
});
}));
});