forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·96 lines (79 loc) · 2.34 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
/* eslint-disable no-process-exit */
import path from 'path'
import fs from 'fs-extra'
import tailwind from '..'
import postcss from 'postcss'
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')
return postcss([tailwind(config)])
.process(input, { from: inputFile })
.then(result => {
write(result.css)
console.log('Finished building Tailwind!')
})
.catch(error => console.log(error))
}
const packageJson = require(path.resolve(__dirname, '../package.json'))
program.version(packageJson.version).usage('<command> [<args>]')
program
.command('init [filename]')
.usage('[options] [filename]')
.action((filename = 'tailwind.js') => {
let destination = path.resolve(filename)
if (!path.extname(filename).includes('.js')) {
destination += '.js'
}
if (fs.existsSync(destination)) {
console.log(`Destination ${destination} already exists, aborting.`)
process.exit(1)
}
const output = fs.readFileSync(path.resolve(__dirname, '../defaultConfig.stub.js'), 'utf8')
fs.outputFileSync(destination, output.replace('// let defaultConfig', 'let defaultConfig'))
fs.outputFileSync(
destination,
output.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
)
console.log(`Generated Tailwind config: ${destination}`)
process.exit()
})
program
.command('build')
.usage('[options] <file ...>')
.option('-c, --config [path]', 'Path to config file')
.option('-o, --output [path]', 'Output file')
.action((file, options) => {
let inputFile = program.args[0]
if (!inputFile) {
console.error('No input file given!')
process.exit(1)
}
buildTailwind(inputFile, options.config, writeStrategy(options)).then(() => {
process.exit()
})
})
program
.command('*', null, {
noHelp: true,
})
.action(() => {
program.help()
})
program.parse(process.argv)
if (program.args.length === 0) {
program.help()
process.exit()
}