forked from tailwindlabs/tailwindcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
123 lines (107 loc) · 2.48 KB
/
utils.js
File metadata and controls
123 lines (107 loc) · 2.48 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import chalk from 'chalk'
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
import { findKey, mapValues, trimStart } from 'lodash'
import * as emoji from './emoji'
import packageJson from '../../package.json'
/**
* Gets CLI parameters.
*
* @param {string[]} cliArgs
* @return {string[]}
*/
export function parseCliParams(cliArgs) {
const firstOptionIndex = cliArgs.findIndex(cliArg => cliArg.startsWith('-'))
return firstOptionIndex > -1 ? cliArgs.slice(0, firstOptionIndex) : cliArgs
}
/**
* Gets mapped CLI options.
*
* @param {string[]} cliArgs
* @param {object} [optionMap]
* @return {object}
*/
export function parseCliOptions(cliArgs, optionMap = {}) {
let options = {}
let currentOption = []
cliArgs.forEach(cliArg => {
const option = cliArg.startsWith('-') && trimStart(cliArg, '-').toLowerCase()
const resolvedOption = findKey(optionMap, aliases => aliases.includes(option))
if (resolvedOption) {
currentOption = options[resolvedOption] || (options[resolvedOption] = [])
} else if (option) {
currentOption = []
} else {
currentOption.push(cliArg)
}
})
return { ...mapValues(optionMap, () => undefined), ...options }
}
/**
* Prints messages to console.
*
* @param {...string} [msgs]
*/
export function log(...msgs) {
console.log(' ', ...msgs)
}
/**
* Prints application header to console.
*/
export function header() {
log()
log(chalk.bold(packageJson.name), chalk.bold.cyan(packageJson.version))
}
/**
* Prints application footer to console.
*/
export function footer() {
log()
}
/**
* Prints error messages to console.
*
* @param {...string} [msgs]
*/
export function error(...msgs) {
log()
console.error(' ', emoji.no, chalk.bold.red(msgs.join(' ')))
}
/**
* Kills the process. Optionally prints error messages to console.
*
* @param {...string} [msgs]
*/
export function die(...msgs) {
msgs.length && error(...msgs)
footer()
process.exit(1) // eslint-disable-line
}
/**
* Checks if path exists.
*
* @param {string} path
* @return {boolean}
*/
export function exists(path) {
return existsSync(path)
}
/**
* Gets file content.
*
* @param {string} path
* @return {string}
*/
export function readFile(path) {
return readFileSync(path, 'utf-8')
}
/**
* Writes content to file.
*
* @param {string} path
* @param {string} content
* @return {string}
*/
export function writeFile(path, content) {
ensureFileSync(path)
return outputFileSync(path, content)
}