Skip to content
Myr

Bash template generator with dummy app

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent fbbe136 · Browse files · 3 files+137 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+137 added−54 removed

  • other:+137 added−54 removed

3 files+137 added−54 removed

modified:bin/express+132 added-53 removed
1#!/usr/bin/env node1#!/usr/bin/env bash23var arg,4 dir = '.',5 fs = require('fs'),6 sys = require('sys'),7 args = process.argv.slice(2)3#4# Output usage information and exit.5# 69// Parse arguments7function usage() {8 echo 'usage: express [options] [dir]'9 exit 110}1111while (arg = args.shift())12 switch (arg) {13 case '-h':14 case '--help':15 sys.print('Usage: express [options] [dir]\n')16 break17 default:18 dir = arg19 }20 21// Helpers12#13# Create template in the givin [dir] or cwd.14# 15# [dir]16#1723function confirm(msg, fn) {24 sys.print(msg)25 var stdin = process.openStdin()26 stdin.setEncoding('utf8')27 stdin.addListener('data', function(chunk){28 switch (chunk.trim().toLowerCase()) {29 case 'yes':30 case 'y':31 return stdin.destroy(), fn(true)32 case 'no':33 case 'n':34 return stdin.destroy(), fn(false)35 default:36 sys.print(msg)37 }38 })18function create_template() {19 local dir=${1-.}20 mkdir -p $dir21 [[ $(ls $dir) ]] && confirm "$dir is not empty, continue?"22 mkdir -p $dir/public/{javascripts,stylesheets,images}23 mkdir -p $dir/views/partials24 app_template > $dir/app.js25 layout_template > $dir/views/layout.html.haml26 front_template > $dir/views/front.html.haml27 sass_template > $dir/views/style.css.sass28}2941function createTemplateIn(dir, mode) {42 fs.writeFile(dir + '/app.js', '// your app here')43 fs.mkdir(dir + '/views', mode)44 fs.mkdir(dir + '/public', mode, function(err){45 if (err) throw err46 fs.mkdir(dir + '/public/javascripts', mode)47 fs.mkdir(dir + '/public/stylesheets', mode)48 fs.mkdir(dir + '/public/images', mode)49 })30#31# views/style.css.sass32# 3334function sass_template() {35 cat <<-EOF36blue: #03ADF037light: #4d4d4d38lighter: #eee3940body41 :margin 80px42 :font 14px/1.5 "Helvetica Nueue", "Lucida Grande", "Arial", sans-serif4344code45 :padding 3px 10px46 :border 1px solid !lighter47 :color !light4849a50 :color !blue51 :text-decoration none52 &:hover53 :text-decoration underline54EOF55}5652// Setup template57#58# views/front.html.haml59#6054fs.readdir(dir, function(err, files){55 if (err) throw err56 if (files.length)57 confirm(dir + ' is not empty, continue? ', function(ok){58 if (ok) createTemplateIn(dir, 0666)59 })60 else61 createTemplateIn(dir, 0666)62})61function front_template() {62 cat <<-EOF63%h1 Express64%p65 Generated by the66 <code>express</code>67 executable.68%p69 Visit70 %a{ href: 'http://expressjs.com' } ExpressJS.com71 for more information. 72EOF73}7475#76# views/layout.html.haml77# 7879function layout_template() {80 cat <<-EOF81!!! strict82%html83 %head84 %title Express85 %link{ rel: 'stylesheet', href: 'style.css' }86 %body!= body87EOF88}8990#91# app.js92# 9394function app_template() {95 cat <<-EOF96// If you are using the kiwi package manager97var kiwi = require('kiwi'),98 express = kiwi.require('express')99100// Otherwise you will need to expose the path to express101// require.paths.unshift('path/to/express/lib')102// require('express')103104configure(function(){105 set('root', __dirname)106})107108get('/', function(){109 this.render('front.html.haml')110})111112get('/*.css', function(path){113 this.render(path + '.css.sass', { layout: false })114})115116run()117EOF118}119120#121# Confirm <msg> or exit.122# 123# <msg>124#125126function confirm() {127 echo -n "$1 "128 read answer129 case $answer in130 n|N|no) exit 1 ;;131 y|Y|yes) ;;132 *) confirm "yes or no?" ;;133 esac134}135136# Process arguments137138case $1 in139 -h|--help|help) usage ;;140 *) create_template $1 ;;141esac
modified:Makefile+1 added-1 removed
3 unmodified lines
45all: test6 7install: bin/express8 install bin/express /usr/local/bin/express8 ln -s bin/express /usr/local/bin/express910test:11 @$(NODE) spec/node.js all12
15 unmodified lines
modified:Readme.md+4 added