<commit_before>###
<< Adding a local module >>
1. Add a symlink to base dir in createSymlinks
2. Add a symlink to image dir in createSymlinks (optional)
3. Add css to concat:css (optional)
4. Add main.js to requirejs:options:paths
###

fs = require 'fs'
path = require 'path'

connect_middleware = (connect, options) ->
	[
		(req, res, next) ->
			contentTypesMap =
				'.html': 'text/html'
				'.css': 'text/css'
				'.js': 'application/javascript'
				'.map': 'application/javascript' # js source maps
				'.gif': 'image/gif'
				'.jpg': 'image/jpeg'
				'.jpeg': 'image/jpeg'
				'.png': 'image/png'
				'.ico': 'image/x-icon'
				'.ttf': 'application/octet-stream'
			
			sendFile = (reqUrl) ->
				filePath = path.join options.base, reqUrl
				console.log "Fetching #{filePath}"
				
				res.writeHead 200,
					'Content-Type': contentTypesMap[extName] || 'text/html'
					'Content-Length': fs.statSync(filePath).size

				readStream = fs.createReadStream filePath
				readStream.pipe res

			# Create a favicon on the fly if it does not exist
			if req.url is '/favicon.ico' and not fs.existsSync(options.base + req.url)
				fs.openSync options.base + req.url, 'w'
			
			extName = path.extname req.url

			if contentTypesMap[extName]?
				sendFile req.url
			else
				sendFile 'index.html'
	]

module.exports = (grunt) ->

	##############
	### CONFIG ###
	##############

	grunt.initConfig

		### SHELL ###

		shell:
			options:
				stdout: true
				stderr: true
			mocha: 
				command: 'mocha-phantomjs -R dot http://localhost:9002/.test/index.html'
			emptydist:
				command:
					'rm -rf dist/*'
			emptycompiled:
				command:
					'rm -rf compiled/*'
			# rsync:
			# 	command:
			# 		'rsync --copy-links --compress --archive --verbose --checksum --exclude=.svn --chmod=a+r dist/ <EMAIL>:elab4testFE/'
			bowerinstall:
				command: 'bower install'
			groc:
				command: 'groc "src/coffee/**/*.coffee" --out=compiled/docs'

		createSymlinks:
			compiled: [
				src: 'images'
				dest: 'compiled/images'
			# ,
			# 	src: '/home/gijs/Projects/faceted-search'
			# 	dest: 'compiled/lib/faceted-search'
			# ,
			# 	src: '/home/gijs/Projects/faceted-search/images'
			# 	dest: 'images/faceted-search'
			]
			dist: [{
				src: 'images'
				dest: 'dist/images'
			}]

		### SERVER ###	

		connect:
			keepalive:
				options:
					port: 9000
					base: 'compiled'
					middleware: connect_middleware
					keepalive: true
			compiled:
				options:
					port: 9000
					base: 'compiled'
					middleware: connect_middleware
			dist:
				options:
					port: 9001
					base: 'dist'
					middleware: connect_middleware			
			test:
				options:
					port: 9002
					base: ''
					middleware: connect_middleware

		### STATIC FILES ###
		rsync:
			options:
				args: ["--verbose", "-i"]
				exclude: [".git*"]
				recursive: true
			compiled:
				options:
					src: "./src/static/"
					dest: "./compiled/static"
			dist:
				options:
					src: "./src/static/"
					dest: "./dist/static"

		### HTML ###
		
		jade:
			init:
				files: [
					expand: true
					cwd: 'src/jade'
					src: '**/*.jade'
					dest: 'compiled/html'
					rename: (dest, src) -> 
						dest + '/' + src.replace(/.jade/, '.html') # Use rename to preserve multiple dots in filenames (nav.user.coffee => nav.user.js)
				,
					'compiled/index.html': 'src/index.jade'
				]
			compile:
				options:
					pretty: true

		replace:
			html:
				src: 'compiled/index.html'
				dest: 'dist/index.html'
				replacements: [
					from: '<script data-main="/js/main" src="/lib/requirejs/require.js"></script>'
					to: '<script src="/js/main.js"></script>'
				]

		### CSS ###

		stylus:
			compile:
				options:
					paths: ['src/stylus/import']
					import: ['variables', 'functions', 'helpers']
				files:
					'compiled/css/project.css': [
						'src/stylus/**/*.styl'
						'!src/stylus/import/*.styl'
					]

		concat:
			css:
				src: [
					'compiled/lib/normalize-css/normalize.css'
					'compiled/css/project.css'
					# 'compiled/lib/faceted-search/compiled/css/main.css'
					# 'compiled/lib/supertinyeditor/main.css'
				]
				dest:
					'compiled/css/main.css'

		cssmin:
			dist:
				files:<commit_msg>Added JSON as static resource, removed helpers<commit_after>###
