automerge daemon to keep local files in sync

Merge branch 'main' of github.com:Chickensoupwithrice/unison-automerge

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 15 17 const repo = new Repo({ ··· 70 72 // Create the root folder handle that will accumulate all documents 71 73 const folderHandle = repo.create<Folder>({ 72 74 name: path.basename(startPath), 75 + contentType: "application/vnd.automerge.folder", 73 76 contents: [] 74 77 }) 75 78 ··· 87 90 88 91 if (stats.isFile()) { 89 92 const fileHandle = repo.create<ImportedFile>() 90 - const contents = await readFile(currentPath, 'utf-8') 93 + 94 + const isBinary = isBinaryPath(currentPath); 95 + const buffer = await readFile(currentPath); 96 + const mimeType = mime.lookup(currentPath); 97 + 98 + 99 + console.log({ currentPath, mimeType, isBinary }) 91 100 92 - fileHandle.change(d => { 93 - d.contents = contents 94 - d.name = path.basename(currentPath) 95 - d.executable = !!(stats.mode & 0o111) 96 - }) 101 + if (isBinary) { 102 + fileHandle.change(d => { 103 + d.contents = Uint8Array.from(buffer) 104 + d.contentType = mimeType || "application/octet-stream" 105 + d.name = path.basename(currentPath) 106 + d.executable = !!(stats.mode & 0o111) 107 + }) 108 + } else { 109 + const contents = await readFile(currentPath, 'utf-8') 110 + fileHandle.change(d => { 111 + d.contents = contents 112 + d.contentType = mimeType || "text/plain" 113 + d.name = path.basename(currentPath) 114 + d.executable = !!(stats.mode & 0o111) 115 + }) 116 + } 97 117 98 118 parentHandle.change(d => { 99 119 d.contents.push({ ··· 108 128 if (stats.isDirectory()) { 109 129 const dirHandle = repo.create<Folder>({ 110 130 name: path.basename(currentPath), 131 + contentType: "application/vnd.automerge.folder", 111 132 contents: [] 112 133 }) 113 134
+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 }