<commit_before>portscanner = require 'portscanner'
async = require 'async'
{clone} = require 'lodash'
extend = require 'deep-extend'
port = require './port'

openLogFile = require './log'
spawn = require './spawn'
verify = require './verify'


convert = (proc) ->
  proc = extend {}, defaults, proc
  proc.spawnOpts.cwd ?= process.cwd()

  (callback) ->
    openLogFile proc.spawnOpts.cwd, proc.logFilePath, (error, results) ->
      return callback(error) if error?
      {fd: logHandle, filename: logPath} = results

      spawnOpts =
        stdio: [ 'ignore', logHandle, logHandle ]
        env: process.env
      extend spawnOpts, proc.spawnOpts

      port.findOpen proc.port, (error, availablePort) ->
        return callback(error) if error?

        child = spawn(proc.name, proc.command, availablePort, logPath, logHandle, spawnOpts)

        verify child, proc.verify, proc.verifyInterval, proc.verifyTimeout, availablePort, (error) ->
          return callback(error) if error?
          callback(null, child)


autoable = (name, proc) ->
  proc.name = name
  func = convert(proc)

  if proc.dependsOn?.length > 0
    array = clone(proc.dependsOn)
    array.push(func)
    array
  else
    func

defaults =
  port: 0 # get random available port
  logFilePath: './log/process.log' # TODO: use proc name
  spawnOpts: {}
  verifyInterval: 100
  verifyTimeout: 3000
  verify: (port, callback) ->
    portscanner.checkPortStatus port, '127.0.0.1', (error, status) ->
      return callback(error) if error?<commit_msg>default logPath to use proc name<commit_after>portscanner = require 'portscanner'
async = require 'async'
{clone} = require 'lodash'
extend = require 'deep-extend'
port = require './port'

openLogFile = require './log'
spawn = require './spawn'
verify = require './verify'


convert = (proc) ->
  proc = extend {}, defaults(proc.name), proc
  proc.spawnOpts.cwd ?= process.cwd()

  (callback) ->
    openLogFile proc.spawnOpts.cwd, proc.logFilePath, (error, results) ->
      return callback(error) if error?
      {fd: logHandle, filename: logPath} = results

      spawnOpts =
        stdio: [ 'ignore', logHandle, logHandle ]
        env: process.env
      extend spawnOpts, proc.spawnOpts

      port.findOpen proc.port, (error, availablePort) ->
        return callback(error) if error?

        child = spawn(proc.name, proc.command, availablePort, logPath, logHandle, spawnOpts)

        verify child, proc.verify, proc.verifyInterval, proc.verifyTimeout, availablePort, (error) ->
          return callback(error) if error?
          callback(null, child)


autoable = (name, proc) ->
  proc.name = name
  func = convert(proc)

  if proc.dependsOn?.length > 0
    array = clone(proc.dependsOn)
    array.push(func)
    array
  else
    func

defaults = (procName) ->
  port: 0 # get random available port
  logFilePath: "./log/#{procName}.log"
  spawnOpts: {}
  verifyInterval: 100
  verifyTimeout: 3000
  verify: (port, callback) ->
    portscanner.checkPortStatus port, '127.0.0.1', (error, status) ->
      return callback(error) if error?
{'fraction_non_alphanumeric': 0.1262199089134678, 'fraction_numerical': 0.009759271307742356, 'mean_word_length': 3.161028416779432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '607601', 'n_tokens_mistral': 982, 'n_tokens_neox': 957, 'n_words': 263}