From 277aed6870a6ca3b1762b651862db74cbc001003 Mon Sep 17 00:00:00 2001 From: guybedford Date: Sun, 21 Dec 2014 17:54:17 +0200 Subject: [PATCH] configuration workflow refinements (#326) --- cli.js | 10 +++--- lib/config.js | 6 ++-- lib/config/loader.js | 10 ++++-- lib/config/package.js | 80 +++++++++++++++++++++---------------------- lib/core.js | 25 ++------------ lib/install.js | 2 +- 6 files changed, 60 insertions(+), 73 deletions(-) diff --git a/cli.js b/cli.js index 479160ec2..16b3c98bd 100755 --- a/cli.js +++ b/cli.js @@ -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 + [--force skips cache] [--latest]\n' + + 'jspm install [--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' @@ -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; diff --git a/lib/config.js b/lib/config.js index 272a71a7b..edb2c0edc 100644 --- a/lib/config.js +++ b/lib/config.js @@ -52,7 +52,7 @@ exports.loader = null; var loadPromise; exports.loaded = false; -exports.load = function() { +exports.load = function(prompts) { if (loadPromise) return loadPromise; @@ -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)) @@ -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; diff --git a/lib/config/loader.js b/lib/config/loader.js index 1f862d78e..02811024d 100644 --- a/lib/config/loader.js +++ b/lib/config/loader.js @@ -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; diff --git a/lib/config/package.js b/lib/config/package.js index aabfd03af..c1b1517ea 100644 --- a/lib/config/package.js +++ b/lib/config/package.js @@ -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 = {}; @@ -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 @@ -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 @@ -207,30 +221,23 @@ 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)); @@ -238,27 +245,20 @@ function checkCreatePackageJSON(initialPjson, pjsonPath) { 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; }); } diff --git a/lib/core.js b/lib/core.js index cfb5f1e00..8d9ac4985 100644 --- a/lib/core.js +++ b/lib/core.js @@ -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'); @@ -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) + '%'); diff --git a/lib/install.js b/lib/install.js index f580e8643..1189cda65 100644 --- a/lib/install.js +++ b/lib/install.js @@ -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; }