-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
fs.ts
350 lines (327 loc) · 6.79 KB
/
fs.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { invokeTauriCommand } from './helpers/tauri'
export enum BaseDirectory {
Audio = 1,
Cache,
Config,
Data,
LocalData,
Desktop,
Document,
Download,
Executable,
Font,
Home,
Picture,
Public,
Runtime,
Template,
Video,
Resource,
App,
Current
}
export interface FsOptions {
dir?: BaseDirectory
}
export interface FsDirOptions {
dir?: BaseDirectory
recursive?: boolean
}
export interface FsTextFileOption {
path: string
contents: string
}
export interface FsBinaryFileOption {
path: string
contents: ArrayBuffer
}
export interface FileEntry {
path: string
// name of the directory/file
// can be null if the path terminates with `..`
name?: string
// children of this entry if it's a directory; null otherwise
children?: FileEntry[]
}
/**
* @name readTextFile
* @description Reads a file as text
* @param {string} filePath path to the file
* @param {FsOptions} [options] configuration object
* @param {BaseDirectory} [options.dir] base directory
* @return {Promise<string>}
*/
async function readTextFile(
filePath: string,
options: FsOptions = {}
): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Fs',
message: {
cmd: 'readTextFile',
path: filePath,
options
}
})
}
/**
* @name readBinaryFile
* @description Reads a file as binary
* @param {string} filePath path to the file
* @param {FsOptions} [options] configuration object
* @param {BaseDirectory} [options.dir] base directory
* @return {Promise<number[]>}
*/
async function readBinaryFile(
filePath: string,
options: FsOptions = {}
): Promise<number[]> {
return invokeTauriCommand<number[]>({
__tauriModule: 'Fs',
message: {
cmd: 'readBinaryFile',
path: filePath,
options
}
})
}
/**
* writes a text file
*
* @param file
* @param file.path path of the file
* @param file.contents contents of the file
* @param [options] configuration object
* @param [options.dir] base directory
* @return
*/
async function writeFile(
file: FsTextFileOption,
options: FsOptions = {}
): Promise<void> {
if (typeof options === 'object') {
Object.freeze(options)
}
if (typeof file === 'object') {
Object.freeze(file)
}
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'writeFile',
path: file.path,
contents: file.contents,
options
}
})
}
const CHUNK_SIZE = 65536
/**
* convert an Uint8Array to ascii string
*
* @param arr
* @return ASCII string
*/
function uint8ArrayToString(arr: Uint8Array): string {
if (arr.length < CHUNK_SIZE) {
return String.fromCharCode.apply(null, Array.from(arr))
}
let result = ''
const arrLen = arr.length
for (let i = 0; i < arrLen; i++) {
const chunk = arr.subarray(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
result += String.fromCharCode.apply(null, Array.from(chunk))
}
return result
}
/**
* convert an ArrayBuffer to base64 encoded string
*
* @param buffer
* @return base64 encoded string
*/
function arrayBufferToBase64(buffer: ArrayBuffer): string {
const str = uint8ArrayToString(new Uint8Array(buffer))
return btoa(str)
}
/**
* writes a binary file
*
* @param file
* @param file.path path of the file
* @param file.contents contents of the file
* @param [options] configuration object
* @param [options.dir] base directory
* @return
*/
async function writeBinaryFile(
file: FsBinaryFileOption,
options: FsOptions = {}
): Promise<void> {
if (typeof options === 'object') {
Object.freeze(options)
}
if (typeof file === 'object') {
Object.freeze(file)
}
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'writeBinaryFile',
path: file.path,
contents: arrayBufferToBase64(file.contents),
options
}
})
}
/**
* list directory files
*
* @param dir path to the directory to read
* @param [options] configuration object
* @param [options.recursive] whether to list dirs recursively or not
* @param [options.dir] base directory
* @return
*/
async function readDir(
dir: string,
options: FsDirOptions = {}
): Promise<FileEntry[]> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'readDir',
path: dir,
options
}
})
}
/**
* Creates a directory
* If one of the path's parent components doesn't exist
* and the `recursive` option isn't set to true, it will be rejected
*
* @param dir path to the directory to create
* @param [options] configuration object
* @param [options.recursive] whether to create the directory's parent components or not
* @param [options.dir] base directory
* @return
*/
async function createDir(
dir: string,
options: FsDirOptions = {}
): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'createDir',
path: dir,
options
}
})
}
/**
* Removes a directory
* If the directory is not empty and the `recursive` option isn't set to true, it will be rejected
*
* @param dir path to the directory to remove
* @param [options] configuration object
* @param [options.recursive] whether to remove all of the directory's content or not
* @param [options.dir] base directory
* @return
*/
async function removeDir(
dir: string,
options: FsDirOptions = {}
): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'removeDir',
path: dir,
options
}
})
}
/**
* Copy file
*
* @param source
* @param destination
* @param [options] configuration object
* @param [options.dir] base directory
* @return
*/
async function copyFile(
source: string,
destination: string,
options: FsOptions = {}
): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'copyFile',
source,
destination,
options
}
})
}
/**
* Removes a file
*
* @param file path to the file to remove
* @param [options] configuration object
* @param [options.dir] base directory
* @return
*/
async function removeFile(
file: string,
options: FsOptions = {}
): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'removeFile',
path: file,
options: options
}
})
}
/**
* Renames a file
*
* @param oldPath
* @param newPath
* @param [options] configuration object
* @param [options.dir] base directory
* @return
*/
async function renameFile(
oldPath: string,
newPath: string,
options: FsOptions = {}
): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'renameFile',
oldPath,
newPath,
options
}
})
}
export {
BaseDirectory as Dir,
readTextFile,
readBinaryFile,
writeFile,
writeBinaryFile,
readDir,
createDir,
removeDir,
copyFile,
removeFile,
renameFile
}