Repository: cordova-lib Updated Branches: refs/heads/master 3caefb7fd -> 38068fa4b
Style fixes in src/cordova/ustil.js - Add jshint config line. - Make jshint happy. - Use regular function defs instead of inline assignment. This reduces indent, and hoists the functions up so they can use each other. Most of the diff is the white space of reduced indent. Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/38068fa4 Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/38068fa4 Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/38068fa4 Branch: refs/heads/master Commit: 38068fa4baa5fce9d7142512505d08c2643dced4 Parents: 3caefb7 Author: Mark Koudritsky <[email protected]> Authored: Tue Jun 10 15:30:55 2014 -0400 Committer: Mark Koudritsky <[email protected]> Committed: Tue Jun 10 15:30:55 2014 -0400 ---------------------------------------------------------------------- cordova-lib/src/cordova/util.js | 274 +++++++++++++++++++---------------- 1 file changed, 147 insertions(+), 127 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/38068fa4/cordova-lib/src/cordova/util.js ---------------------------------------------------------------------- diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js index 7ea3fff..279aa06 100644 --- a/cordova-lib/src/cordova/util.js +++ b/cordova-lib/src/cordova/util.js @@ -16,6 +16,9 @@ specific language governing permissions and limitations under the License. */ + +/* jshint node:true, laxcomma:true, strict:false, trailing:true, unused:vars */ + var fs = require('fs'), path = require('path'), CordovaError = require('../CordovaError'), @@ -27,6 +30,23 @@ var global_config_path = path.join(HOME, '.cordova'); var lib_path = path.join(global_config_path, 'lib'); shell.mkdir('-p', lib_path); +exports.binname = 'cordova'; +exports.globalConfig = global_config_path; +exports.libDirectory = lib_path; +addModuleProperty(module, 'plugin_parser', './plugin_parser'); + +exports.isCordova = isCordova; +exports.cdProjectRoot = cdProjectRoot; +exports.deleteSvnFolders = deleteSvnFolders; +exports.listPlatforms = listPlatforms; +exports.findPlugins = findPlugins; +exports.appDir = appDir; +exports.projectWww = projectWww; +exports.projectConfig = projectConfig; +exports.preProcessOptions = preProcessOptions; +exports.addModuleProperty = addModuleProperty; + + function isRootDir(dir) { if (fs.existsSync(path.join(dir, 'www'))) { if (fs.existsSync(path.join(dir, 'config.xml'))) { @@ -45,138 +65,141 @@ function isRootDir(dir) { return 0; } -exports = module.exports = { - binname: 'cordova', - globalConfig:global_config_path, - libDirectory:lib_path, - // Runs up the directory chain looking for a .cordova directory. - // IF it is found we are in a Cordova project. - // Omit argument to use CWD. - isCordova: function isCordova(dir) { - if (!dir) { - // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly (CB-5687). - var pwd = process.env.PWD; - var cwd = process.cwd(); - if (pwd && pwd != cwd) { - return this.isCordova(pwd) || this.isCordova(cwd); - } - return this.isCordova(cwd); +// Runs up the directory chain looking for a .cordova directory. +// IF it is found we are in a Cordova project. +// Omit argument to use CWD. +function isCordova(dir) { + if (!dir) { + // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly (CB-5687). + var pwd = process.env.PWD; + var cwd = process.cwd(); + if (pwd && pwd != cwd) { + return this.isCordova(pwd) || this.isCordova(cwd); } - var bestReturnValueSoFar = false; - for (var i = 0; i < 1000; ++i) { - var result = isRootDir(dir); - if (result === 2) { - return dir; - } - if (result === 1) { - bestReturnValueSoFar = dir; - } - var parentDir = path.normalize(path.join(dir, '..')); - // Detect fs root. - if (parentDir == dir) { - return bestReturnValueSoFar; - } - dir = parentDir; + return this.isCordova(cwd); + } + var bestReturnValueSoFar = false; + for (var i = 0; i < 1000; ++i) { + var result = isRootDir(dir); + if (result === 2) { + return dir; } - console.error('Hit an unhandled case in util.isCordova'); - return false; - }, - // Cd to project root dir and return its path. Throw CordovaError if not in a Corodva project. - cdProjectRoot: function() { - var projectRoot = this.isCordova(); - if (!projectRoot) { - throw new CordovaError('Current working directory is not a Cordova-based project.'); + if (result === 1) { + bestReturnValueSoFar = dir; } - process.chdir(projectRoot); - return projectRoot; - }, - // Recursively deletes .svn folders from a target path - deleteSvnFolders:function(dir) { - var contents = fs.readdirSync(dir); - contents.forEach(function(entry) { - var fullpath = path.join(dir, entry); - if (fs.statSync(fullpath).isDirectory()) { - if (entry == '.svn') { - shell.rm('-rf', fullpath); - } else module.exports.deleteSvnFolders(fullpath); - } - }); - }, - listPlatforms:function(project_dir) { - var core_platforms = require('./platforms'); - var platforms_dir = path.join(project_dir, 'platforms'); - if ( !fs.existsSync(platforms_dir)) { - return []; - } - var subdirs = fs.readdirSync(platforms_dir); - return subdirs.filter(function(p) { - return Object.keys(core_platforms).indexOf(p) > -1; - }); - }, - // list the directories in the path, ignoring any files - findPlugins:function(pluginPath) { - var plugins = [], - stats; - - if (fs.existsSync(pluginPath)) { - plugins = fs.readdirSync(pluginPath).filter(function (fileName) { - stats = fs.statSync(path.join(pluginPath, fileName)); - return fileName != '.svn' && fileName != 'CVS' && stats.isDirectory(); - }); + var parentDir = path.normalize(path.join(dir, '..')); + // Detect fs root. + if (parentDir == dir) { + return bestReturnValueSoFar; } + dir = parentDir; + } + console.error('Hit an unhandled case in util.isCordova'); + return false; +} + +// Cd to project root dir and return its path. Throw CordovaError if not in a Corodva project. +function cdProjectRoot() { + var projectRoot = this.isCordova(); + if (!projectRoot) { + throw new CordovaError('Current working directory is not a Cordova-based project.'); + } + process.chdir(projectRoot); + return projectRoot; +} - return plugins; - }, - appDir: function(projectDir) { - return projectDir; - }, - projectWww: function(projectDir) { - return path.join(projectDir, 'www'); - }, - projectConfig: function(projectDir) { - var rootPath = path.join(projectDir, 'config.xml'); - var wwwPath = path.join(projectDir, 'www', 'config.xml'); - if (fs.existsSync(rootPath)) { - return rootPath; - } else if (fs.existsSync(wwwPath)) { - return wwwPath; +// Recursively deletes .svn folders from a target path +function deleteSvnFolders(dir) { + var contents = fs.readdirSync(dir); + contents.forEach(function(entry) { + var fullpath = path.join(dir, entry); + if (fs.statSync(fullpath).isDirectory()) { + if (entry == '.svn') { + shell.rm('-rf', fullpath); + } else module.exports.deleteSvnFolders(fullpath); } + }); +} + +function listPlatforms(project_dir) { + var core_platforms = require('./platforms'); + var platforms_dir = path.join(project_dir, 'platforms'); + if ( !fs.existsSync(platforms_dir)) { + return []; + } + var subdirs = fs.readdirSync(platforms_dir); + return subdirs.filter(function(p) { + return Object.keys(core_platforms).indexOf(p) > -1; + }); +} + +// list the directories in the path, ignoring any files +function findPlugins(pluginPath) { + var plugins = [], + stats; + + if (fs.existsSync(pluginPath)) { + plugins = fs.readdirSync(pluginPath).filter(function (fileName) { + stats = fs.statSync(path.join(pluginPath, fileName)); + return fileName != '.svn' && fileName != 'CVS' && stats.isDirectory(); + }); + } + + return plugins; +} + +function appDir(projectDir) { + return projectDir; +} + +function projectWww(projectDir) { + return path.join(projectDir, 'www'); +} + +function projectConfig(projectDir) { + var rootPath = path.join(projectDir, 'config.xml'); + var wwwPath = path.join(projectDir, 'www', 'config.xml'); + if (fs.existsSync(rootPath)) { return rootPath; - }, - preProcessOptions: function (inputOptions) { - /** - * Current Desired Arguments - * options: {verbose: boolean, platforms: [String], options: [String]} - * Accepted Arguments - * platformList: [String] -- assume just a list of platforms - * platform: String -- assume this is a platform - */ - var result = inputOptions || {}; - if (Array.isArray(inputOptions)) { - result = { platforms: inputOptions }; - } else if (typeof inputOptions === 'string') { - result = { platforms: [inputOptions] }; - } - result.verbose = result.verbose || false; - result.platforms = result.platforms || []; - result.options = result.options || []; + } else if (fs.existsSync(wwwPath)) { + return wwwPath; + } + return rootPath; +} - var projectRoot = this.isCordova(); +function preProcessOptions (inputOptions) { + /** + * Current Desired Arguments + * options: {verbose: boolean, platforms: [String], options: [String]} + * Accepted Arguments + * platformList: [String] -- assume just a list of platforms + * platform: String -- assume this is a platform + */ + var result = inputOptions || {}; + if (Array.isArray(inputOptions)) { + result = { platforms: inputOptions }; + } else if (typeof inputOptions === 'string') { + result = { platforms: [inputOptions] }; + } + result.verbose = result.verbose || false; + result.platforms = result.platforms || []; + result.options = result.options || []; - if (!projectRoot) { - throw new CordovaError('Current working directory is not a Cordova-based project.'); - } - var projectPlatforms = this.listPlatforms(projectRoot); - if (projectPlatforms.length === 0) { - throw new CordovaError('No platforms added to this project. Please use `'+exports.binname+' platform add <platform>`.'); - } - if (result.platforms.length === 0) { - result.platforms = projectPlatforms; - } + var projectRoot = this.isCordova(); - return result; + if (!projectRoot) { + throw new CordovaError('Current working directory is not a Cordova-based project.'); + } + var projectPlatforms = this.listPlatforms(projectRoot); + if (projectPlatforms.length === 0) { + throw new CordovaError('No platforms added to this project. Please use `'+exports.binname+' platform add <platform>`.'); } -}; + if (result.platforms.length === 0) { + result.platforms = projectPlatforms; + } + + return result; +} // opt_wrap is a boolean: True means that a callback-based wrapper for the promise-based function // should be created. @@ -189,14 +212,14 @@ function addModuleProperty(module, symbol, modulePath, opt_wrap, opt_obj) { // If args exist and the last one is a function, it's the callback. var args = Array.prototype.slice.call(arguments); var cb = args.pop(); - val.apply(module.exports, args).done(function(result) {cb(undefined, result)}, cb); + val.apply(module.exports, args).done(function(result) { cb(undefined, result); }, cb); } else { val.apply(module.exports, arguments).done(null, function(err) { throw err; }); } }; } else { Object.defineProperty(opt_obj || module.exports, symbol, { - get : function() { return val = val || module.require(modulePath); }, + get : function() { val = val || module.require(modulePath); return val; }, set : function(v) { val = v; } }); } @@ -204,12 +227,9 @@ function addModuleProperty(module, symbol, modulePath, opt_wrap, opt_obj) { // Add the module.raw.foo as well. if(module.exports.raw) { Object.defineProperty(module.exports.raw, symbol, { - get : function() { return val = val || module.require(modulePath); }, + get : function() { val = val || module.require(modulePath); return val; }, set : function(v) { val = v; } }); } } -addModuleProperty(module, 'plugin_parser', './plugin_parser'); - -exports.addModuleProperty = addModuleProperty;
