<commit_before>_ = require('underscore')
yaml = require('js-yaml')
colors = require('colors')
fs = require('fs')
commander = require('commander')

proxy = require('./proxy')

NumberList = (str) ->
  str.split(',').map(Number)

DEFAULTS =
  port: 80
  httpsPort: 443

commander
  .usage("Run it in the root of your project to start proxying requests as defined in the project's .vee file")
  .option('-p, --port [80]', "Port to serve http requests from.  Comma seperate to bind onto multiple ports.", NumberList)
  .option('-s, --https-port [443]', "Port to serve https requests from (0 to disable).", NumberList)
  .option('-d, --debug', "Output route matching debug info.", Boolean)
  .parse(process.argv)

watch = (file) ->
  fs.watch file, {persistent: false}, ->
    console.log "A config file changed, restarting".yellow
    restart()

loadCfg = (file) ->
  cfg = fs.readFileSync(file).toString('utf8')

  watch file

  try
    return yaml.safeLoad(cfg)
  catch e
    console.error "Config file at #{ file } is not valid YAML: #{ e.toString() }".red
    process.exit(1)

start = ->
  # Options can come from four sources:
  #
  # - The project's .vee file
  # - Defaults in the system's ~/.hubspot/vee.yaml (in the `default` section)
  # - Project specific options in ~/.hubspot/vee.yaml (in a section titled the project's .name property)
  # - Command line flags

  try
    project = loadCfg '.vee'
  catch e
    if e.code is 'ENOENT'
      console.error ".vee configuration file not found in the current directory".red
      process.exit(1)
    else
      throw e

  try
    system = loadCfg "#{ process.env.HOME }/.hubspot/vee.yaml"
  catch e
    throw e unless e.code is 'ENOENT'

  defaults = system?['default'] ? {}

  personal = {}
  if project.name? and system?[project.name]?
    personal = system[project.name]

  options = _.extend {}, DEFAULTS, defaults, project, personal, _.pick(commander, 'port', 'httpsPort', 'debug')

  options.httpPort = options.httpPort ? options.port
  delete options.port

  unless _.isArray options.httpPort
    options.httpPort = [options.httpPort]

  if options.httpsPort and not _.isArray options.httpsPort
    options.httpsPort = [options.httpsPort]

  options.routes = _.extend {}, defaults.routes, project.routes, personal.routes

  proxy.start options

stop = ->
  proxy.stop()

restart = ->
  stop()
  start()

start()
<commit_msg>Changed extend/defaults order of routes. This should allow project specific routes to be gone through first, followed by personal routes, and finally default routes. The order used to be reversed, so if you had a default catch-all route, you would never get to the project specific routes. This makes use of `_.defaults` which will only set up the route if it is not already defined (so catch-alls for projects won't be overwritten by defaults).<commit_after>_ = require('underscore')
yaml = require('js-yaml')
colors = require('colors')
fs = require('fs')
commander = require('commander')

proxy = require('./proxy')

NumberList = (str) ->
  str.split(',').map(Number)

DEFAULTS =
  port: 80
  httpsPort: 443

commander
  .usage("Run it in the root of your project to start proxying requests as defined in the project's .vee file")
  .option('-p, --port [80]', "Port to serve http requests from.  Comma seperate to bind onto multiple ports.", NumberList)
  .option('-s, --https-port [443]', "Port to serve https requests from (0 to disable).", NumberList)
  .option('-d, --debug', "Output route matching debug info.", Boolean)
  .parse(process.argv)

watch = (file) ->
  fs.watch file, {persistent: false}, ->
    console.log "A config file changed, restarting".yellow
    restart()

loadCfg = (file) ->
  cfg = fs.readFileSync(file).toString('utf8')

  watch file

  try
    return yaml.safeLoad(cfg)
  catch e
    console.error "Config file at #{ file } is not valid YAML: #{ e.toString() }".red
    process.exit(1)

start = ->
  # Options can come from four sources:
  #
  # - The project's .vee file
  # - Defaults in the system's ~/.hubspot/vee.yaml (in the `default` section)
  # - Project specific options in ~/.hubspot/vee.yaml (in a section titled the project's .name property)
  # - Command line flags

  try
    project = loadCfg '.vee'
  catch e
    if e.code is 'ENOENT'
      console.error ".vee configuration file not found in the current directory".red
      process.exit(1)
    else
      throw e

  try
    system = loadCfg "#{ process.env.HOME }/.hubspot/vee.yaml"
  catch e
    throw e unless e.code is 'ENOENT'

  defaults = system?['default'] ? {}

  personal = {}
  if project.name? and system?[project.name]?
    personal = system[project.name]

  options = _.extend {}, DEFAULTS, defaults, project, personal, _.pick(commander, 'port', 'httpsPort', 'debug')

  options.httpPort = options.httpPort ? options.port
  delete options.port

  unless _.isArray options.httpPort
    options.httpPort = [options.httpPort]

  if options.httpsPort and not _.isArray options.httpsPort
    options.httpsPort = [options.httpsPort]

  options.routes = _.extend {}, project.routes
  _.defaults options.routes, personal.routes
  _.defaults options.routes, defaults.routes

  proxy.start options

stop = ->
  proxy.stop()

restart = ->
  stop()
  start()

start()

{'fraction_non_alphanumeric': 0.12771221926151505, 'fraction_numerical': 0.0053292729349067374, 'mean_word_length': 3.408557046979866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '699522', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1595, 'n_words': 553}