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
107 lines (93 loc) · 2.17 KB
/
utils.js
File metadata and controls
107 lines (93 loc) · 2.17 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
import chalk from 'chalk'
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
import { findKey, mapValues, trimStart } from 'lodash'
import emoji from './emoji'
/**
* Gets CLI parameters.
*
* @param {string[]} args CLI arguments
* @return {string[]}
*/
export function parseCliParams(args) {
const firstOptionIndex = args.findIndex(arg => arg.startsWith('-'))
return firstOptionIndex > -1 ? args.slice(0, firstOptionIndex) : args
}
/**
* Gets mapped CLI options.
*
* @param {string[]} args CLI arguments
* @param {object} [optionMap]
* @return {object}
*/
export function parseCliOptions(args, optionMap = {}) {
let options = {}
let currentOption = []
args.forEach(arg => {
const option = arg.startsWith('-') && trimStart(arg, '-').toLowerCase()
const resolvedOption = findKey(optionMap, aliases => aliases.includes(option))
if (resolvedOption) {
currentOption = options[resolvedOption] || (options[resolvedOption] = [])
} else if (option) {
currentOption = []
} else {
currentOption.push(arg)
}
})
return { ...mapValues(optionMap, () => undefined), ...options }
}
/**
* Prints messages to console.
*
* @param {...string} msgs
*/
export function log(...msgs) {
console.log(' ', ...msgs)
}
/**
* 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)
log()
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)
}