Permalink
Browse files

Add an optional config parameter to buildFromPackage

  • Loading branch information...
1 parent 54345f3 commit 665bb81f8cb8c1c65a86fa4953df1c2224ab8eb2 @ctice committed Sep 19, 2011
Showing with 24 additions and 6 deletions.
  1. +24 −6 main.js
View
30 main.js
@@ -24,12 +24,17 @@ function build(main, config, callback) {
});
}
-exports.buildFromPackage = function(p, callback) {
+exports.buildFromPackage = function(p, config, callback) {
+ if (!callback) {
+ callback = config;
+ config = {};
+ }
+
fs.stat(p, function(err, stat) {
if (err) {
callback(err);
} else {
- var packageFile, config = {};
+ var packageFile;
if (stat.isDirectory()) {
config.root = p;
packageFile = path.join(p, 'package.json');
@@ -38,15 +43,28 @@ exports.buildFromPackage = function(p, callback) {
packageFile = p;
}
jsonFs.readFile(packageFile, function(err, json) {
- var modulrConfig = json.modulr_config;
+ // get package-specific config options
+ var modulrConfig = json.modulr || {};
if (err) {
callback(err);
} else {
- config.paths = modulrConfig.paths ? modulrConfig.paths : [];
+ var paths = modulrConfig.paths ? modulrConfig.paths : [];
+ // we may want to include additional paths that are dynamic at build time
tobie
tobie Sep 20, 2011

Is there an actual use case for this? If so, I feel like we might want to offer another API instead:

modulr.buildFromPackage(p, function(packageConfig) {
    packageConfig.paths.push('./foo');
}, callback);

Thoughts?

ctice
ctice Sep 20, 2011 Owner

yea, there is a use case I'm encountering. the location of the bolt source directory is different during development and automatic build, so we would need to set the build path dynamically depending on the context that the build is running.

is that API common though? i feel like it's a little strange to pass in a function that should modify the params that are passed to it. maybe i've just never seen that idiom.. on the other hand, with this API it's a little strange that some properties are modifies (paths) and others are straight overrides (everything else).

+ if (config.paths) {
+ config.paths = config.paths.concat(paths);
+ } else {
+ config.paths = paths;
+ }
config.paths.push('.');
- config.lazyEval = modulrConfig.lazy_eval;
+
+ if (config.lazyEval === undefined) {
+ config.lazyEval = !!modulrConfig.lazy_eval;
+ }
config.isPackageAware = true;
- config.verbose = !!modulrConfig.verbose;
+ if (config.verbose === undefined) {
+ config.verbose = !!modulrConfig.verbose;
+ }
+
build(json.main, config, callback);
}
});

0 comments on commit 665bb81

Please sign in to comment.