command.coffee | |
---|---|
This module holds everything related to interacting with brewer.js from the shell, including the bin/brewer utility, and different shell printing functions. | |
Essential modules are imported | fs = require 'fs'
path = require 'path'
Command = require('commander').Command
clr = require('ansi-color').set
_ = require 'underscore' |
This function returns the version number from the package.json file. | exports.version = getVersion = ->
fs = require 'fs'
path = require 'path'
pkg = JSON.parse fs.readFileSync path.resolve __dirname, '../package.json'
pkg.version |
This function returns a | getLocalProject = ->
new (require './index').Project |
CLI utility functionsThese functions are used to standardize how certain types of of messages are displayed in the shell, are all exported and used throughout brewer.js. | _.extend exports, cli = {
finished: (action, target) ->
console.log clr(action, 'blue'), target
debug: (msgs...) ->
console.log clr('DEBUG', 'red'), msgs...
warning: (msgs...) ->
console.log clr('Warning', 'yellow'), msgs...
info: (msgs...) ->
console.log clr('Info', 'green'), msgs...
error: (msgs...) ->
console.log clr('Error', 'red'), msgs...
} |
bin/brewerMaking heavy use of
commander.js.
A | exports.run = (argv) ->
(program = new Command)
.version(getVersion())
.option('-t, --template <template>',
'Specify the project template to use (default to \'lesscoffee\') when initializing.')
|
The
|
program
.command('init')
.description(" Initialize #{clr('Brewer', 'green')} in the current directory")
.action ->
templ = program.template ? 'lesscoffee'
template = path.join(__dirname, '..', 'templates', "#{templ}.coffee")
content = fs.readFileSync template, 'utf-8'
cli.info 'Writing Brewfile'
fs.writeFileSync 'Brewfile', content, 'utf-8'
cli.info 'Making initial folder structure'
getLocalProject().prepare()
|
The
|
program
.command('watch')
.description("""
Watch for modifications in source and configuration files,
automatically re-making when they occur.
""")
.action ->
getLocalProject().watch()
cli.info 'Watching project', process.cwd()
|
The
|
program
.command('clean [packages]*')
.description(" Delete bundles and compiled files from the given packages (or all)")
.action (pkgs) ->
pkgs ?= 'all'
project = getLocalProject()
if pkgs is 'all'
packages = project
else
names = pkgs.split(',')
packages = (pkg for pkg in project when pkg.name in names)
for pkg in packages
do (pkg) -> pkg.clean()
|
The
|
program
.command('install')
.description(" Install missing modules required to manage the current project")
.action ->
project = getLocalProject()
if project.missingModules().length > 0
project.installMissingModules()
else
cli.info 'No modules are missing.'
|
The main
|
program
.command('*')
.description(" Aggregate bundles from the given packages (or all)")
.action (pkgs...) ->
pkgs ?= 'all'
project = getLocalProject()
if pkgs[0] is 'all'
packages = project
else
packages = (pkg for pkg in project when pkg.name in pkgs)
for pkg in packages
do (pkg) ->
pkg.actualize ->
cli.info "Finished making #{clr(pkg.name, 'underline')} package"
|
This tells the | program.parse argv
|