Skip to content

Commit 4493804

Browse files
committed
implement --help
1 parent 4d0ed34 commit 4493804

File tree

1 file changed

+144
-31
lines changed

1 file changed

+144
-31
lines changed

src/cli.js

+144-31
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import tailwindAot from './processTailwindFeatures'
1212
import resolveConfigInternal from '../resolveConfig'
1313
import fastGlob from 'fast-glob'
1414
import getModuleDependencies from './lib/getModuleDependencies'
15+
import packageJson from '../package.json'
1516

1617
let env = {
1718
DEBUG: process.env.DEBUG !== undefined,
@@ -37,6 +38,54 @@ function formatNodes(root) {
3738
}
3839
}
3940

41+
function help({ message, usage, commands, options }) {
42+
// Render header
43+
console.log()
44+
console.log(' ', packageJson.name, packageJson.version)
45+
46+
// Render message
47+
if (message) {
48+
console.log()
49+
console.log(' ', message)
50+
}
51+
52+
// Render usage
53+
if (usage && usage.length > 0) {
54+
console.log()
55+
console.log(' ', 'Usage:')
56+
for (let example of usage) {
57+
console.log(' ', ' ', example)
58+
}
59+
}
60+
61+
// Render commands
62+
if (commands && commands.length > 0) {
63+
console.log()
64+
console.log(' ', 'Commands:')
65+
for (let command of commands) {
66+
console.log(' ', ' ', command)
67+
}
68+
}
69+
70+
// Render options
71+
if (options) {
72+
let groupedOptions = {}
73+
for (let [key, value] of Object.entries(options)) {
74+
if (typeof value === 'object') {
75+
groupedOptions[key] = { ...value, flags: [key] }
76+
} else {
77+
groupedOptions[value].flags.push(key)
78+
}
79+
}
80+
81+
console.log()
82+
console.log(' ', 'Options:')
83+
for (let { flags, description } of Object.values(groupedOptions)) {
84+
console.log(' ', ' ', flags.slice().reverse().join(', ').padEnd(15, ' '), description)
85+
}
86+
}
87+
}
88+
4089
// ---
4190

4291
/*
@@ -50,46 +99,100 @@ function formatNodes(root) {
5099
- [ ] Support AOT mode
51100
- [ ] Prebundle peer-dependencies
52101
- [ ] Make minification work
53-
- [ ] --help option
54-
- [ ] conditional flags based on arguments
102+
- [x] --help option
103+
- [x] conditional flags based on arguments
55104
init -f, --full
56105
build -f, --files
57106
- [ ] --jit
58107
59108
Future:
60109
- Detect project type, add sensible purge defaults
61110
*/
111+
let commands = {
112+
init: {
113+
run: init,
114+
args: {
115+
'--jit': { type: Boolean, description: 'Enable `JIT` mode' },
116+
'--full': { type: Boolean, description: 'Generate a full tailwind.config.js file' },
117+
'--postcss': { type: Boolean, description: 'Generate a PostCSS file' },
118+
'-f': '--full',
119+
'-p': '--postcss',
120+
},
121+
},
122+
build: {
123+
run: build,
124+
args: {
125+
'--jit': { type: Boolean, description: 'Build using `JIT` mode' },
126+
'--files': { type: String, description: 'Use a glob as files to use' },
127+
'--config': {
128+
type: String,
129+
description: 'Provide a custom config file, default: ./tailwind.config.js',
130+
},
131+
'--input': { type: String, description: 'The input css file' },
132+
'--output': { type: String, description: 'The output css file' },
133+
'--minify': { type: Boolean, description: 'Whether or not the result should be minified' },
134+
'--watch': { type: Boolean, description: 'Start watching for changes' },
135+
'-f': '--files',
136+
'-c': '--config',
137+
'-i': '--input',
138+
'-o': '--output',
139+
'-m': '--minify',
140+
'-w': '--watch',
141+
},
142+
},
143+
}
62144

63-
let args = arg({
64-
'--jit': Boolean,
65-
'--files': String,
66-
'--config': String,
67-
'--input': String,
68-
'--output': String,
69-
'--minify': Boolean,
70-
'--watch': Boolean,
71-
'--postcss': Boolean,
72-
'--full': Boolean,
73-
'-p': '--postcss',
74-
'-f': '--files',
75-
'-c': '--config',
76-
'-i': '--input',
77-
'-o': '--output',
78-
'-m': '--minify',
79-
'-w': '--watch',
80-
})
81-
82-
let input = args['--input']
83-
let output = args['--output']
84-
let shouldWatch = args['--watch']
85-
let shouldMinify = args['--minify']
86-
87-
if (args['_'].includes('init')) {
88-
init()
89-
} else {
90-
build()
145+
let sharedFlags = {
146+
'--help': { type: Boolean, description: 'Prints this help message' },
147+
'-h': '--help',
148+
}
149+
let command = process.argv.slice(2).find((arg) => !arg.startsWith('-')) || 'build'
150+
151+
if (commands[command] === undefined) {
152+
help({
153+
message: `Invalid command: ${command}`,
154+
usage: ['tailwind <command> [options]'],
155+
commands: ['init [file]', 'build <file> [options]'],
156+
options: sharedFlags,
157+
})
158+
process.exit(1)
159+
}
160+
161+
// Execute command
162+
let { args: flags, run } = commands[command]
163+
let args = (() => {
164+
try {
165+
return arg(
166+
Object.fromEntries(
167+
Object.entries({ ...flags, ...sharedFlags }).map(([key, value]) => [
168+
key,
169+
typeof value === 'object' ? value.type : value,
170+
])
171+
)
172+
)
173+
} catch (err) {
174+
if (err.code === 'ARG_UNKNOWN_OPTION') {
175+
help({
176+
message: err.message,
177+
usage: ['tailwind <command> [options]'],
178+
options: sharedFlags,
179+
})
180+
process.exit(1)
181+
}
182+
throw err
183+
}
184+
})()
185+
186+
if (args['--help']) {
187+
help({
188+
options: { ...flags, ...sharedFlags },
189+
usage: [`tailwind ${command} [options]`],
190+
})
191+
process.exit(0)
91192
}
92193

194+
run()
195+
93196
function init() {
94197
let tailwindConfigLocation = path.resolve('./tailwind.config.js')
95198
if (fs.existsSync(tailwindConfigLocation)) {
@@ -137,6 +240,11 @@ function init() {
137240
}
138241

139242
function build() {
243+
let input = args['--input']
244+
let output = args['--output']
245+
let shouldWatch = args['--watch']
246+
let shouldMinify = args['--minify']
247+
140248
if (args['--config'] && !fs.existsSync(args['--config'])) {
141249
console.error(`Specified config file ${args['--config']} does not exist.`)
142250
process.exit(9)
@@ -164,7 +272,12 @@ function build() {
164272
}
165273

166274
if (!output) {
167-
throw new Error('Missing required output file: --output, -o, or first argument')
275+
help({
276+
message: 'Missing required output file: --output, -o, or first argument',
277+
usage: [`tailwind ${command} [options]`],
278+
options: { ...flags, ...sharedFlags },
279+
})
280+
process.exit(1)
168281
}
169282

170283
function extractContent(config) {

0 commit comments

Comments
 (0)