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

const generateFiles = (templatePath, templateFiles, output) => {
  return new Promise((resolve, reject) => {
    let writtenFiles = []

    templateFiles.forEach(templateFile => {
      const fullTemplateFilePath = path.resolve(templatePath, templateFile)
      const outputFile = templateFile.split(path.sep).reduce((prev, curr) => {
        const parsedFile = path.parse(curr)
        return path.join(prev, parsedFile.name === '_index' ? `${output}${parsedFile.ext}` : curr)
      }, '')

      if (fs.lstatSync(fullTemplateFilePath).isDirectory()) {
        try {
          fs.mkdirSync(`${outputFile}`)
        } catch (err) {
          return reject(new Error(`Unable to create directory '${err.path}'`))
        }
      } else {
        let data

        try {
          data = fs.readFileSync(fullTemplateFilePath, 'utf8')
        } catch (err) {
          return reject(new Error(`Unable to read template file '${err.path}'`))
        }

        data = compile(data, [
          { name: 'file', value: path.basename(output) }
        ])

        try {
          if (fs.existsSync(outputFile)) {
            return reject(new Error(`File '${outputFile}' already exists`))
          }

          fs.writeFileSync(outputFile, data)
        } catch (err) {
          return reject(new Error(`Unable to write to '${outputFile}'`))
        }
      }

      writtenFiles.push(outputFile)
    })

    resolve(writtenFiles)
  })
}

const compile = (body, variables) => {
  variables.forEach(variable => {
    body = body.split(`{{${variable.name}}}`).join(variable.value)
  })

  return body
}

module.exports = {
  compile,
  generateFiles
}

@@ -1503,24 +1503,19 @@
  (body, 
-variable
+tag
 s) =%3E %7B%0A
@@ -1516,24 +1516,19 @@
  =%3E %7B%0A  
-variable
+tag
 s.forEac
@@ -1529,24 +1529,19 @@
 forEach(
-variable
+tag
  =%3E %7B%0A  
@@ -1565,24 +1565,19 @@
 it(%60%7B%7B$%7B
-variable
+tag
 .name%7D%7D%7D
@@ -1588,16 +1588,11 @@
 oin(
-variable
+tag
 .val

Rename variables to tags