Skip to content

Instantly share code, notes, and snippets.

@fat
Created January 12, 2012 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fat/1601997 to your computer and use it in GitHub Desktop.
Save fat/1601997 to your computer and use it in GitHub Desktop.
Bootstrap Build Server
// ========================================================================
// bootstrap-builder v0.1.0
// http://twitter.github.com/bootstrap
// ========================================================================
// Copyright 2011 Twitter, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
var express = require('express')
, uglifyJS = require('uglify-js')
, https = require('https')
, path = require('path')
, app = express.createServer()
// parse form posts
app.use(express.bodyParser());
// Setup the API
//
// args:
// - compress: boolean
// - filenames: array of filenames
// - branch: string representing branch name
// - dir: string representing the directory location of the filenames
app.post('/', function(req, res) {
var branch = req.body.branch || 'master'
, compress = req.body.compress === 'true'
, filenames = req.body.filenames.split(',')
, dir = req.body.dir
, content = []
, done = 0
filenames.forEach(function (filename) {
var options = {
host: 'raw.github.com'
, port: 443
, path: path.join('/twitter/bootstrap/', branch, dir, filename)
, method: 'GET'
}
var hreq = https.request(options, function(hres) {
hres.setEncoding('utf8')
hres.on('data', function (chunk) {
content.push(chunk)
})
hres.on('end', function () {
if (++done == filenames.length) {
content = content.join(' ')
if (compress) {
try {
content = uglify(content, filenames)
} catch (e) {
content = 'error minifying source - please raise issue on http://github.com/twitter/bootstrap! thank you :)'
}
}
res.attachment(compress ? 'boostrap.min.js' : 'bootstrap.js')
res.send(content, { 'Content-Type': 'text/javascript' }, 201)
}
})
})
hreq.end();
})
})
app.listen(process.env.PORT || 3000)
// UGLIFY.js Utility method
function uglify(content, names) {
var tok = uglifyJS.parser.tokenizer(content)
, c = tok()
, result
, ast
result = '/**\n'
+ '* Bootstrap.js by @fat & @mdo\n'
+ '* plugins: ' + names.join(', ') + '\n'
+ '* Copyright 2012 Twitter, Inc.\n'
+ '* http://www.apache.org/licenses/LICENSE-2.0.txt\n'
+ '*/\n'
ast = uglifyJS.parser.parse(content)
ast = uglifyJS.uglify.ast_mangle(ast)
ast = uglifyJS.uglify.ast_squeeze(ast)
return result += uglifyJS.uglify.gen_code(ast)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment