-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
147 lines (127 loc) · 4.13 KB
/
index.ts
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
/**
* fs-extra on Promises.
* Exports native fs methods as-is.
* API is promised-based version of fs-extra package:
* https://github.com/jprichardson/node-fs-extra#methods
*/
import {
copy as copy_,
move as move_,
createFile as createFile_,
mkdirp as mkdirp_,
outputFile as outputFile_,
outputJson as outputJson_,
readJson as readJson_,
remove as remove_,
writeJson as writeJson_,
ensureDir as ensureDir_,
ensureFile as ensureFile_,
ensureLink as ensureLink_,
ensureSymlink as ensureSymlink_,
emptyDir as emptyDir_,
// Explicit type import
// https://github.com/Microsoft/TypeScript/issues/9944#issuecomment-244448079
// tslint:disable-next-line
CopyFilterFunction,
CopyFilter,
CopyOptions,
MoveOptions,
MkdirOptions,
OpenOptions
} from 'fs-extra'
export {
copySync,
createFileSync,
mkdirsSync,
mkdirpSync,
outputFileSync,
outputJsonSync,
outputJSONSync,
readJsonSync,
readJSONSync,
removeSync,
writeJsonSync,
writeJSONSync,
ensureDirSync,
ensureFileSync,
ensureLinkSync,
ensureSymlinkSync,
emptyDirSync,
CopyFilter,
CopyOptions
} from 'fs-extra'
export * from 'fs'
type Callback = (err: Error) => void
const promisify1 = <T>(
fn: (_: T, cb: Callback) => void
) => <U>(arg: T) => new Promise<U>((resolve, reject) => {
fn(
arg,
(err: Error, result?: U) => {
if (err) reject(err)
else resolve(result)
}
)
})
const promisify2 = <T1, T2>(
fn: (_1: T1, _2: T2, cb: Callback) => void
) => <U>(arg1: T1, arg2: T2) => new Promise<U>((resolve, reject) => {
fn(
arg1,
arg2,
(err: Error, result?: U) => {
if (err) reject(err)
else resolve(result)
}
)
})
const promisify3 = <T1, T2, T3>(
fn: (_1: T1, _2: T2, _3: T3, cb: Callback) => void
) => <U>(arg1: T1, arg2: T2, arg3: T3) => new Promise<U>((resolve, reject) => {
fn(
arg1,
arg2,
arg3,
(err: Error, result?: U) => {
if (err) reject(err)
else resolve(result)
}
)
})
export const copy = (src: string, dest: string, filterOrOptions?: CopyFilter | CopyOptions) => {
if (filterOrOptions !== undefined) return promisify3<string, string, CopyFilter | CopyOptions>(
copy_)<void>(src, dest, filterOrOptions)
else return promisify2<string, string>(copy_)<void>(src, dest)
}
export const move = (src: string, dest: string, options?: MoveOptions) => {
if (options !== undefined) return promisify3<string, string, MoveOptions>(
move_)<void>(src, dest, options)
else return promisify2<string, string>(move_)<void>(src, dest)
}
export const createFile = (path: string) => promisify1<string>(createFile_)<void>(path)
export const mkdirp = (dir: string, options?: MkdirOptions) => {
if (options !== undefined) return promisify2<string, MkdirOptions>(mkdirp_)<void>(dir, options)
else return promisify1<string>(mkdirp_)<void>(dir)
}
export const mkdirs = mkdirp
export const outputFile = (file: string, data: any) =>
promisify2<string, any>(outputFile_)<void>(file, data)
export const outputJson = (file: string, data: any) =>
promisify2<string, any>(outputJson_)<void>(file, data)
export const outputJSON = outputJson
export const readJson = (file: string, options?: OpenOptions) => {
if (options !== undefined) return promisify2<string, OpenOptions>(readJson_)<any>(file, options)
else return promisify1<string>(readJson_)<any>(file)
}
export const readJSON = readJson
export const remove = (dir: string) => promisify1<string>(remove_)<void>(dir)
export const writeJson = (file: string, object: any, options?: OpenOptions) => {
if (options !== undefined) return promisify3<string, any, OpenOptions>(
writeJson_)<void>(file, object, options)
else return promisify2<string, any>(writeJson_)<void>(file, object)
}
export const ensureDir = (path: string) => promisify1<string>(ensureDir_)<void>(path)
export const ensureFile = (path: string) => promisify1<string>(ensureFile_)<void>(path)
export const ensureLink = (path: string) => promisify1<string>(ensureLink_)<void>(path)
export const ensureSymlink = (path: string) => promisify1<string>(ensureSymlink_)<void>(path)
export const emptyDir = (path: string) => promisify1<string>(emptyDir_)<void>(path)