Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update runtime.selectFile() to also return the file name #81

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randomBytes } from 'node:crypto'
import { basename } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineMessages } from '@formatjs/intl'
import debug from 'debug'
Expand Down Expand Up @@ -243,7 +244,14 @@ function initMainWindow({ appMode, services }) {
: undefined,
})

return result.filePaths[0]
const selectedFilePath = result.filePaths[0]

if (!selectedFilePath) return undefined

return {
name: basename(selectedFilePath),
path: selectedFilePath,
}
})

APP_STATE.browserWindows.set(mainWindow, {
Expand Down
30 changes: 25 additions & 5 deletions src/preload/main-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,36 @@ const runtimeApi = {

// Files
async selectFile(extensionFilters) {
const filePath = await ipcRenderer.invoke('files:select', {
/** @type {unknown} */
const result = await ipcRenderer.invoke('files:select', {
extensionFilters,
})

if (!(typeof filePath === 'string' || typeof filePath === 'undefined')) {
throw new Error(`File path is unexpected type: ${typeof filePath}`)
}
if (!result) return undefined

validateSelectedFileResult(result)

return filePath
return result
},
}

/**
* @param {NonNullable<unknown>} value
*
* @returns {asserts value is import('./runtime.js').SelectedFile}
*/
function validateSelectedFileResult(value) {
if (!('path' in value && 'name' in value)) {
throw new Error('Value has invalid shape')
}

if (typeof value.path !== 'string') {
throw new Error('Value has invalid path field')
}

if (typeof value.name !== 'string') {
throw new Error('Value has invalid name field')
}
}
Comment on lines +49 to +61
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally would use valibot for this but I don't have bundling set up for the preload just yet so I think it would require some additional tooling work.


contextBridge.exposeInMainWorld('runtime', runtimeApi)
9 changes: 8 additions & 1 deletion src/preload/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export type SelectedFile = {
name: string
path: string
}

export type RuntimeApi = {
getLocale: () => Promise<string>
updateLocale: (locale: string) => void
selectFile: (extensionFilters?: Array<string>) => Promise<string | undefined>
selectFile: (
extensionFilters?: Array<string>,
) => Promise<SelectedFile | undefined>
}
Loading