forked from tailwindlabs/tailwindcss
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
158 lines (139 loc) · 3.2 KB
/
utils.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { copyFileSync, ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
import { findKey, mapValues, startsWith, trimStart } from 'lodash'
import path from 'path'
import * as colors from './colors'
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(colors.bold(packageJson.name), colors.info(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, colors.error(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)
}
/**
* Copies file source to destination.
*
* @param {string} source
* @param {string} destination
*/
export function copyFile(source, destination) {
copyFileSync(source, destination)
}
/**
* Gets file content.
*
* @param {string} path
* @return {string}
*/
export function readFile(path) {
return readFileSync(path, 'utf-8')
}
/**
* Checks if current package.json uses type "module"
*
* @return {boolean}
*/
export function isModule() {
const pkgPath = path.resolve('./package.json')
if (exists(pkgPath)) {
const pkg = JSON.parse(readFile(pkgPath))
return pkg.type && pkg.type === 'module'
}
return false
}
/**
* Writes content to file.
*
* @param {string} path
* @param {string} content
* @return {string}
*/
export function writeFile(path, content) {
ensureFileSync(path)
return outputFileSync(path, content)
}
/**
* Strips leading ./ from path
*
* @param {string} path
* @return {string}
*/
export function getSimplePath(path) {
return startsWith(path, './') ? path.slice(2) : path
}