Skip to content

Commit 8633827

Browse files
committed
Generate snippets to dist
1 parent aaab2e7 commit 8633827

File tree

9 files changed

+46
-135
lines changed

9 files changed

+46
-135
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
*.vsix
3+
dist

javascript.code-snippets

Lines changed: 0 additions & 66 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
},
3939
"scripts": {
4040
"publish": "vsce package && vsce publish",
41-
"generate": "deno run --allow-write src/app.ts"
41+
"generate": "deno run --allow-write --allow-read src/app.ts",
42+
"docs": "deno run --allow-write --allow-read src/docs-gen.ts"
4243
},
4344
"contributes": {
4445
"snippets": [

src/app.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
import { javascript, typescript } from "./snippets/app.ts";
1+
import { variants } from "./snippets/app.ts";
22
import {
33
convertToVscSnippet,
44
groupSnippets,
5-
logTables,
65
writeSnippetFile,
76
} from "./utils/app.ts";
87

9-
const javascriptVscArray = javascript.map(convertToVscSnippet);
10-
const typescriptVscArray = typescript.map(convertToVscSnippet);
11-
12-
// Log tables for every section of every language
13-
logTables("JavaScript", javascriptVscArray);
14-
logTables("TypeScript", typescriptVscArray);
15-
16-
// Create single snippet object
17-
const snippetJavascript = groupSnippets(javascriptVscArray);
18-
const snippetTypescript = groupSnippets(typescriptVscArray);
19-
20-
// Single snippet files
21-
writeSnippetFile("javascript", snippetJavascript);
22-
writeSnippetFile("typescript", snippetTypescript);
8+
variants.forEach((variant) => {
9+
const snippetSections = variant.snippets.map(convertToVscSnippet);
10+
const snippetSingle = groupSnippets(snippetSections);
11+
writeSnippetFile(variant.language.toLowerCase(), snippetSingle);
12+
});

src/docs-gen.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { variants } from "./snippets/app.ts";
2+
import { convertToVscSnippet, logTables } from "./utils/app.ts";
3+
4+
variants.forEach((option) => {
5+
const snippetSections = option.snippets.map(convertToVscSnippet);
6+
logTables(option.language, snippetSections);
7+
});

src/snippets/app.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
export * from "./js/app.ts";
2-
export * from "./ts/app.ts";
1+
import { javascript } from "./js/app.ts";
2+
import { typescript } from "./ts/app.ts";
3+
4+
export const variants = [
5+
{
6+
language: "JavaScript",
7+
snippets: javascript,
8+
},
9+
{
10+
language: "TypeScript",
11+
snippets: typescript,
12+
},
13+
];

src/snippets/js/declarations.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ export const declarations = {
88
body: "let $0",
99
},
1010
ca: {
11-
name: "const assignment",
11+
name: "const assignment",
1212
body: "const $1 = $0",
1313
},
1414
la: {
15-
name: "let assignment",
15+
name: "let assignment",
1616
body: "let $1 = $0",
1717
},
1818
cas: {
19-
name: "const string",
19+
name: "const string",
2020
body: "const $1 = '$0'",
2121
},
2222
car: {
23-
name: "const array",
23+
name: "const array",
2424
body: "const $1 = [$0]",
2525
},
2626
cao: {
27-
name: "const object",
27+
name: "const object",
2828
body: "const $1 = { $0 }",
2929
},
3030
};

src/utils/app.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { markdownTable } from "https://esm.sh/markdown-table@3";
22
import { VscSnippet, VscSnippetDict, XSnippetDict } from "../models/app.ts";
3+
import { ensureDirSync } from "https://deno.land/std@0.141.0/fs/ensure_dir.ts";
4+
5+
const SYMBOL = "⚡";
6+
7+
const code = (str: string) => `\`${str}\``;
8+
const replaceSymbol = (str: string) => str.replace(` ${SYMBOL}`, "");
39

410
// only to move current code-snippets to my format
511
export const vscToCustom = (snippets: VscSnippetDict): XSnippetDict => {
612
return Object.entries(snippets).reduce((acc, [name, { prefix, body }]) => {
7-
acc[prefix as string] = { name: name.replace(` ⚡`, ""), body };
13+
acc[prefix as string] = { name: replaceSymbol(name), body };
814
return acc;
915
}, {} as XSnippetDict);
1016
};
1117

1218
export const convertToVscSnippet = (snippets: XSnippetDict): VscSnippetDict => {
1319
return Object.entries(snippets)
1420
.reduce((acc, [prefix, { name, body }]) => {
15-
const styledName = `${name} `;
21+
const styledName = `${name} ${SYMBOL}`;
1622
acc[styledName] = { prefix, body } as VscSnippet;
1723
return acc;
1824
}, {} as VscSnippetDict);
@@ -26,19 +32,18 @@ export const groupSnippets = (dicts: VscSnippetDict[]) => {
2632
};
2733

2834
export const writeSnippetFile = (name: string, data: VscSnippetDict) => {
35+
const file = `./dist/${name}.code-snippets`; // `deep` doesn't need to exist
36+
ensureDirSync("./dist"); // ensures directory (and all parent directories
37+
2938
Deno.writeTextFileSync(
30-
`./${name}.code-snippets`,
39+
file,
3140
JSON.stringify(data, null, 2),
3241
);
3342
};
3443

3544
export const generateMarkdownTable = (input: VscSnippetDict) => {
3645
const header = ["Prefix", "Description", "Body"];
3746

38-
const symbol = "⚡";
39-
const replaceSymbol = (str: string) => str.replace(` ${symbol}`, "");
40-
const code = (str: string) => `\`${str}\``;
41-
4247
const rows = Object.entries(input)
4348
.map(([name, { prefix, body }]) => [
4449
code(prefix as string),

typescript.code-snippets

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)