Skip to content

Instantly share code, notes, and snippets.

@shyiko
Created July 22, 2013 20:29
Show Gist options
  • Select an option

  • Save shyiko/6057362 to your computer and use it in GitHub Desktop.

Select an option

Save shyiko/6057362 to your computer and use it in GitHub Desktop.
Template of shell script in CoffeScript
#!/usr/bin/env coffee
###
prerequisites;
$ sudo apt-get install nodejs # whatever
$ sudo npm install -g coffee-script
###
bootstrap = do ->
exec = require('child_process').exec
npmInstall = (modules, callback) ->
exec "npm install #{modules.join(' ')}", (err, stdout, stderr) ->
if err and stderr then console.error stderr
callback()
resolve = (dependencies) ->
resolved = {}; unresolved = {}
for own key, value of dependencies
try resolved[key] = require(value)
catch e
unresolved[key] = value
return resolved: resolved, unresolved: unresolved
values = (obj) -> value for own key, value of obj
(dependencies, callback) ->
state = resolve(dependencies)
return callback(state.resolved) if Object.keys(state.unresolved).length is 0
console.log "Some of the required modules weren't found. Trying to 'npm install' them."
npmInstall values(state.unresolved), ->
state = resolve(dependencies)
if Object.keys(state.unresolved).length
throw new Error("Unable to resolve #{values(state.unresolved).join(', ')}")
console.log 'All good. Bootstrapping application.'
callback(state.resolved)
bootstrap cliparser: 'optimist', shell: 'shelljs', (context) ->
{cliparser, shell} = context
cli =
cliparser.
options('h', alias: 'hostname', demand: true).
options('p', alias: 'port', default: 8080, describe: 'magic port', demand: true).
argv
console.log "#{cli.hostname}:#{cli.port}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment