Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5ecc3272ec
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 240 lines (184 sloc) 7.481 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
path = require('path')
fs = require('fs')
optimist = require('optimist')
strata = require('strata')
tracer = require('tracer')
js = require('./js')
css = require('./css')




argv = optimist.usage([
  ' usage: weber COMMAND',
  ' init create a weber.json config file',
  ' server start a dynamic development server',
  ' build serialize application to disk',
  ' watch build & watch disk for changes'
].join("\n"))
.alias('p', 'port')
.alias('d', 'debug')
.argv


help = ->
    optimist.showHelp()
    process.exit()


class Weber
    options: {
        conf: './weber.json'
        port: process.env.PORT or argv.port or 9294
        #
        # See sample config at the bottom or run "weber init" to see sample config
        #
    }

    @exec: (command, options) ->
        (new @(options)).exec(command)

    constructor: (options = {}) ->
        @logger = tracer.colorConsole
            format: '<{{title}}> {{message}}'
        @options[key] = value for key, value of options
        @_loadConfig()

    exec: (command = argv._[0]) ->
        return help() unless @[command]
        @[command]()

    server: ->
        @app = new strata.Builder
        @app.use(strata.contentLength)

        @app.use(strata.static, @options.docroot, ['index.html', 'index.htm'])

        for urlpath, options of @options.js
            @app.get urlpath, js.createPackage(options).createServer()

        for urlpath, options of @options.css
            @app.get urlpath, css.createPackage(options).createServer()

        strata.run(@app, port: @options.port)


    build: (log = true) ->
        for urlpath, options of @options.js
            fs.writeFileSync(options.build, js.createPackage(options).compile())

        for urlpath, options of @options.css
            fs.writeFileSync(options.build, css.createPackage(options).compile())

        @logger.info "Built outputs" if log


    watch: ->
        @build(false)
        logger = @logger

        for urlpath, options of @options.js
            all_inputs = [].concat options.lib, options.module
            for f in all_inputs
                f = path.resolve(f)
                continue unless path.existsSync(f)
                ( (buildConfig, fileToWatch) =>
                    fs.watch fileToWatch, (e,f) =>
                        logger.debug "File changed: #{f}, rebuilding #{buildConfig.build}"
                        fs.writeFileSync(buildConfig.build, js.createPackage(buildConfig).compile())
                )(options, f)

        for urlpath, options of @options.css
            for f in options.input
                path.resolve(f)
                continue unless path.existsSync(f)
                ( (buildConfig, fileToWatch) ->
                    fs.watch fileToWatch, (e,f) ->
                        logger.debug "File changed: #{f}, rebuilding #{buildConfig.build}"
                        fs.writeFileSync(buildConfig.build, css.createPackage(buildConfig).compile())
                )(options, f)

            output = if options.build? then path.resolve(options.build) else path.join(@options.docroot, urlpath)
            fs.writeFileSync(output, css.createPackage(options).compile())

        @logger.info "Watching for changes..."


    init: ->
        if path.existsSync(@options.conf)
            @logger.info "#{@options.conf} already exists"
        else
            fs.writeFileSync(@options.conf, sample_conf)
            @logger.info "Wrote #{@options.conf}"


    # Private

    _loadConfig: (conf = @options.conf) ->
        return unless conf and path.existsSync(conf)
        slug = JSON.parse(fs.readFileSync(conf, 'utf-8'))

        # parse it

        if not slug.hasOwnProperty("/")
            @options.docroot = path.dirname(path.resolve(conf))
        else
            @options.docroot = path.join(path.dirname(path.resolve(conf)), slug["/"])

        @options.js = {}
        @options.css = {}
        @options.static = {}

        for urlpath, options of slug
            continue if 0 <= ["conf","port"].indexOf(urlpath)
            if "/" isnt urlpath.charAt(0)
                @logger.warn "Skipping #{urlpath}, must start with /"
                continue

            # what type of object is this?
            switch path.extname(urlpath)
                when ".js"
                    item =
                        id: urlpath
                        build: @options.docroot + urlpath
                        minify: true
                        dependency: []
                        lib: []
                        module: []

                    if Array.isArray(options)
                        item.module = options
                    else if "object" is typeof options
                        if not options.input?
                            @logger.warn "Skipping #{urlpath}, no inputs found"
                            continue
                        # minification
                        item.minify = options.minify if options.minify?
                        # if it's an array then treat as an array of modules
                        if Array.isArray(options.input)
                            options.input =
                                module: options.input
                        # check input types
                        for inputType in ["dependency", "lib","module"]
                            if options.input[inputType]
                                item[inputType] = options.input[inputType]

                        item.build = path.resolve(options.build) if options.build?

                    else
                        @logger.warn "Skipping #{urlpath}, unable to parse options"
                        continue

                    @options.js[urlpath] = item
                    @logger.info "JS: #{urlpath}"

                #css
                when ".css"
                    item =
                        id: urlpath
                        build: @options.docroot + urlpath
                        minify: true
                        input: options

                    if not Array.isArray(options) and "object" is typeof options
                        if not options.input? or not Array.isArray(options.input)
                            @logger.warn "Skipping #{urlpath}, no inputs found"
                            continue
                        item.minify = options.minify if options.minify?
                        item.build = options.build if options.build?
                        item.input = options.input

                    @options.css[urlpath] = item
                    @logger.info "CSS: #{urlpath}"

                # unrecognized path type
                else
                    if "/" isnt urlpath
                        @logger.warn "Unrecognized path type: #{urlpath}"





module.exports = Weber



sample_conf = """
{
"/" : "./public_assets",

"/css/app.css" : [
"./css/reset.css",
"./css/main.styl"
],

"/css/test.css" : {
"minify": false,
"input" : [
"./css/test"
]
},

"/js/app.js" : {
"build" : "./app.js",
"input" : {
"dependency" : [ "es5-shimify" ],
"lib" : [ "./lib/jquery.js" ],
"module": [ "./coffee" ]
}
},

"/js/test.js" : {
"minify": false,
"input": [
"./test/testbase.js",
"./test/test.coffee"
]
}
}
"""
Something went wrong with that request. Please try again.