Skip to content

Instantly share code, notes, and snippets.

@r-k-b
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-k-b/749be69fbe89560c8a7c to your computer and use it in GitHub Desktop.
Save r-k-b/749be69fbe89560c8a7c to your computer and use it in GitHub Desktop.
improving the base package.json for less & autoprefixer preprocessing on Business Catalyst sites
var gulp = require('gulp'),
path = require('path'),
sftp = require('gulp-sftp'),
less = require('gulp-less'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer'),
rename = require('gulp-rename'),
browserSync = require('browser-sync');
var setup = {
siteDomain: 'test-incontextediting.worldsecuresystems.com',
pathToMainLessFile: "assets/less/", // include the trailing forward slash
mainLessFileName: "central", //.less is implied; leave it out
lessOptions = {
compress: true
}
};
setup.lessFileWithPath = path.join(setup.pathToMainLessFile, setup.mainLessFileName);
setup.outputPath = setup.pathToMainLessFile;
// NOTE: `.less` files that aren't in `setup.pathToMainLessFile` won't be
// watched by the less compiler.
// NOTE: All generated files will be uploaded to the path `setup.outputPath`
// on the remote.
// TODO: Upload generated files to the same remote path as the local path (relative
// to project root)
var homedir = (process.platform === 'win32' || process.platform === 'win64') ? process.env.HOMEPATH : process.env.HOME;
var credentials = require(path.join(homedir, 'bc-credentials.json'));
setup.remoteHost = {
host: setup.siteDomain,
port: 22,
user: setup.siteDomain + '/' + credentials.user,
pass: credentials.pass,
remotePath: '/' + setup.outputPath
};
var dontWatchFoldersRE = /node_modules/i;
var browserSyncTask = function() {
browserSync({
// TODO: find a better way to exclude folders like `node_modules`
// and `.git`. This may be a good starting point:
// https://github.com/shama/gaze/issues/109
// FIXME: Watcher is currently limited to 4 levels depth, for
// startup performance.
files : [
'*.css',
'*.html',
'*.htm',
'*.js',
'*/*.css',
'*/*.html',
'*/*.htm',
'*/*.js',
'*/*/*.css',
'*/*/*.html',
'*/*/*.htm',
'*/*/*.js',
'*/*/*/*.css',
'*/*/*/*.html',
'*/*/*/*.htm',
'*/*/*/*.js',
'*/*/*/*/*.css',
'*/*/*/*/*.html',
'*/*/*/*/*.htm',
'*/*/*/*/*.js'
],
watchOptions: {
// WHY wasn't `filter` documented in browser-sync or gaze???
filter: function(srcPath, opts) {
var isMatch = !dontWatchFoldersRE.test(srcPath);
//process.stdout.write(isMatch ? 'T' : '.');
return isMatch;
}
},
port: 3001,
proxy: setup.siteDomain,
reloadDelay: 5000,
open: false,
minify: false
});
}
var buildLess = function() {
return gulp.src(setup.lessFileWithPath + '.less')
.pipe(sourcemaps.init())
.pipe(less(setup.lessOptions))
.pipe(autoprefixer())
.pipe(rename({extname:'.min.prefixed.css'}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(setup.outputPath));
}
var buildAndUpload = function() {
// TODO: reduce repetition by extending `buildLess()`
// FIXME: keep the SSH connection alive & reusable
return gulp.src(setup.lessFileWithPath + '.less')
.pipe(sourcemaps.init())
.pipe(less(setup.lessOptions))
.pipe(autoprefixer())
.pipe(rename({extname:'.min.prefixed.css'}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(setup.outputPath))
.pipe(sftp(setup.remoteHost));
}
gulp.task('build-less', buildLess);
gulp.task('build-and-upload', buildAndUpload);
gulp.task('watch', function() {
gulp.watch(
setup.pathToMainLessFile + '*.less',
['build-and-upload']
);
});
gulp.task('browser-sync', browserSyncTask);
gulp.task('default', [
'watch',
'browser-sync'
]);
{
"//": [
"Packages used for less & autoprefixer preprocessing on Business Catalyst sites",
"Replace SITE-NAME-HERE with your site name if you feel like it; it's not used anywhere (yet)"
],
"name": "SITE-NAME-HERE",
"version": "0.0.1",
"description": "",
"main": "gruntfile.js",
"dependencies": {},
"devDependencies": {
"browser-sync": "^1.9.1",
"gulp": "^3.8.10",
"gulp-autoprefixer": "^2.1.0",
"gulp-less": "^2.0.1",
"gulp-rename": "^1.2.0",
"gulp-sftp": "^0.1.4",
"gulp-sourcemaps": "^1.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "x:\\\\git-repos\\\\SITE-NAME-HERE\\\\"
},
"author": "",
"license": "ISC"
}
@chriswetterman
Copy link

Have you determined how to keep the sftp connection alive? I haven't dug into the gulp-sftp source yet, but it seems it may be explicitly closing the connection after all uploads have completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment