Skip to content

Instantly share code, notes, and snippets.

@egorvinogradov
Created August 15, 2015 01:52
Show Gist options
  • Save egorvinogradov/e71fa42ff06f06e3ef9a to your computer and use it in GitHub Desktop.
Save egorvinogradov/e71fa42ff06f06e3ef9a to your computer and use it in GitHub Desktop.
REQUIRED_KEYS = [
"API_SERVER_URL"
]
ENVIRONMENT = {}
for key in REQUIRED_KEYS
unless key of process.env
console.error "MISSING REQUIRED KEY: " + key
process.exit 1
else
ENVIRONMENT[key] = process.env[key]
module.exports = (grunt) ->
grunt.config.init
getLinkTag: (path) -> """<link href="#{path}?#{Math.random()}" rel="stylesheet">"""
getScriptTag: (path) -> """<script src="#{path}?#{Math.random()}"></script>"""
overdrive:
PATHS:
SERVE: ".tmp" # Folder for temporary compiled Sass and Coffee files for local serving on localhost:9003
BUILD: ".dist" # Folder for concatenated and minified static files for divshot build
# Watch for changes in files
watch:
files: [
# "app/coffee/*.coffee" # TODO: uncomment if decide to use CoffeeScript
# "app/sass/*.sass" # TODO: uncomment if decide to use CoffeeScript
"app/js/**/*.js" # TODO: remove if decide to use CoffeeScript
"app/css/*.css" # TODO: remove if decide to use CoffeeScript
"app/views/*.html"
"app/index.html"
]
# tasks: "onwatch" # TODO: uncomment if decide to use CoffeeScript
tasks: "copy:coffee_sass_temporary_replacement_serve" # TODO: remove if decide to use CoffeeScript
options:
livereload: "<%= connect.options.livereload %>"
# Compile Coffee files into JS
coffee:
options:
bare: true
serve:
expand: true
cwd: "app/coffee"
src: ["**/*.coffee"]
dest: "<%= overdrive.PATHS.SERVE %>/js/"
ext: ".js"
build:
expand: true
cwd: "app/coffee"
src: ["**/*.coffee"]
dest: "<%= overdrive.PATHS.BUILD %>/js/"
ext: ".js"
# Compile Sass files into CSS
sass:
options:
sourcemap: "auto"
serve:
expand: true
cwd: "app/sass"
src: ["*.sass"]
dest: "<%= overdrive.PATHS.SERVE %>/css/"
ext: ".css"
build:
expand: true
cwd: "app/sass"
src: ["*.sass"]
dest: "<%= overdrive.PATHS.BUILD %>/css/"
ext: ".css"
# TODO: remove if using uglify
concat:
options:
separator: "\n\n\n;//-----\n\n\n",
# Join and minify JS files
uglify:
options:
banner: "/*! <%= grunt.template.today('mm-dd-yyyy h:MM:ss TT') %> */\n"
sourceMap: false
#beautify: true
# Join and minify CSS files
cssmin:
options:
roundingPrecision: 3
# Copy necessary files to .dist folder
copy:
index:
src: "app/index.html"
dest: "<%= overdrive.PATHS.BUILD %>/index.html"
views:
expand: true
flatten: true
src: "app/views/*"
dest: "<%= overdrive.PATHS.BUILD %>/views/"
images:
expand: true
flatten: true
src: "app/img/*"
dest: "<%= overdrive.PATHS.BUILD %>/img/"
fonts:
files: [
{
expand: true
flatten: true
src: "app/bower_components/**/*.{ttf,woff,woff2,eot}"
dest: "<%= overdrive.PATHS.BUILD %>/fonts/"
}
{
expand: true
flatten: true
src: "app/bower_components/angular-ui-grid/**/*.{ttf,woff,woff2,eot}"
dest: "<%= overdrive.PATHS.BUILD %>/css/"
}
]
# Fast and dirty replacement for coffee/sass in grunt
# TODO: remove the code below if decide to use CoffeeScript
coffee_sass_temporary_replacement_serve:
files: [{
expand: true
flatten: true
src: "app/app.js"
dest: "<%= overdrive.PATHS.SERVE %>/js/"
}, {
expand: true
cwd: "app/js/leads"
src: "**/*"
dest: "<%= overdrive.PATHS.SERVE %>/js/"
}, {
expand: true
cwd: "app/css"
src: ["overdrive.css", "outreach-base.css", "new-overdrive.css", "leadgenius.css", "lg-ui-grid.css"]
dest: "<%= overdrive.PATHS.SERVE %>/css/"
}
]
# Fast and dirty replacement for coffee/sass in grunt
# TODO: remove the code below if decide to use CoffeeScript
coffee_sass_temporary_replacement_build:
files: [{
expand: true
flatten: true
src: "app/app.js"
dest: "<%= overdrive.PATHS.BUILD %>/js/"
}, {
expand: true
cwd: "app/js/leads"
src: "**/*"
dest: "<%= overdrive.PATHS.BUILD %>/js/"
}, {
expand: true
cwd: "app/css"
src: ["overdrive.css", "outreach-base.css", "new-overdrive.css", "leadgenius.css", "lg-ui-grid.css"]
dest: "<%= overdrive.PATHS.BUILD %>/css/"
}
]
# Minify HTML
htmlmin:
dist:
options:
removeComments: true
collapseWhitespace: true
src: "<%= overdrive.PATHS.BUILD %>/index.html"
dest: "<%= overdrive.PATHS.BUILD %>/index.html"
# Erases old build directory (.dist) when new build starts
clean:
build: "<%= overdrive.PATHS.BUILD %>"
# Prepare the configuration to replace links to many JS/CSS files
# with links to a few joined files in HTML-template
useminPrepare:
html: "app/index.html"
options:
dest: "<%= overdrive.PATHS.BUILD %>"
# Replace links to many JS/CSS files with links to a few joined files in HTML-template
usemin:
html: "<%= overdrive.PATHS.BUILD %>/index.html"
options:
blockReplacements:
css: (block) ->
grunt.config("overdrive.static.css", block.src)
grunt.config("getLinkTag")(block.dest)
js: (block) ->
grunt.config("overdrive.static.js", block.src)
grunt.config("getScriptTag")(block.dest)
sass: (block) ->
grunt.config("overdrive.static.sass", block.src)
grunt.config("getLinkTag")(block.dest)
coffee: (block) ->
grunt.config("overdrive.static.coffee", block.src)
grunt.config("getScriptTag")(block.dest)
# Run server on localhost:3000
connect:
options:
port: 9003
hostname: "localhost"
livereload: if grunt.option("noreload") then false else 35732
livereload:
options:
open: true
middleware: (connect) ->
servePath = grunt.config "overdrive.PATHS.SERVE"
[
connect().use("/js", connect.static("./#{servePath}/js"))
connect().use("/css", connect.static("./#{servePath}/css"))
connect().use("/img", connect.static("./#{servePath}/img"))
connect.static("./app")
]
# Join and minify CSS files (into libs.css and main.css) and JS files (into libs.js and main.js)
grunt.registerTask "collect-static", ->
staticFiles = {}
for type, files of grunt.config "overdrive.static"
staticFiles[type] = ("app/" + path for path in files)
grunt.config "cssmin.main.src", ".dist/css/**/*.css"
grunt.config "cssmin.main.dest", ".dist/css/main.css"
grunt.config "cssmin.libs.src", staticFiles.css
grunt.config "cssmin.libs.dest", ".dist/css/libs.css"
grunt.config "uglify.main.src", ".dist/js/**/*.js"
grunt.config "uglify.main.dest", ".dist/js/main.js"
grunt.config "uglify.libs.src", staticFiles.js
grunt.config "uglify.libs.dest", ".dist/js/libs.js"
# TODO: replace with uglify (above)
grunt.config "concat.main.src", ".dist/js/**/*.js"
grunt.config "concat.main.dest", ".dist/js/main.js"
grunt.config "concat.libs.src", staticFiles.js
grunt.config "concat.libs.dest", ".dist/js/libs.js"
# Compile all JS/CSS libs into .dist/js/libs.js or .dist/css/libs.css
# Then compile all project Coffee/Sass files into .dist/js/main.js or .dist/css/main.css
#grunt.task.run "cssmin:main", "cssmin:libs", "uglify:main", "uglify:libs"
grunt.task.run "cssmin:main", "cssmin:libs", "uglify:main", "concat:libs"
#grunt.task.run "cssmin:main", "cssmin:libs", "concat:main", "concat:libs" # TODO: replace with uglify (above)
# Write environment variables to __/env.js
grunt.registerTask "write-env", ->
content = "window.__env = " + JSON.stringify ENVIRONMENT
grunt.file.write(grunt.config("overdrive.PATHS.SERVE") + "/__/env.js", content)
# Watch for changes in templates and static files and recompiles Sass and Coffee
# grunt.registerTask "onwatch", [ # TODO: uncomment if decide to use CoffeeScript
# "coffee:serve"
# "sass:serve"
# ]
# Run server on localhost:3000, watch for changes and reload the page
# To run it with livereload: grunt serve
# To run it without livereload: grunt serve --noreload
grunt.registerTask "serve", [
# "onwatch" # TODO: uncomment if decide to use CoffeeScript
"copy:coffee_sass_temporary_replacement_serve" # TODO: remove if decide to use CoffeeScript
"connect:livereload"
"write-env"
"watch"
]
# Compile Sass and Coffee, join CSS and JS files and replace static paths in index.html
# To run it: grunt build
grunt.registerTask "build", [
"clean"
"copy:index"
"copy:views"
"copy:images"
"copy:fonts"
"useminPrepare"
"copy:coffee_sass_temporary_replacement_build" # TODO: remove if decide to use CoffeeScript
# "coffee:build" # TODO: uncomment if decide to use CoffeeScript
# "sass:build" # TODO: uncomment if decide to use CoffeeScript
"usemin"
"collect-static"
"htmlmin"
]
grunt.loadNpmTasks "grunt-contrib-watch"
grunt.loadNpmTasks "grunt-contrib-connect"
grunt.loadNpmTasks "grunt-contrib-copy"
# grunt.loadNpmTasks "grunt-contrib-sass" # TODO: uncomment if decide to use CoffeeScript
# grunt.loadNpmTasks "grunt-contrib-coffee" # TODO: uncomment if decide to use CoffeeScript
grunt.loadNpmTasks "grunt-contrib-concat"
grunt.loadNpmTasks "grunt-contrib-clean"
grunt.loadNpmTasks "grunt-contrib-uglify"
grunt.loadNpmTasks "grunt-contrib-cssmin"
grunt.loadNpmTasks "grunt-contrib-htmlmin"
grunt.loadNpmTasks "grunt-usemin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment