Skip to content

Commit

Permalink
rewrote builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Dec 25, 2013
1 parent e023d00 commit 2b4b9f5
Show file tree
Hide file tree
Showing 23 changed files with 6,133 additions and 709 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ file with a name that's unlikely to change (no version numbers).


(1) It's ok to target a specific platform, like node.js, or WebKit. (1) It's ok to target a specific platform, like node.js, or WebKit.


(2) To check the minified and compressed size of your library from the command line, either use `./build -v` (see below) or run: (2) To check the minified and compressed size of your library from the command line, either use `./build.js -v` (see below) or run:


$ uglifyjs yourlib.js | gzip -9f | wc -c $ uglifyjs yourlib.js | gzip -9f | wc -c


Expand Down
24 changes: 0 additions & 24 deletions build

This file was deleted.

17 changes: 17 additions & 0 deletions build.js
@@ -0,0 +1,17 @@
#!/usr/bin/env node

// see lib/main.js for info

const ghAuth = require('./lib/ghauth')
, main = require('./lib/main')
, stats = require('./lib/stats')

const config = require('./config')

config.verbose = process.argv.indexOf('-v') > -1
config.quiet = process.argv.indexOf('-q') > -1

if (process.argv.indexOf('-s') > -1)
return stats(config)

ghAuth(config, main.bind(null, config))
25 changes: 25 additions & 0 deletions config.js
@@ -0,0 +1,25 @@
module.exports = {
inFile : 'data.js'
, outFile : 'data-min.js'
// for now; need to tweak this later
, githubLastPushCutoffMonths : 48
, sizeExceptions : [
'Backbone'
, 'Zepto'
]
, minifyExceptions : [
'cm.js'
]
, errorCountThreshold : 10
, maxGzipped : 5 * 1024
// these 3 are basic sanity cecks, minimums that we would expect the contents
// to be, otherwise something might be wrong
, minRaw : 300
, minMinified : 150
, minGzipped : 100
// this is to compare raw with minified, if it's too close then we may have
// been given the minified source in the first place
, minifierTollerance : 50
// give them this much wriggle room with the reported 'size' vs actual gzipped size
, expectedTolleranceFraction : 0.5
}
5,438 changes: 5,437 additions & 1 deletion data-min.js

Large diffs are not rendered by default.

113 changes: 55 additions & 58 deletions lib/atom.js
@@ -1,73 +1,70 @@
var uuid = require('node-uuid') const uuid = require('node-uuid')
, fs = require('fs') , fs = require('fs')
, async = require('async') , memo = require('async-memo')
, xml = require('./xml') , moment = require('moment')
, xml = require('./xml')


require('date-utils') const file = 'atom.xml'

, footRegex = /(\n<\/feed>\n?)/
var file = 'atom.xml' , dateStr = moment.utc().format('YYYY-MM-DD\'T\'HH:mm:ssZ')
, footRegex = /(\n<\/feed>\n)/ , entryTemplate =
, dateStr = new Date().addMinutes(new Date().getTimezoneOffset()).toFormat('YYYY-MM-DD\'T\'HH24:MI:SSZ') '\t<entry>\n'
, entryTemplate = + '\t\t<title>{xmlEncodedName}</title>\n'
'\t<entry>\n' + '\t\t<link href="{url}" />\n'
+ '\t\t<title>{xmlEncodedName}</title>\n' + '\t\t<id>urn:uuid:{atomUuid}</id>\n'
+ '\t\t<link href="{url}" />\n' + '\t\t<updated>{atomDate}</updated>\n'
+ '\t\t<id>urn:uuid:{atomUuid}</id>\n' + '\t\t<summary>{xmlEncodedDescription}</summary>\n'
+ '\t\t<updated>{atomDate}</updated>\n' + '\t</entry>\n\n'
+ '\t\t<summary>{xmlEncodedDescription}</summary>\n' + '$1'
+ '\t</entry>\n\n' // memoize so we only read once, but keep contents global so we can edit it
+ '$1' , fileContents = memo(
, contents

// memoize so we only read once, but keep contents global so we can edit it
, fileContents = async.memoize(
function (callback) { function (callback) {
fs.readFile(file, 'utf-8', function (err, _contents) { fs.readFile(file, 'utf-8', function (err, _contents) {
contents = _contents contents = _contents
callback(err) callback(err)
}) })
} })
, function () { return '_' }
)


