-
Notifications
You must be signed in to change notification settings - Fork 6
/
fs.ts
121 lines (112 loc) · 3.24 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
import { $ } from './$';
import { bufferSourceToArrayBuffer, pathToString, toPromise } from './utils';
import { encoder } from './polyfills/text-encoder';
import type { PathLike } from './switch';
/**
* Creates the directory at the provided `path`, as well as any necessary parent directories.
*
* @example
*
* ```typescript
* const count = Switch.mkdirSync('sdmc:/foo/bar/baz');
* console.log(`Created ${count} directories`);
* // Created 3 directories
* ```
*
* @param path Path of the directory to create.
* @param mode The file mode to set for the directories. Default: `0o777`.
* @returns The number of directories created. If the directory already exists, returns `0`.
*/
export function mkdirSync(path: PathLike, mode = 0o777) {
return $.mkdirSync(pathToString(path), mode);
}
/**
* Returns a Promise which resolves to an `ArrayBuffer` containing
* the contents of the file at `path`.
*
* @example
*
* ```typescript
* const buffer = await Switch.readFile('sdmc:/switch/awesome-app/state.json');
* const gameState = JSON.parse(new TextDecoder().decode(buffer));
* ```
*/
export function readFile(path: PathLike) {
return toPromise($.readFile, pathToString(path));
}
/**
* Synchronously returns an array of the file names within `path`.
*
* @example
*
* ```typescript
* for (const file of Switch.readDirSync('sdmc:/')) {
* // … do something with `file` …
* }
* ```
*/
export function readDirSync(path: PathLike) {
return $.readDirSync(pathToString(path));
}
/**
* Synchronously returns an `ArrayBuffer` containing the contents
* of the file at `path`.
*
* @example
*
* ```typescript
* const buffer = Switch.readFileSync('sdmc:/switch/awesome-app/state.json');
* const appState = JSON.parse(new TextDecoder().decode(buffer));
* ```
*/
export function readFileSync(path: PathLike) {
return $.readFileSync(pathToString(path));
}
/**
* Synchronously writes the contents of `data` to the file at `path`.
*
* @example
*
* ```typescript
* const appStateJson = JSON.stringify(appState);
* Switch.writeFileSync('sdmc:/switch/awesome-app/state.json', appStateJson);
* ```
*/
export function writeFileSync(path: PathLike, data: string | BufferSource) {
const d = typeof data === 'string' ? encoder.encode(data) : data;
const ab = bufferSourceToArrayBuffer(d);
return $.writeFileSync(pathToString(path), ab);
}
/**
* Synchronously removes the file or directory recursively specified by `path`.
*
* @param path File path to remove.
*/
export function removeSync(path: PathLike) {
return $.removeSync(pathToString(path));
}
/**
* Removes the file or directory recursively specified by `path`.
*
* @param path File path to remove.
*/
export function remove(path: PathLike) {
return toPromise($.remove, pathToString(path));
}
/**
*
* @param path File path to retrieve file stats for.
* @returns Object containing the file stat information of `path`, or `null` if the file does not exist.
*/
export function statSync(path: PathLike) {
return $.statSync(pathToString(path));
}
/**
* Returns a Promise which resolves to an object containing
* information about the file pointed to by `path`.
*
* @param path File path to retrieve file stats for.
*/
export function stat(path: PathLike) {
return toPromise($.stat, pathToString(path));
}