split out cordova subcommands build and platform into separate files
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/commit/fe7656c7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/tree/fe7656c7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/diff/fe7656c7 Branch: refs/heads/cordova-client Commit: fe7656c70f6c69cf7c6d4023ae26ecb571dc879f Parents: 87f04f5 Author: Fil Maj <maj....@gmail.com> Authored: Tue Jul 17 13:40:22 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Tue Jul 17 13:40:22 2012 -0700 ---------------------------------------------------------------------- README.md | 4 +- cordova.js | 116 ++++++++++---------------------------------- platforms.js | 1 + src/build.js | 38 +++++++++++++++ src/config_parser.js | 2 +- src/platform.js | 36 ++++++++++++++ src/platforms.js | 1 - src/util.js | 21 ++++++++ 8 files changed, 126 insertions(+), 93 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/README.md ---------------------------------------------------------------------- diff --git a/README.md b/README.md index 4282f4e..c64f8cb 100644 --- a/README.md +++ b/README.md @@ -77,10 +77,11 @@ project. #### Removing A Platform + $ cordova platform remove [platform] + Removes the platform as a build target from the current Cordova-based project. - ### Building Your Project $ cordova build [platform] @@ -95,7 +96,6 @@ Will compile and launch your app on all platforms added to your Cordova project. You can optionally specify a specific platform to launch for. - # Examples ## Creating a sample project http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/cordova.js ---------------------------------------------------------------------- diff --git a/cordova.js b/cordova.js index 87556c6..bd68252 100755 --- a/cordova.js +++ b/cordova.js @@ -7,50 +7,33 @@ var fs = require('fs') , wrench = require('wrench') , config_parser = require('./src/config_parser') -// Runs up the directory chain looking for a .cordova directory. -// IF it is found we are in a Cordova project. -// If not.. we're not. -function isCordova(dir) { - if (dir) { - var contents = fs.readdirSync(dir); - if (contents && contents.length && (contents.indexOf('.cordova') > -1)) { - return dir; - } else { - var parent = path.join(dir, '..'); - if (parent && parent.length > 1) { - return isCordova(parent); - } else return false; - } - } else return false; -} module.exports = { help: function help () { - var raw = fs.readFileSync(path.join(__dirname, 'doc', 'help.txt')).toString('utf8').split("\n") + var raw = fs.readFileSync(path.join(__dirname, 'doc', 'help.txt')).toString('utf8').split("\n"); console.log(raw.map(function(line) { if (line.match(' ')) { var prompt = ' $ ' - , isPromptLine = !!(line.indexOf(prompt) != -1) + , isPromptLine = !!(line.indexOf(prompt) != -1); if (isPromptLine) { - return prompt.green + line.replace(prompt, '') + return prompt.green + line.replace(prompt, ''); } else { return line.split(/\./g).map( function(char) { if (char === '') { - return '.'.grey + return '.'.grey; } else { - return char + return char; } - }).join('') + }).join(''); } } else { - return line.magenta + return line.magenta; } - }).join("\n")) - } - , + }).join("\n")); + }, docs: function docs () { var express = require('express') @@ -59,19 +42,18 @@ module.exports = { , server = express.createServer(); server.configure(function() { - server.use(express.static(static)) - server.use(express.errorHandler({ dumpExceptions: true, showStack: true })) - }) + server.use(express.static(static)); + server.use(express.errorHandler({ dumpExceptions: true, showStack: true })); + }); server.get('/', function(req, res) { - return res.render('index.html') - }) + return res.render('index.html'); + }); - console.log("\nServing Cordova/Docs at: ".grey + 'http://localhost:2222'.blue.underline + "\n") - console.log('Hit ctrl + c to terminate the process.'.cyan) - server.listen(parseInt(port, 10)) - } - , + console.log("\nServing Cordova/Docs at: ".grey + 'http://localhost:2222'.blue.underline + "\n"); + console.log('Hit ctrl + c to terminate the process.'.cyan); + server.listen(parseInt(port, 10)); + }, create: function create (dir) { var mkdirp = wrench.mkdirSyncRecursive, cpr = wrench.copyDirSyncRecursive; @@ -96,61 +78,17 @@ module.exports = { // Copy in base template cpr(path.join(__dirname, 'templates', 'www'), path.join(dir, 'www')); - } - , - platform: function platform(command, target) { - var projectRoot = isCordova(process.cwd()); - - if (!projectRoot) { - console.error('Current working directory is not a Cordova-based project.'); - return; - } - if (arguments.length === 0) command = 'ls'; - - var xml = path.join(projectRoot, 'www', 'config.xml'); - var cfg = new config_parser(xml); - - switch(command) { - case 'ls': - var platforms = cfg.ls_platforms(); - if (platforms.length) { - platforms.map(function(p) { - console.log(p); - }); - } else console.log('No platforms added. Use `cordova platforms add <platform>`.'); - break; - case 'add': - cfg.add_platform(target); - break; - case 'remove': - cfg.remove_platform(target); - break; - default: - console.error('Unrecognized command "' + command + '". Use either `add`, `remove`, or `ls`.'); - break; - } - } - , - build: function build () { - var cmd = util.format("%s/cordova/debug", process.cwd()) - exec(cmd, function(err, stderr, stdout) { - if (err) - console.error('An error occurred while building project.', err) - - console.log(stdout) - console.log(stderr) - }) - } - , + }, + platform:require('./src/platform'), + build:require('./src/build'), emulate: function emulate() { - var cmd = util.format("%s/cordova/emulate", process.cwd()) + var cmd = util.format("%s/cordova/emulate", process.cwd()); exec(cmd, function(err, stderr, stdout) { if (err) - console.error('An error occurred attempting to start emulator.', err) + console.error('An error occurred attempting to start emulator.', err); - console.log(stdout) - console.log(stderr) - }) + console.log(stdout); + console.log(stderr); + }); } - // end of module defn -} +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/platforms.js ---------------------------------------------------------------------- diff --git a/platforms.js b/platforms.js new file mode 100644 index 0000000..c6c5601 --- /dev/null +++ b/platforms.js @@ -0,0 +1 @@ +module.exports = ['ios','android']; http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/src/build.js ---------------------------------------------------------------------- diff --git a/src/build.js b/src/build.js new file mode 100644 index 0000000..40a3cc2 --- /dev/null +++ b/src/build.js @@ -0,0 +1,38 @@ +var cordova_util = require('./util'), + path = require('path'), + config_parser = require('./config_parser'), + fs = require('fs'), + util = require('util'); + +module.exports = function build () { + var projectRoot = cordova_util.isCordova(process.cwd()); + + if (!projectRoot) { + console.error('Current working directory is not a Cordova-based project.'); + return; + } + + var xml = path.join(projectRoot, 'www', 'config.xml'); + var cfg = new config_parser(xml); + var platforms = cfg.ls_platforms(); + + // Check if we have projects setup for each platform already. + platforms.map(function(platform) { + var dir = path.join(projectRoot, 'platforms', platform); + try { + fs.lstatSync(dir); + } catch(e) { + // Does not exist. + } + + }); + + var cmd = util.format("%s/cordova/debug", process.cwd()); + exec(cmd, function(err, stderr, stdout) { + if (err) + console.error('An error occurred while building project.', err) + + console.log(stdout) + console.log(stderr) + }); +} http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/src/config_parser.js ---------------------------------------------------------------------- diff --git a/src/config_parser.js b/src/config_parser.js index bfef967..1790172 100644 --- a/src/config_parser.js +++ b/src/config_parser.js @@ -1,5 +1,5 @@ var et = require('elementtree'), - platforms = require('./platforms'), + platforms = require('./../platforms'), fs = require('fs'); function config_parser(xmlPath) { http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/src/platform.js ---------------------------------------------------------------------- diff --git a/src/platform.js b/src/platform.js new file mode 100644 index 0000000..bc7254c --- /dev/null +++ b/src/platform.js @@ -0,0 +1,36 @@ +var config_parser = require('./config_parser'), + util = require('./util'), + path = require('path'); + +module.exports = function platform(command, target) { + var projectRoot = util.isCordova(process.cwd()); + + if (!projectRoot) { + console.error('Current working directory is not a Cordova-based project.'); + return; + } + if (arguments.length === 0) command = 'ls'; + + var xml = path.join(projectRoot, 'www', 'config.xml'); + var cfg = new config_parser(xml); + + switch(command) { + case 'ls': + var platforms = cfg.ls_platforms(); + if (platforms.length) { + platforms.map(function(p) { + console.log(p); + }); + } else console.log('No platforms added. Use `cordova platforms add <platform>`.'); + break; + case 'add': + cfg.add_platform(target); + break; + case 'remove': + cfg.remove_platform(target); + break; + default: + console.error('Unrecognized command "' + command + '". Use either `add`, `remove`, or `ls`.'); + break; + } +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/src/platforms.js ---------------------------------------------------------------------- diff --git a/src/platforms.js b/src/platforms.js deleted file mode 100644 index c6c5601..0000000 --- a/src/platforms.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = ['ios','android']; http://git-wip-us.apache.org/repos/asf/incubator-cordova-labs/blob/fe7656c7/src/util.js ---------------------------------------------------------------------- diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..2679450 --- /dev/null +++ b/src/util.js @@ -0,0 +1,21 @@ +var fs = require('fs'), + path = require('path'); + +module.exports = { + // Runs up the directory chain looking for a .cordova directory. + // IF it is found we are in a Cordova project. + // If not.. we're not. + isCordova: function isCordova(dir) { + if (dir) { + var contents = fs.readdirSync(dir); + if (contents && contents.length && (contents.indexOf('.cordova') > -1)) { + return dir; + } else { + var parent = path.join(dir, '..'); + if (parent && parent.length > 1) { + return isCordova(parent); + } else return false; + } + } else return false; + } +};