Skip to content

Commit

Permalink
configuration refactoring completion
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Oct 22, 2014
1 parent 0ad8221 commit 3b0f9f5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 120 deletions.
50 changes: 9 additions & 41 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,28 @@ var ui = require('./ui');
var path = require('path');
var config = require('./config');
var systemBuilder = require('systemjs-builder');
var globalConfig = require('./global-config');

var oldPaths = {};
function setLocalPaths() {
// set local
var jspmPackages = path.relative(config.baseDir, config.jspmPackages);
Object.keys(globalConfig.config.endpoints).forEach(function(e) {
if (config.paths[e + ':*'])
oldPaths[e + ':*'] = config.paths[e + ':*'];
config.paths[e + ':*'] = jspmPackages + '/' + e + '/*.js';
});
}
function revertLocalPaths() {
var jspmPackages = path.relative(config.baseDir, config.jspmPackages);
config.endpoints.forEach(function(e) {
config.paths[e + ':*'] = oldPaths[e + ':*'];
});
}

exports.depCache = function(moduleName) {
var oldPaths;
return config.load()
.then(function() {
moduleName = moduleName || config.main;
if (!moduleName)
return ui.input('No main entry point is provided, please specify the module to trace', '~/app').then(function(main) {
config.main = main;
});
moduleName = moduleName || config.loader.main;
})
.then(function() {
ui.log('info', 'Injecting the traced dependency tree for `' + moduleName + '`...');
setLocalPaths();
return systemBuilder.trace(moduleName, config.curConfig);
return systemBuilder.trace(moduleName, config.loader.getConfig());
})
.then(function(output) {
var traceTree = output.tree;
moduleName = output.moduleName;
var depCache = config.curConfig.depCache = config.curConfig.depCache || {};
var depCache = config.loader.depCache = config.loader.depCache || {};
for (var d in traceTree)
depCache[d] = traceTree[d].deps.map(function(dep) {
return traceTree[d].depMap[dep];
});
revertLocalPaths();
})
.then(config.save)
.then(function() {
ui.log('ok', 'Depenency tree injected');
// ui.log('info', '\n' + logTree(config.curConfig.depCache, moduleName, 1) + '\n');
})
.catch(function(e) {
ui.log('err', e.stack || e);
Expand Down Expand Up @@ -104,10 +78,9 @@ exports.bundle = function(moduleExpression, fileName, inject) {
return config.load()
.then(function() {
ui.log('info', 'Building the bundle tree for `' + moduleExpression + '`...');
setLocalPaths();

// trace the starting module
return systemBuilder.trace(firstModule, config.curConfig);
return systemBuilder.trace(firstModule, config.loader.getConfig());
})
.then(function(trace) {

Expand Down Expand Up @@ -143,19 +116,18 @@ exports.bundle = function(moduleExpression, fileName, inject) {
if (inject) {
// Add the bundle to config if the inject flag was given.
var bundleName = extractBundleName(fileName);
if (!config.curConfig.bundles) {
config.curConfig.bundles = {};
if (!config.loader.bundles) {
config.loader.bundles = {};
}
config.curConfig.bundles[bundleName] = Object.keys(buildTree).filter(function(moduleName) {
config.loader.bundles[bundleName] = Object.keys(buildTree).filter(function(moduleName) {
return buildTree[moduleName].metadata.build !== false;
});
ui.log('ok', '`' + bundleName + '` added to config bundles.');
}
return systemBuilder.buildTree(buildTree, fileName);
})
.then(function() {
delete config.curConfig.depCache;
revertLocalPaths();
delete config.loader.depCache;
})
.then(config.save)
.then(function() {
Expand All @@ -172,12 +144,8 @@ exports.bundleSFX = function(moduleName, fileName) {
return config.load()
.then(function() {
ui.log('info', 'Building the single-file sfx bundle for `' + moduleName + '`...');
setLocalPaths();

return systemBuilder.buildSFX(moduleName, config.curConfig, fileName);
})
.then(function() {
revertLocalPaths();
return systemBuilder.buildSFX(moduleName, config.loader.getConfig(), fileName);
})
.then(config.save)
.then(function() {
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.derivePackageConfig = function(pjson, override) {
// apply the jspm internal overrides first
if (pjson.jspm) {
if (pjson.jspm.dependencies && !pjson.registry)
pjson.registry = 'jspm';
pjson.jspm.registry = 'jspm';
extend(dpjson, pjson.jspm);
delete dpjson.jspm;
}
Expand Down
95 changes: 57 additions & 38 deletions lib/config/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var hasProperties = require('../common').hasProperties;
var fs = require('graceful-fs');
var asp = require('rsvp').denodeify;
var path = require('path');
var PackageName = require('./package-name');

/*
* Loader Configuration Class
Expand All @@ -31,6 +32,7 @@ var path = require('path');
*
* paths
* bundles
* depCache
*
* baseMap
* depMap
Expand All @@ -52,7 +54,34 @@ Config.prototype.read = function() {
.catch(function() {
return '';
})
.then(loadConfig)
.then(function(source) {
var cfg = {};
var System = {
config: function(_cfg) {
for (var c in _cfg) {
var v = _cfg[c];
if (typeof v == 'object') {
cfg[c] = cfg[c] || {};
for (var p in v)
cfg[c][p] = v[p];
}
else
cfg[c] = v;
}
},
paths: {},
map: {},
versions: {}
};
eval(source.toString());

// allow declarative form too
var config = System.config;
delete System.config;
config(System);

return cfg;
})
.then(function(cfg) {
self.originalConfig_ = cfg;

Expand All @@ -71,13 +100,14 @@ Config.prototype.read = function() {
}
}
// create a structured configuration for the application path if present
self.app = new PathName(config.pjson.name, self.paths[config.pjson.name]);
self.app = new PathName(self.paths[config.pjson.name]);
delete self.paths[config.pjson.name];

self.paths = cfg.paths;

self.shim = cfg.shim;
self.bundles = cfg.bundles;
self.depCache = cfg.depCache;

// separate map into baseMap and depMap
self.baseMap = {};
Expand Down Expand Up @@ -119,6 +149,18 @@ Config.prototype.ensureEndpoint = function(endpointName) {
isLocal ? ep.setLocal() : ep.setRemote();
}

// return the loader configuration for a server loading use
Config.prototype.getConfig = function() {
var cfg = extend({}, this.originalConfig_);

// set all endpoint paths to be local paths
cfg.paths = extend({}, cfg.paths);
for (var e in this.endpoints)
cfg.paths[e + ':*'] = this.endpoints[e].local

return cfg;
}

/*
* PathName object, can be empty
*
Expand All @@ -127,8 +169,8 @@ Config.prototype.ensureEndpoint = function(endpointName) {
function PathName(path) {
this.path = path;
}
PathName.prototype.setPath = function(path) {
this.path = path.relative(config.pjson.baseURL, path) + '/*.js';
PathName.prototype.setPath = function(_path) {
this.path = path.relative(config.pjson.baseURL, _path) + '/*.js';
}
PathName.prototype.write = function() {
return this.path;
Expand All @@ -141,55 +183,26 @@ var jspmPackages;
function EndpointPath(name, endpointPath) {
jspmPackages = jspmPackages || path.relative(config.pjson.baseURL, config.pjson.packages);
var endpointRemote = endpoint.load(name).remote;
this.remote_ = endpointRemote + '/*.js';
this.local_ = jspmPackages + '/' + name + '/*.js';
this.remote = endpointRemote + '/*.js';
this.local = jspmPackages + '/' + name + '/*.js';

this.mode = 'local';
if (path == this.remote_)
if (path == this.remote)
this.mode = 'remote';
this.path = endpointPath;
}
EndpointPath.prototype.setLocal = function() {
this.path = this.local_;
this.path = this.local;
this.mode = 'local';
}
EndpointPath.prototype.setRemote = function() {
this.path = this.remote_;
this.path = this.remote;
this.mode = 'remote';
}
EndpointPath.prototype.write = function() {
return this.path;
}

function loadConfig(source) {
var cfg = {};
var System = {
config: function(_cfg) {
for (var c in _cfg) {
var v = _cfg[c];
if (typeof v == 'object') {
cfg[c] = cfg[c] || {};
for (var p in v)
cfg[c][p] = v[p];
}
else
cfg[c] = v;
}
},
paths: {},
map: {},
versions: {}
};
eval(source);

// allow declarative form too
var config = System.config;
delete System.config;
config(System);

return cfg;
}

// convert structured configuration into a plain object
// by calling the .write() methods of structured classes
// properties ending in _ are considered private
Expand Down Expand Up @@ -226,6 +239,12 @@ Config.prototype.write = function() {
cfg.paths[config.pjson.name + '/*'] = cfg.app;
delete cfg.app;

for (var e in cfg.endpoints) {
var val = cfg.endpoints[e];
delete cfg.endpoints[e];
cfg.endpoints[e + ':*'] = val;
}

extend(cfg.paths, cfg.endpoints);
delete cfg.endpoints;

Expand Down
55 changes: 30 additions & 25 deletions lib/config/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var readJSON = require('../common').readJSON;
* lib
* dist
* packages
* format
* map
* buildConfig
*
Expand Down Expand Up @@ -109,6 +110,7 @@ PackageJSON.prototype.read = function() {
defaults.configFile = path.resolve(self.baseURL, 'config.js');
self.configFile = pjson.configFile && path.resolve(self.dir, pjson.configFile) || defaults.configFile;

self.format = pjson.format;
self.map = extend({}, pjson.map || {});

self.buildConfig = extend({}, pjson.buildConfig || {});
Expand All @@ -128,6 +130,32 @@ PackageJSON.prototype.write = function() {
if (this.main != defaults.main)
pjson.main = this.main;

if (this.format)
pjson.format = this.format;

var directories = {};
if (this.baseURL != defaults.baseURL)
directories.baseURL = this.baseURL;
if (this.lib != defaults.lib)
directories.lib = this.lib;
if (this.dist != defaults.dist)
directories.dist = this.dist;
if (this.packages != defaults.packages)
directories.packages = this.packages;

if (hasProperties(directories)) {
for (var d in directories)
directories[d] = path.relative(this.dir, directories[d]);
pjson.directories = extend(pjson.directories || {}, directories);
}

if (this.configFile != defaults.configFile)
pjson.configFile = path.relative(this.dir, this.configFile);

pjson.map = this.map;
if (!hasProperties(pjson.map))
delete pjson.map;

// reuse existing package.json dependencies if possible to maintain ordering
pjson.dependencies = {};
var depValue;
Expand All @@ -148,36 +176,13 @@ PackageJSON.prototype.write = function() {
}
if (!hasProperties(pjson.dependencies))
delete pjson.dependencies;

var directories = {};
if (this.baseURL != defaults.baseURL)
directories.baseURL = this.baseURL;
if (this.lib != defaults.lib)
directories.lib = this.lib;
if (this.dist != defaults.dist)
directories.dist = this.dist;
if (this.packages != defaults.packages)
directories.packages = this.packages;

if (hasProperties(directories)) {
for (var d in directories)
directories[d] = path.relative(this.dir, directories[d]);
pjson.directories = extend(pjson.directories || {}, directories);
}

if (this.configFile != defaults.configFile)
pjson.configFile = path.relative(this.dir, this.configFile);

pjson.buildConfig = this.buildConfig;
if (!hasProperties(pjson.buildConfig))
delete pjson.buildConfig;

pjson.map = this.map;
if (!hasProperties(pjson.map))
delete pjson.map;

// NB check that the file hasn't changed since we opened it and if so, prompt
return asp(fs.writeFile)(JSON.stringify(this.originalPjson, null, 2) + '\n');
return asp(fs.writeFile)(this.fileName, JSON.stringify(this.originalPjson, null, 2) + '\n');
}

// can take an existing non-jspm package.json
Expand All @@ -193,7 +198,7 @@ function checkCreatePackageJSON(initialPjson) {

return Promise.resolve()
.then(function() {
return ui.confirm('Would you like jspm to prefix its package.json properties under %jspm%?', true);
return ui.confirm('Would you like jspm to prefix the jspm package.json properties under %jspm%?', true);
})
.then(function(prefix) {
if (prefix)
Expand Down
Loading

0 comments on commit 3b0f9f5

Please sign in to comment.