-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
171 lines (156 loc) · 3.51 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
159
160
161
162
163
164
165
166
167
168
169
170
171
const fs = require('fs')
const fsp = require('fs').promises
const path = require('path')
async function htmlFiles (fsBase, dir, isExcluded) {
const files = (await fsp.readdir(dir)).filter(e => !e.startsWith('.')).map(file => path.join(dir, file))
const stats = await Promise.all(files.map(file => fsp.stat(file)))
const htmls = []
for (let i = 0; i < files.length; i++) {
if (stats[i].isFile() && files[i].toLowerCase().endsWith('.html')) {
const path = files[i].replace(fsBase, '')
if (isExcluded(path)) {
warn(align('Ignoring path:'), `${path}`)
} else {
log(align('Found path:'), `${path}`)
htmls.push(path)
}
} else if (stats[i].isDirectory()) {
htmls.push(...(await htmlFiles(fsBase, files[i], isExcluded)))
}
}
return htmls
}
async function sleep (n) {
return new Promise((resolve) => {
setTimeout(() => resolve(), n)
})
}
function fileExists (f) {
return new Promise((resolve) => {
fs.access(f, (err) => {
if (err) {
resolve(false)
} else {
resolve(true)
}
})
})
}
const isChildOf = (child, parent) => {
if (child === parent) return false
const parentTokens = parent.split('/').filter(i => i.length)
const childTokens = child.split('/').filter(i => i.length)
return parentTokens.every((t, i) => childTokens[i] === t)
}
let verbose = false
function enableVerbose () {
verbose = true
}
const chalk = require('chalk')
function logLine (c, ...args) {
console.log(c(...args))
}
function clog (color, ...args) {
if (verbose) {
if (color) {
logLine(chalk.hex(color), ...args)
} else {
logLine(chalk, ...args)
}
}
}
function log (...args) {
clog(false, ...args)
}
function warn (...args) {
clog('#ffff00', ...args)
}
function error (...args) {
logLine(chalk.hex('#ff0000').underline, ...args)
}
function align (s) {
if (s.length >= 60) {
return s + '\n' + '-'.repeat(60)
} else {
return s + '-'.repeat(60 - s.length)
}
}
const fac = 0.7
function tooClose (r1, r2, r3) {
const sorted = [r1, r2, r3].sort((a, b) => a - b)
// console.log(sorted)
return sorted[0] / sorted[1] > fac || sorted[1] / sorted[2] > fac
}
function shuffle (rand, arr) {
const result = []
while (arr.length !== 0) {
const idx = rand(arr.length)
result.push(arr[idx])
arr.splice(idx, 1)
}
return result
}
const random = require('random-seed')
function randomColor (s) {
const rand = random.create(s)
let r
let g
let b
do {
const r1 = rand(200) + 56
const r2 = rand(200) + 56
const r3 = rand(200) + 56
const cols = shuffle(rand, [r1, r2, r3])
r = cols[0]
g = cols[1]
b = cols[2]
} while (tooClose(r, g, b))
return '#' + r.toString(16).padStart(2, '0') + g.toString(16).padStart(2, '0') + b.toString(16).padStart(2, '0')
}
if (require.main === module) {
verbose = true
for (let i = 0; i < 100; i++) {
const color = randomColor(i)
// let color = rc({
// luminosity: 'bright'
// });
clog(color, color)
}
}
function thread (afn) {
let ended = false
const promise = (async () => {
// eslint-disable-next-line
while (!ended) {
try {
await afn()
} catch (e) {
break
}
}
})()
return async () => {
ended = true
await promise
}
}
function trace (value) {
console.log(value)
return value
}
module.exports = {
htmlFiles,
sleep,
fileExists,
logLine,
clog,
log,
warn,
error,
align,
randomColor,
enableVerbose,
isChildOf,
thread,
trace
}