<commit_before>var touch = require('touch')
var spawn = require('child_process').spawn

// flow control is fun!
function queue () {
  var args = [].slice.call(arguments)
  var cb = args.pop()
  go(args.shift())
  function go (fn) {
    if (!fn) return cb()
    fn(function (er) {
      if (er) return cb(er)
      go(args.shift())
    })
  }
}

var children = []
function exec (cmd, args, wait, cb) {
  if (typeof wait === 'function') cb = wait, wait = 200
  var opts = {stdio:'inherit'}
  // windows is kind of a jerk sometimes.
  if (process.platform === 'win32') {
    args = ['/c', '"' + cmd + '"'].concat(args)
    cmd = 'cmd'
    opts.windowsVerbatimArguments = true
  }
  var child = spawn(cmd, args, opts)
  var called = false

  var timer = setTimeout(function () {
    called = true
    cb()
  }, wait)

  child.on('exit', function (code) {
    if (wait) return cb(code)
    clearTimeout(timer)
    if (code) {
      msg = cmd + ' ' + args.join(' ') + ' fell over: '+code
      console.error(msg)
      if (!called) cb(new Error(msg))
    }
  })
  children.push(child)
}

queue(function (cb) {
  // first, make sure that we have the databases, or replicate will fail
  touch('dev/couch/registry.couch', cb)

}, function (cb) {
  touch('dev/couch/public_users.couch', cb)

}, function (cb) {
  touch('dev/couch/downloads.couch', cb)

}, function (cb) {
  // spawn couchdb, and make sure it stays up for a little bit
  exec('couchdb', ['-a', 'dev/couch/couch.ini'], cb)

}, function (cb) {
  // do the same for redis.
  exec('redis-server', ['dev/redis/redis.conf'], cb)

}, function (cb) {
  // and elasticsearch
  exec('elasticsearch', [
    '-Des.config=dev/elasticsearch/elasticsearch.yml'
    , '-f'
  ], 1000, cb)

}, function (cb) {
  // and npm2es
  exec(process.execPath, [
    './node_modules/npm2es/bin/npm2es.js'
    , '--couch=http://localhost:15984/registry'
    , '--es=http://127.0.0.1:9200/npm'
  ], cb)

}, function (cb) {
  // give it a few seconds to download some interesting data.
  // otherwise the site is pretty empty.
  exec(process.execPath, [require.resolve('./replicate.js')], 5000, cb)

}, function (er) {
  if (er) throw er

  console.error('\n\n\nSTARTING DEV SITE NOW\n\n\n')

  // force the configs to be pointed at our local redis and couchdb.
  var config = {}
  config.couchAuth = '<PASSWORD>'
  config.registryCouch = 'http://localhost:15984/'
  config.registryCouch = 'http://localhost:15984/'
  config.redis = { host: '127.0.0.1', port: 16379 }
  config.redisAuth = 'i-am-using-redis-in-development-mode-for-npm-www'<commit_msg>dev: Order scripts so that npm2es is more likely to work<commit_after>var touch = require('touch')
var spawn = require('child_process').spawn
var once = require('once')

// flow control is fun!
function queue () {
  var args = [].slice.call(arguments)
  var cb = args.pop()
  go(args.shift())
  function go (fn) {
    if (!fn) return cb()
    fn(function (er) {
      if (er) return cb(er)
      go(args.shift())
    })
  }
}

var children = []
function exec (cmd, args, wait, cb) {
  if (typeof wait === 'function') cb = wait, wait = 200
  cb = once(cb)

  var opts = {stdio:'inherit'}
  // windows is kind of a jerk sometimes.
  if (process.platform === 'win32') {
    args = ['/c', '"' + cmd + '"'].concat(args)
    cmd = 'cmd'
    opts.windowsVerbatimArguments = true
  }
  var child = spawn(cmd, args, opts)

  var timer = setTimeout(cb, wait)

  child.on('exit', function (code) {
    clearTimeout(timer)
    var er
    if (code) {
      msg = cmd + ' ' + args.join(' ') + ' fell over: '+code
      console.error(msg)
      er = new Error(msg)
    }
    cb(er)
  })
  children.push(child)
}

// best effort
process.on('exit', function() {
  children.forEach(function(child) {
    try {
      child.kill('SIGKILL')
    } catch (er) {}
  })
})

queue(function (cb) {
  // first, make sure that we have the databases, or replicate will fail
  touch('dev/couch/registry.couch', cb)

}, function (cb) {
  touch('dev/couch/public_users.couch', cb)

}, function (cb) {
  touch('dev/couch/downloads.couch', cb)

}, function (cb) {
  // start elasticsearch
  exec('elasticsearch', [
    '-Des.config=dev/elasticsearch/elasticsearch.yml'
    , '-f'
  ], 5000, cb)

}, function (cb) {
  // spawn couchdb, and make sure it stays up for a little bit
  exec('couchdb', ['-a', 'dev/couch/couch.ini'], cb)

}, function (cb) {
  // do the same for redis.
  exec('redis-server', ['dev/redis/redis.conf'], cb)

}, function (cb) {
  // give it a few seconds to download some interesting data.
  // otherwise the site is pretty empty.
  exec(process.execPath, [require.resolve('./replicate.js')], 5000, cb)

}, function (cb) {
  // by now, elastic search is probably up
  exec(process.execPath, [
    './node_modules/npm2es/bin/npm2es.js'
    , '--couch=http://localhost:15984/registry'
    , '--es=http://127.0.0.1:9200/npm'
  ], function (code) {
    console.error('did npm2es', code)
    cb(code)
  })

}, function (er) {
  if (er) throw er

  console.error('\n\n\nSTARTING DEV SITE NOW\n\n\n')

  // force the configs to be pointed at our local redis and couchdb.
  var config = {}
  config.couchAuth = '<PASSWORD>:<PASSWORD>'
  config.registryCouch = 'http://localhost:15984/'
  config.registryCouch = 'http://localhost:15984/'
  config.redis = { host: '127.0.0.1', port: 16379 }
  config.redisAuth = 'i-am-using-redis-in-development-mode-for-npm-www'
{'fraction_non_alphanumeric': 0.174450803027506, 'fraction_numerical': 0.01938342255861178, 'mean_word_length': 3.022271714922049, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2668638', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1925, 'n_words': 497}