<commit_before>#!/usr/bin/env node

import fs from 'fs-extra'
import _ from 'lodash'
import path from 'path'
import postcss from 'postcss'
import defaultConfig from '../defaultConfig'
import program from 'commander'
import tailwind from '..'

function loadConfig(configPath) {
  if (configPath === undefined) {
    return undefined
  }

  if (! fs.existsSync(path.resolve(configPath))) {
    console.error(`Config file [${configPath}] does not exist.`)
    process.exit(1)
  }

  return require(path.resolve(configPath))
}

function writeStrategy(program) {
  if (program.output === undefined) {
    return (output) => {
      process.stdout.write(output)
    }
  }
  return (output) => {
    fs.outputFileSync(program.output, output)
  }
}

function buildTailwind(inputFile, config, write) {
  console.log('Building Tailwind!')

  const input = fs.readFileSync(inputFile, 'utf8')

  return postcss([tailwind(config)])
    .process(input)
    .then(result => {
      write(result.css)
      console.log('Finished building Tailwind!')
    })
    .catch(error => console.log(error))
}

program.version('0.1.0').usage('<command> [<args>]')

program.command('init [filename]')
  .usage('[options] [filename]')
  .action(function (filename = 'tailwind.js') {
    const destination = path.resolve(filename)

    if (fs.existsSync(destination)) {
      console.log(`Destination ${destination} already exists, aborting.`)
      process.exit(1)
    }

    const output = fs.readFileSync(path.resolve(__dirname + '/../defaultConfig.js'), 'utf8')
    fs.outputFileSync(destination, output.replace('// var defaultConfig', 'var defaultConfig'))
    process.exit()
  })

program.command('build')
  .usage('[options] <file ...>')
  .option('-c, --config [path]', 'Path to config file')
  .option('-o, --output [path]', 'Output file')
  .action(function () {
    let inputFile = program.args[0]

    if (! inputFile) {
      console.error('No input file given!')
      process.exit(1)
    }

    buildTailwind(inputFile, loadConfig(program.config), writeStrategy(program))
      .then(function () {
        process.exit()
      })
  })

program.command('*')<commit_msg>Fix `tailwind build` CLI command<commit_after>#!/usr/bin/env node

import fs from 'fs-extra'
import _ from 'lodash'
import path from 'path'
import postcss from 'postcss'
import defaultConfig from '../defaultConfig'
import program from 'commander'
import tailwind from '..'

function loadConfig(configPath) {
  if (configPath === undefined) {
    return undefined
  }

  if (! fs.existsSync(path.resolve(configPath))) {
    console.error(`Config file [${configPath}] does not exist.`)
    process.exit(1)
  }

  return require(path.resolve(configPath))
}

function writeStrategy(options) {
  if (options.output === undefined) {
    return (output) => {
      process.stdout.write(output)
    }
  }
  return (output) => {
    fs.outputFileSync(options.output, output)
  }
}

function buildTailwind(inputFile, config, write) {
  console.log('Building Tailwind!')

  const input = fs.readFileSync(inputFile, 'utf8')

  return postcss([tailwind(config)])
    .process(input)
    .then(result => {
      write(result.css)
      console.log('Finished building Tailwind!')
    })
    .catch(error => console.log(error))
}

program.version('0.1.0').usage('<command> [<args>]')

program.command('init [filename]')
  .usage('[options] [filename]')
  .action(function (filename = 'tailwind.js') {
    const destination = path.resolve(filename)

    if (fs.existsSync(destination)) {
      console.log(`Destination ${destination} already exists, aborting.`)
      process.exit(1)
    }

    const output = fs.readFileSync(path.resolve(__dirname + '/../defaultConfig.js'), 'utf8')
    fs.outputFileSync(destination, output.replace('// var defaultConfig', 'var defaultConfig'))
    process.exit()
  })

program.command('build')
  .usage('[options] <file ...>')
  .option('-c, --config [path]', 'Path to config file')
  .option('-o, --output [path]', 'Output file')
  .action(function (file, options) {
    let inputFile = program.args[0]

    if (! inputFile) {
      console.error('No input file given!')
      process.exit(1)
    }

    buildTailwind(inputFile, loadConfig(program.config), writeStrategy(options))
      .then(function () {
        process.exit()
      })
  })

program.command('*')
{'fraction_non_alphanumeric': 0.16343937298294145, 'fraction_numerical': 0.004149377593360996, 'mean_word_length': 3.685745140388769, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '1909674', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1367, 'n_words': 289}