, escapeRegExp = function (string) { var contents
return string.replace(/[\-\[\]\{\}\(\)*+?.\\\^$|,#\s]/g, function (match) { return '\\' + match })
}


, exists = function (contents, lib) { function escapeRegExp (string) {
return new RegExp(escapeRegExp('<title>' + lib.xmlEncodedName + '</title>')).test(contents) return string.replace(/[\-\[\]\{\}\(\)*+?.\\\^$|,#\s]/g, function (match) { return '\\' + match })
} }


, t = function (s, d){ function exists (contents, lib) {
for (var p in d) return new RegExp(escapeRegExp('<title>' + lib.xmlEncodedName + '</title>')).test(contents)
s = s.replace(new RegExp('{' + p + '}', 'g'), d[p]) }
return s
}


, processLibrary = function (lib, callback) { function t (s, d) {
fileContents(function (err) { for (var p in d)
if (err) s = s.replace(new RegExp('{' + p + '}', 'g'), d[p])
throw err return s
}


lib.xmlEncodedName = xml.encodeXmlEntities(lib.name) function processLibrary (lib, callback) {
if (exists(contents, lib)) fileContents(function (err) {
return callback() if (err)
throw err


lib.atomUuid = uuid.v1() lib.xmlEncodedName = xml.encodeXmlEntities(lib.name)
lib.atomDate = dateStr if (exists(contents, lib))
lib.xmlEncodedDescription = xml.encodeXmlEntities(lib.description) return callback()
contents = contents.replace(footRegex, t(entryTemplate, lib))
;delete lib.atomUuid
;delete lib.atomDate
callback()
})
}


, write = function (callback) { lib.atomUuid = uuid.v1()
fs.writeFile(file, contents, 'utf-8', callback) lib.atomDate = dateStr
} lib.xmlEncodedDescription = xml.encodeXmlEntities(lib.description)
contents = contents.replace(footRegex, t(entryTemplate, lib))
;delete lib.atomUuid
;delete lib.atomDate
;delete lib.xmlEncodedName
;delete lib.xmlEncodedDescription
callback()
})
}


module.exports = { function write (callback) {
processLibrary: processLibrary fs.writeFile(file, contents, 'utf-8', callback)
, write: write
} }

module.exports.processLibrary = processLibrary
module.exports.write = write
68 changes: 30 additions & 38 deletions lib/fetcher.js
@@ -1,47 +1,39 @@
var zip = require('zip') var zip = require('zip')
, request = require('request') , hyperquest = require('hyperquest')
, bl = require('bl')


, requestPool = { maxSockets: 20 } function unzip (zipEntry, buffer) {
var reader = zip.Reader(buffer)
, data = null


, unzip = function (zipEntry, buffer) { // iterate through zipfile entries looking for the right name
var reader = zip.Reader(buffer) reader.forEach(function(entry) {
, data = null if (zipEntry === entry._header.file_name) {
// iterate through zipfile entries looking for the right name data = entry._stream.toString();
reader.forEach(function(entry) {
if (zipEntry === entry._header.file_name) {
data = entry._stream.toString();
}
})
return data
} }
})
return data
}


, fetch = function (sourceUrl, zipEntry, callback) { function fetch (sourceUrl, zipEntry, callback) {
var options = { hyperquest(sourceUrl, { headers: { 'User-Agent': 'microjs.com-build-script' } })
url: sourceUrl .pipe(bl(function (err, content) {
, encoding: zipEntry ? null : 'utf8' if (err)
, timeout: 1 * 60 * 1000 return callback(err)
, pool: requestPool
, maxRedirects: 2
, headers: {"User-Agent" : "microjs.com-build-script"}
}
, handle = function (err, response, body) {
if (err)
return callback(err)


if (zipEntry) { if (zipEntry) {
try { try {
body = unzip(zipEntry, body) content = unzip(zipEntry, content)
} catch (e) { } catch (e) {
callback('unzip error' + e) return callback('unzip error' + e)
} }
if (!body)
return callback('could not find entry "' + zipEntry + '" inside zip file')
}


callback(null, body) if (!content)
} return callback('could not find entry ' + zipEntry + ' inside zip file')
}


request(options, handle) callback(null, content.toString())
} }))
}


module.exports = fetch module.exports = fetch
36 changes: 18 additions & 18 deletions lib/ghauth.js
@@ -1,21 +1,21 @@
var read = require('read') const read = require('read')
, colors = require('colors') , colors = require('colors')


, auth = function (config, callback) { function auth (config, callback) {
console.log('To collect GitHub stats we need you to authenticate so we can exceed API rate-limiting.'.bold.green) console.log('To collect GitHub stats we need you to authenticate so we can exceed API rate-limiting.'.bold.green)
console.log('Leave the username field blank to skip stats collection.'.bold.green) console.log('Leave the username field blank to skip stats collection.'.bold.green)
console.log() console.log()


read({ prompt: 'GitHub username:'.bold }, function (err, data) { read({ prompt: 'GitHub username:'.bold }, function (err, data) {
if (err) return callback(err) if (err) return callback(err)
if (data === '') return callback() if (data === '') return callback()
config.ghUser = data config.ghUser = data
read({ prompt: 'GitHub password:'.bold, silent: true, replace: '\u2714' }, function (err, data) { read({ prompt: 'GitHub password:'.bold, silent: true, replace: '\u2714' }, function (err, data) {
if (err) return callback(err) if (err) return callback(err)
config.ghPass = data config.ghPass = data
callback() callback()
}) })
}) })
} }


module.exports = auth module.exports = auth

0 comments on commit 2b4b9f5

Please sign in to comment.