// this is a template!

const fs = require('fs')
const path = require('path')
const root = __dirname
const writeProcessIoTo = path.resolve(root, {{writeProcessIoTo}})

exports.afterProcess = function (args, result) {
  // const source = args[0]
  const filePath = args[1]
  const relPath = path.relative(root, filePath)
  if (filePath.startsWith(`${root}${path.sep}`)) {
    const dest = `${path.join(writeProcessIoTo, relPath)}.json`
    const segments = relPath.split(path.sep)
    segments.pop()
    const madeSegments = []
    segments.forEach(segment => {
      madeSegments.push(segment)
      const p = path.join(writeProcessIoTo, madeSegments.join(path.sep))
      if (!fs.existsSync(p)) fs.mkdirSync(p)
    })
    fs.writeFileSync(dest, JSON.stringify({
      in: args,
      out: result,
    }), 'utf8')
  }
}
