Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanups + add tests

  • Loading branch information...
commit f2ed8af69461ed4e730a3dc790ff2bd709b2f59e 1 parent 7d37115
@paulbaumgart paulbaumgart authored
Showing with 93 additions and 37 deletions.
  1. +56 −37 lib/dev-cdn.js
  2. +37 −0 test/test-devcdn.js
View
93 lib/dev-cdn.js
@@ -12,7 +12,7 @@ var DEV_CDN_APPENDS = "self.devRequire = function(request) {\
return _$_require((_$_moduleMap && _$_moduleMap[request]) || request);\
};";
-function _findBundleNames(cwd, env, cb) {
+function _findBundles(cwd, env, callback) {
exec("find . -type f -iname package.json" +
" '!' -iwholename '*/node_modules/*'",
{ cwd: cwd },
@@ -44,49 +44,66 @@ function _findBundleNames(cwd, env, cb) {
}
});
- cb(bundleNameToConfigMap);
+ callback(bundleNameToConfigMap);
});
}
-var extendConfig = function(port, name, config) {
- var bundleUrl = 'http://localhost:' + port + '/' + name;
- config.bundleUrl = JSON.stringify(bundleUrl);
- config.watch = true;
- config.bundleName = name;
- config.devCdnAppends = DEV_CDN_APPENDS;
+function _createBundle(port, bundleName, config) {
+ var bundleUrl = 'http://localhost:' + port + '/' + bundleName;
+ config.bundleUrl = JSON.stringify(bundleUrl);
+ config.watch = true;
+ config.bundleName = bundleName;
+ config.devCdnAppends = DEV_CDN_APPENDS;
- return config;
-};
+ process.stderr.write('\nLoading ' + bundleName + '... ');
-module.exports = function(port, env, cwdOrConfigs) {
+ var bundle = new Bundle(config);
+
+ if (bundle.error) {
+ console.error('\n' + bundle.error.stack + '\n');
+ }
+ console.error('done. URL: ' + bundleUrl);
+
+ return bundle;
+}
+
+function _createBundles(port, bundleConfigs) {
var bundles = {};
- if (typeof cwdOrConfigs === 'string') {
- _findBundleNames(cwd || process.cwd(), env, function(bundleConfigs) {
- _(bundleConfigs).each(function(config, bundleName) {
- process.stderr.write('\nLoading ' + bundleName + '... ');
-
- var bundle = new Bundle(extendConfig(port, bundleName, config));
- bundles[bundleName] = bundle;
-
- if (bundle.error) {
- console.error('\n' + bundle.error.stack + '\n');
- }
- console.error('done. URL: ' + bundleUrl);
- });
- if (_(bundles).keys().length === 0) {
- throw new Error('Dev CDN could not find any package.json files to serve.');
- }
- console.error('\nAll bundles loaded. Dev CDN started on port: ' + port);
- });
- }
- else
- Object.keys(cwdOrConfigs).forEach(function(name) {
- console.error('Found bundle: ' + name);
- var bundle = new Bundle(extendConfig(port, name, cwdOrConfigs[name]));
- bundles[name] = bundle;
- });
+ if (!bundleConfigs || _(bundleConfigs).keys().length === 0) {
+ throw new Error('Dev CDN could not find any bundles to serve.');
+ }
+
+ _(bundleConfigs).each(function(config, bundleName) {
+ bundles[bundleName] = _createBundle(port, bundleName, config);
+ });
+
+ console.error('All bundles loaded.');
+
+ return bundles;
+}
+
+function createDevCdn(port, envOrBundleConfigs, cwd, loadedCallback) {
+ var bundles;
+
+ if (typeof envOrBundleConfigs === 'string') {
+ var env = envOrBundleConfigs;
+ _findBundles(cwd || process.cwd(), env, function(bundleConfigs) {
+ bundles = _createBundles(port, bundleConfigs);
+ if (typeof loadedCallback === 'function') {
+ loadedCallback(null, bundles);
+ }
+ });
+ } else {
+ var bundleConfigs = envOrBundleConfigs;
+ process.nextTick(function() {
+ bundles = _createBundles(port, bundleConfigs);
+ if (typeof loadedCallback === 'function') {
+ loadedCallback(null, bundles);
+ }
+ });
+ }
return function(req, resp, next) {
var bundleName = req.url.replace(/^\/+|\?.+$/g, '');
@@ -133,5 +150,7 @@ module.exports = function(port, env, cwdOrConfigs) {
resp.end();
}
};
-};
+}
+
+module.exports = createDevCdn;
View
37 test/test-devcdn.js
@@ -0,0 +1,37 @@
+var vows = require('vows');
+var assert = require('assert');
+var _ = require('underscore');
+var createDevCdn = require('../lib/dev-cdn');
+
+vows.describe('test Dev CDN').addBatch({
+ "Dev CDN with port, env, and cwd": {
+ topic: function() {
+ createDevCdn(4444, 'production', __dirname + '/../', this.callback);
+ },
+
+ "found bundles": function(err, bundleConfigs) {
+ assert.isNull(err);
+ assert.equal(typeof bundleConfigs['jsbundle.js'], 'object');
+ assert.equal(_(bundleConfigs).keys().length, 1);
+ }
+ },
+
+ "Dev CDN with port and bundleConfigs": {
+ topic: function() {
+ var bundleConfigs = {
+ abc: {
+ entryFile: __dirname + '/fixtures/abc.js'
+ }
+ };
+ createDevCdn(4444, bundleConfigs, null, this.callback);
+ },
+
+ "found bundles": function(err, bundleConfigs) {
+ assert.isNull(err);
+ assert.equal(typeof bundleConfigs.abc, 'object');
+ assert.equal(_(bundleConfigs).keys().length, 1);
+ }
+ }
+}).export(module);
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.