-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
77 lines (64 loc) · 2.11 KB
/
main.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
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
interface SnippetsJson {
[key: string]: Snippet;
}
interface Snippet {
prefix: string;
body: string[];
description?: string;
}
// TODO(kaiinui): Currently it will brake existing snippets jsons due to not reading them before process .snippets dir.
// TODO(kaiinui): No error handling about directory structure, even if given dir is not exist.
async function main() {
await new Command()
.name("dotsnippets")
.description("Manage your VSCode snippets in .snippets directory.")
.version("v0.0.1")
.option("-d, --dir <path>", "The .snippets dir path.", {
default: getVsCodeSnippetsRootPath(),
})
.action(async ({dir}) => {
await iterateSnippetsFolder(dir);
}).parse()
}
async function iterateSnippetsFolder(snippetsFolderPath: string) {
for await (const file of Deno.readDir(snippetsFolderPath)) {
if (!file.isDirectory) {
continue;
}
const json = await transformSnippetsDirectoryToSnippetsJson(
`${snippetsFolderPath}/${file.name}`,
);
const out = JSON.stringify(json, null, 2);
const outPath = `${getVsCodeSnippetsRootPath()}${file.name}.json`;
await Deno.writeTextFile(outPath, out);
}
}
async function transformSnippetsDirectoryToSnippetsJson(
path: string,
): Promise<SnippetsJson> {
const json: SnippetsJson = {};
for await (const file of Deno.readDir(path)) {
if (!file.isFile) {
continue;
}
const fileNameComponents = file.name.split(".");
// given jest.config.default.js as file -> expect jest.config.default
// given jest.js as file -> expect jest
const prefix = fileNameComponents.slice(0, fileNameComponents.length - 1)
.join(".");
const body = await Deno.readTextFile(`${path}/${file.name}`);
const bodyLines = body.split("\n");
json[prefix] = {
prefix: prefix,
body: bodyLines,
};
}
return json;
}
function getVsCodeSnippetsRootPath(): string {
// FIXME(kaiinui): Currently MacOS only.
return Deno.env.get("HOME") +
"/Library/Application Support/Code/User/snippets/";
}
await main();