Skip to content

Commit

Permalink
configuration workflow refinements (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 21, 2014
1 parent 12532a9 commit 277aed6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 73 deletions.
10 changes: 5 additions & 5 deletions cli.js
Expand Up @@ -48,12 +48,12 @@ process.on('uncaughtException', function(err) {
ui.log('\n'
+ 'jspm run main Run a jspm module in Node\n'
+ '\n'
+ 'jspm init Create / validate project configuration file\n'
+ 'jspm init [--prompts] Create / validate project configuration file\n'
+ '\n'
+ 'jspm install <name[=target]>+ [--force skips cache] [--latest]\n'
+ 'jspm install <name[=target]+> [--force skips cache] [--latest]\n'
+ ' install jquery Install a package from the registry to latest\n'
+ ' install react=npm:react Install a package from an endpoint to latest\n'
+ ' install jquery=2 Install a package to a version or range\n'
+ ' install jquery=2 react Install a package to a version or range\n'
+ '\n'
+ ' install Reproducible / shrinkwrap install package.json\n'
+ '\n'
Expand Down Expand Up @@ -237,12 +237,12 @@ process.on('uncaughtException', function(err) {
break;

case 'init':
var options = readOptions(args, ['--yes']);
var options = readOptions(args, ['--yes', '--prompts']);

if (options.yes)
ui.useDefaults();

core.init();
core.init(options.prompts);
break;


Expand Down
6 changes: 3 additions & 3 deletions lib/config.js
Expand Up @@ -52,7 +52,7 @@ exports.loader = null;

var loadPromise;
exports.loaded = false;
exports.load = function() {
exports.load = function(prompts) {
if (loadPromise)
return loadPromise;

Expand All @@ -65,7 +65,7 @@ exports.load = function() {
config.pjsonPath = process.env.jspmConfigPath || path.resolve(process.cwd(), 'package.json');

config.pjson = new PackageConfig(config.pjsonPath);
return config.pjson.read();
return config.pjson.read(prompts);
})
.then(function() {
if (fs.existsSync(config.pjson.configFile))
Expand All @@ -82,7 +82,7 @@ exports.load = function() {
})
.then(function() {
config.loader = new LoaderConfig(config.pjson.configFile);
return config.loader.read();
return config.loader.read(prompts);
})
.then(function() {
config.loaded = true;
Expand Down
10 changes: 8 additions & 2 deletions lib/config/loader.js
Expand Up @@ -108,8 +108,14 @@ Config.prototype.read = function() {
}
}
// create a structured configuration for the application path if present
self.app = new PathName(self.paths[config.pjson.name]);
delete self.paths[config.pjson.name];
if (self.paths[config.pjson.name + '/*']) {
self.app = new PathName(self.paths[config.pjson.name + '/*']);
delete self.paths[config.pjson.name];
}
else {
self.app = new PathName();
self.app.setPath(path.relative(config.pjson.baseURL, config.pjson.lib));
}

self.paths = cfg.paths;

Expand Down
80 changes: 40 additions & 40 deletions lib/config/package.js
Expand Up @@ -56,35 +56,41 @@ function PackageJSON(fileName) {
this.fileName = fileName;
}

PackageJSON.prototype.read = function() {
PackageJSON.prototype.read = function(prompts) {
if (this.read_)
throw "Package.json file already read";

this.read_ = true;
var self = this;
var pjson;

return readJSON(this.fileName)
.then(function(pjson) {
return checkCreatePackageJSON(pjson, self.fileName);
})
.then(function(pjson) {
self.originalPjson = pjson;

.then(function(_pjson) {
self.originalPjson = _pjson;

self.jspmPrefix = true;
if (_pjson.registry && !_pjson.jspm)
self.jspmPrefix = false;

// derive the jspm config from the 'jspm' property in the package.json
// sets the registry property if dependencies are jspm-prefixed
self.jspmPrefix = !!pjson.jspm;
pjson = config.derivePackageConfig(pjson);

// also sets the registry property if dependencies are jspm-prefixed
pjson = config.derivePackageConfig(_pjson);

if (prompts || (!_pjson.jspm && !_pjson.registry))
return doInitPrompts(pjson, self.fileName, self.jspmPrefix)
.then(function(prefixed) {
self.jspmPrefix = prefixed;
});
})
.then(function() {
self.dir = path.dirname(self.fileName);

// populate defaults as we go
var defaults = self.defaults = {};

// create structured configuration with defaults
self.name = pjson.name;

defaults.main = self.name ? self.name + '/index' : 'index';
self.main = pjson.main || defaults.main;
defaults.name = 'app';
self.name = pjson.name || defaults.name;

self.registry = pjson.registry;
self.dependencies = {};
Expand All @@ -109,6 +115,9 @@ PackageJSON.prototype.read = function() {
defaults.dist = path.resolve(self.baseURL, 'dist');
defaults.packages = path.resolve(self.baseURL, 'jspm_packages');

defaults.main = defaults.lib + '/index';
self.main = pjson.main || defaults.main;

self.lib = pjson.directories && pjson.directories.lib && path.resolve(self.dir, pjson.directories.lib) || defaults.lib;
self.dist = pjson.directories && pjson.directories.dist && path.resolve(self.dir, pjson.directories.dist) || defaults.dist;
// NB can remove jspmPackages suport in time
Expand All @@ -131,6 +140,11 @@ PackageJSON.prototype.read = function() {
PackageJSON.prototype.write = function() {
var pjson = this.jspmPrefix ? {} : this.originalPjson;

if (!this.jspmPrefix) {
delete pjson.jspm;
pjson.registry = pjson.registry || 'jspm';
}

var defaults = this.defaults;

// only set properties that differ from the defaults
Expand Down Expand Up @@ -207,58 +221,44 @@ PackageJSON.prototype.write = function() {
}

// can take an existing non-jspm package.json
function checkCreatePackageJSON(initialPjson, pjsonPath) {
function doInitPrompts(pjson, pjsonPath, prefixed) {
var baseDir = path.dirname(pjsonPath);

initialPjson = initialPjson || {};

// already jspm-optimized
if (initialPjson.jspm || initialPjson.registry)
return initialPjson;

var pjson = initialPjson;
var base;

pjson.directories = pjson.directories || {};

return Promise.resolve()
.then(function() {
return ui.confirm('Would you like jspm to prefix the jspm package.json properties under %jspm%?', true);
return ui.confirm('Would you like jspm to prefix the jspm package.json properties under %jspm%?', prefixed);
})
.then(function(prefix) {
if (prefix)
initialPjson.jspm = pjson = {};
return ui.input('Enter a name for the project (optional)', initialPjson.name);
prefixed = prefix;
return ui.input('Enter a name for the project (optional)', pjson.name);
})
.then(function(name) {
pjson.name = name;
return ui.input('Enter baseURL path', '.');
return ui.input('Enter baseURL (public folder) path', pjson.directories.baseURL || '.');
})
.then(function(baseURL) {
base = path.relative(process.cwd(), path.resolve(baseURL));
baseURL = path.relative(baseDir, path.resolve(baseURL));
if (!base)
base = '.';
base += path.sep;
pjson.directories = pjson.directories || {};
pjson.directories.baseURL = baseURL;
return ui.input('Enter project source folder', base + 'lib');
return ui.input('Enter project code folder', pjson.directories.lib || base + 'lib');
})
.then(function(lib) {
pjson.directories = pjson.directories || {};
pjson.directories.lib = path.relative(baseDir, path.resolve(lib));
return ui.input('Enter project built folder (optional)');
})
.then(function(dist) {
if (dist)
pjson.directories.dist = path.relative(baseDir, path.resolve(dist));
return ui.input('Enter packages folder', base + 'jspm_packages');
return ui.input('Enter jspm packages folder', pjson.directories.packages || base + 'jspm_packages');
})
.then(function(packages) {
pjson.directories.packages = path.relative(baseDir, path.resolve(packages));
return ui.input('Enter config file path', base + 'config.js');
return ui.input('Enter config file path', pjson.configFile || base + 'config.js');
})
.then(function(configFile) {
pjson.configFile = path.relative(baseDir, path.resolve(configFile))
return initialPjson;
return prefixed;
});
}

Expand Down
25 changes: 3 additions & 22 deletions lib/core.js
Expand Up @@ -130,22 +130,6 @@ exports.setMode = function(modes) {

msg += 'Loader set to CDN library sources\n';
})
.then(function(unmatched) {
if (modes.indexOf('production') == -1)
return unmatched;

// set production
config.loader.app.setPath(config.pjson.dist);
msg += 'Local package URL set to %' + path.relative(process.cwd(), config.pjson.dist) + '%.';
})
.then(function(unmatched) {
if (modes.indexOf('dev') == -1)
return unmatched;

// set dev
config.loader.app.setPath(config.pjson.lib);
msg += 'Local package URL set to %' + path.relative(process.cwd(), config.pjson.lib) + '%.';
})
.then(function(unmatched) {
if (unmatched)
return ui.log('warn', 'Invalid mode');
Expand Down Expand Up @@ -304,13 +288,10 @@ exports.dlLoader = function(unminified, edge) {
});
}

exports.init = function init() {
return config.load()
exports.init = function init(ask) {
return config.load(ask)
.then(function() {
if (config.pjson.name)
return core.setMode('dev');
else
return config.save();
return config.save();
})
.then(function() {
ui.log('ok', 'Verified package.json at %' + path.relative(process.cwd(), config.pjsonPath) + '%\nVerified config file at %' + path.relative(process.cwd(), config.pjson.configFile) + '%');
Expand Down
2 changes: 1 addition & 1 deletion lib/install.js
Expand Up @@ -644,7 +644,7 @@ function loadExistingRange(name, parent, inject) {
})
.then(function(target) {
if (!target) {
ui.log('warn', (parent ? '%' + parent + '% dependency %' : '%') + name + '% is not a specified dependency in the package.json.');
ui.log('warn', (parent ? '%' + parent + '% dependency %' : '%') + name + '% is installed in the config file, but is not a dependency in the package.json. It is advisable to add it to the package.json file.');
return;
}

Expand Down

0 comments on commit 277aed6

Please sign in to comment.