/
utils.js
56 lines (42 loc) 路 1.18 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
import terminal from 'term-size'
import { promisify } from 'util'
import fs from 'fs'
import childProcess from 'child_process'
import dot from 'dot-prop'
// Terminal
export const maxCellSize = () => terminal().columns / 4
// Yarn
const canAccessFile = promisify(fs.access)
const exec = promisify(childProcess.exec)
const isYarnInstalled = async () => {
try {
await exec(`yarnpkg --version`, { stdio: `ignore` })
return true
} catch (err) {
return false
}
}
export const shouldUseYarn = async () => {
try {
await canAccessFile('package-lock.json')
return false
} catch (err) {
return isYarnInstalled()
}
}
// Additional
export const notEmpty = x => x.length !== 0
export const isEmpty = x => x.length === 0
const getCellPadding = (hits, hit) => attr => {
const cells = hits.map(_hit => dot.get(_hit, attr))
const cellWidth = Math.max(...cells.map(cell => (cell ? cell.length : 0)))
const cellValueWidth =
dot.get(hit, attr) === null ? 0 : dot.get(hit, attr).length
const width = cellWidth - cellValueWidth
return ` `.repeat(width)
}
export const hitsToCells = hits =>
hits.map(hit => ({
...hit,
_cell: getCellPadding(hits, hit),
}))