$ yarn
$ yarn dev
# For windows
$ yarn build:win
# For macOS
$ yarn build:mac
# For Linux
$ yarn build:linux
Reference:
Example:
Add to renderer/store/notes.ts
const loadNotes = async () => {
const notes = await window.context.getNotes()
// sort them by most recently edited
return notes.sort((a, b) => b.lastEditTime - a.lastEditTime)
}
const notesAtomAsync = atom<NoteInfo[] | Promise<NoteInfo[]>>(loadNotes())
export const notesAtom = unwrap(notesAtomAsync, (prev) => prev)
export const selectedNoteIndexAtom = atom<number | null>(null)
- Define function types in
@shared/types
export type GetNotes = () => Promise<NoteInfo[]>
- Define function in
main/lib
export const getNotes: GetNotes = async () => {
const rootDir = getRootDir()
await ensureDir(rootDir)
const notesFileNames = await readdir(rootDir, {
encoding: fileEncoding,
withFileTypes: false
})
const notes = notesFileNames.filter((fileName) => fileName.endsWith('.md'))
if (isEmpty(notes)) {
console.info('No notes found, creating a welcome note')
const content = await readFile(welcomeNoteFile, { encoding: fileEncoding })
// create the welcome note
await writeFile(`${rootDir}/${welcomeNoteFilename}`, content, { encoding: fileEncoding })
notes.push(welcomeNoteFilename)
}
return Promise.all(notes.map(getNoteInfoFromFilename))
}
- Register listener in
main/index.ts
ipcMain.handle('getNotes', (_, ...args: Parameters<GetNotes>) => getNotes(...args))
- Add to
preload/index.ts
contextBridge.exposeInMainWorld('context', {
locale: navigator.language,
// Here
getNotes: (...args: Parameters<GetNotes>) => ipcRenderer.invoke('getNotes', ...args),
})
- Add type to
context
inpreload/index.d.ts
- Call from renderer through window ->
window.context.getNotes()
import {appDirectoryName} from "@shared/constants"
export const getRootDir = () => {
return `${homedir()}/${appDirectoryName}`
}