<commit_before>'use strict'

const path = require('path')
const chalk = require('chalk')
const fs = require('fs')
const Config = require('./config')
const Templates = require('./templates')
const Prompt = require('./prompt')
const Parser = require('./parser')

const config = new Config()
const templatesPath = config.read().templatesPath
const templates = new Templates(templatesPath)
const prompt = new Prompt()

module.exports = class Commands {
  config () {
    let cfg = config.read()
    let questions = [
      {
        name: 'templatesPath',
        message: 'Templates Path',
        default: cfg.templatesPath,
        validate: function (val) {
          if (fs.existsSync(val)) {
            return true
          } else {
            return 'Directory "' + val + '" does not exist'
          }
        }
      },
      {
        name: 'handlebarsDelimiters',
        message: 'Handlebars Delimiters',
        default: cfg.handlebarsDelimiters,
        validate: function (val) {
          if (val.indexOf('[') !== -1 || val.indexOf(']') !== -1) {
            return 'Can not contain "[]"' // due to regex problem
          }

          if (val.split(' ').length !== 2) {
            return 'Must have format "open[SPACE]close", for example "<% %>"'
          }

          return true
        }
      }
    ]

    prompt.ask(questions, (answers) => {
      config.save(answers)
      console.log(chalk.green('Saved.'))
    })
  }

  list () {
    let templatesObj = templates.getTemplates()
    let templatesLog = []

    for (let templateName in templatesObj) {
      let template = templatesObj[templateName]
      let meta = chalk.green(template.name || templateName)
      let files = chalk.grey(templateName + ': ' + template.files.join(' | '))

      if (template.description) {
        meta += chalk.yellow(' - ' + template.description)
      }

      templatesLog.push(meta + '\n' + files)
    }

    console.log(chalk.cyan('Templates Path:'), templatesPath + '\n')
    console.log(templatesLog.join('\n\n'))
  }

  create (templateName) {
    let templatesObj = templates.getTemplates()
    let parseTemplate = function (templateName) {
      let template = templatesObj[templateName]
      let parser = new Parser(template.delimiters)
      let variables = parser.parseFiles(template.path, template.files)
      let questions = prompt.questionsFromVariables(variables)

      prompt.ask(questions, (answers) => {
        console.log(answers)
      })
    }

    if (templateName) {
      let name = templates.findTemplateName(templateName)

      if (name && templatesObj[name]) {
        parseTemplate(name)
      } else {
        console.log(
          chalk.red('Can not find template "' + templateName + '".\n') +
          chalk.yellow('List templates with: ') +
          'lehm list'
        )
      }
    } else {
      let questions = prompt.questionsFromTemplates(templatesObj)

      prompt.ask(questions, (answers) => {
        parseTemplate(answers.template)
      })
    }
  }
}
<commit_msg>init parser class with correct delimiters<commit_after>'use strict'

const path = require('path')
const chalk = require('chalk')
const fs = require('fs')
const Config = require('./config')
const Templates = require('./templates')
const Prompt = require('./prompt')
const Parser = require('./parser')

const config = (new Config()).read()
const templates = new Templates(templatesPath)
const prompt = new Prompt()

const templatesPath = config.templatesPath
const handlebarsDelimiters = config.handlebarsDelimiters

module.exports = class Commands {
  config () {
    let questions = [
      {
        name: 'templatesPath',
        message: 'Templates Path',
        default: templatesPath,
        validate: function (val) {
          if (fs.existsSync(val)) {
            return true
          } else {
            return 'Directory "' + val + '" does not exist'
          }
        }
      },
      {
        name: 'handlebarsDelimiters',
        message: 'Handlebars Delimiters',
        default: handlebarsDelimiters,
        validate: function (val) {
          if (val.indexOf('[') !== -1 || val.indexOf(']') !== -1) {
            return 'Can not contain "[]"' // due to regex problem
          }

          if (val.split(' ').length !== 2) {
            return 'Must have format "open[SPACE]close", for example "<% %>"'
          }

          return true
        }
      }
    ]

    prompt.ask(questions, (answers) => {
      config.save(answers)
      console.log(chalk.green('Saved.'))
    })
  }

  list () {
    let templatesObj = templates.getTemplates()
    let templatesLog = []

    for (let templateName in templatesObj) {
      let template = templatesObj[templateName]
      let meta = chalk.green(template.name || templateName)
      let files = chalk.grey(templateName + ': ' + template.files.join(' | '))

      if (template.description) {
        meta += chalk.yellow(' - ' + template.description)
      }

      templatesLog.push(meta + '\n' + files)
    }

    console.log(chalk.cyan('Templates Path:'), templatesPath + '\n')
    console.log(templatesLog.join('\n\n'))
  }

  create (templateName) {
    let templatesObj = templates.getTemplates()
    let parseTemplate = function (templateName) {
      let template = templatesObj[templateName]
      let parser = new Parser(template.delimiters || handlebarsDelimiters)
      let variables = parser.parseFiles(template.path, template.files)
      let questions = prompt.questionsFromVariables(variables)

      prompt.ask(questions, (answers) => {
        console.log(answers)
      })
    }

    if (templateName) {
      let name = templates.findTemplateName(templateName)

      if (name && templatesObj[name]) {
        parseTemplate(name)
      } else {
        console.log(
          chalk.red('Can not find template "' + templateName + '".\n') +
          chalk.yellow('List templates with: ') +
          'lehm list'
        )
      }
    } else {
      let questions = prompt.questionsFromTemplates(templatesObj)

      prompt.ask(questions, (answers) => {
        parseTemplate(answers.template)
      })
    }
  }
}

{'fraction_non_alphanumeric': 0.13372472276581865, 'fraction_numerical': 0.0009784735812133072, 'mean_word_length': 2.375343973582829, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '1724883', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1727, 'n_words': 402}