Permalink
Browse files

extracted args-parse.js to ender-args-parser lib

  • Loading branch information...
rvagg committed Dec 27, 2012
1 parent ae728d8 commit 10d060f9a192d9c789da9c0ba044513486e920bb
View
@@ -1,203 +0,0 @@
-/*!
- * ENDER - The open module JavaScript framework
- *
- * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is furnished
- * to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-
-/******************************************************************************
- * Our own argument-parser. It's unfortunate that we have to do this but
- * the argument structure of Ender rules out most other argument parser
- * modules. Specifically the fact that --sandbox can have multiple arguments
- * to it, normally you'd only have one per --/-.
- * See the tests for this for a more comprehensive treatment of what we expect
- * from this code.
- */
-
-var UnknownMainError = require('./errors').UnknownMainError
- , UnknownOptionError = require('./errors').UnknownOptionError
-
- // the default array option to collect arguments for ('packages'), so all non
- // --/- prefixed arguments will be sent to this list until we receive a --/-
- // that is an 'Array' type.
- // e.g. `ender build foo bar baz`, our 'main' is 'build', 'foo bar baz' are
- // all collected into 'packages'. Whereas `ender build foo bar --sandbox foo baz`
- // we get 'foo bar' in packages and 'foo baz' in 'sandbox'.
- , defaultArray
-
- // available options
- , options = (function () {
- var arr = []
- , add = function (name, short, type, altMain) {
- var o
-
- if (typeof short != 'string') {
- altMain = type
- type = short
- short = null
- }
-
- o = {
- name : name
- , short : short
- , type : type
- , altMain : altMain
- }
-
- if (short === '' && type === Array) defaultArray = name
- arr.push(o)
- }
-
- // --foo -f [type] [alternate main cmd]
- add('packages' , '' , Array)
- add('output' , 'o' , String)
- add('use' , 'u' , String)
- add('max' , Number)
- add('sandbox' , Array)
- add('noop' , 'x' , Boolean)
- add('silent' , 's' , Boolean)
- add('help' , 'h' , Boolean, true)
- add('sans' , Boolean)
- add('debug' , Boolean)
- add('version' , 'v' , Boolean, true)
- add('externs' , Array)
- add('client-lib' , String)
- add('level' , String)
- add('quiet' , Boolean)
- add('force-install' , Boolean)
- add('minifier' , String)
-
- return arr
- }())
-
- // allowable first-arguments that resolve to our main-X.js modules.
- , mains = {
- 'help' : 'help'
- , 'build' : 'build'
- , 'add' : 'add'
- , 'set' : 'add'
- , 'refresh' : 'refresh'
- , 'remove' : 'remove'
- , 'rm' : 'remove'
- , 'info' : 'info'
- , 'ls' : 'info'
- , 'list' : 'info'
- , 'search' : 'search'
- , 'compile' : 'compile'
- , 'version' : 'version'
- }
-
- , findOption = function (s) {
- var i
- , option
- , match = s.match(/^(--?)([a-z\-]+)$/)
-
- if (!match) return
-
- for (i = 0; i < options.length; i++) {
- var hasOption
-
- option = options[i]
- hasOption = (match[1] === '--' && match[2] === option.name)
- || (match[1] === '-' && option.short && match[2] === option.short)
-
- if (hasOption) return option
- }
- throw new UnknownOptionError('Unknown option "' + s + '"')
- }
-
- // reverse a parse! Turn an `options` object into a parsable commandline string
- // mainly so we can include it in the 'Build:' string in the source header but
- // it has other uses.
- , toContextString = function (options) {
- var str = options.main
- , p
-
- if (options.packages.length) str += ' ' + options.packages.join(' ')
-
- for (p in options) {
- if (p === 'packages' || p === 'main') continue
- str += ' --' + p
- if (Array.isArray(options[p])) str += ' ' + options[p].join(' ')
- else if (typeof options[p] !== 'boolean') str += ' ' + options[p]
- }
- return str
- }
-
- , parse = function (argv, slice, options) {
- var args = Array.prototype.slice.call(argv, slice) // slice might be 0 for clean or 2 for raw
- , options = options || {}
- , currentArray
- , arg
- , o
- , setCurrentArray = function (name) {
- currentArray = name
- options[currentArray] = options[currentArray]
- ? Array.prototype.slice.call(options[currentArray], 0) // clone
- : []
- }
-
- setCurrentArray(defaultArray)
-
- while (arg = args.shift()) {
- o = findOption(arg)
- if (o) {
- currentArray = defaultArray
-
- if (o.type === Boolean) {
- options[o.name] = true
- if (o.altMain && !options.main) options.main = o.name
- } else if ((o.type === String || o.type === Number) && args.length) {
- // converstion through type constructor
- options[o.name] = o.type(args.shift())
- } else if (o.type === Array) {
- setCurrentArray(o.name)
- }
-
- } else if (!options.main) {
- options.main = arg
- } else {
- options[currentArray].push(arg)
- }
- }
-
- if (!options.main) {
- if (Object.keys(options).length > 1) throw new UnknownMainError('No main command supplied')
- options.main = 'help'
- }
-
- if (!mains[options.main]) throw new UnknownMainError('Unknown main command "' + options.main + '"')
- options.main = mains[options.main]
-
- return options
- }
-
- // merge two options objects together, for `add` and `remove`.
- , extend = function (originalArgs, newArgs) {
- return parse(toContextString(newArgs).split(' '), 1, originalArgs)
- }
-
-module.exports = {
- parse : function (argv) { return parse(argv, 2) } // with 2 additional args 'node script.js'
- , parseClean : function (argv) { return parse(argv, 0) } // without
- , extend : extend
- , toContextString : toContextString
-}
View
@@ -32,8 +32,8 @@
* module which does all the hard work.
*/
-var util = require('./util')
- , argsParse = require('./args-parse')
+var argsParser = require('ender-args-parser')
+ , util = require('./util')
, parseContext = require('./parse-context')
, mainBuild = require('./main-build')
@@ -43,7 +43,7 @@ var util = require('./util')
parseContext(filename, function (err, context) {
if (err) return callback(err) // err wrapped in SourceBuild.parseContext()
// merge commandline args with the build command in ender.js
- options = argsParse.extend(context.options, options)
+ options = argsParser.extend(context.options, options)
mainBuild.exec(options, out, callback)
})
}
View
@@ -35,9 +35,9 @@
var async = require('async')
, repository = require('ender-repository')
+ , argsParser = require('ender-args-parser')
, util = require('./util')
, mainBuild = require('./main-build')
- , argsParse = require('./args-parse')
, mainBuildUtil = require('./main-build-util')
, parseContext = require('./parse-context')
@@ -52,13 +52,13 @@ var async = require('async')
}
; delete options.use // don't want --use showing up in the 'Build:' context string
- options.packages = [] // reset the packages list so argsParse.extend() doesn't include them
+ options.packages = [] // reset the packages list so argsParser.extend() doesn't include them
parseContext(filename, function (err, context) {
if (err) return callback(err) // wrapped in source-build.js
// merge the commandline with the ender.js build command
- options = argsParse.extend(context.options, options)
+ options = argsParser.extend(context.options, options)
options.packages = options.packages.filter(function (p) {
return packages.indexOf(p) == -1 // do the remove!
})
View
@@ -42,9 +42,8 @@
process.title = 'Ender'
var sysUtil = require('util')
+ , argsParser = require('ender-args-parser')
, Output = require('./output/output')
- , argsParse = require('./args-parse')
- , EnderError = require('./errors').EnderError
// basic error handler, differentiates between 'known' EnderErrors and everything else
, complete = function (out, callback, err) {
@@ -66,15 +65,15 @@ var sysUtil = require('util')
}
try {
- options = argsParse[parseType](argv)
+ options = argsParser[parseType](argv)
// get the module to execute and it's partner output module
exe = options && require('./main-' + options.main)
out = options && require('./output/main-' + options.main + '-output').create(sysUtil, options.debug, options.quiet)
if (exe && out) {
exe.exec(options, out, complete.bind(null, out, callback))
- } // else err? argsParse should take care of this if it's list of mains corresponds to the modules we have
+ } // else err? argsParser should take care of this if it's list of mains corresponds to the modules we have
} catch (ex) {
// create a generic/base 'out' module which can do the error printing
out = Output.create(sysUtil, argv.indexOf('--debug') != -1)
@@ -23,12 +23,12 @@
*/
-var archy = require('archy')
- , colors = require('colors')
- , extend = require('../util').extend
- , toKb = require('../util').toKb
- , Output = require('./output')
- , argsParse = require('../args-parse')
+var archy = require('archy')
+ , colors = require('colors')
+ , argsParser = require('ender-args-parser')
+ , extend = require('../util').extend
+ , toKb = require('../util').toKb
+ , Output = require('./output')
, InfoOutput = extend(Output, { // inherit from Output
@@ -45,7 +45,7 @@ var archy = require('archy')
prepareTree(archyTree)
//this.log('Your current build type is ' + ('"' + options.main + '"').yellow)
- this.log('Your current build command is: ' + ('ender ' + argsParse.toContextString(options)).yellow)
+ this.log('Your current build command is: ' + ('ender ' + argsParser.toContextString(options)).yellow)
this.log(
'Your current build size is: '
+ toKb(sizes.raw).yellow + ' raw'
View
@@ -25,11 +25,11 @@
/******************************************************************************
* a utility to partially read an ender build file and parse the head comment
* to pull out the 'Build:' and 'Packages:' lines. Returns the build command as a properly
- * parsed options object (via argsParse).
+ * parsed options object (via argsParser).
*/
const fs = require('fs')
- , argsParse = require('./args-parse')
+ , argsParser = require('ender-args-parser')
, FilesystemError = require('./errors').FilesystemError
, BuildParseError = require('./errors').BuildParseError
@@ -56,7 +56,7 @@ var parseContext = function (file, callback) {
}
try {
- options = argsParse.parseClean(match[1].split(' '))
+ options = argsParser.parseClean(match[1].split(' '))
} catch (ex) {
error = 'Could not parse ender spec from "' + file + '"'
return callback(new BuildParseError(error, ex))
View
@@ -29,11 +29,11 @@
* on the list of SourcePackages.
*/
-var async = require('async')
- , util = require('./util')
- , minify = require('./minify')
- , template = require('./template')
- , argsParse = require('./args-parse')
+var async = require('async')
+ , argsParser = require('ender-args-parser')
+ , util = require('./util')
+ , minify = require('./minify')
+ , template = require('./template')
, templateFile = '../resources/build.mustache'
@@ -59,7 +59,7 @@ var async = require('async')
, tmplData = function (sources) {
return {
source: sources.join('\n\n')
- , context: argsParse.toContextString(this.options)
+ , context: argsParser.toContextString(this.options)
, sandbox: !!this.options.sandbox
, packages: this.packages.map(function (p) {
return p.identifier
View
@@ -1,7 +1,7 @@
{
"name": "ender"
, "description": "Open Module JavaScript Framework"
- , "version": "0.9.9-dev"
+ , "version": "0.9.10-dev"
, "authors": [
"Jacob Thornton @fat <jacob@twitter.com> (https://github.com/fat)"
, "Dustin Diaz @ded <dustin@obvious.com> (https://github.com/ded)"
@@ -15,6 +15,8 @@
, "ender-minify" : "0.1.0"
, "ender-dependency-graph"
: "0.0.1"
+ , "ender-args-parser"
+ : "0.0.1"
, "colors" : "~0.6.0"
, "async" : "~0.1.22"
, "hogan.js" : "~2.0.0"
Oops, something went wrong.

0 comments on commit 10d060f

Please sign in to comment.