Skip to content

Instantly share code, notes, and snippets.

@rclark
Created December 14, 2012 02:40
Show Gist options
  • Save rclark/4282102 to your computer and use it in GitHub Desktop.
Save rclark/4282102 to your computer and use it in GitHub Desktop.
Convert Jade files to JavaScript functions
fs = require 'fs'
pathutil = require 'path'
jade = require 'jade'
parseFiles = (dirname) ->
filenames = fs.readdirSync dirname
for file in filenames
continue if file.slice(0,1) is '.'
path = pathutil.join dirname, file
stats = fs.statSync pathutil.join dirname, path
if stats.isDirectory()
parseFiles(file)
else if file.slice(file.length - 5) is '.jade'
fileContents = fs.readFileSync path, 'utf8'
compiled = jade.compile fileContents,
client: true
compileDebug: false
filename: file
writeToOutput compiled, file.replace '.jade', ''
writeToOutput = (fn, fnName) ->
finalFnName = "Templates.#{fnName}"
fnString = fn.toString().replace('function anonymous(', "function #{finalFnName}(")
outFileStream.write(fnString)
jadeFilePath = process.argv[2] || '.'
outFilePath = process.argv[3] || './templates.js'
outFileStream = fs.createWriteStream outFilePath,
flags: 'w'
outFileStream.write("var Templates = {};\n")
parseFiles jadeFilePath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment