Skip to content

Commit d61105b

Browse files
feat: file read and write abstraction (#47)
1 parent 877fcc1 commit d61105b

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/files/files.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { arrayFromJava } from '../java';
2+
3+
export interface FileStat {
4+
/**
5+
* The size of the file in bytes.
6+
*/
7+
size: number;
8+
9+
/**
10+
* Whether or not this file is a directory.
11+
*/
12+
isDirectory: boolean;
13+
}
14+
15+
/**
16+
* A module for reading and writing files.
17+
*/
18+
export const Files = {
19+
/**
20+
* Checks if a file exists.
21+
* @param filename the name of the file to check
22+
* @returns true if the file exists, false otherwise
23+
*/
24+
exists: (filename: string): boolean => {
25+
const file = new java.io.File(filename);
26+
return file.exists();
27+
},
28+
29+
/**
30+
* Gets stats about a file.
31+
* @param filename the name of the file to stat
32+
* @returns the stats of the file, or null if the file doesn't exist
33+
*/
34+
stat: (filename: string): FileStat | null => {
35+
const file = new java.io.File(filename);
36+
if (!file.exists()) return null;
37+
return {
38+
size: file.length(),
39+
isDirectory: file.isDirectory(),
40+
};
41+
},
42+
43+
/**
44+
* Lists the contents of a directory.
45+
* @param dirname the name of the directory to read
46+
*/
47+
readdir: (dirname: string): string[] | null => {
48+
const file = new java.io.File(dirname);
49+
if (!file.exists() || !file.isDirectory()) return null;
50+
return arrayFromJava(file.list() ?? []);
51+
},
52+
53+
/**
54+
* Gets the name of the directory containing a file.
55+
* @param filename the name of the file to get the directory name of
56+
* @returns the directory name of the file, or null if the file doesn't exist
57+
*/
58+
dirname: (filename: string): string | null => {
59+
const file = new java.io.File(filename);
60+
return file.getParent();
61+
},
62+
63+
/**
64+
* Creates a directory.
65+
* @param dirname the name of the directory to create
66+
* @param recursive whether or not to create parent directories if they don't exist
67+
*/
68+
mkdir: (dirname: string, recursive = false): void => {
69+
const file = new java.io.File(dirname);
70+
if (recursive) {
71+
file.mkdirs();
72+
} else {
73+
file.mkdir();
74+
}
75+
},
76+
77+
/**
78+
* Removes a file or directory. Directories must be empty to be removed.
79+
* @param filename the name of the file or directory to remove
80+
*/
81+
remove: (filename: string): void => {
82+
const file = new java.io.File(filename);
83+
if (!file.exists()) return;
84+
file.delete();
85+
},
86+
87+
/**
88+
* Reads the contents of a file and returns it as a string.
89+
* @param filename the name of the file to read
90+
* @returns the contents of the file as a string, or null if the file doesn't exist
91+
*/
92+
readString: (filename: string): string | null => {
93+
const file = new java.io.File(filename);
94+
if (!file.exists() || !file.isFile()) return null;
95+
const scanner = new java.util.Scanner(file).useDelimiter('\\Z');
96+
return scanner.next();
97+
},
98+
99+
/**
100+
* Writes a string to a file.
101+
* @param filename the name of the file to write
102+
* @param content the content to write to the file
103+
*/
104+
writeString: (filename: string, content: string): void => {
105+
const file = new java.io.File(filename);
106+
const writer = new java.io.FileWriter(file);
107+
writer.write(content);
108+
writer.close();
109+
},
110+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export const Bukkit = org.bukkit.Bukkit;
1212
export * from './command/CommandCall';
1313
export * from './command/ServerCommands';
1414
export * from './events/ServerEvents';
15+
export * from './files/files';

0 commit comments

Comments
 (0)