|
@@ -96,6 +96,7 @@ export enum BaseDirectory {
|
|
|
|
|
|
interface FsOptions {
|
|
|
dir?: BaseDirectory
|
|
|
+ // note that adding fields here needs a change in the writeBinaryFile check
|
|
|
}
|
|
|
|
|
|
interface FsDirOptions {
|
|
@@ -111,12 +112,14 @@ interface FsTextFileOption {
|
|
|
contents: string
|
|
|
}
|
|
|
|
|
|
+type BinaryFileContents = Iterable<number> | ArrayLike<number>
|
|
|
+
|
|
|
/** Options object used to write a binary data to a file. */
|
|
|
interface FsBinaryFileOption {
|
|
|
/** Path to the file to write. */
|
|
|
path: string
|
|
|
/** The byte array contents. */
|
|
|
- contents: Iterable<number> | ArrayLike<number>
|
|
|
+ contents: BinaryFileContents
|
|
|
}
|
|
|
|
|
|
interface FileEntry {
|
|
@@ -177,19 +180,61 @@ async function readBinaryFile(
|
|
|
/**
|
|
|
* Writes a UTF-8 text file.
|
|
|
*
|
|
|
- * @param file File configuration object.
|
|
|
+ * @param path The file path.
|
|
|
+ * @param contents The file contents.
|
|
|
+ * @param options Configuration object.
|
|
|
+ * @returns A promise indicating the success or failure of the operation.
|
|
|
+ */
|
|
|
+async function writeTextFile(
|
|
|
+ path: string,
|
|
|
+ contents: string,
|
|
|
+ options?: FsOptions
|
|
|
+): Promise<void>
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writes a UTF-8 text file.
|
|
|
+ *
|
|
|
+ * @param file The object containing the file path and contents.
|
|
|
* @param options Configuration object.
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
*/
|
|
|
-async function writeFile(
|
|
|
+async function writeTextFile(
|
|
|
file: FsTextFileOption,
|
|
|
- options: FsOptions = {}
|
|
|
+ options?: FsOptions
|
|
|
+): Promise<void>
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writes a UTF-8 text file.
|
|
|
+ *
|
|
|
+ * @param file File configuration object.
|
|
|
+ * @param options Configuration object.
|
|
|
+ * @returns A promise indicating the success or failure of the operation.
|
|
|
+ */
|
|
|
+async function writeTextFile(
|
|
|
+ path: string | FsTextFileOption,
|
|
|
+ contents?: string | FsOptions,
|
|
|
+ options?: FsOptions
|
|
|
): Promise<void> {
|
|
|
if (typeof options === 'object') {
|
|
|
Object.freeze(options)
|
|
|
}
|
|
|
- if (typeof file === 'object') {
|
|
|
- Object.freeze(file)
|
|
|
+ if (typeof path === 'object') {
|
|
|
+ Object.freeze(path)
|
|
|
+ }
|
|
|
+
|
|
|
+ const file: FsTextFileOption = { path: '', contents: '' }
|
|
|
+ let fileOptions: FsOptions | undefined = options
|
|
|
+ if (typeof path === 'string') {
|
|
|
+ file.path = path
|
|
|
+ } else {
|
|
|
+ file.path = path.path
|
|
|
+ file.contents = path.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ if (typeof contents === 'string') {
|
|
|
+ file.contents = contents ?? ''
|
|
|
+ } else {
|
|
|
+ fileOptions = contents
|
|
|
}
|
|
|
|
|
|
return invokeTauriCommand({
|
|
@@ -198,7 +243,7 @@ async function writeFile(
|
|
|
cmd: 'writeFile',
|
|
|
path: file.path,
|
|
|
contents: Array.from(new TextEncoder().encode(file.contents)),
|
|
|
- options
|
|
|
+ options: fileOptions
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -206,19 +251,62 @@ async function writeFile(
|
|
|
/**
|
|
|
* Writes a byte array content to a file.
|
|
|
*
|
|
|
- * @param file Write configuration object.
|
|
|
+ * @param path The file path.
|
|
|
+ * @param contents The file contents.
|
|
|
+ * @param options Configuration object.
|
|
|
+ * @returns A promise indicating the success or failure of the operation.
|
|
|
+ */
|
|
|
+async function writeBinaryFile(
|
|
|
+ path: string,
|
|
|
+ contents: BinaryFileContents,
|
|
|
+ options?: FsOptions
|
|
|
+): Promise<void>
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writes a byte array content to a file.
|
|
|
+ *
|
|
|
+ * @param file The object containing the file path and contents.
|
|
|
* @param options Configuration object.
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
*/
|
|
|
async function writeBinaryFile(
|
|
|
file: FsBinaryFileOption,
|
|
|
- options: FsOptions = {}
|
|
|
+ options?: FsOptions
|
|
|
+): Promise<void>
|
|
|
+
|
|
|
+/**
|
|
|
+ * Writes a byte array content to a file.
|
|
|
+ *
|
|
|
+ * @param file Write configuration object.
|
|
|
+ * @param options Configuration object.
|
|
|
+ * @returns A promise indicating the success or failure of the operation.
|
|
|
+ */
|
|
|
+async function writeBinaryFile(
|
|
|
+ path: string | FsBinaryFileOption,
|
|
|
+ contents?: BinaryFileContents | FsOptions,
|
|
|
+ options?: FsOptions
|
|
|
): Promise<void> {
|
|
|
if (typeof options === 'object') {
|
|
|
Object.freeze(options)
|
|
|
}
|
|
|
- if (typeof file === 'object') {
|
|
|
- Object.freeze(file)
|
|
|
+ if (typeof path === 'object') {
|
|
|
+ Object.freeze(path)
|
|
|
+ }
|
|
|
+
|
|
|
+ const file: FsBinaryFileOption = { path: '', contents: [] }
|
|
|
+ let fileOptions: FsOptions | undefined = options
|
|
|
+ if (typeof path === 'string') {
|
|
|
+ file.path = path
|
|
|
+ } else {
|
|
|
+ file.path = path.path
|
|
|
+ file.contents = path.contents
|
|
|
+ }
|
|
|
+
|
|
|
+ if (contents && 'dir' in contents) {
|
|
|
+ fileOptions = contents
|
|
|
+ } else {
|
|
|
+ // @ts-expect-error
|
|
|
+ file.contents = contents ?? []
|
|
|
}
|
|
|
|
|
|
return invokeTauriCommand({
|
|
@@ -227,7 +315,7 @@ async function writeBinaryFile(
|
|
|
cmd: 'writeFile',
|
|
|
path: file.path,
|
|
|
contents: Array.from(file.contents),
|
|
|
- options
|
|
|
+ options: fileOptions
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -371,6 +459,7 @@ export type {
|
|
|
FsOptions,
|
|
|
FsDirOptions,
|
|
|
FsTextFileOption,
|
|
|
+ BinaryFileContents,
|
|
|
FsBinaryFileOption,
|
|
|
FileEntry
|
|
|
}
|
|
@@ -379,7 +468,8 @@ export {
|
|
|
BaseDirectory as Dir,
|
|
|
readTextFile,
|
|
|
readBinaryFile,
|
|
|
- writeFile,
|
|
|
+ writeTextFile,
|
|
|
+ writeTextFile as writeFile,
|
|
|
writeBinaryFile,
|
|
|
readDir,
|
|
|
createDir,
|