-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs-utils.js
87 lines (68 loc) · 2.34 KB
/
fs-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
import { createHash } from 'node:crypto'
import { join, dirname, basename, extname } from 'node:path'
import { brotliCompressSync } from 'node:zlib'
import {
rmSync,
lstatSync,
mkdirSync,
existsSync,
unlinkSync,
readdirSync,
readFileSync,
copyFileSync,
writeFileSync
} from 'node:fs'
export const reIsSourceCode = /^(?!.*\.test).*\.js$/ // *.js not ending in .test.js
export const read = f => readFileSync(f, 'utf8')
export const readIfExists = f => existsSync(f) ? read(f) : ''
export const exists = f => existsSync(f)
export const remove = f => unlinkSync(f)
export function copy(src, dest) { copyFileSync(src, dest) }
export function write(fname, data) {
mkdirSync(dirname(fname), { recursive: true })
writeFileSync(fname, data, 'utf8')
}
export function makeDir(dir) { mkdirSync(dir, { recursive: true }) }
export function removeDir(dir) { rmSync(dir, { recursive: true, force: true }) } // forcing allows removing non-existing directories
export const sizeOf = f => lstatSync(f).size
export const sha1 = f => createHash('sha1').update(readFileSync(f)).digest('base64url')
export const listFiles = (dir, regex) => readdirSync(dir)
.filter(f => regex.test(f))
.map(f => join(dir, f))
export const listFilesWithoutDirs = dir => readdirSync(dir)
.filter(f => lstatSync(join(dir, f)).isFile())
export const forEachFileInDir = (dir, fn) => readdirSync(dir)
.filter(f => lstatSync(join(dir, f)).isFile())
.forEach(f => fn(join(dir, f)))
export function listFilesRecursively(dir, regex, res = []) {
for (const f of readdirSync(dir)) {
const fPath = join(dir, f)
if (lstatSync(fPath).isDirectory())
listFilesRecursively(fPath, regex, res)
else if (regex.test(fPath))
res.push(fPath)
}
return res
}
export const readAsJSON = (name) =>
JSON.parse(readFileSync(name, 'utf8'))
export const saveAsJSON = (name, data) => {
writeFileSync(name, JSON.stringify(data, null, '\t'), 'utf8')
}
export function copyDir(from, to) {
mkdirSync(to, { recursive: true })
for (const fname of readdirSync(from)) {
const source = join(from, fname)
const target = join(to, fname)
if (lstatSync(source).isDirectory())
copyDir(source, target)
else
copyFileSync(source, target)
}
}
export function brotli(f) {
writeFileSync(f + '.br', brotliCompressSync(readFileSync(f, 'utf8')))
}
export function removeExtension(file) {
return basename(file, extname(file))
}