diff --git a/package.json b/package.json index 78e1777..ba28184 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "dependencies": {}, "devDependencies": { "mocha": "*", - "chai": "*" + "chai": "*", + "folio": "0.2.x" }, "optionalDependencies": {}, "engines": { diff --git a/support/browser/prefix.js b/support/browser/prefix.js new file mode 100644 index 0000000..bc2c110 --- /dev/null +++ b/support/browser/prefix.js @@ -0,0 +1,48 @@ +!function (name, definition) { + if (typeof define == 'function' && typeof define.amd == 'object') define(definition); + else this[name] = definition(); +}('chai_timers', function () { + // CommonJS require() + function require(p){ + var path = require.resolve(p) + , mod = require.modules[path]; + if (!mod) throw new Error('failed to require "' + p + '"'); + if (!mod.exports) { + mod.exports = {}; + mod.call(mod.exports, mod, mod.exports, require.relative(path)); + } + return mod.exports; + } + + require.modules = {}; + + require.resolve = function (path){ + var orig = path + , reg = path + '.js' + , index = path + '/index.js'; + return require.modules[reg] && reg + || require.modules[index] && index + || orig; + }; + + require.register = function (path, fn){ + require.modules[path] = fn; + }; + + require.relative = function (parent) { + return function(p){ + if ('.' != p[0]) return require(p); + + var path = parent.split('/') + , segs = p.split('/'); + path.pop(); + + for (var i = 0; i < segs.length; i++) { + var seg = segs[i]; + if ('..' == seg) path.pop(); + else if ('.' != seg) path.push(seg); + } + + return require(path.join('/')); + }; + }; diff --git a/support/browser/suffix.js b/support/browser/suffix.js new file mode 100644 index 0000000..6a208ed --- /dev/null +++ b/support/browser/suffix.js @@ -0,0 +1,4 @@ + return require('timers'); +}); + +chai.use(chai_timers); diff --git a/support/compile.js b/support/compile.js new file mode 100644 index 0000000..897e2a6 --- /dev/null +++ b/support/compile.js @@ -0,0 +1,86 @@ +/*! + * chai-spies :: browser build script + * Copyright (c) 2012 Jake Luer + * MIT Licensed + */ + +/*! + * Script dependancies + */ + +var fs = require('fs') + , path = require('path') + , join = path.join + , folio = require('folio'); + +/*! + * Script variables + */ + +var appfiles = [] + , basepath = join(__dirname, '..', 'lib'); + +/** + * Recursively iterate through a given path + * and add all `.js` files to an array. + * + * @param {String} absolute path + */ + +function iteratePath (p) { + var self = this + , files = fs.readdirSync(p); + files.forEach(function (filename) { + var file = path.join(p, filename) + , stat = fs.statSync(file); + if (stat.isDirectory()) { + iteratePath(file); + } else if (stat.isFile()) { + if (path.extname(file) == '.js') + appfiles.push(file); + } + }); +}; + +// Go! +iteratePath(basepath); + +/** + * Package together all found files into a + * folio.Glossary, defining a custom "compiler" + * that will wrap our script with the needed commonjs + * + * @param {Array} files + * @param {Object} folio glossary configuration + */ + +var applicationJs = new folio.Glossary(appfiles, { + compilers: { + js: function (name, source, filename) { + var title = filename.replace(basepath + '/', '').replace('.js', '') + , buf = '\nrequire.register("' + title + '", function (module, exports, require) {\n'; + buf += source; + buf += '\n}); // module ' + name; + return buf; + } + } +}); + +/*! + * Load up our prefix/suffix + */ + +var prefix = fs.readFileSync(join(__dirname, 'browser', 'prefix.js'), 'utf8') + , suffix = fs.readFileSync(join(__dirname, 'browser', 'suffix.js'), 'utf8') + +/** + * Compile the folio.Glossary, applying our wrapper, + * and output the src. Wrap with prefix/suffix and + * write to file. + */ + +applicationJs.compile(function (err, src) { + var content = prefix + src + suffix; + fs.writeFileSync(join(__dirname, '..', 'chai-timers.js'), content, 'utf8'); + console.log('completed: chai-timers.js'); +});