mpneuried
10/23/2015 - 6:01 AM

Fontello Grunt edit helper

Fontello Grunt edit helper

###

USAGE:

1. Make sure you have a valid fontello config file
2. call "grunt edit-font" -> your browser will open with a session based on your config file
3. Edit your font within fontello and click "Save session"
4. call "grunt update-fonts". This will call the task "update-fonts-config" to download the session zip and replace the config. Then it's call the task `fontello:standard` based on your config.json.

###


fs = require( "fs" )
path = require( "path" )
request = require( "request" )

_fontello =
	# The url to fontello
	url = "http://fontello.com/"
	# The path to your local fontello config file
	configPath: "_src_static/fontello/"
	# the name of your fontello config file
	configFile: "config.json"
	# the file-path to the fontello session storage. This is used to store the sessionid and creation date
	sessionFile: "./.fontello-session"
	# the session lifetime in ms until us have to call "grunt edit-font" again to create a new session. Fontello will kill your session after 24h
	sessionLifetime: ( 1000 * 60 * 60 *23 )
	# A temp folder to unzip the fontello download and pick the new config out of it.
	tmpFolder: path.resolve( "./_tmp/" )

module.exports = (grunt) ->
	grunt.initConfig
		# Standard grunt fontello task
		fontello:
			standard:
				options:
					config  : '#{_fontello.configPath}/#{_fontello.configFile}',
					fonts   : 'static/font',
					styles  : '_src_static/fontello/css',
					scss	: false,
					force   : true

	# call fontello api and create a session with the local config.json
	grunt.registerTask "edit-fonts", ->
		done = this.async()
		_opt =
			uri: _fontello.url
			method: "POST"
			formData:
				config: fs.createReadStream(__dirname + "/" + _fontello.configPath + _fontello.configFile )
				
		request _opt, ( err, res, session )->
			if err
				grunt.log.error(err)
				done()
				return
				
			_fontello.session =
				session: session
				date: Date.now()
			# persist the session to the disk
			fs.writeFile _fontello.sessionFile, JSON.stringify( _fontello.session ), ( err )->
				if err
					grunt.log.error(err)
					done()
					return
				# open the generated session with your browser
				exec( "open #{_fontello.url}#{session}" )
				done()
				return
			return
		return
	
	# open the generated session with your browser
	grunt.registerTask "update-fonts-config", ->
		done = this.async()
		# load and check for a valid session
		fs.readFile _fontello.sessionFile, ( err, data )->
			if err
				grunt.log.error(err)
				done()
				return
			_fontello.session = JSON.parse( data )
			if parseInt( _fontello.session.date, 10 ) + _fontello.sessionLifetime < Date.now()
				
				grunt.log.error("fontello session timed out. Please call `grunt edit-fonts`")
				done()
				return
			
			# try to create the tmp folder
			try
				grunt.log.writeln( _fontello.tmpFolder )
				fs.mkdirSync(_fontello.tmpFolder)
			
			# download the zip pick the config file and remove the downloaded zip
			tempZipFilePath = _fontello.tmpFolder + "/fontello" + Date.now()
			exec( "curl #{_fontello.url + _fontello.session.session}/get > #{tempZipFilePath}; unzip -oj #{tempZipFilePath} \"*/#{_fontello.configFile}\" -d ./#{_fontello.configPath} -o; rm -f #{tempZipFilePath};" )
			done()
			return
		return
	

	grunt.loadNpmTasks "grunt-fontello"
	
	grunt.registerTask "update-fonts", [ "update-fonts-config", "fontello:standard" ]
	
	return