2017-08-27 23:07:55 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
import fs from 'fs'
|
2017-08-28 12:55:53 -05:00
|
|
|
import _ from 'lodash'
|
2017-08-27 23:07:55 -05:00
|
|
|
import path from 'path'
|
|
|
|
|
import postcss from 'postcss'
|
|
|
|
|
import defaultConfig from './defaultConfig'
|
|
|
|
|
import program from 'commander'
|
2017-08-28 15:02:22 -05:00
|
|
|
import tailwind from '..'
|
2017-08-27 23:07:55 -05:00
|
|
|
|
|
|
|
|
let splitFileName = filename => {
|
|
|
|
|
return filename.split('.')
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
let buildTailwind = config => {
|
|
|
|
|
console.log('Building Tailwind!')
|
|
|
|
|
|
|
|
|
|
fs.readFile(inputFile, (err, css) => {
|
|
|
|
|
postcss([tailwind(config)])
|
|
|
|
|
.process(css)
|
|
|
|
|
.then(result => {
|
|
|
|
|
fs.writeFileSync(outputFile, result.css)
|
|
|
|
|
})
|
|
|
|
|
.catch(error => console.log(error))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log('Finished building Tailwind!')
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 21:10:29 -05:00
|
|
|
let getConfig = (customConfig, replace) => {
|
|
|
|
|
if (replace) {
|
2017-08-28 12:55:53 -05:00
|
|
|
return customConfig
|
|
|
|
|
} else {
|
|
|
|
|
return _.merge(defaultConfig, customConfig)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-27 23:07:55 -05:00
|
|
|
program
|
|
|
|
|
.version('0.1.0')
|
|
|
|
|
.usage('[options] <file ...>')
|
2017-08-28 12:55:53 -05:00
|
|
|
.option('-c, --config [path]', 'set config path')
|
|
|
|
|
.option(
|
|
|
|
|
'-r, --replace',
|
|
|
|
|
'replace the built-in configuration with the provided config file'
|
|
|
|
|
)
|
2017-08-27 23:07:55 -05:00
|
|
|
.parse(process.argv)
|
|
|
|
|
|
|
|
|
|
let inputFile = program.args[0]
|
|
|
|
|
|
|
|
|
|
if (!inputFile) {
|
|
|
|
|
console.error('No input file given!')
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
let outputFile =
|
|
|
|
|
program.args[1] || `${splitFileName(program.args[0])[0]}-output.css`
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
if (program.config !== undefined) {
|
|
|
|
|
fs.exists(program.config, exists => {
|
|
|
|
|
if (!exists) {
|
|
|
|
|
console.error(`Config file [${program.config}] does not exist.`)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
fs.readFile(program.config, (err, config) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error(
|
|
|
|
|
`There was a problem reading config file [${program.config}].`
|
|
|
|
|
)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
const customConfig = JSON.parse(config.toString())
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
let finalConfig = getConfig(customConfig, program.replace)
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-08-28 12:55:53 -05:00
|
|
|
buildTailwind(finalConfig)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
buildTailwind(defaultConfig)
|
|
|
|
|
}
|