freeCodeCamp/gulpfile.js

462 lines
11 KiB
JavaScript
Raw Normal View History

// enable debug for gulp
2017-03-28 18:25:08 +00:00
/* eslint-disable prefer-object-spread/prefer-object-spread */
2016-01-27 19:34:44 +00:00
process.env.DEBUG = process.env.DEBUG || 'fcc:*';
require('dotenv').load();
require('babel-core/register');
2017-03-28 18:25:08 +00:00
const Rx = require('rx'),
gulp = require('gulp'),
2015-06-26 23:27:10 +00:00
path = require('path'),
debug = require('debug')('fcc:gulp'),
yargs = require('yargs'),
sortKeys = require('sort-keys'),
del = require('del'),
2015-06-26 23:27:10 +00:00
// utils
2015-07-25 15:51:41 +00:00
plumber = require('gulp-plumber'),
2015-06-26 23:27:10 +00:00
notify = require('gulp-notify'),
gutil = require('gulp-util'),
reduce = require('gulp-reduce-file'),
2015-11-11 02:27:58 +00:00
concat = require('gulp-concat'),
2015-11-23 04:26:44 +00:00
uglify = require('gulp-uglify'),
2015-11-11 02:27:58 +00:00
merge = require('merge-stream'),
2015-11-13 19:10:23 +00:00
babel = require('gulp-babel'),
2015-11-30 22:27:39 +00:00
sourcemaps = require('gulp-sourcemaps'),
gulpif = require('gulp-if'),
2015-06-26 23:27:10 +00:00
2015-06-29 16:50:25 +00:00
// react app
webpack = require('webpack'),
webpackStream = require('webpack-stream'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
2015-06-29 16:50:25 +00:00
webpackConfig = require('./webpack.config.js'),
2015-06-26 23:27:10 +00:00
// server process
nodemon = require('gulp-nodemon'),
browserSync = require('browser-sync'),
2015-06-26 23:27:10 +00:00
// css
less = require('gulp-less'),
2015-06-26 23:27:10 +00:00
// rev
rev = require('gulp-rev'),
2015-09-11 02:01:12 +00:00
revDel = require('rev-del'),
2015-06-26 23:27:10 +00:00
// lint
2015-08-24 23:19:34 +00:00
jsonlint = require('gulp-jsonlint'),
eslint = require('gulp-eslint'),
2016-01-01 01:53:16 +00:00
// unit-tests
tape = require('gulp-tape'),
tapSpec = require('tap-spec');
2015-06-26 23:27:10 +00:00
2015-08-12 18:46:34 +00:00
Rx.config.longStackSupport = true;
2017-03-28 18:25:08 +00:00
const sync = browserSync.create('fcc-sync-server');
function resolve(filepath, thisString, withThisString) {
const newPath = require.resolve(filepath);
if (thisString && withThisString) {
return newPath.replace(thisString, withThisString);
}
return newPath;
}
2015-08-12 18:46:34 +00:00
// user definable
2017-03-28 18:25:08 +00:00
const __DEV__ = !yargs.argv.p;
const host = process.env.HOST || 'localhost';
2017-03-28 18:25:08 +00:00
const port = yargs.argv.port || process.env.PORT || '3001';
const syncPort = yargs.argv['sync-port'] || process.env.SYNC_PORT || '3000';
// make sure sync ui port does not interfere with proxy port
2017-03-28 18:25:08 +00:00
const syncUIPort = yargs.argv['sync-ui-port'] ||
process.env.SYNC_UI_PORT ||
parseInt(syncPort, 10) + 2;
2017-03-28 18:25:08 +00:00
const paths = {
server: './server/server.js',
2015-06-26 23:27:10 +00:00
serverIgnore: [
'gulpfile.js',
'public/',
'node_modules/',
'client/',
2015-09-15 00:31:24 +00:00
'seed',
'server/manifests/*.json',
'server/rev-manifest.json'
2015-06-26 23:27:10 +00:00
],
2015-07-16 23:01:25 +00:00
2015-06-26 23:27:10 +00:00
publicJs: './public/js',
css: 'public/css',
2015-06-26 23:27:10 +00:00
loopback: {
client: './client/loopbackClient',
root: path.join(__dirname, 'client/'),
clientName: 'lbApp'
},
2015-06-29 16:50:25 +00:00
client: {
src: './client',
dest: 'public/js'
},
2015-11-11 02:27:58 +00:00
vendorChallenges: [
2017-03-28 18:25:08 +00:00
resolve('jshint', 'src', 'dist'),
resolve('chai', 'index.js', 'chai.js'),
resolve('codemirror'),
resolve('codemirror', 'lib/codemirror.js', 'addon/comment/comment.js'),
resolve('codemirror', 'lib/codemirror.js', 'addon/edit/closebrackets.js'),
resolve('codemirror', 'lib/codemirror.js', 'addon/edit/matchbrackets.js'),
resolve('codemirror', 'lib/codemirror.js', 'addon/lint/lint.js'),
resolve('codemirror', 'lib/codemirror.js', 'addon/lint/javascript-lint.js'),
resolve('codemirror', 'lib/codemirror.js', 'mode/javascript/javascript.js'),
resolve('codemirror', 'lib/codemirror.js', 'mode/xml/xml.js'),
resolve('codemirror', 'lib/codemirror.js', 'mode/css/css.js'),
resolve('codemirror', 'lib/codemirror.js', 'mode/htmlmixed/htmlmixed.js'),
resolve('emmet-codemirror'),
2015-11-30 22:27:39 +00:00
'public/js/lib/loop-protect/loop-protect.js'
2015-11-11 02:27:58 +00:00
],
vendorMain: [
2017-03-28 18:25:08 +00:00
resolve('jquery', '.js', '.min.js'),
resolve('bootstrap', 'npm.js', 'bootstrap.min.js'),
resolve('d3', '.js', '.min.js'),
resolve('cal-heatmap'),
resolve('moment', '.js', '.min.js'),
resolve(
'moment-timezone',
'index.js',
'builds/moment-timezone-with-data.min.js'
),
resolve('mousetrap', '.js', '.min.js'),
resolve('lightbox2', '.js', '.min.js'),
resolve('rx', 'index.js', 'dist/rx.all.min.js')
2015-11-11 02:27:58 +00:00
],
js: [
'client/main.js',
2016-05-20 19:42:26 +00:00
'client/frame-runner.js',
2015-11-30 22:27:39 +00:00
'client/plugin.js'
],
less: './client/less/main.less',
lessFiles: [
'./client/**/*.less',
'./common/**/*.less'
],
manifest: 'server/manifests/',
2015-06-29 16:50:25 +00:00
node: {
src: './client',
dest: 'common/app'
2015-06-29 16:50:25 +00:00
},
2015-06-26 23:27:10 +00:00
syncWatch: [
'public/**/*.*'
2015-08-24 23:19:34 +00:00
],
challenges: [
'seed/challenges/*/*.json'
2015-06-26 23:27:10 +00:00
]
};
2017-03-28 18:25:08 +00:00
const webpackOptions = {
2015-07-25 16:02:56 +00:00
devtool: 'inline-source-map'
};
2017-03-28 18:25:08 +00:00
const errorNotifier = notify.onError({
title: 'Compile Error',
message: '<%= error %>'
});
2015-07-25 15:51:41 +00:00
2017-03-28 18:25:08 +00:00
function errorHandler(...args) {
2015-07-25 15:51:41 +00:00
// Send error to notification center with gulp-notify
2017-03-28 18:25:08 +00:00
errorNotifier.apply(this, args);
2015-07-25 15:51:41 +00:00
// Keep gulp from hanging on this task
this.emit('end');
}
function delRev(dest, manifestName) {
// in production do not delete old revisions
if (!__DEV__) {
return gutil.noop();
}
return revDel({
oldManifest: path.join(paths.manifest, manifestName),
dest: dest
});
}
gulp.task('serve', function(cb) {
let called = false;
nodemon({
script: paths.server,
ext: '.jsx .js .json',
ignore: paths.serverIgnore,
exec: path.normalize('node_modules/.bin/babel-node'),
env: {
2016-04-25 04:54:48 +00:00
NODE_ENV: process.env.NODE_ENV || 'development',
DEBUG: process.env.DEBUG || 'fcc:*',
PORT: port
}
})
.on('start', function() {
if (!called) {
called = true;
cb();
}
})
.on('restart', function(files) {
if (files) {
debug('Nodemon will restart due to changes in: ', files);
}
});
});
2017-03-28 18:25:08 +00:00
const syncDepenedents = [
'serve',
'js',
2016-06-01 22:52:08 +00:00
'less'
];
gulp.task('dev-server', syncDepenedents, function() {
webpackConfig.entry.bundle = [
'webpack/hot/dev-server',
'webpack-hot-middleware/client'
].concat(webpackConfig.entry.bundle);
2017-03-28 18:25:08 +00:00
const bundler = webpack(webpackConfig);
sync.init(null, {
ui: {
port: syncUIPort
},
proxy: {
target: `http://${host}:${port}`,
reqHeaders: ({ url: { hostname } }) => ({
host: `${hostname}:${syncPort}`
})
},
logLeval: 'debug',
2015-06-26 23:27:10 +00:00
files: paths.syncWatch,
port: syncPort,
open: false,
middleware: [
webpackDevMiddleware(bundler, {
publicPath: webpackConfig.output.publicPath,
stats: 'errors-only'
}),
webpackHotMiddleware(bundler)
]
});
});
2014-10-13 21:14:51 +00:00
2015-08-24 23:19:34 +00:00
gulp.task('lint-js', function() {
2015-11-07 05:12:14 +00:00
return gulp.src([
'common/**/*.js',
'common/**/*.jsx',
'client/**/*.js',
'client/**/*.jsx',
'server/**/*.js',
'config/**/*.js'
])
.pipe(eslint())
.pipe(eslint.format());
});
2015-08-24 23:19:34 +00:00
gulp.task('lint-json', function() {
return gulp.src(paths.challenges)
.pipe(jsonlint())
.pipe(jsonlint.reporter());
});
gulp.task('test-challenges', ['lint-json']);
gulp.task('pack-client', function() {
2015-11-23 04:26:44 +00:00
if (!__DEV__) { console.log('\n\nbundling production\n\n'); }
function condition(file) {
2017-03-28 18:25:08 +00:00
const filepath = file.relative;
return __DEV__ || (/json$/).test('' + filepath);
}
2017-03-28 18:25:08 +00:00
const dest = webpackConfig.output.path;
return gulp.src(webpackConfig.entry.bundle)
2017-03-28 18:25:08 +00:00
.pipe(plumber({ errorHandler }))
.pipe(webpackStream(Object.assign(
{},
webpackConfig,
webpackOptions
)))
.pipe(gulpif(condition, gutil.noop(), uglify()))
.pipe(gulp.dest(dest));
});
2017-03-28 18:25:08 +00:00
const webpackManifestFiles = [ 'react-manifest.json', 'chunk-manifest.json' ];
gulp.task('move-webpack-manifest', ['pack-client'], function() {
2017-03-28 18:25:08 +00:00
const files = webpackManifestFiles.map(function(filename) {
return path.join(webpackConfig.output.path, filename);
});
return gulp.src(files).pipe(gulp.dest(paths.manifest));
});
2017-03-28 18:25:08 +00:00
const cleanDeps = ['pack-client', 'move-webpack-manifest'];
gulp.task('clean-webpack-manifest', cleanDeps, function() {
return del(webpackManifestFiles.map(function(filename) {
return path.join(webpackConfig.output.path, filename);
}))
.then(function(pathsDeleted) {
gutil.log('[clean-webpack-manifest]', 'paths deleted' + pathsDeleted);
})
.catch(function(err) {
throw new gutil.PluginError('clean-webpack-manifest', err);
});
});
gulp.task('less', function() {
2017-03-28 18:25:08 +00:00
const manifestName = 'css-manifest.json';
const dest = paths.css;
return gulp.src(paths.less)
2017-03-28 18:25:08 +00:00
.pipe(plumber({ errorHandler }))
.pipe(__DEV__ ? sourcemaps.init() : gutil.noop())
2015-11-11 02:27:58 +00:00
// compile
.pipe(less({
paths: [
path.join(__dirname, 'client', 'less'),
path.join(__dirname, 'common')
]
}))
.pipe(__DEV__ ?
sourcemaps.write({ sourceRoot: '/less' }) :
gutil.noop()
)
.pipe(gulp.dest(dest))
// add revision
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : rev())
// copy files to public
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : gulp.dest(dest))
// create and merge manifest
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : rev.manifest(manifestName))
.pipe(__DEV__ ? gutil.noop() : delRev(
dest,
manifestName
))
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : gulp.dest(paths.manifest));
});
2015-11-11 02:27:58 +00:00
function getFilesGlob(files) {
if (!__DEV__) {
return files;
}
return files.map(function(file) {
return file
.replace('.min.', '.')
// moment breaks the pattern
.replace('/min/', '/');
});
}
gulp.task('js', function() {
2017-03-28 18:25:08 +00:00
const manifestName = 'js-manifest.json';
const dest = paths.publicJs;
2015-11-11 02:27:58 +00:00
2017-03-28 18:25:08 +00:00
const jsFiles = merge(
2015-11-11 02:27:58 +00:00
gulp.src(getFilesGlob(paths.vendorMain))
2015-12-03 07:19:02 +00:00
.pipe(__DEV__ ? sourcemaps.init() : gutil.noop())
.pipe(concat('vendor-main.js'))
.pipe(
__DEV__ ?
sourcemaps.write({ sourceRoot: '/vendor' }) :
gutil.noop()
),
2015-11-11 02:27:58 +00:00
gulp.src(paths.vendorChallenges)
2015-12-03 07:19:02 +00:00
.pipe(__DEV__ ? sourcemaps.init() : gutil.noop())
2015-11-11 02:27:58 +00:00
.pipe(__DEV__ ? gutil.noop() : uglify())
2015-12-03 07:19:02 +00:00
.pipe(concat('vendor-challenges.js'))
.pipe(
__DEV__ ?
sourcemaps.write({ sourceRoot: '/vendor' }) :
gutil.noop()
),
2015-11-11 02:27:58 +00:00
gulp.src(paths.js)
2017-03-28 18:25:08 +00:00
.pipe(plumber({ errorHandler }))
2015-11-13 19:10:23 +00:00
.pipe(babel())
.pipe(__DEV__ ? gutil.noop() : uglify())
);
return jsFiles
.pipe(gulp.dest(dest))
// create registry file
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : rev())
// copy revisioned assets to dest
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : gulp.dest(dest))
// create manifest file
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : rev.manifest(manifestName))
.pipe(__DEV__ ? gutil.noop() : delRev(
dest,
manifestName
))
// copy manifest file to dest
2016-03-18 19:56:01 +00:00
.pipe(__DEV__ ? gutil.noop() : gulp.dest(paths.manifest));
});
function collector(file, memo) {
return Object.assign({}, JSON.parse(file.contents), memo);
}
function done(manifest) {
return sortKeys(manifest);
}
2017-03-28 18:25:08 +00:00
const buildDependents = [
'less',
'js',
'pack-client',
'move-webpack-manifest'
];
gulp.task('build-manifest', buildDependents, function() {
return gulp.src(paths.manifest + '*.json')
.pipe(reduce('rev-manifest.json', collector, done, {}))
.pipe(gulp.dest('server/'));
});
gulp.task('build', [
'less',
'js',
'pack-client',
'move-webpack-manifest',
'clean-webpack-manifest',
'build-manifest'
]);
2017-03-28 18:25:08 +00:00
const watchDependents = [
'less',
'js',
'serve',
'dev-server'
];
gulp.task('watch', watchDependents, function() {
2015-10-16 20:28:53 +00:00
gulp.watch(paths.lessFiles, ['less']);
gulp.watch(paths.js.concat(paths.vendorChallenges), ['js']);
gulp.watch(paths.js, ['js']);
2015-06-16 19:32:12 +00:00
});
gulp.task('default', [
'less',
'serve',
'watch',
'dev-server'
]);
gulp.task('test', function() {
return gulp.src('test/**/*.js')
.pipe(tape({
reporter: tapSpec()
}));
});