Skip to content

Instantly share code, notes, and snippets.

@dwayne
Created June 2, 2012 12:12
Show Gist options
  • Save dwayne/2858081 to your computer and use it in GitHub Desktop.
Save dwayne/2858081 to your computer and use it in GitHub Desktop.
A way to structure CoffeeScript applications
# References
# * https://github.com/sstephenson/stitch
# * http://arcturo.github.com/library/coffeescript/06_applications.html
# A possible directory structure
#
# app [application code]
# |-- model
# |-- view
# |-- controller
# |-- lib
# lib [third party libraries]
# public
# |-- js
# |-- css
# |-- index.html
# index.coffee [this file]
# package.json
# README.md
stitch = require 'stitch'
fs = require 'fs'
connect = require 'connect'
APP_PATH = __dirname + '/app'
LIB_PATH = __dirname + '/lib'
PUBLIC_PATH = __dirname + '/public'
DIST_NAME = 'app.js' # change to whatever you like
dist = stitch.createPackage(
# paths to automatically bundle up
paths: [ APP_PATH ]
# base libraries
dependencies: [
# LIB_PATH + '/jquery.js' for example
]
)
generate_dist = ->
dist.compile (err, source) ->
throw err if err?
write_dist source
write_dist = (data) ->
js_path = PUBLIC_PATH + '/js'
write_file = ->
fs.writeFile js_path + "/#{DIST_NAME}", data, (err) ->
throw err if err?
console.log "Compiled #{DIST_NAME}"
run()
mkdir_and_write_file = ->
fs.mkdir js_path, 0o775, (err) ->
throw err if err?
write_file()
try
stats = fs.lstatSync js_path
if stats.isDirectory()
write_file()
else
mkdir_and_write_file()
catch e
mkdir_and_write_file()
run = ->
app = connect()
app.use connect.static(PUBLIC_PATH)
app.use connect.errorHandler()
port = 3000
app.listen port
console.log "Browse to http://localhost:#{port} to use [Ctrl-C to exit]"
generate_dist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment