Created
July 22, 2013 20:29
-
-
Save shyiko/6057362 to your computer and use it in GitHub Desktop.
Template of shell script in CoffeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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