@@ -5,76 +5,135 @@ import postcss from 'postcss'
55import prettyHrtime from 'pretty-hrtime'
66
77import commands from '.'
8- import constants from '../constants'
98import emoji from '../emoji'
109import tailwind from '../..'
11- import { error , exists , die , log , readFile , writeFile } from '../utils'
10+ import { die , error , exists , footer , header , log , readFile , writeFile } from '../utils'
1211
1312export const usage = 'build <file> [options]'
1413export const description = 'Compiles Tailwind CSS file.'
1514
1615export const options = [
1716 {
18- usage : '-c --config <file>' ,
19- description : 'Tailwind config file.' ,
17+ usage : '-o, --output <file>' ,
18+ description : 'Output file.' ,
2019 } ,
2120 {
22- usage : '-o --output <file>' ,
23- description : 'Compiled CSS file. Default: ' + chalk . bold . magenta ( constants . defaultOutputFile ) ,
21+ usage : '-c, --config <file>' ,
22+ description : 'Tailwind config file.' ,
2423 } ,
2524]
2625
2726export const optionMap = {
28- config : [ 'c ' , 'config ' ] ,
29- output : [ 'o ' , 'output ' ] ,
27+ output : [ 'output ' , 'o ' ] ,
28+ config : [ 'config ' , 'c ' ] ,
3029}
3130
3231/**
33- * Runs the command .
32+ * Prints the error message and stops the process .
3433 *
35- * @param {string[] } cliParams
36- * @param {object } cliOptions
34+ * @param {...string } [msgs]
35+ */
36+ function stop ( ...msgs ) {
37+ header ( )
38+ error ( ...msgs )
39+ die ( )
40+ }
41+
42+ /**
43+ * Prints the error message and help for this command, then stops the process.
44+ *
45+ * @param {...string } [msgs]
46+ */
47+ function stopWithHelp ( ...msgs ) {
48+ header ( )
49+ error ( ...msgs )
50+ commands . help . forCommand ( commands . build )
51+ die ( )
52+ }
53+
54+ /**
55+ * Compiles CSS file.
56+ *
57+ * @param {string } inputFile
58+ * @param {string } configFile
59+ * @param {string } outputFile
3760 * @return {Promise }
3861 */
39- export function run ( cliParams , cliOptions ) {
62+ function build ( inputFile , configFile , outputFile ) {
63+ const css = readFile ( inputFile )
64+
4065 return new Promise ( ( resolve , reject ) => {
41- const time = process . hrtime ( )
42- const inputFile = cliParams [ 1 ]
43- const configFile = cliOptions . config && cliOptions . config [ 0 ]
44- const outputFile = ( cliOptions . output && cliOptions . output [ 0 ] ) || constants . defaultOutputFile
66+ postcss ( [ tailwind ( configFile ) , autoprefixer ] )
67+ . process ( css , {
68+ from : inputFile ,
69+ to : outputFile ,
70+ } )
71+ . then ( resolve )
72+ . catch ( reject )
73+ } )
74+ }
4575
46- if ( ! inputFile ) {
47- error ( 'CSS file is required.' )
48- commands . help . forCommand ( this )
49- die ( )
50- }
76+ /**
77+ * Compiles CSS file and writes it to stdout.
78+ *
79+ * @param {string } inputFile
80+ * @param {string } configFile
81+ * @param {string } outputFile
82+ * @return {Promise }
83+ */
84+ function buildToStdout ( inputFile , configFile , outputFile ) {
85+ return build ( inputFile , configFile , outputFile ) . then ( result => process . stdout . write ( result . css ) )
86+ }
5187
52- ! exists ( inputFile ) && die ( chalk . bold . magenta ( inputFile ) , 'does not exist.' )
53- configFile && ! exists ( configFile ) && die ( chalk . bold . magenta ( configFile ) , 'does not exist.' )
88+ /**
89+ * Compiles CSS file and writes it to a file.
90+ *
91+ * @param {string } inputFile
92+ * @param {string } configFile
93+ * @param {string } outputFile
94+ * @param {int[] } startTime
95+ * @return {Promise }
96+ */
97+ function buildToFile ( inputFile , configFile , outputFile , startTime ) {
98+ header ( )
99+ log ( )
100+ log ( emoji . go , 'Building...' , chalk . bold . cyan ( inputFile ) )
54101
55- log ( )
56- log ( emoji . go , 'Building' , chalk . bold . cyan ( inputFile ) )
102+ return build ( inputFile , configFile , outputFile ) . then ( result => {
103+ writeFile ( outputFile , result . css )
57104
58- const css = readFile ( inputFile )
105+ const prettyTime = prettyHrtime ( process . hrtime ( startTime ) )
59106
60- const postcssPromise = postcss ( [ tailwind ( configFile ) , autoprefixer ] ) . process ( css , {
61- from : inputFile ,
62- to : outputFile ,
63- } )
107+ log ( )
108+ log ( emoji . yes , 'Finished in' , chalk . bold . magenta ( prettyTime ) )
109+ log ( emoji . pack , 'Size:' , chalk . bold . magenta ( bytes ( result . css . length ) ) )
110+ log ( emoji . disk , 'Saved to' , chalk . bold . cyan ( outputFile ) )
111+ footer ( )
112+ } )
113+ }
64114
65- postcssPromise
66- . then ( result => {
67- writeFile ( outputFile , result . css )
115+ /**
116+ * Runs the command.
117+ *
118+ * @param {string[] } cliParams
119+ * @param {object } cliOptions
120+ * @return {Promise }
121+ */
122+ export function run ( cliParams , cliOptions ) {
123+ return new Promise ( ( resolve , reject ) => {
124+ const startTime = process . hrtime ( )
125+ const inputFile = cliParams [ 0 ]
126+ const configFile = cliOptions . config && cliOptions . config [ 0 ]
127+ const outputFile = cliOptions . output && cliOptions . output [ 0 ]
68128
69- const prettyTime = prettyHrtime ( process . hrtime ( time ) )
129+ ! inputFile && stopWithHelp ( 'CSS file is required.' )
130+ ! exists ( inputFile ) && stop ( chalk . bold . magenta ( inputFile ) , 'does not exist.' )
131+ configFile && ! exists ( configFile ) && stop ( chalk . bold . magenta ( configFile ) , 'does not exist.' )
70132
71- log ( )
72- log ( emoji . yes , 'Finished in' , chalk . bold . magenta ( prettyTime ) )
73- log ( emoji . pack , 'Size:' , chalk . bold . magenta ( bytes ( result . css . length ) ) )
74- log ( emoji . disk , 'Saved to' , chalk . bold . cyan ( outputFile ) )
133+ const promise = outputFile
134+ ? buildToFile ( inputFile , configFile , outputFile , startTime )
135+ : buildToStdout ( inputFile , configFile , outputFile )
75136
76- resolve ( )
77- } )
78- . catch ( reject )
137+ promise . then ( resolve ) . catch ( reject )
79138 } )
80139}
0 commit comments