-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
129 lines (103 loc) · 2.62 KB
/
index.mjs
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
import path from 'path'
import fs from 'fs-extra'
import fg from 'fast-glob'
import { $, cd } from 'zx'
const resolve = (...args) => path.resolve($.cwd || process.cwd(), ...args)
function interpret(args) {
return typeof args[0] === 'string' ? args[0] : String.raw(...args)
}
function interpretPath(args) {
return resolve(interpret(args))
}
async function createObject(read, write) {
const obj = {
data: await read(),
save: async (data = obj.data) => {
return await write(data)
},
read: async () => {
obj.data = await read()
return obj.data
},
}
return obj
}
function createSyncObject(read, write) {
const obj = {
data: read(),
save: (data = obj.data) => {
return write(data)
},
read: () => {
obj.data = read()
return obj.data
},
}
return obj
}
export function exists(...args) {
const path = interpretPath(args)
return fs.existsSync(resolve(path))
}
export async function remove(...args) {
const path = interpretPath(args)
return fs.remove(resolve(path))
}
export async function read(...args) {
const path = interpretPath(args)
return fs.readFile(path, 'utf-8')
}
read.sync = function (...args) {
const path = interpretPath(args)
return fs.readFileSync(path, 'utf-8')
}
export async function write(path, content) {
return fs.writeFile(resolve(path), content, 'utf-8')
}
write.sync = function (path, content) {
return fs.writeFileSync(resolve(path), content, 'utf-8')
}
export async function io(...args) {
const path = interpretPath(args)
return await createObject(
() => fs.readFile(path, 'utf-8'),
data => fs.writeFile(path, data, 'utf-8'),
)
}
io.sync = function (...args) {
const path = interpretPath(args)
return createSyncObject(
() => fs.readFileSync(path, 'utf-8'),
data => fs.writeFileSync(path, data, 'utf-8'),
)
}
async function jsonRead(...args) {
const path = interpretPath(args)
return fs.readJSON(path)
}
jsonRead.sync = function (...args) {
const path = interpretPath(args)
return fs.readJSONSync(path)
}
read.json = jsonRead
async function jsonIO(...args) {
const path = interpretPath(args)
return await createObject(
() => fs.readJSON(path),
data => fs.writeJSON(path, data, { spaces: jsonIO.spaces }),
)
}
jsonIO.sync = function (...args) {
const path = interpretPath(args)
return createSyncObject(
() => fs.readJSONSync(path),
data => fs.writeJSONSync(path, data, { spaces: jsonIO.spaces }),
)
}
jsonIO.spaces = 0
io.json = jsonIO
export function glob(...args) {
const input = interpretPath(args)
return fg(input, { cwd: resolve() })
}
export { cd, fs, fg }