<commit_before>import regeneratorRuntime from 'regenerator-runtime-only'
global.regeneratorRuntime = regeneratorRuntime

import express from 'express'
import cors from 'cors'
import portfinder from 'portfinder'
import flintjs from 'flint-js'
import flinttools from 'flint-tools'

import { p, readFile, readdir, handleError } from './lib/fns'

const newLine = "\n"
let OPTS, PORT

export default function wport() {
  return 2283 + parseInt(PORT, 10)
}

Array.prototype.move = function(from, to) {
  this.splice(to, 0, this.splice(from, 1)[0]);
}

function scriptTags(files) {
  return files
    .map(file => `<script src="/_/${file}" class="__flintScript"></script>`)
    .join(newLine)
}

function devToolsDisabled(req) {
  return OPTS.config && OPTS.config.tools === 'false' || req && req.query && req.query['!dev']
}

async function readScripts() {
  try {
    const dir = await readdir({ root: OPTS.outDir })
    const files = dir.files.filter(f => /\.jsf?$/.test(f.name)) // filter sourcemaps
    const hasFiles = files.length

    let paths = []

    // deterministic order
    if (hasFiles) {
      paths = files.map(file => file.path).sort()

      let mainIndex = 0

      paths.forEach((p, i) => {
        if (/[Mm]ain\.jsf?$/.test(p))
          mainIndex = i
      })

      if (mainIndex !== -1)
        paths.move(mainIndex, 0)
    }

    return paths
  }
  catch(e) {
    handleError(e)
  }
}

async function getScripts({ disableTools }) {
  try {
    const files = await readScripts()

    return [
      '<div id="_flintdevtools" class="_flintdevtools"></div>',
      newLine,
      '<!-- FLINT JS -->',
      '<script src="/__/react.dev.js"></script>',
      '<script src="/__/flint.dev.js"></script>',
      '<script>_FLINT_WEBSOCKET_PORT = ' + wport() + '</script>',
      '<script src="/__/devtools.dev.js"></script>',
`
  <script>
    var Flint = exports['flint']
    Flint.init()
  </script>
`,

      // devtools
      disableTools ? '' : [
        '<script src="/__/tools/externals.js"></script>',
        '<script src="/__/tools/internals.js"></script>',
        '<script src="/__/tools/flinttools.js"></script>',
`
  <script>
    Flint.run("_flintdevtools", exports["flinttools"])
  </script>
`
      ].join(newLine),
      // user files
      newLine,
      '<!-- APP -->',
`
  <script>
    // set up for dev mode, essentially faking a closure on window
    Flint = Flint.run(`${OPTS.saneName}`)
  </script>
`,
      '<script src="/__/externals.js" id="__flintExternals"></script>',<commit_msg>fix tools require<commit_after>import regeneratorRuntime from 'regenerator-runtime-only'
global.regeneratorRuntime = regeneratorRuntime

import express from 'express'
import cors from 'cors'
import portfinder from 'portfinder'
import flintjs from 'flint-js'
import flinttools from 'flint-tools'

import { p, readFile, readdir, handleError } from './lib/fns'

const newLine = "\n"
let OPTS, PORT

export default function wport() {
  return 2283 + parseInt(PORT, 10)
}

Array.prototype.move = function(from, to) {
  this.splice(to, 0, this.splice(from, 1)[0]);
}

function scriptTags(files) {
  return files
    .map(file => `<script src="/_/${file}" class="__flintScript"></script>`)
    .join(newLine)
}

function devToolsDisabled(req) {
  return OPTS.config && OPTS.config.tools === 'false' || req && req.query && req.query['!dev']
}

async function readScripts() {
  try {
    const dir = await readdir({ root: OPTS.outDir })
    const files = dir.files.filter(f => /\.jsf?$/.test(f.name)) // filter sourcemaps
    const hasFiles = files.length

    let paths = []

    // deterministic order
    if (hasFiles) {
      paths = files.map(file => file.path).sort()

      let mainIndex = 0

      paths.forEach((p, i) => {
        if (/[Mm]ain\.jsf?$/.test(p))
          mainIndex = i
      })

      if (mainIndex !== -1)
        paths.move(mainIndex, 0)
    }

    return paths
  }
  catch(e) {
    handleError(e)
  }
}

async function getScripts({ disableTools }) {
  try {
    const files = await readScripts()

    return [
      '<div id="_flintdevtools" class="_flintdevtools"></div>',
      newLine,
      '<!-- FLINT JS -->',
      '<script src="/__/react.dev.js"></script>',
      '<script src="/__/flint.dev.js"></script>',
      '<script>_FLINT_WEBSOCKET_PORT = ' + wport() + '</script>',
      '<script src="/__/devtools.dev.js"></script>',
`
  <script>
    var Flint = exports['flint']
    Flint.init()
  </script>
`,

      // devtools
      disableTools ? '' : [
        '<script src="/__/tools/externals.js"></script>',
        '<script src="/__/tools/internals.js"></script>',
        '<script src="/__/tools/flinttools.js"></script>',
`
  <script>
    Flint.run("flinttools", { node: "_flintdevtools" })
  </script>
`
      ].join(newLine),
      // user files
      newLine,
      '<!-- APP -->',
`
  <script>
    // set up for dev mode, essentially faking a closure on window
    Flint = Flint.run("${OPTS.saneName}")
  </script>
`,
      '<script src="/__/externals.js" id="__flintExternals"></script>',
{'fraction_non_alphanumeric': 0.17898909811694746, 'fraction_numerical': 0.004757185332011893, 'mean_word_length': 2.8935185185185186, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 67, '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': '4369291', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1720, 'n_words': 370}