diff --git a/Alloy/alloy.js b/Alloy/alloy.js index 1dd6c929f..aff896667 100755 --- a/Alloy/alloy.js +++ b/Alloy/alloy.js @@ -3,119 +3,68 @@ * Copyright (c) 2012 by Appcelerator, Inc. All Rights Reserved. * See LICENSE for more information on licensing. */ -var program = require('commander'), - logger = require('./logger'), - os = require('os'), - U = require('./utils'), - colors = require('colors'), - _ = require('lodash'), - pkginfo = require('pkginfo')(module, 'version'), - path = require('path'), - fs = require('fs'), - CONST = require('./common/constants'); +const _ = require('lodash'); +const colors = require('colors'); +const CONST = require('./common/constants'); +const fs = require('fs'); +const logger = require('./logger'); +const path = require('path'); +const U = require('./utils'); +const pkg = require('../package.json'); // patch to remove the warning in node >=0.8 path.existsSync = fs.existsSync || path.existsSync; -// avoid io issues on Windows in nodejs 0.10.X: https://github.com/joyent/node/issues/3584 -if (process.env.ALLOY_TESTS && /^win/i.test(os.platform())) { - console.error = function(m) { - fs.writeSync(2, m); - }; - console.log = console.warn = console.info = function(m) { - fs.writeSync(1, m); - }; -} - -//////////////////////////////////// -////////// MAIN EXECUTION ////////// -//////////////////////////////////// - -// Process command line input -program - .version(module.exports.version, '-v, --version') - .description('Alloy command line') - .usage('COMMAND [ARGS] [OPTIONS]') - .option('-a, --app ', 'Test app folder for running "alloy test"') - .option('-A, --apply', 'Applies command changes [extract-i18n]') - .option('-b, --noBanner', 'Disable the banner') - .option('-c, --config ', 'Pass in compiler configuration') - .option('-f, --force', 'Force the command to execute') - .option('-l, --logLevel ', 'Log level (default: 3 [DEBUG])') - .option('-n, --no-colors', 'Turn off colors') - .option('-o, --outputPath ', 'Output path for generated code') - .option('-p, --project-dir ', 'Titanium project directory') - .option('-q, --platform ', 'Target mobile platform [android,ios]') - .option('-s, --spec ', 'test spec to use with "alloy test"') - .option('-w, --all', 'require flag for generate styles') - .option('-x, --column ', 'Column for source map query', 1) - .option('-y, --line ', 'Line for source map query', 1) - .option('-z, --source ', 'Source original file for source map query') - .option('--widgetname ', 'Widget name, used with generate command') - .option('--testapp ', 'Test app name to import, used with new command'); - -program.command('new'.blue + ' '.white) - .description(' create a new alloy project'.grey); - -program.command('compile'.blue + ' [dir]'.white) - .description(' compile into titanium source code'.grey); - -program.command('extract-i18n'.blue + ' '.white) - .description(' extracts i18n strings from the source code (js and tss files)'.grey); - -program.command('generate'.blue + ' '.white) - .description(' generate a new alloy type such as a controller'.grey); - -program.command('copy'.blue + ' '.white) - .description(' copy the controller, view, and style files from to '.grey); - -program.command('move'.blue + ' '.white) - .description(' move the controller, view, and style files from to '.grey); - -program.command('remove'.blue + ' '.white) - .description(' remove the controller, view, and style files at '.grey); - -program.parse(process.argv); - - -// Setup up logging output -Error.stackTraceLimit = Infinity; -logger.stripColors = program['colors'] === false; -logger.logLevel = program['logLevel'] || logger.TRACE; -if (program.config && program.config.indexOf('logLevel') !== -1) { - logger.logLevel = -1; -} - -if (!program.noBanner && program.args[0] !== 'info' && (program.config && program.config.indexOf('noBanner') === -1)) { - banner(); -} - -if (program.args.length === 0) { - var help = program.helpInformation(); - help = help.replace('Usage: alloy COMMAND [ARGS] [OPTIONS]', 'Usage: ' + 'alloy'.blue + ' COMMAND'.white + ' [ARGS] [OPTIONS]'.grey); - help = logger.stripColors ? colors.stripColors(help) : help; - console.log(help); - process.exit(0); -} +class Alloy { + + constructor({ program }) { + + this.program = program; + //TODO: Temporary fix until proper constructor parameters are configured + if ( _.isNil(program)) { + U.die('program is a required parameter '); + } + + // Setup up logging output + Error.stackTraceLimit = Infinity; + logger.stripColors = program['colors'] === false; + logger.logLevel = program['logLevel'] || logger.TRACE; + if (program.config && program.config.indexOf('logLevel') !== -1) { + logger.logLevel = -1; + } + + if (!program.noBanner && program.args[0] !== 'info' && (program.config && program.config.indexOf('noBanner') === -1)) { + this.banner(); + } + + if (program.args.length === 0) { + var help = program.helpInformation(); + help = help.replace('Usage: turbo COMMAND [ARGS] [OPTIONS]', 'Usage: ' + 'alloy'.blue + ' COMMAND'.white + ' [ARGS] [OPTIONS]'.grey); + help = logger.stripColors ? colors.stripColors(help) : help; + console.log(help); + process.exit(0); + } + + if (program.platform && !_.includes(CONST.PLATFORM_FOLDERS_ALLOY, program.platform)) { + U.die('Invalid platform "' + program.platform + '" specified, must be [' + CONST.PLATFORM_FOLDERS_ALLOY.join(',') + ']'); + } + + // Validate the given command + this.command = program.args[0]; + if (!_.includes(this.getCommands(), this.command)) { + U.die('Unknown command: ' + this.command.red); + } -if (program.platform && !_.includes(CONST.PLATFORM_FOLDERS_ALLOY, program.platform)) { - U.die('Invalid platform "' + program.platform + '" specified, must be [' + CONST.PLATFORM_FOLDERS_ALLOY.join(',') + ']'); -} + } -// Validate the given command -var command = program.args[0]; -if (!_.includes(getCommands(), command)) { - U.die('Unknown command: ' + command.red); -} + execute() { + // Launch command with given arguments and options + (require('./commands/' + this.command + '/index'))(this.program.args.slice(1), this.program); + } -// Launch command with given arguments and options -(require('./commands/' + command + '/index'))(program.args.slice(1), program); -/////////////////////////////// -////////// FUNCTIONS ////////// -/////////////////////////////// -function banner() { - var str = + banner() { + var str = ' .__ .__ \n' + '_____ | | | | ____ ___.__.\n' + '\\__ \\ | | | | / _ < | |\n' + @@ -123,20 +72,24 @@ function banner() { '(____ /____/____/\\____// ____|\n' + ' \\/ \\/'; - if (!program.dump) { - console.log(logger.stripColors ? str : str.blue); - var m = 'Alloy ' + module.exports.version + ' by Appcelerator. The MVC app framework for Titanium.\n'.white; - console.log(logger.stripColors ? colors.stripColors(m) : m); + if (!this.program.dump) { + console.log(logger.stripColors ? str : str.blue); + var m = 'Alloy ' + pkg.version + ' by Appcelerator. The MVC app framework for Titanium.\n'.white; + console.log(logger.stripColors ? colors.stripColors(m) : m); + } } -} -function getCommands() { - try { - var commandsPath = path.join(__dirname, 'commands'); - return _.filter(fs.readdirSync(commandsPath), function(file) { - return path.existsSync(path.join(commandsPath, file, 'index.js')); - }); - } catch (e) { - U.die('Error getting command list', e); + getCommands() { + try { + var commandsPath = path.join(__dirname, 'commands'); + return _.filter(fs.readdirSync(commandsPath), function(file) { + return path.existsSync(path.join(commandsPath, file, 'index.js')); + }); + } catch (e) { + U.die('Error getting command list', e); + } } + } + +module.exports = Alloy; \ No newline at end of file diff --git a/Alloy/cli.js b/Alloy/cli.js new file mode 100644 index 000000000..3eda69980 --- /dev/null +++ b/Alloy/cli.js @@ -0,0 +1,73 @@ +/** + * Alloy + * Copyright (c) 2012 by Appcelerator, Inc. All Rights Reserved. + * See LICENSE for more information on licensing. + */ +const program = require('commander'); +const path = require('path'); +const fs = require('fs'); +const colors = require('colors'); + +// patch to remove the warning in node >=0.8 +path.existsSync = fs.existsSync || path.existsSync; + +// avoid io issues on Windows in nodejs 0.10.X: https://github.com/joyent/node/issues/3584 +if (process.env.ALLOY_TESTS && /^win/i.test(os.platform())) { + console.error = function(m) { + fs.writeSync(2, m); + }; + console.log = console.warn = console.info = function(m) { + fs.writeSync(1, m); + }; +} + +// Process command line input +program + .version(module.exports.version, '-v, --version') + .description('Alloy command line') + .usage('COMMAND [ARGS] [OPTIONS]') + .option('-a, --app ', 'Test app folder for running "alloy test"') + .option('-A, --apply', 'Applies command changes [extract-i18n]') + .option('-b, --noBanner', 'Disable the banner') + .option('-c, --config ', 'Pass in compiler configuration') + .option('-f, --force', 'Force the command to execute') + .option('-l, --logLevel ', 'Log level (default: 3 [DEBUG])') + .option('-n, --no-colors', 'Turn off colors') + .option('-o, --outputPath ', 'Output path for generated code') + .option('-p, --project-dir ', 'Titanium project directory') + .option('-q, --platform ', 'Target mobile platform [android,ios]') + .option('-s, --spec ', 'test spec to use with "alloy test"') + .option('-w, --all', 'require flag for generate styles') + .option('-x, --column ', 'Column for source map query', 1) + .option('-y, --line ', 'Line for source map query', 1) + .option('-z, --source ', 'Source original file for source map query') + .option('--widgetname ', 'Widget name, used with generate command') + .option('--testapp ', 'Test app name to import, used with new command'); + +program.command('new'.blue + ' '.white) + .description(' create a new alloy project'.grey); + +program.command('compile'.blue + ' [dir]'.white) + .description(' compile into titanium source code'.grey); + +program.command('extract-i18n'.blue + ' '.white) + .description(' extracts i18n strings from the source code (js and tss files)'.grey); + +program.command('generate'.blue + ' '.white) + .description(' generate a new alloy type such as a controller'.grey); + +program.command('copy'.blue + ' '.white) + .description(' copy the controller, view, and style files from to '.grey); + +program.command('move'.blue + ' '.white) + .description(' move the controller, view, and style files from to '.grey); + +program.command('remove'.blue + ' '.white) + .description(' remove the controller, view, and style files at '.grey); + +program.parse(process.argv); + + +const Alloy = require('./alloy'); +const alloy = new Alloy({program}); +alloy.execute(); \ No newline at end of file diff --git a/bin/alloy b/bin/alloy index 039236b4c..a9480f488 100755 --- a/bin/alloy +++ b/bin/alloy @@ -1,3 +1,3 @@ #!/usr/bin/env node -require('../Alloy/alloy'); +require('../Alloy/cli');