<commit_before>#!/usr/bin/env node

var seaport = require('seaport')
  , cc = require('config-chain')
  , path = require('path')
  , fs = require('fs')
  , log = require('npmlog')
  , optimist = require('optimist')


function extend(dest, src) {
  Object.keys(src).forEach(function (key) {
    dest[key] = src[key]
  })
}


var argv = optimist
  .describe('h', 'Seaport hostname')
  .alias('h', 'host')

  .describe('p', 'Seaport port (required)')
  .alias('p', 'port')

  .describe('q', 'Quiet mode')
  .alias('q', 'quiet')

  .describe('k', 'Seaport key file (see Seaport documentation)')
  .alias('k', 'key')

  .describe('help', 'Displays this help message')
  .argv


if (argv.help) {
  optimist.showHelp()
  return
}


log.heading = 'nodeport'


var file


var conf = cc( argv , cc.env('NODEPORT_') )

file = path.resolve('.nodeport')
if (fs.existsSync(file))
  conf.addFile(file, 'ini', 'local')

file = path.resolve(process.env.HOME, '.nodeport')
if (fs.existsSync(file))
  conf.addFile(file, 'ini', 'global')


// Defaults config
conf.root = {}
conf.root.host = '127.0.0.1'


if (conf.get('quiet')) {
  log.level = 'silent'
} else {
  log.level = conf.get('loglevel')
}



if (!conf.get('port')) {
  log.error('Missing seaport port')
  return
}


var main
  , role
  , meta = {}

file = path.resolve(argv._[0])

var pkgFile = path.basename(file) == 'package.json' ? file : path.join(file, 'package.json')
if (fs.existsSync( pkgFile )) {
  var pkg = require(pkgFile)

  if (pkg.nodeport) main = pkg.nodeport
  else if (pkg.main) main = pkg.main
  else if (pkg.scripts && pkg.scripts.start) main = pkg.scripts.start

  if (main) main = path.resolve(path.dirname(pkgFile), main)

  role = pkg.name + '@' + pkg.version
  if (pkg.seaport) extend(meta, pkg.seaport)
}

if (!main) {
  main = path.join(file, 'server.js')
  if (!fs.existsSync(main)) {
    main = file
  }
}

if (!role) {
  role = path.basename(file)
}

try {
  main = require(main)
} catch(err) {
  log.error(err)
  log.error('Failed to load main')
  return
}

if (!main.listen) {
  log.error('Main must have a listen method')
  return
}

var opts = conf.get('key') && JSON.parse(fs.readFileSync(path.resolve(conf.get('key')), 'utf8')) || {}

var ports = seaport.connect(conf.get('port'), conf.get('host'), opts)

var argMeta = conf.get('meta')
if (typeof argMeta === 'string') {
  argMeta = JSON.parse(fs.readFileSync(path.resolve(argMeta), 'utf8')) || {}
}
if (typeof argMeta === 'object') {
  extend(meta, argMeta)
}


main.listen(ports.register(role, meta))


process.on('SIGINT', function() {
  log.info('Got SIGINT, closeing seaport connection')
  ports.close()
  if (typeof main.close === 'function') main.close()
})<commit_msg>remove SIGINT listener after first SIGINT<commit_after>#!/usr/bin/env node

var seaport = require('seaport')
  , cc = require('config-chain')
  , path = require('path')
  , fs = require('fs')
  , log = require('npmlog')
  , optimist = require('optimist')


function extend(dest, src) {
  Object.keys(src).forEach(function (key) {
    dest[key] = src[key]
  })
}


var argv = optimist
  .describe('h', 'Seaport hostname')
  .alias('h', 'host')

  .describe('p', 'Seaport port (required)')
  .alias('p', 'port')

  .describe('q', 'Quiet mode')
  .alias('q', 'quiet')

  .describe('k', 'Seaport key file (see Seaport documentation)')
  .alias('k', 'key')

  .describe('help', 'Displays this help message')
  .argv


if (argv.help) {
  optimist.showHelp()
  return
}


log.heading = 'nodeport'


var file


var conf = cc( argv , cc.env('NODEPORT_') )

file = path.resolve('.nodeport')
if (fs.existsSync(file))
  conf.addFile(file, 'ini', 'local')

file = path.resolve(process.env.HOME, '.nodeport')
if (fs.existsSync(file))
  conf.addFile(file, 'ini', 'global')


// Defaults config
conf.root = {}
conf.root.host = '127.0.0.1'


if (conf.get('quiet')) {
  log.level = 'silent'
} else {
  log.level = conf.get('loglevel')
}



if (!conf.get('port')) {
  log.error('Missing seaport port')
  return
}


var main
  , role
  , meta = {}

file = path.resolve(argv._[0])

var pkgFile = path.basename(file) == 'package.json' ? file : path.join(file, 'package.json')
if (fs.existsSync( pkgFile )) {
  var pkg = require(pkgFile)

  if (pkg.nodeport) main = pkg.nodeport
  else if (pkg.main) main = pkg.main
  else if (pkg.scripts && pkg.scripts.start) main = pkg.scripts.start

  if (main) main = path.resolve(path.dirname(pkgFile), main)

  role = pkg.name + '@' + pkg.version
  if (pkg.seaport) extend(meta, pkg.seaport)
}

if (!main) {
  main = path.join(file, 'server.js')
  if (!fs.existsSync(main)) {
    main = file
  }
}

if (!role) {
  role = path.basename(file)
}

try {
  main = require(main)
} catch(err) {
  log.error(err)
  log.error('Failed to load main')
  return
}

if (!main.listen) {
  log.error('Main must have a listen method')
  return
}

var opts = conf.get('key') && JSON.parse(fs.readFileSync(path.resolve(conf.get('key')), 'utf8')) || {}

var ports = seaport.connect(conf.get('port'), conf.get('host'), opts)

var argMeta = conf.get('meta')
if (typeof argMeta === 'string') {
  argMeta = JSON.parse(fs.readFileSync(path.resolve(argMeta), 'utf8')) || {}
}
if (typeof argMeta === 'object') {
  extend(meta, argMeta)
}


main.listen(ports.register(role, meta))


process.once('SIGINT', function () {
  log.info('Got SIGINT, closeing seaport connection')
  ports.close()
  if (typeof main.close === 'function') main.close()
})
{'fraction_non_alphanumeric': 0.1945926062166636, 'fraction_numerical': 0.0033106492551039177, 'mean_word_length': 3.8596961572832886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '686992', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2033, 'n_words': 447}