bun.lockb
bun.lockb
This is a binary file and will not be displayed.
+27
-6
index.ts
+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
+2
package.json