-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
114 lines (101 loc) · 2.33 KB
/
tools.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
const fs = require('fs')
const child_process = require('child_process')
const readline = require('readline')
const { Readable } = require('stream')
const path = require('path')
const figlet = require('figlet')
const _ = require('lodash')
const colors = require('colors/safe')
const warnlog = (string) => {
console.log(colors.yellow(string))
}
const errlog = (string) => {
console.log(colors.red(string))
}
const successlog = (string) => {
console.log(colors.cyan(string))
}
const logoLog = (string) => {
console.log(colors.blue(string))
}
const getStat = (path) =>
new Promise((resolve) => {
fs.stat(path, (err) => {
resolve(!err)
})
})
const tryToExcu = (func, arg) =>
new Promise((resolve) => {
func(arg)
.then((res) => {
resolve([res, null])
})
.catch((err) => {
resolve([null, err])
})
})
const copyDir = (src, dist) => {
child_process.spawn('cp', ['-r', src, dist])
}
const outputLogo = (text) =>
new Promise((resolve) => {
figlet.text(text, { font: 'Stick Letters' }, function (err, data) {
if (err) {
logoLog(null)
resolve(false)
}
logoLog(data)
resolve(true)
})
})
const writeFileRecursive = (_path, buffer) => {
let dirPath = path.dirname(_path)
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
fs.writeFileSync(_path, buffer)
}
const findAllFile = (startPath, filterSuffix, ignoreDir = ['node_modules']) => {
if (!fs.existsSync(startPath)) {
return []
}
if (_.every(ignoreDir, (ignore) => startPath.includes(ignore))) {
return []
}
return fs
.readdirSync(startPath)
.map((dirInner) => {
dirInner = path.resolve(startPath, dirInner)
const stat = fs.statSync(dirInner)
return stat.isDirectory()
? findAllFile(dirInner, filterSuffix, ignoreDir)
: dirInner.endsWith(filterSuffix)
? dirInner
: []
})
.flat(Infinity)
}
const readStream = (url) =>
readline.createInterface({
input: fs.createReadStream(url),
crlfDelay: Infinity
})
const text2Stream = (text) =>
readline.createInterface({
input: Readable.from(text),
crlfDelay: Infinity
})
module.exports = {
getStat,
tryToExcu,
copyDir,
outputLogo,
writeFileRecursive,
findAllFile,
readStream,
text2Stream,
warnlog,
errlog,
successlog,
logoLog
}