<< Adding a local module >>
1. Add a symlink to base dir in createSymlinks
2. Add a symlink to image dir in createSymlinks (optional)
3. Add css to concat:css (optional)
4. Add main.js to requirejs:options:paths
###

fs = require 'fs'
path = require 'path'

connect_middleware = (connect, options) ->
	[
		(req, res, next) ->
			contentTypesMap =
				'.html': 'text/html'
				'.css': 'text/css'
				'.js': 'application/javascript'
				'.map': 'application/javascript' # js source maps
				'.json': 'application/json'
				'.gif': 'image/gif'
				'.jpg': 'image/jpeg'
				'.jpeg': 'image/jpeg'
				'.png': 'image/png'
				'.ico': 'image/x-icon'
				'.ttf': 'application/octet-stream'
			
			sendFile = (reqUrl) ->
				filePath = path.join options.base, reqUrl
				console.log "Fetching #{filePath}"
				
				res.writeHead 200,
					'Content-Type': contentTypesMap[extName] || 'text/html'
					'Content-Length': fs.statSync(filePath).size

				readStream = fs.createReadStream filePath
				readStream.pipe res

			# Create a favicon on the fly if it does not exist
			if req.url is '/favicon.ico' and not fs.existsSync(options.base + req.url)
				fs.openSync options.base + req.url, 'w'
			
			extName = path.extname req.url

			if contentTypesMap[extName]?
				sendFile req.url
			else
				sendFile 'index.html'
	]

module.exports = (grunt) ->

	##############
	### CONFIG ###
	##############

	grunt.initConfig

		### SHELL ###

		shell:
			options:
				stdout: true
				stderr: true
			mocha: 
				command: 'mocha-phantomjs -R dot http://localhost:9002/.test/index.html'
			emptydist:
				command:
					'rm -rf dist/*'
			emptycompiled:
				command:
					'rm -rf compiled/*'
			# rsync:
			# 	command:
			# 		'rsync --copy-links --compress --archive --verbose --checksum --exclude=.svn --chmod=a+r dist/ <EMAIL>.knaw.nl:elab4testFE/'
			bowerinstall:
				command: 'bower install'
			groc:
				command: 'groc "src/coffee/**/*.coffee" --out=compiled/docs'

		createSymlinks:
			compiled: [
				src: 'images'
				dest: 'compiled/images'
			# ,
			# 	src: '/home/gijs/Projects/faceted-search'
			# 	dest: 'compiled/lib/faceted-search'
			# ,
			# 	src: '/home/gijs/Projects/faceted-search/images'
			# 	dest: 'images/faceted-search'
			]
			dist: [{
				src: 'images'
				dest: 'dist/images'
			}]

		### SERVER ###	

		connect:
			keepalive:
				options:
					port: 9000
					base: 'compiled'
					middleware: connect_middleware
					keepalive: true
			compiled:
				options:
					port: 9000
					base: 'compiled'
					middleware: connect_middleware
			dist:
				options:
					port: 9001
					base: 'dist'
					middleware: connect_middleware			
			test:
				options:
					port: 9002
					base: ''
					middleware: connect_middleware

		### STATIC FILES ###
		rsync:
			options:
				args: ["--verbose", "-i"]
				exclude: [".git*"]
				recursive: true
			compiled:
				options:
					src: "./src/static/"
					dest: "./compiled/static"
			dist:
				options:
					src: "./src/static/"
					dest: "./dist/static"

		### HTML ###
		
		jade:
			init:
				files: [
					expand: true
					cwd: 'src/jade'
					src: '**/*.jade'
					dest: 'compiled/html'
					rename: (dest, src) -> 
						dest + '/' + src.replace(/.jade/, '.html') # Use rename to preserve multiple dots in filenames (nav.user.coffee => nav.user.js)
				,
					'compiled/index.html': 'src/index.jade'
				]
			compile:
				options:
					pretty: true

		replace:
			html:
				src: 'compiled/index.html'
				dest: 'dist/index.html'
				replacements: [
					from: '<script data-main="/js/main" src="/lib/requirejs/require.js"></script>'
					to: '<script src="/js/main.js"></script>'
				]

		### CSS ###

		stylus:
			compile:
				options:
					paths: ['src/stylus/import']
					import: ['variables', 'functions']
				files:
					'compiled/css/project.css': [
						'src/stylus/**/*.styl'
						'!src/stylus/import/*.styl'
					]

		concat:
			css:
				src: [
					'compiled/lib/normalize-css/normalize.css'
					'compiled/css/project.css'
					# 'compiled/lib/faceted-search/compiled/css/main.css'
					# 'compiled/lib/supertinyeditor/main.css'
				]
				dest:
					'compiled/css/main.css'

		cssmin:
			dist:
				files:
{'fraction_non_alphanumeric': 0.1640922053231939, 'fraction_numerical': 0.006653992395437262, 'mean_word_length': 2.4383169934640523, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3926078', 'n_tokens_mistral': 3990, 'n_tokens_neox': 2995, 'n_words': 654}