Skip to content

Instantly share code, notes, and snippets.

@austenc
Last active August 1, 2018 14:09
Show Gist options
  • Save austenc/d891117941c0a5c3c99d to your computer and use it in GitHub Desktop.
Save austenc/d891117941c0a5c3c99d to your computer and use it in GitHub Desktop.
Example gruntfile with phantomjs / codeception integration, and asset building
'use strict';
var root = "~/Code/toolbox";
var bench = "~/Code/toolbox/workbench/hdmaster/nucleus";
var ssh = 'ssh vagrant@127.0.0.1 -p 2222 -t';
module.exports = function(grunt) {
grunt.initConfig({
// Execute codeception tests (or test!) on VM from command on host machine
exec: {
testbuild: {
command: ssh+' "cd '+root+' && php artisan test:build --path="/workbench/hdmaster/nucleus/tests""'
},
assetpublish: {
command: ssh+' "cd '+root+' && php artisan asset:publish --bench="hdmaster/nucleus""'
},
codeceptsingle: {
command: ssh+' "cd '+bench+' && vendor/bin/codecept run '
+'<%= codecept.suite %> '
+'<%= codecept.file %> "'
},
codecept: {
command: ssh+' "cd '+bench+' && vendor/bin/codecept run"'
},
startphantom: {
command: ssh+' "nohup phantomjs --webdriver=4444 0<&- &>/dev/null &"'
},
stopphantom: {
command: ssh+' "sudo killall -9 phantomjs"'
}
},
// Ability to run phpunit tests
phpunit: {
classes: {
dir: 'app/tests'
},
options: {
bin: 'vendor/bin/phpunit',
colors: true
}
},
// Compile the .less files into .css
less: {
dist: {
files: {
'public/css/style.min.css': [
'public/packages/hdmaster/nucleus/less/style.less'
],
'public/css/print.min.css': [
'public/packages/hdmaster/nucleus/less/print.less'
]
},
options: {
compress: true
}
}
},
// Apply browser vendor prefixes to css automatically
autoprefixer: {
dist: {
files: {
'public/css/style.min.css': 'public/css/style.min.css'
}
}
},
// Minify JS includes into one
uglify: {
options: {
mangle: {
except: ['headroom']
}
},
dist: {
files: {
'public/js/scripts.min.js': [
'public/vendor/jquery/dist/jquery.min.js',
'public/vendor/bootstrap/dist/js/bootstrap.min.js',
'public/vendor/bootstrap-datepicker/js/bootstrap-datepicker.js',
'public/vendor/jquery-color/jquery.color.js',
'public/vendor/ladda-bootstrap/dist/spin.min.js',
'public/vendor/ladda-bootstrap/dist/ladda.min.js',
'public/vendor/headroom.js/dist/headroom.min.js',
'public/vendor/headroom.js/dist/jQuery.headroom.min.js',
'public/packages/hdmaster/nucleus/js/common.js'
],
}
}
},
// Watch / livereload for several filetypes
watch: {
less: {
files: [
'workbench/hdmaster/nucleus/public/less/**/*.less'
],
tasks: ['compileLess']
},
styles: {
files: ['style.min.css'],
tasks: ['autoprefixer']
},
js: {
files: [
'workbench/hdmaster/nucleus/public/js/common.js',
'workbench/hdmaster/nucleus/public/js/**/*.js'
],
tasks: ['compileJs']
},
views: {
files: [
'app/views/**/*.php'
]
},
tests: {
files: [
'workbench/hdmaster/nucleus/tests/*/*.php',
],
tasks: ['testsingle'],
options: {
spawn: false,
},
},
options: {
spawn: false
}
},
browserSync: {
dev: {
bsFiles: {
src: ['public/css/*.css', 'workbench/hdmaster/nucleus/src/views/**/*.blade.php']
},
options: {
// change this to your local host / url, including port
proxy: "workbench.dev:8000",
watchTask: true
}
}
},
clean: {
dist: [
'public/css/style.min.css',
'public/js/scripts.min.js'
]
}
}); // initConfig
// On watch events look at the filepath
// IF it's a codecept test that was saved, run only that single testfile
grunt.event.on('watch', function(action, filepath) {
var suite = 'unit'
if(filepath.indexOf("functional") > -1)
{
suite = 'functional';
}
else if(filepath.indexOf("acceptance") > -1)
{
suite = 'acceptance';
}
grunt.config('codecept.file', filepath.replace(/^.*[\\\/]/, ''));
grunt.config('codecept.suite', suite);
});
// Load
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-browser-sync');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-phpunit');
grunt.loadNpmTasks('grunt-exec');
// ----------------------------------------------------
// TASKS
// ----------------------------------------------------
grunt.registerTask('default', [
'clean',
'less',
'uglify',
'phpunit'
]);
grunt.registerTask('build', [
'exec:assetpublish',
'clean',
'less',
'autoprefixer',
'uglify'
]);
grunt.registerTask('compileLess', [
'exec:assetpublish',
'less'
]);
grunt.registerTask('compileJs', [
'exec:assetpublish',
'uglify'
]);
grunt.registerTask('sync', ['browserSync', 'watch']);
// Testing
grunt.registerTask('test', [
'exec:testbuild',
'exec:startphantom',
'exec:codecept',
'exec:stopphantom'
]);
// Test File
grunt.registerTask('testsingle', [
'exec:testbuild',
'exec:startphantom',
'exec:codeceptsingle',
'exec:stopphantom'
]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment