Skip to content

Instantly share code, notes, and snippets.

@oroce
Created January 30, 2012 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oroce/1707225 to your computer and use it in GitHub Desktop.
Save oroce/1707225 to your computer and use it in GitHub Desktop.
.jade template into AMD Compatible client side modules
fs = require "fs"
jade = require "jade"
path = require "path"
walk = ( start, callback ) ->
fs.lstat start, (err, stat) ->
return callback err if err?
if stat.isDirectory()
fs.readdir(
start,
(err, files) ->
coll = files.reduce(
(acc, i) ->
abspath = path.join start, i
if fs.statSync(abspath).isDirectory()
walk(abspath, callback)
acc.dirs.push(abspath)
else
acc.names.push(abspath)
acc
,
"names": [],
"dirs": []
)
callback null, start, coll.dirs, coll.names
)
else
return callback(new Error("path: " + start + " is not a directory"))
wrap = (content,jadeModule) ->
return "define( ['jade', 'underscore'], function( jade, _ ){ return #{content} });"
reJade = /\.jade$/
output = __dirname + "/public/templates/"
dir = __dirname + "/views"
walk dir, ( err, currDir, dirs, files ) ->
if err
return console.log( err)
files.forEach ( file ) ->
if reJade.test file
filename = path.relative( dir, file )
newFile = output + filename.replace( ".jade", ".js" )
console.log( "saving #{filename}(#{file}) into #{newFile}")
writeFile( newFile,file, filename )
writeFile = ( newFile,filename ) ->
source = wrap( jade.compile( fs.readFileSync(filename,'utf8'), client:true,compileDebug:false,filename:filename ).toString() );
fs.writeFileSync( newFile, source );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment