Skip to content

Instantly share code, notes, and snippets.

@jiewmeng
Created August 15, 2012 03:15
Show Gist options
  • Save jiewmeng/3355383 to your computer and use it in GitHub Desktop.
Save jiewmeng/3355383 to your computer and use it in GitHub Desktop.
Script to create express app with CoffeeScript etc. and modular folder structure
node_modules/
public/css/app
public/js/app
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
pids
logs
results
npm-debug.log

This (bash) script aims to automate the process of creating an express app:

  • Modular folder structure
  • Installing dependencies (Mongoose, Stylus, Nib, ...)
  • Getting dependencies (jQuery, Backbone, Require ...)
  • Setup testing folders (Mocha, Zombie)
  • Cakefile (To start development server and testing tasks)

Just download expressapp, chmod to allow execution. Then expressapp {projname} then have a cup of tea

{spawn, exec} = require "child_process"
# Helpers
# -------
# **`procExec(procName)`**
# returns the path to executable in `node_`
procExec = (procName) ->
"./node_modules/.bin/" + procName
# **`processOutput(proc)`**
# handle the output of a `spawn`-ed process
# see **[link](http://stackoverflow.com/a/7376108/292291)**
processOutput = (proc) ->
proc.stdout.on "data", (data) ->
console.log data.toString().trim()
# **`handleOutput(err, output)`**
# handle the output from a `exec`-ed process
handleOutput = (err, output) ->
if err then throw err
console.log output
# **`runMochaOn(folder)`**
# execute `mocha` on given `folder` recursively
runMochaOn = (folder) ->
exec procExec("mocha") + " -c --reporter spec --recursive --compilers coffee:coffee-script --require should --require tests/backend/initDatabase.js " + folder, handleOutput
# Start/Wacth/Compile Development Server
# --------------------------------------
# **`cake startdev`**
# Starts the server with `nodemon`
# Watch and compile `.coffee` and `.styl` files in `/client`
task "startdev", "Starts server with nodemon and watch files for changes", ->
# start nodemon server
nodemon = spawn procExec("nodemon"), ["server.coffee"]
processOutput nodemon
# watch and compile CoffeeScript
coffee = spawn procExec("coffee"), ["-o", "public/js/app", "-cw", "client/coffee"]
processOutput coffee
# watch and compile Stylus
stylus = spawn procExec("stylus"), ["client/stylus", "-l", "-w", "-o", "public/css/app"]
processOutput stylus
# Running Tests
# -------------
# Run tests with **`cake test*`**
task "test-ui", "Runs UI tests", ->
runMochaOn "tests/ui"
task "test-backend", "Runs backend unit tests", ->
runMochaOn "tests/backend"
task "test-frontend", "Runs frontend unit tests", ->
runMochaOn "tests/frontend"
task "test", "Runs all tests", ->
runMochaOn "tests"
# configuration for app
# FIXME: replace app as applicable
module.exports =
database:
development: "app-dev"
testing: "app-testing"
production: "app"
#!/bin/bash
# Create express project
echo "Creating express project: $1"
express $1
cd "$1"
# install dependencies
echo "Installing dependencies"
npm install --save coffee-script jade stylus nib mongoose express
# ... for development
echo "Installing development dependencies"
npm install --save mocha should groc zombie nodemon
# convert app to CoffeeScript
echo "Converting app to CoffeeScript"
js2coffee app.js > server.coffee
rm app.js
js2coffee routes/index.js > routes/index.coffee
rm routes/index.js
# build app structure
echo "Building app structure"
mkdir client client/coffee client/stylus
mkdir public/js public/js/app public/css public/css/app public/img
mkdir tests tests/backend tests/frontend tests/ui
# convert existing default css to stylus
echo "Converting CSS to Stylus"
stylus --css < public/stylesheets/style.css > client/stylus/styles.styl
# remove old public folders
echo "Cleanup"
rm -r public/images public/javascripts public/stylesheets
# get files
echo "Getting files"
wget http://code.jquery.com/jquery-1.8.0.js -O public/js/jquery.js
wget http://underscorejs.org/underscore.js -O public/js/underscore.js
wget https://raw.github.com/douglascrockford/JSON-js/master/json2.js -O public/js/json2.js
wget http://backbonejs.org/backbone.js -O public/js/backbone.js
wget https://raw.github.com/derickbailey/backbone.marionette/master/lib/backbone.marionette.js -O public/js/backbone.marionette.js
wget http://requirejs.org/docs/release/2.0.5/comments/require.js -O public/js/require.js
wget https://raw.github.com/gist/3355383/56cc028c0b769b622e49dae8e806a5fc73c388dc/config.coffee
wget https://raw.github.com/gist/3355383/9ff9ad2f946c2ecc07b5baae47384532572db455/initDatabase.js -O tests/backend/initDatabase.js
wget https://raw.github.com/gist/3355383/c7a37e525e7136a36089af3d712aa7ebff028660/layout.jade -O views/layout.jade
wget https://raw.github.com/gist/3355383/7e542ada6febece413b5e29d1bd465bf9c90e94f/init.coffee -O client/coffee/init.coffee
wget https://raw.github.com/gist/3355383/a73741636f4611fe04613298e9bac0c6096af5b6/Cakefile
wget https://raw.github.com/gist/3355383/153582d7eaecafdb6183c0abe6f46f252108502e/.gitignore
echo "============================="
echo "Done!!! Todos: "
echo "============================="
echo " - Edit config.coffee"
echo " - Edit package.json versions, name, desc etc"
echo " - Start the application using cake startdev"
# /client/coffee/init.coffee
# Initializes RequireJS
require.config
baseUrl: "js"
shim:
jquery:
exports: "jQuery"
underscore:
exports: "_"
backbone:
deps: ["underscore", "json2", "jquery"]
exports: "Backbone"
"backbone.marionette":
deps: ["backbone"]
exports: ["Backbone.Marionette"]
require [
"jquery"
], ($) ->
jQuery ->
console.log "OK!"
// initDatabase.js
// Helper to connect to database for testing
require("coffee-script")
config = require("../../config")
mongoose = require("mongoose")
mongoose.connect("localhost", config.database.testing)
doctype 5
html
head
title= title
script(src="js/require.js", data-main="js/app/init.js")
link(rel="stylesheet", href="css/app/styles.css")
body
block content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment