automerge daemon to keep local files in sync

okay, mime-type detection

bun.lockb

This is a binary file and will not be displayed.

+27 -6
index.ts
··· 10 10 import { cosmiconfig } from 'cosmiconfig'; 11 11 import ignore, { Ignore } from 'ignore'; 12 12 import type { Stats } from 'fs'; 13 + import isBinaryPath from 'is-binary-path'; 14 + import mime from "mime-types"; 13 15 14 16 const repo = new Repo({ 15 17 network: [ ··· 69 71 // Create the root folder handle that will accumulate all documents 70 72 const folderHandle = repo.create<Folder>({ 71 73 name: path.basename(startPath), 74 + contentType: "application/vnd.automerge.folder", 72 75 contents: [] 73 76 }) 74 77 ··· 86 89 87 90 if (stats.isFile()) { 88 91 const fileHandle = repo.create<ImportedFile>() 89 - const contents = await readFile(currentPath, 'utf-8') 92 + 93 + const isBinary = isBinaryPath(currentPath); 94 + const buffer = await readFile(currentPath); 95 + const mimeType = mime.lookup(currentPath); 96 + 97 + 98 + console.log({ currentPath, mimeType, isBinary }) 90 99 91 - fileHandle.change(d => { 92 - d.contents = contents 93 - d.name = path.basename(currentPath) 94 - d.executable = !!(stats.mode & 0o111) 95 - }) 100 + if (isBinary) { 101 + fileHandle.change(d => { 102 + d.contents = Uint8Array.from(buffer) 103 + d.contentType = mimeType || "application/octet-stream" 104 + d.name = path.basename(currentPath) 105 + d.executable = !!(stats.mode & 0o111) 106 + }) 107 + } else { 108 + const contents = await readFile(currentPath, 'utf-8') 109 + fileHandle.change(d => { 110 + d.contents = contents 111 + d.contentType = mimeType || "text/plain" 112 + d.name = path.basename(currentPath) 113 + d.executable = !!(stats.mode & 0o111) 114 + }) 115 + } 96 116 97 117 parentHandle.change(d => { 98 118 d.contents.push({ ··· 107 127 if (stats.isDirectory()) { 108 128 const dirHandle = repo.create<Folder>({ 109 129 name: path.basename(currentPath), 130 + contentType: "application/vnd.automerge.folder", 110 131 contents: [] 111 132 }) 112 133
+2
package.json
··· 5 5 "@automerge/automerge-repo-network-websocket": "2.0.0-alpha.13", 6 6 "commander": "^13.0.0", 7 7 "cosmiconfig": "^9.0.0", 8 + "file-type": "^19.6.0", 8 9 "ignore": "^7.0.0", 10 + "is-binary-path": "^3.0.0", 9 11 "ora": "^8.1.1" 10 12 } 11 13 }
+1
types.ts
··· 8 8 } 9 9 10 10 export interface Folder { 11 + contentType: string 11 12 name: string 12 13 contents: FolderItem[] 13 14 }