Permalink
Browse files

Switch argv parsing module to minimist

Fixes #258
  • Loading branch information...
1 parent aaa1d40 commit c4e53c1388d28d6ee42e712bce6bc8fcea175ba8 @jnordberg committed Oct 23, 2015
Showing with 96 additions and 80 deletions.
  1. +1 −1 bin/dev/cli
  2. +1 −1 bin/wintersmith
  3. +1 −1 package.json
  4. +8 −8 src/cli/build.coffee
  5. +34 −24 src/cli/common.coffee
  6. +26 −22 src/cli/index.coffee
  7. +8 −6 src/cli/new.coffee
  8. +8 −8 src/cli/plugin.coffee
  9. +8 −9 src/cli/preview.coffee
  10. +1 −0 src/core/utils.coffee
View
@@ -1,4 +1,4 @@
#!/usr/bin/env node
require('coffee-script/register');
-require('../../src/cli/').main();
+require('../../src/cli/').main(process.argv);
View
@@ -1,3 +1,3 @@
#!/usr/bin/env node
-require('../lib/cli/').main();
+require('../lib/cli/').main(process.argv);
View
@@ -35,10 +35,10 @@
"marked": "~0.3.3",
"mime": "~1.3.4",
"minimatch": "~2.0.4",
+ "minimist": "^1.2.0",
"mkdirp": "~0.3.5",
"ncp": "~2.0.0",
"npm": "~2.8.4",
- "optimist": "~0.6.0",
"rimraf": "~2.3.2",
"server-destroy": "~1.0.0",
"slugg": "~0.1.2",
View
@@ -4,8 +4,8 @@ fs = require 'fs'
path = require 'path'
rimraf = require 'rimraf'
-{extend, fileExistsSync} = require './../core/utils'
-{loadEnv, commonOptions, commonUsage} = require './common'
+{fileExistsSync} = require './../core/utils'
+{loadEnv, commonOptions, commonUsage, extendOptions} = require './common'
{logger} = require './../core/logger'
usage = """
@@ -34,13 +34,13 @@ usage = """
"""
options =
- output:
- alias: 'o'
- clean:
- alias: 'X'
- default: false
+ alias:
+ output: 'o'
+ clean: 'X'
+ boolean: ['clean']
+ string: ['output']
-extend options, commonOptions
+extendOptions options, commonOptions
build = (argv) ->
start = new Date()
View
@@ -8,36 +8,42 @@ stream = require 'stream'
{readJSON, fileExists} = require './../core/utils'
exports.commonOptions = defaults =
- config:
- alias: 'c'
- default: './config.json'
- chdir:
- alias: 'C'
- default: null
- contents:
- alias: 'i'
- templates:
- alias: 't'
- locals:
- alias: 'L'
- require:
- alias: 'R'
- plugins:
- alias: 'P'
- ignore:
- alias: 'I'
+ string: ['chdir', 'config', 'contents', 'templates', 'locals', 'require', 'plugins', 'ignore']
+ default:
+ config: './config.json'
+ chdir: null
+ alias:
+ config: 'c'
+ chdir: 'C'
+ contents: 'i'
+ templates: 't'
+ locals: 'L'
+ require: 'R'
+ plugins: 'P'
+ ignore: 'I'
exports.commonUsage = [
"-C, --chdir [path] change the working directory"
- " -c, --config [path] path to config (defaults to #{ defaults.config.default })"
- " -i, --contents [path] contents location (defaults to #{ defaults.contents.default })"
- " -t, --templates [path] template location (defaults to #{ defaults.templates.default })"
+ " -c, --config [path] path to config (defaults to #{ defaults.default.config })"
+ " -i, --contents [path] contents location (defaults to #{ Config.defaults.contents })"
+ " -t, --templates [path] template location (defaults to #{ Config.defaults.templates })"
" -L, --locals [path] optional path to json file containing template context data"
" -R, --require comma separated list of modules to add to the template context"
" -P, --plugins comma separated list of modules to load as plugins"
" -I, --ignore comma separated list of files/glob-patterns to ignore"
].join '\n'
+exports.extendOptions = (base, extra) ->
+ for type in ['string', 'boolean']
+ base[type] ?= []
+ if extra[type]?
+ base[type] = base[type].concat extra[type]
+ for type in ['alias', 'default']
+ base[type] ?= {}
+ if extra[type]?
+ base[type][key] = value for key, value of extra[type]
+ return
+
exports.loadEnv = (argv, callback) ->
### creates a new wintersmith environment
options are resolved with the hierarchy: argv > configfile > defaults ###
@@ -60,12 +66,16 @@ exports.loadEnv = (argv, callback) ->
(config, callback) ->
# ovveride config options with any command line options
- config._cliopts = {} # used to restore command line overrides when server is restarted
+ config._cliopts = {} # used to restore command line overrides when
+ # preview server is restarted and config is reloaded
for key, value of argv
- # don't include optimist stuff and cli-specific options
+ # don't include cli-specific options
excluded = ['_', 'chdir', 'config', 'clean']
- if key[0] is '$' or key.length is 1 or key in excluded
+ if key in excluded
continue
+ if key in ['port']
+ # numeric values
+ value = Number value
if key in ['ignore', 'require', 'plugins']
# split comma separated values to arrays
value = value.split ','
View
@@ -1,8 +1,10 @@
chalk = require 'chalk'
-optimist = require 'optimist'
+parseArgv = require 'minimist'
path = require 'path'
{logger} = require './../core/logger'
+{extendOptions} = require './common'
+
usage = """
@@ -27,46 +29,48 @@ usage = """
"""
globalOptions =
- verbose:
- alias: 'v'
- quiet:
- alias: 'q'
- version:
- alias: 'V'
- help:
- alias: 'h'
-
-main = ->
-
- argv = optimist.options(globalOptions).argv
- if argv._[0]?
+ boolean: ['verbose', 'quiet', 'version', 'help']
+ alias:
+ verbose: 'v'
+ quiet: 'q'
+ version: 'V'
+ help: 'h'
+
+main = (argv) ->
+
+ opts = parseArgv argv, globalOptions
+ cmd = argv[2]
+
+ if cmd?
try
- cmd = require "./#{ argv._[0] }"
+ cmd = require "./#{ cmd }"
catch error
if error.code is 'MODULE_NOT_FOUND'
- console.log "'#{ argv._[0] }' - no such command"
+ console.log "'#{ cmd }' - no such command"
process.exit 1
else
throw error
- if argv.version
+ if opts.version
console.log require './version'
process.exit 0
- if argv.help or !cmd
+ if opts.help or !cmd
console.log if cmd then cmd.usage else usage
process.exit 0
- if argv.verbose
- if '-vv' in process.argv
+ if opts.verbose
+ if '-vv' in argv
logger.transports.cli.level = 'silly'
else
logger.transports.cli.level = 'verbose'
- if argv.quiet
+ if opts.quiet
logger.transports.cli.quiet = true
if cmd
- cmd optimist.options(globalOptions).options(cmd.options).argv
+ extendOptions cmd.options, globalOptions
+ opts = parseArgv argv, cmd.options
+ cmd opts
module.exports.main = main
View
@@ -40,16 +40,18 @@ usage = """
"""
options =
- force:
- alias: 'f'
- template:
- alias: 'T'
- default: 'blog'
+ string: ['template']
+ boolean: ['force']
+ alias:
+ force: 'f'
+ template: 'T'
+ default:
+ template: 'blog'
createSite = (argv) ->
### copy example directory to *location* ###
- location = argv._[1]
+ location = argv._[3]
if !location? or !location.length
logger.error 'you must specify a location'
return
View
@@ -6,8 +6,8 @@ npm = require 'npm'
mkdirp = require 'mkdirp'
childProcess = require 'child_process'
-{NpmAdapter, getStorageDir, loadEnv, commonOptions} = require './common'
-{fileExists, readJSON, extend} = require './../core/utils'
+{NpmAdapter, getStorageDir, loadEnv, commonOptions, extendOptions} = require './common'
+{fileExists, readJSON} = require './../core/utils'
{logger} = require './../core/logger'
maxListAge = 3 * 24 * 60 * 60 * 1000 # 3 days, in ms
@@ -32,11 +32,11 @@ usage = """
"""
options =
- update:
- alias: 'U'
- default: false
+ alias:
+ update: 'U'
+ boolean: ['update']
-extend options, commonOptions
+extendOptions options, commonOptions
max = (array, get) ->
get ?= (item) -> item
@@ -139,7 +139,7 @@ normalizePluginName = (name) ->
name.replace /^wintersmith\-/, ''
main = (argv) ->
- action = argv._[1]
+ action = argv._[3]
if not action?
console.log usage
@@ -151,7 +151,7 @@ main = (argv) ->
installPlugin = (res, callback) ->
[env, list] = res
- name = argv._[2]
+ name = argv._[4]
plugin = null
for p in list.plugins
@@ -1,20 +1,19 @@
async = require 'async'
util = require 'util'
-{extend} = require './../core/utils'
{Config} = require './../core/config'
{logger} = require './../core/logger'
-{loadEnv, commonUsage, commonOptions} = require './common'
+{loadEnv, commonUsage, commonOptions, extendOptions} = require './common'
usage = """
usage: wintersmith preview [options]
options:
- -p, --port [port] port to run server on (defaults to 8080)
- -d, --domain [domain] host to run server on (defaults to localhost)
+ -p, --port [port] port to run server on (defaults to #{ Config.defaults.port })
+ -H, --hostname [host] host to bind server onto (defaults to INADDR_ANY)
#{ commonUsage }
all options can also be set in the config file
@@ -27,12 +26,12 @@ usage = """
"""
options =
- port:
- alias: 'p'
- domain:
- alias: 'd'
+ string: ['port', 'hostname']
+ alias:
+ port: 'p'
+ hostname: 'H'
-extend options, commonOptions
+extendOptions options, commonOptions
preview = (argv) ->
logger.info 'starting preview server'
@@ -11,6 +11,7 @@ fileExistsSync = fs.existsSync or path.existsSync
extend = (obj, mixin) ->
for name, method of mixin
obj[name] = method
+ return
stripExtension = (filename) ->
### Remove the file-extension from *filename* ###

0 comments on commit c4e53c1

Please sign in to comment.