tailwindcss/src/cli.js

93 lines
2.2 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2017-11-04 01:32:25 -05:00
/* eslint-disable no-process-exit */
import path from 'path'
2017-11-04 01:32:25 -05:00
import fs from 'fs-extra'
import tailwind from '..'
import postcss from 'postcss'
2017-11-04 01:32:25 -05:00
import process from 'process'
import program from 'commander'
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')
2017-10-20 14:23:24 -04:00
return postcss([tailwind(config)])
.process(input)
.then(result => {
write(result.css)
console.log('Finished building Tailwind!')
})
.catch(error => console.log(error))
}
2017-11-08 08:27:00 -05:00
const packageJson = require(path.resolve(__dirname, '../package.json'))
program.version(packageJson.version).usage('<command> [<args>]')
2017-10-20 12:32:15 -04:00
program
.command('init [filename]')
.usage('[options] [filename]')
2017-11-04 01:32:25 -05:00
.action((filename = 'tailwind.js') => {
2017-11-05 21:49:17 +05:30
let destination = path.resolve(filename)
2017-11-05 13:32:02 -06:00
if (!path.extname(filename).includes('.js')) {
2017-11-05 21:49:17 +05:30
destination += '.js'
}
if (fs.existsSync(destination)) {
console.log(`Destination ${destination} already exists, aborting.`)
process.exit(1)
}
2017-11-08 08:27:00 -05:00
const output = fs.readFileSync(path.resolve(__dirname, '../defaultConfig.js'), 'utf8')
2017-11-05 13:56:58 -06:00
fs.outputFileSync(destination, output.replace('// var defaultConfig', 'var defaultConfig'))
console.log(`Generated Tailwind config: ${destination}`)
2017-10-20 14:23:24 -04:00
process.exit()
2017-10-20 12:32:15 -04:00
})
program
.command('build')
.usage('[options] <file ...>')
.option('-c, --config [path]', 'Path to config file')
.option('-o, --output [path]', 'Output file')
2017-11-04 01:32:25 -05:00
.action((file, options) => {
2017-10-20 12:32:15 -04:00
let inputFile = program.args[0]
if (!inputFile) {
2017-10-20 12:32:15 -04:00
console.error('No input file given!')
process.exit(1)
}
2017-11-08 08:27:00 -05:00
buildTailwind(inputFile, options.config, writeStrategy(options)).then(() => {
process.exit()
})
2017-10-20 14:23:24 -04:00
})
program
.command('*', null, {
2017-11-05 13:32:02 -06:00
noHelp: true,
2017-11-01 17:26:59 -05:00
})
2017-11-04 01:32:25 -05:00
.action(() => {
2017-10-20 14:23:24 -04:00
program.help()
2017-10-20 12:32:15 -04:00
})
2017-10-20 14:23:24 -04:00
program.parse(process.argv)
if (program.args.length === 0) {
2017-10-20 14:23:24 -04:00
program.help()
process.exit()
2017-10-20 12:32:15 -04:00
}