feat(fs): accept baseDir in size (#3623)

In plugins/fs/guest-js/index.ts, the size function only sent `path`
although the Rust command accepts `options.baseDir` and the JSDoc example
passed `{ baseDir: BaseDirectory.AppData }` (a TS error, silently dropped
at runtime, so the relative path was rejected).

Adds an optional `options?: SizeOptions` parameter (new exported type).
api-iife.js regenerated. The e2e spec now measures a file and a directory
relative to AppData instead of working around the missing option.
This commit is contained in:
Lucas Fernandes Nogueira
2026-09-23 12:58:03 -03:00
committed by GitHub
parent b46a88ff55
commit 97e865bc13
4 changed files with 30 additions and 5 deletions
+6
View File
@@ -0,0 +1,6 @@
---
fs: minor
fs-js: minor
---
Added an `options` parameter with `baseDir` to `size`, so relative paths can be measured, like the other functions and like the JSDoc example already showed. The Rust command already accepted it.
+5 -2
View File
@@ -116,10 +116,11 @@ describePlugin('fs', () => {
hasMtime: fileStat.mtime instanceof Date
},
lstatSize: fileLstat.size,
// `size` only takes absolute paths
size: await api.fs.size(
size: await api.fs.size(file, { baseDir }),
absoluteSize: await api.fs.size(
await api.path.join(await api.path.appDataDir(), file)
),
dirSize: await api.fs.size(dir, { baseDir }),
dir: { isFile: dirStat.isFile, isDirectory: dirStat.isDirectory }
}
}, dir)
@@ -132,6 +133,8 @@ describePlugin('fs', () => {
})
expect(result.lstatSize).toBe(10)
expect(result.size).toBe(10)
expect(result.absoluteSize).toBe(10)
expect(result.dirSize).toBeGreaterThanOrEqual(10)
expect(result.dir).toEqual({ isFile: false, isDirectory: true })
})
File diff suppressed because one or more lines are too long
+18 -2
View File
@@ -1493,6 +1493,16 @@ async function watchImmediate(
})
}
/**
* Options for the `size` function.
*
* @since 2.6.0
*/
interface SizeOptions {
/** Base directory for `path`. */
baseDir?: BaseDirectory
}
/**
* Get the size of a file or directory. For files, the `stat` functions can be used as well.
*
@@ -1507,16 +1517,21 @@ async function watchImmediate(
* ```
*
* @param path The path of the file or directory to measure.
* @param options Options defining the base directory of `path` (since 2.6.0).
* @returns A promise resolving to the size in bytes.
* @since 2.1.0
*/
async function size(path: string | URL): Promise<number> {
async function size(
path: string | URL,
options?: SizeOptions
): Promise<number> {
if (path instanceof URL && path.protocol !== 'file:') {
throw new TypeError('Must be a file URL.')
}
return await invoke('plugin:fs|size', {
path: path instanceof URL ? path.toString() : path
path: path instanceof URL ? path.toString() : path,
options
})
}
@@ -1609,6 +1624,7 @@ export type {
TruncateOptions,
WriteFileOptions,
ExistsOptions,
SizeOptions,
FileInfo,
WatchOptions,
DebouncedWatchOptions,