<commit_before>const fs = require('fs-extra')
const path = require('path')
const shortid = require('shortid')
const sharp = require('sharp')

const {
  getDesigners,
  updateDesignerFile,
  getProjects,
  updateProjectFile,
  getInfo,
  updateInfoFile,
} = require('./data')

module.exports = {
  processImages: main,
  getImageData
}

const imageDir = path.join(__dirname, '../static/images')

async function main () {
  await Promise.all([
    processDesigners(),
    processProjects(),
    processInfo(),
  ])
}

async function processDesigners () {
  const designers = await getDesigners()

  // process works of all designers
  const processedWorks = await Promise.all(designers.map(designer =>
    processDesignerWorks(designer)
  ))

  // update yaml files of all designers
  await Promise.all(designers.map((designer, index) =>
    updateDesigner(designer, processedWorks[index])
  ))
}

async function processProjects () {
  const projects = await getProjects()

  // process project images
  const processedProjectImages = await Promise.all(projects.map(project =>
    processProjectImages(project)
  ))

  // update yaml files of all designers
  await Promise.all(projects.map((project, index) =>
    updateProject(project, processedProjectImages[index])
  ))
}

async function processInfo () {
  const info = await getInfo()
  const processedImages = await processInfoImages(info)
  await updateInfo(info, processedImages)
}

async function processDesignerWorks (data) {
  const { works } = data
  return Promise.all(works.map(async (work) => {
    // find images that need processing
    const images = work.images || []
    const imagesToProcess = images.filter(image =>
      image.file && !image.file.includes('static/images/works')
    )

    // process them
    const processedImages = await Promise.all(imagesToProcess.map(image => {
      const newFilename = workImageFilename(data, work, image)
      return processImage(image, newFilename)
    }))

    // return relevant info
    return processedImages
      .filter(item => !!item)
      .map((item, i) => Object.assign({}, item, {
      originalImageIndex: images.indexOf(imagesToProcess[i])
    }))
  }))
}

async function processProjectImages (project) {
  // find images that need processing
  const images = project.images || []
  const imagesToProcess = images.filter(image =>
    image.file && !image.file.includes('static/images/projects')
  )

  // process them
  const processedImages = await Promise.all(imagesToProcess.map(image => {
    const newFilename = projectImageFilename(project, image)
    return processImage(image, newFilename)
  }))

  // return relevant info
  return processedImages
    .filter(item => !!item)
    .map((item, i) => Object.assign({}, item, {
    originalImageIndex: images.indexOf(imagesToProcess[i])
  }))
}

async function processInfoImages (info) {
  // find images that need processing
  const images = info.images || []
  const imagesToProcess = images.filter(image =>
    image.file && !image.file.includes('static/images/info/')
  )

  // process them
  const processedImages = await Promise.all(imagesToProcess.map(image => {
    const newFilename = infoImageFilename(image)
    return processImage(image, newFilename)
  }))

  // return relevant info<commit_msg>bugfix: fixes bug where images are always resized<commit_after>const fs = require('fs-extra')
const path = require('path')
const shortid = require('shortid')
const sharp = require('sharp')

const {
  getDesigners,
  updateDesignerFile,
  getProjects,
  updateProjectFile,
  getInfo,
  updateInfoFile,
} = require('./data')

module.exports = {
  processImages: main,
  getImageData
}

const imageDir = path.join(__dirname, '../static/images')

async function main () {
  await Promise.all([
    processDesigners(),
    processProjects(),
    processInfo(),
  ])
}

async function processDesigners () {
  const designers = await getDesigners()

  // process works of all designers
  const processedWorks = await Promise.all(designers.map(designer =>
    processDesignerWorks(designer)
  ))

  // update yaml files of all designers
  await Promise.all(designers.map((designer, index) =>
    updateDesigner(designer, processedWorks[index])
  ))
}

async function processProjects () {
  const projects = await getProjects()

  // process project images
  const processedProjectImages = await Promise.all(projects.map(project =>
    processProjectImages(project)
  ))

  // update yaml files of all designers
  await Promise.all(projects.map((project, index) =>
    updateProject(project, processedProjectImages[index])
  ))
}

async function processInfo () {
  const info = await getInfo()
  const processedImages = await processInfoImages(info)
  await updateInfo(info, processedImages)
}

async function processDesignerWorks (data) {
  const { works } = data
  return Promise.all(works.map(async (work) => {
    // find images that need processing
    const images = work.images || []
    const imagesToProcess = images.filter(image =>
      image.file && !image.file.includes('images/works')
    )

    // process them
    const processedImages = await Promise.all(imagesToProcess.map(image => {
      const newFilename = workImageFilename(data, work, image)
      return processImage(image, newFilename)
    }))

    // return relevant info
    return processedImages
      .filter(item => !!item)
      .map((item, i) => Object.assign({}, item, {
      originalImageIndex: images.indexOf(imagesToProcess[i])
    }))
  }))
}

async function processProjectImages (project) {
  // find images that need processing
  const images = project.images || []
  const imagesToProcess = images.filter(image =>
    image.file && !image.file.includes('images/projects')
  )

  // process them
  const processedImages = await Promise.all(imagesToProcess.map(image => {
    const newFilename = projectImageFilename(project, image)
    return processImage(image, newFilename)
  }))

  // return relevant info
  return processedImages
    .filter(item => !!item)
    .map((item, i) => Object.assign({}, item, {
    originalImageIndex: images.indexOf(imagesToProcess[i])
  }))
}

async function processInfoImages (info) {
  // find images that need processing
  const images = info.images || []
  const imagesToProcess = images.filter(image =>
    image.file && !image.file.includes('images/info/')
  )

  // process them
  const processedImages = await Promise.all(imagesToProcess.map(image => {
    const newFilename = infoImageFilename(image)
    return processImage(image, newFilename)
  }))

  // return relevant info
{'fraction_non_alphanumeric': 0.11925352753755121, 'fraction_numerical': 0.0, 'mean_word_length': 3.886582653817643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '5410458', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1844, 'n_words': 464}