Skip to content

Instantly share code, notes, and snippets.

@brandontrowe
Last active July 21, 2016 16:11
Show Gist options
  • Save brandontrowe/16f9865d442873477b89b69395967dee to your computer and use it in GitHub Desktop.
Save brandontrowe/16f9865d442873477b89b69395967dee to your computer and use it in GitHub Desktop.
'use strict';
const gulp = require('gulp');
const events = require('events');
const eventEmitter = new events.EventEmitter();
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const prompt = require('gulp-prompt');
const gutil = require('gulp-util');
const browserSync = require('browser-sync').create();
const fs = require('fs');
const gulpif = require('gulp-if');
const profilePath = './profile.json';
let useProfile = false;
let profile = {};
let profileDefaults = {
serverUrl: 'http://example.net:10001/client/promo'
}
gulp.task('test', function(){ // delete me
});
gulp.task('load-profile', function() {
fs.readFile(profilePath, 'utf8', function(err, data) {
if (!err) {
profile = JSON.parse(data);
useProfile = true;
eventEmitter.emit('profile-loaded');
} else {
if (err.code === 'ENOENT') {
// profile file wasn't found, let's create one
gulp.start('create-profile');
} else {
// something else went wrong. throw things
throw err;
}
}
});
});
gulp.task('create-profile', function(){
gulp.src('./gulpfile.js')
// ask question of the user for the profile file
.pipe(prompt.prompt([{
type: 'input',
name: 'serverUrl',
default: profileDefaults.serverUrl,
message: 'What is the server URL?'
}], function(res){
// attach the answers to the profile object before writing the file
profile.serverUrl = res.serverUrl;
useProfile = true;
eventEmitter.emit('profile-loaded');
fs.writeFile(profilePath, JSON.stringify(profile), 'utf8');
}));
});
gulp.task('reset-profile', function(){
fs.access(profilePath, fs.F_OK, function(err) {
if (!err) {
fs.unlink(profilePath);
} else {
gutil.log(gutil.colors.red('No profile file found.'));
}
});
});
gulp.task('sass', function () {
gulp.src('public/scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions', 'ie 9'],
cascade: false,
remove: true
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/css/'))
.pipe(gulpif(useProfile, browserSync.stream()));
});
// Static Server + watching scss/html files
gulp.task('serve', function() {
if (profile.serverUrl && profile.serverUrl.length) {
gutil.log(gutil.colors.blue('Serving up:'), gutil.colors.bgBlue(profile.serverUrl));
browserSync.init({
proxy: profile.serverUrl
});
gulp.watch('public/scss/**/*.scss', ['sass']);
gulp.watch([
// Array of files to watch, changes reload the server
'copydeck/**/*.yml',
'public/js/*.js',
'public/css/*.css',
'public/css/**/*.css'
]).on('change', browserSync.reload);
} else {
gutil.log(gutil.colors.red('No server URL found.'));
}
});
gulp.task('reset', ['reset-profile'], function(){});
gulp.task('default', ['load-profile', 'sass'], function() {
eventEmitter.on('profile-loaded', function(){
gulp.start('serve')
});
gulp.watch('public/scss/**/*.scss', ['sass']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment