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'
|
2017-10-20 13:43:42 -04:00
|
|
|
import defaultConfig from '../defaultConfig'
|
2017-08-27 23:07:55 -05:00
|
|
|
import program from 'commander'
|
2017-08-28 15:02:22 -05:00
|
|
|
import tailwind from '..'
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-10-13 19:38:33 -04:00
|
|
|
function loadConfig(configPath) {
|
|
|
|
|
if (configPath === undefined) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
2017-08-28 12:55:53 -05:00
|
|
|
|
2017-10-13 19:38:33 -04:00
|
|
|
if (! fs.existsSync(path.resolve(configPath))) {
|
|
|
|
|
console.error(`Config file [${configPath}] does not exist.`)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
2017-08-28 12:55:53 -05:00
|
|
|
|
2017-10-13 19:38:33 -04:00
|
|
|
return require(path.resolve(configPath))
|
2017-08-28 12:55:53 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 19:38:33 -04:00
|
|
|
function writeStrategy(program) {
|
|
|
|
|
if (program.output === undefined) {
|
|
|
|
|
return (output) => {
|
|
|
|
|
process.stdout.write(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (output) => {
|
|
|
|
|
fs.writeFileSync(program.output, output)
|
2017-08-28 12:55:53 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 19:38:33 -04:00
|
|
|
function buildTailwind(inputFile, config, write) {
|
|
|
|
|
console.log('Building Tailwind!')
|
|
|
|
|
|
|
|
|
|
const input = fs.readFileSync(inputFile, 'utf8')
|
|
|
|
|
|
2017-10-20 14:23:24 -04:00
|
|
|
return postcss([tailwind(config)])
|
2017-10-13 19:38:33 -04:00
|
|
|
.process(input)
|
|
|
|
|
.then(result => {
|
|
|
|
|
write(result.css)
|
|
|
|
|
console.log('Finished building Tailwind!')
|
|
|
|
|
})
|
|
|
|
|
.catch(error => console.log(error))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 14:23:24 -04:00
|
|
|
program.version('0.1.0').usage('<command> [<args>]')
|
2017-10-20 12:32:15 -04:00
|
|
|
|
|
|
|
|
program.command('init')
|
2017-10-20 14:23:24 -04:00
|
|
|
.usage('[options]')
|
2017-10-20 12:32:15 -04:00
|
|
|
.action(function () {
|
2017-10-20 13:43:42 -04:00
|
|
|
const output = fs.readFileSync(path.resolve(__dirname + '/../defaultConfig.js'), 'utf8')
|
2017-10-20 12:32:15 -04:00
|
|
|
fs.writeFileSync(path.resolve('./tailwind.js'), output)
|
2017-10-20 14:23:24 -04:00
|
|
|
process.exit()
|
2017-10-20 12:32:15 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
program.command('build')
|
2017-08-27 23:07:55 -05:00
|
|
|
.usage('[options] <file ...>')
|
2017-10-13 19:38:33 -04:00
|
|
|
.option('-c, --config [path]', 'Path to config file')
|
|
|
|
|
.option('-o, --output [path]', 'Output file')
|
2017-10-20 12:32:15 -04:00
|
|
|
.action(function () {
|
|
|
|
|
let inputFile = program.args[0]
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-10-20 12:32:15 -04:00
|
|
|
if (! inputFile) {
|
|
|
|
|
console.error('No input file given!')
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-10-20 12:32:15 -04:00
|
|
|
buildTailwind(inputFile, loadConfig(program.config), writeStrategy(program))
|
2017-10-20 14:23:24 -04:00
|
|
|
.then(function () {
|
|
|
|
|
process.exit()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
program.command('*')
|
|
|
|
|
.action(function () {
|
|
|
|
|
program.help()
|
2017-10-20 12:32:15 -04:00
|
|
|
})
|
|
|
|
|
|
2017-10-20 13:43:14 -04:00
|
|
|
|
2017-10-20 14:23:24 -04:00
|
|
|
program.parse(process.argv)
|
2017-08-27 23:07:55 -05:00
|
|
|
|
2017-10-20 14:23:24 -04:00
|
|
|
if (program.args.length === 0 ) {
|
|
|
|
|
program.help()
|
2017-10-20 13:37:29 -04:00
|
|
|
process.exit()
|
2017-10-20 12:32:15 -04:00
|
|
|
}
|