-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstore.ts
294 lines (249 loc) · 7.33 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { nanoid } from "nanoid"
import { fs, path, dialog, os } from "@tauri-apps/api"
import { BaseDirectory } from "@tauri-apps/api/fs"
import { createStore } from "solid-js/store"
export interface Snippet {
id: string
name: string
createdAt: string
updatedAt: string
language?: string
deletedAt?: string
vscodeSnippet?: {
prefix?: string
}
}
interface AppData {
folders: string[]
}
const [state, setState] = createStore<{
ready: boolean
app: AppData
folder: string | null
snippets: Snippet[]
isMac: boolean
}>({
ready: false,
app: {
folders: [],
},
folder: null,
snippets: [],
isMac: /macintosh/i.test(navigator.userAgent),
})
export { state }
const writeSnippetsJson = async (folder: string, snippets: Snippet[]) => {
console.log("writing snippets.json")
await fs.writeTextFile(
await path.join(folder, "snippets.json"),
JSON.stringify(snippets)
)
}
const writeAppJson = async (appData: AppData) => {
await fs.createDir("", { dir: BaseDirectory.App, recursive: true })
await fs.writeTextFile("app.json", JSON.stringify(appData), {
dir: BaseDirectory.App,
})
}
const pathExists = async (path: string, baseDir?: BaseDirectory) => {
const exists: boolean = await fs.exists(path, { dir: baseDir })
return exists
}
export const actions = {
init: async () => {
const text = await fs
.readTextFile("app.json", { dir: BaseDirectory.App })
.catch((error) => {
console.error(error)
return "{}"
})
const appData: Partial<AppData> = JSON.parse(text)
if (appData.folders) {
setState("app", "folders", appData.folders)
}
setState("ready", true)
},
setFolder: (folder: string | null) => {
setState("folder", folder)
},
removeFolderFromHistory: async (folder: string) => {
setState(
"app",
"folders",
state.app.folders.filter((f) => f !== folder)
)
await writeAppJson(state.app)
},
loadFolder: async (folder: string) => {
const exists = await pathExists(folder)
if (!exists) {
await actions.removeFolderFromHistory(folder)
await dialog.message("Folder doesn't exist")
return
}
const snippetsPath = await path.join(folder, "snippets.json")
const text = await fs.readTextFile(snippetsPath).catch((error) => {
console.error(error)
return null
})
if (text) {
const snippets = JSON.parse(text)
setState("snippets", snippets)
} else {
setState("snippets", [])
}
if (state.app.folders.includes(folder)) {
setState("app", "folders", [
folder,
...state.app.folders.filter((f) => f !== folder),
])
} else {
setState("app", "folders", [folder, ...state.app.folders.slice(0, 10)])
}
await writeAppJson(state.app)
},
createSnippet: async (snippet: Snippet, content: string) => {
if (!state.folder) return
const filepath = await path.join(state.folder, snippet.id)
await fs.writeTextFile(filepath, content)
const snippets = [...state.snippets, snippet]
await writeSnippetsJson(state.folder, snippets)
setState("snippets", snippets)
},
getRandomId: () => {
return nanoid(10)
},
readSnippetContent: async (id: string) => {
if (!state.folder) return ""
const text = await fs.readTextFile(await path.join(state.folder, id))
return text
},
updateSnippet: async <K extends keyof Snippet, V extends Snippet[K]>(
id: string,
key: K,
value: V
) => {
if (!state.folder) return
const snippets = state.snippets.map((snippet) => {
if (snippet.id === id) {
return { ...snippet, [key]: value, updatedAt: new Date().toISOString() }
}
return snippet
})
setState("snippets", snippets)
await writeSnippetsJson(state.folder, snippets)
await actions.syncSnippetsToVscode()
},
updateSnippetContent: async (id: string, content: string) => {
if (!state.folder) return
await fs.writeTextFile(await path.join(state.folder, id), content)
await actions.updateSnippet(id, "updatedAt", new Date().toISOString())
},
moveSnippetsToTrash: async (ids: string[], restore = false) => {
if (!state.folder) return
const snippets = state.snippets.map((snippet) => {
if (ids.includes(snippet.id)) {
return {
...snippet,
deletedAt: restore ? undefined : new Date().toISOString(),
}
}
return snippet
})
setState("snippets", snippets)
await writeSnippetsJson(state.folder, snippets)
await actions.syncSnippetsToVscode()
},
deleteSnippetForever: async (id: string) => {
if (!state.folder) return
const snippets = state.snippets.filter((snippet) => {
return id !== snippet.id
})
await writeSnippetsJson(state.folder, snippets)
await fs.removeFile(await path.join(state.folder, id))
setState("snippets", snippets)
},
emptyTrash: async () => {
if (!state.folder) return
const toDelete: string[] = []
const snippets = state.snippets.filter((snippet) => {
if (snippet.deletedAt) {
toDelete.push(snippet.id)
}
return !snippet.deletedAt
})
await writeSnippetsJson(state.folder, snippets)
await Promise.all(
toDelete.map(async (id) => {
return fs.removeFile(await path.join(state.folder!, id))
})
)
setState("snippets", snippets)
},
getFolderHistory: async () => {
const text = await fs
.readTextFile("folders.json", { dir: BaseDirectory.App })
.catch(() => "[]")
const folders: string[] = JSON.parse(text)
return folders
},
syncSnippetsToVscode: async () => {
if (!state.folder) return
const folderName = state.folder.split(path.sep).pop()!
type VSCodeSnippets = Record<
string,
{ scope: string; prefix: string[]; body: string[]; __folderName: string }
>
const newSnippets: VSCodeSnippets = {}
for (const s of state.snippets) {
const prefix = s.vscodeSnippet?.prefix?.trim()
if (!prefix || s.deletedAt) {
continue
}
newSnippets[s.name] = {
scope: "",
prefix: prefix
.split(",")
.map((v) => v.trim())
.filter(Boolean),
body: [await actions.readSnippetContent(s.id)],
__folderName: folderName,
}
}
const snippetsFileName = "dropcode.code-snippets"
const codeSnippetsDir = `Code${path.sep}User${path.sep}snippets`
const snippetsFilePath = `${codeSnippetsDir}${path.sep}${snippetsFileName}`
// VSCode is not installed
if (!(await pathExists("Code", BaseDirectory.Data))) {
return
}
// Get existing snippets
const snippets: VSCodeSnippets = (await pathExists(
snippetsFilePath,
BaseDirectory.Data
))
? JSON.parse(
await fs.readTextFile(snippetsFilePath, { dir: BaseDirectory.Data })
)
: {}
// Merge old and new snippets
for (const name in snippets) {
const snippet = snippets[name]
if (snippet.__folderName === folderName) {
delete snippets[name]
}
}
Object.assign(snippets, newSnippets)
// Write to file
console.log("writing", snippetsFilePath)
await fs.createDir(codeSnippetsDir, {
recursive: true,
dir: BaseDirectory.Data,
})
await fs.writeTextFile(
snippetsFilePath,
JSON.stringify(snippets, null, 2),
{ dir: BaseDirectory.Data }
)
},
}