<commit_before>'use strict'

const fs = require('fs')
const path = require('path')

const rimraf = require('rimraf')
const chalk = require('chalk')

const console = require('../helpers/color-console')
const sfs = require('../helpers/sfs')
const {installWatcher} = require('../helpers/watcher')

const {build, getBuildFolder} = require('./builder')
const loader = require('./loader')

const {assetsToCopy} = require('../assets')
const config = require('../config')

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------

function generate (dataPath) {
  console.log(`Starting directory: ${chalk.green(dataPath)}`)

  const presskits = findData(dataPath)

  if (countDataFiles(presskits) === 0) {
    console.log('')
    console.warn('No data files found!')
    return
  }

  generateHTML(dataPath, presskits)
  exportAssets(dataPath)

  console.log('')
  console.log('Done! 👌')

  if (config.watch) {
    // Delete the assets (CSS/etc.) and install watchers.
    rimraf(path.join(dataPath, 'build/css'), () => {
      showHeader('Watching files')
      installWatcher(dataPath, generateHTML.bind(null, dataPath, presskits))
    })
  }
}

function findData (dataPath) {
  showHeader('Finding data')

  const presskits = {products: []}

  // Inspect given path for data files and get all correct ones.
  let files = sfs.findAllFiles(dataPath, { maxDepth: 1, ignoredFolders: ['build', 'node_modules'] })

  files.filter(isDataFile).forEach(file => {
    try {
      let presskit = loader.loadDataFile(file)
      if (!presskit) return

      console.log(`- ${presskit.type}: "${presskit.title}" ${chalk.dim(file)}`)

      if (presskit.type === 'company') {
        if (presskits.company) {
          console.warn('Multiple companies detected. This is not supported yet, only the last will be used!')
        }

        presskits.company = { path: dataPath, presskit: presskit }
      } else {
        presskits.products.push({ path: file, presskit: presskit })
      }
    } catch (err) {
      console.error(err)
    }
  })

  return presskits
}

function generateHTML (dataPath, presskits) {
  showHeader('Generating HTML')

  const productsList = []

  for (const product of presskits.products) {
    const outputPath = build(
      dataPath,
      product.path,
      product.presskit,
      {company: presskits.company.presskit}
    )

    productsList.push({path: outputPath, title: product.presskit.title})
  }

  // Company should be the last one, listing all the products
  if (presskits.company) {
    // Notice that we provide a ref to all the products here.
    build(
      dataPath,
      presskits.company.path,
      presskits.company.presskit,
      {products: productsList}
    )
  }
}

function exportAssets (dataPath) {
  showHeader('Exporting assets')

  // Copy assets (CSS and stuff)
  copyMandatoryFiles(dataPath)<commit_msg>Rename presskits to pages in generateHTML()<commit_after>'use strict'

const fs = require('fs')
const path = require('path')

const rimraf = require('rimraf')
const chalk = require('chalk')

const console = require('../helpers/color-console')
const sfs = require('../helpers/sfs')
const {installWatcher} = require('../helpers/watcher')

const {build, getBuildFolder} = require('./builder')
const loader = require('./loader')

const {assetsToCopy} = require('../assets')
const config = require('../config')

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------

function generate (dataPath) {
  console.log(`Starting directory: ${chalk.green(dataPath)}`)

  const presskits = findData(dataPath)

  if (countDataFiles(presskits) === 0) {
    console.log('')
    console.warn('No data files found!')
    return
  }

  generateHTML(dataPath, presskits)
  exportAssets(dataPath)

  console.log('')
  console.log('Done! 👌')

  if (config.watch) {
    // Delete the assets (CSS/etc.) and install watchers.
    rimraf(path.join(dataPath, 'build/css'), () => {
      showHeader('Watching files')
      installWatcher(dataPath, generateHTML.bind(null, dataPath, presskits))
    })
  }
}

function findData (dataPath) {
  showHeader('Finding data')

  const presskits = {products: []}

  // Inspect given path for data files and get all correct ones.
  let files = sfs.findAllFiles(dataPath, { maxDepth: 1, ignoredFolders: ['build', 'node_modules'] })

  files.filter(isDataFile).forEach(file => {
    try {
      let presskit = loader.loadDataFile(file)
      if (!presskit) return

      console.log(`- ${presskit.type}: "${presskit.title}" ${chalk.dim(file)}`)

      if (presskit.type === 'company') {
        if (presskits.company) {
          console.warn('Multiple companies detected. This is not supported yet, only the last will be used!')
        }

        presskits.company = { path: dataPath, presskit: presskit }
      } else {
        presskits.products.push({ path: file, presskit: presskit })
      }
    } catch (err) {
      console.error(err)
    }
  })

  return presskits
}

function generateHTML (dataPath, pages) {
  showHeader('Generating HTML')

  const productsList = []

  for (const product of pages.products) {
    const outputPath = build(
      dataPath,
      product.path,
      product.presskit,
      {
        company: pages.company.presskit,
      }
    )

    productsList.push({path: outputPath, title: product.presskit.title})
  }

  // Company should be the last one, listing all the products
  if (pages.company) {
    // Notice that we provide a ref to all the products here.
    build(
      dataPath,
      pages.company.path,
      pages.company.presskit,
      {
        products: productsList,
      }
    )
  }
}

function exportAssets (dataPath) {
  showHeader('Exporting assets')

  // Copy assets (CSS and stuff)
  copyMandatoryFiles(dataPath)
{'fraction_non_alphanumeric': 0.1750761937013207, 'fraction_numerical': 0.0006772773450728074, 'mean_word_length': 2.7011278195488724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4081008', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1757, 'n_words': 438}