Skip to content

Commit 19bee22

Browse files
committed
Automate markdown table generation, define snippet definition with meta
1 parent bd917d7 commit 19bee22

33 files changed

+1253
-1021
lines changed

README.md

Lines changed: 221 additions & 193 deletions
Large diffs are not rendered by default.

src/app.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { parse } from "https://deno.land/std@0.168.0/flags/mod.ts";
1+
import { parse } from "./deps.ts";
2+
import { VscSnippetDefinition } from "./models/app.ts";
23
import { variants } from "./snippets/app.ts";
4+
import { logTables } from "./utils/markdown.ts";
35
import {
46
convertToVscSnippet,
57
generateSnippetsFile,
68
groupSnippets,
7-
logTables,
8-
} from "./utils/app.ts";
9+
} from "./utils/snippets.ts";
910

1011
const flags = parse(Deno.args, {
1112
boolean: ["table", "snippets"],
@@ -16,12 +17,21 @@ if (!flags.table && !flags.snippets) {
1617
console.log("Please specify at least one flag: --table or --snippets");
1718
} else {
1819
variants.forEach((variant) => {
19-
const categorizedVscSnippets = variant.snippets.map(convertToVscSnippet);
20+
const categorizedVscSnippets: VscSnippetDefinition[] = variant
21+
.snippetsWithMeta.map(
22+
(item) => ({
23+
...item,
24+
snippets: convertToVscSnippet(item.snippets),
25+
}),
26+
);
27+
2028
if (flags.table) {
2129
logTables(variant.label, categorizedVscSnippets);
2230
}
2331
if (flags.snippets) {
24-
const variantVscSnippet = groupSnippets(categorizedVscSnippets);
32+
const variantVscSnippet = groupSnippets(
33+
categorizedVscSnippets.map((item) => item.snippets),
34+
);
2535
generateSnippetsFile(variant.extension, variantVscSnippet);
2636
}
2737
});

src/deps.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { parse } from "https://deno.land/std@0.168.0/flags/mod.ts";
2+
export { ensureDirSync } from "https://deno.land/std@0.141.0/fs/ensure_dir.ts";
3+
export { markdownTable } from "https://esm.sh/markdown-table@3";

src/models/app.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ export type VscSnippet = {
33
body: string | string[];
44
description?: string;
55
};
6-
76
export type VscSnippetDict = Record<string, VscSnippet>;
87

98
export type XSnippet = Omit<VscSnippet, "prefix"> & { name: string };
10-
119
export type XSnippetDict = Record<string, XSnippet>;
10+
11+
export type XSnippetMeta = {
12+
title: string;
13+
description?: string;
14+
};
15+
16+
export type GenericSnippetDictWithMeta<T> = {
17+
meta: XSnippetMeta;
18+
snippets: T;
19+
};
20+
export type XSnippetDefinition = GenericSnippetDictWithMeta<XSnippetDict>;
21+
export type VscSnippetDefinition = GenericSnippetDictWithMeta<VscSnippetDict>;

src/snippets/app.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
import { XSnippetDefinition } from "../models/app.ts";
12
import { javascript } from "./js/app.ts";
23
import { typescript } from "./ts/app.ts";
34

4-
export const variants = [
5+
type SnippetVariant = {
6+
label: string;
7+
language: string;
8+
extension: string;
9+
snippetsWithMeta: XSnippetDefinition[];
10+
};
11+
export const variants: SnippetVariant[] = [
512
{
6-
label: "JavaScript",
13+
label: "Snippets",
714
language: "javascript",
815
extension: "js",
9-
snippets: javascript,
16+
snippetsWithMeta: javascript,
1017
},
1118
{
12-
label: "TypeScript",
19+
label: "TypeScript specific",
1320
language: "typescript",
1421
extension: "ts",
15-
snippets: typescript,
22+
snippetsWithMeta: typescript,
1623
},
1724
];

src/snippets/js/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { arrayMethods } from "./array-methods.ts";
22
import { classes } from "./classes.ts";
33
import { console } from "./console.ts";
44
import { dates } from "./dates.ts";
5-
import { declarations } from "./declarations.ts";
5+
import { assignments } from "./assignments.ts";
66
import { dom } from "./dom.ts";
77
import { flowControl } from "./flow-control.ts";
88
import { functions } from "./functions.ts";
@@ -20,7 +20,7 @@ import { types } from "./types.ts";
2020
import { uncategorized } from "./uncategorized.ts";
2121

2222
export const javascript = [
23-
declarations,
23+
assignments,
2424
flowControl,
2525
functions,
2626
loops,

src/snippets/js/array-methods.ts

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,57 @@
1-
export const arrayMethods = {
2-
fe: {
3-
name: "Array.forEach()",
4-
body: "$1.forEach((${2:item}) => {\n\t$0\n})",
5-
},
6-
map: {
7-
name: "Array.map()",
8-
body: "$1.map((${2:item}) => ${3})$0",
9-
},
10-
reduce: {
11-
name: "Array.reduce()",
12-
body: "$1.reduce((${2:acc}, ${3:curr}) => {\n\t$0\n}, ${4:initial})",
13-
},
14-
"reduce-right": {
15-
name: "Array.reduceRight()",
16-
body: "$1.reduceRight((${2:acc}, ${3:curr}) => {\n\t$0\n}, ${4:initial})",
17-
},
18-
filter: {
19-
name: "Array.filter()",
20-
body: "$1.filter((${2:item}) => ${3})$0",
21-
},
22-
find: {
23-
name: "Array.find()",
24-
body: "$1.find((${2:item}) => ${3})$0",
25-
},
26-
every: {
27-
name: "Array.every()",
28-
body: "$1.every((${2:item}) => ${3})$0",
29-
},
30-
some: {
31-
name: "Array.some()",
32-
body: "$1.some((${2:item}) => ${3})$0",
33-
},
34-
reverse: {
35-
name: "Array.reverse()",
36-
body: "$1.reverse()$0",
37-
},
38-
"map-string": {
39-
name: "Array.map() as string",
40-
body: "$1.map(String)$0",
41-
},
42-
"map-number": {
43-
name: "Array.map() as number",
44-
body: "$1.map(Number)$0",
45-
},
46-
"filter-true": {
47-
name: "Array.filter() truthy",
48-
body: "$1.filter(Boolean)$0",
1+
import { XSnippetDefinition } from "../../models/app.ts";
2+
3+
export const arrayMethods: XSnippetDefinition = {
4+
meta: {
5+
title: "Array methods",
6+
},
7+
snippets: {
8+
fe: {
9+
name: "Array.forEach()",
10+
body: "$1.forEach((${2:item}) => {\n\t$0\n})",
11+
},
12+
map: {
13+
name: "Array.map()",
14+
body: "$1.map((${2:item}) => ${3})",
15+
},
16+
reduce: {
17+
name: "Array.reduce()",
18+
body: "$1.reduce((${2:acc}, ${3:curr}) => {\n\t$0\n}, ${4:initial})",
19+
},
20+
"reduce-right": {
21+
name: "Array.reduceRight()",
22+
body: "$1.reduceRight((${2:acc}, ${3:curr}) => {\n\t$0\n}, ${4:initial})",
23+
},
24+
filter: {
25+
name: "Array.filter()",
26+
body: "$1.filter((${2:item}) => ${3})",
27+
},
28+
find: {
29+
name: "Array.find()",
30+
body: "$1.find((${2:item}) => ${3})",
31+
},
32+
every: {
33+
name: "Array.every()",
34+
body: "$1.every((${2:item}) => ${3})",
35+
},
36+
some: {
37+
name: "Array.some()",
38+
body: "$1.some((${2:item}) => ${3})",
39+
},
40+
reverse: {
41+
name: "Array.reverse()",
42+
body: "$1.reverse()",
43+
},
44+
"map-string": {
45+
name: "Array.map() as string",
46+
body: "$1.map(String)",
47+
},
48+
"map-number": {
49+
name: "Array.map() as number",
50+
body: "$1.map(Number)",
51+
},
52+
"filter-true": {
53+
name: "Array.filter() truthy",
54+
body: "$1.filter(Boolean)",
55+
},
4956
},
5057
};

src/snippets/js/assignments.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { XSnippetDefinition } from "../../models/app.ts";
2+
3+
export const assignments: XSnippetDefinition = {
4+
meta: {
5+
title: "Assignments",
6+
},
7+
snippets: {
8+
c: {
9+
name: "const",
10+
body: "const $0",
11+
},
12+
l: {
13+
name: "let",
14+
body: "let $0",
15+
},
16+
ca: {
17+
name: "const assignment",
18+
body: "const $1 = $0",
19+
},
20+
la: {
21+
name: "let assignment",
22+
body: "let $1 = $0",
23+
},
24+
cas: {
25+
name: "const string assignment",
26+
body: "const $1 = '$0'",
27+
},
28+
car: {
29+
name: "const array assignment",
30+
body: "const $1 = [$0]",
31+
},
32+
cao: {
33+
name: "const object assignment",
34+
body: "const $1 = { $0 }",
35+
},
36+
dob: {
37+
name: "object destructuring",
38+
body: "const { $0 } = ${1:object}",
39+
},
40+
dar: {
41+
name: "array destructuring",
42+
body: "const [$0] = ${1:array}",
43+
},
44+
},
45+
};

src/snippets/js/classes.ts

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
1-
export const classes = {
2-
cs: {
3-
name: "class",
4-
body: "class $1 {\n\t$0\n}",
5-
},
6-
cse: {
7-
name: "class extends",
8-
body: "class $1 extends ${2:Base} {\n\t$0\n}",
9-
},
10-
csc: {
11-
name: "class with constructor",
12-
body: [
13-
"class $1 {",
14-
"\tconstructor($2) {",
15-
"\t\t$0",
16-
"\t}",
17-
"}",
18-
],
19-
},
20-
csec: {
21-
name: "class extends with constructor",
22-
body: [
23-
"class $1 extends ${2:Base} {",
24-
"\tconstructor($3) {",
25-
"\t\t$0",
26-
"\t}",
27-
"}",
28-
],
29-
},
30-
ctor: {
31-
name: "class constructor",
32-
body: "constructor($1) {$0}",
33-
},
34-
get: {
35-
name: "getter",
36-
body: "get ${1:property}() {\n\t$0\n}",
37-
},
38-
set: {
39-
name: "setter",
40-
body: "set ${1:property}(${2:value}) {\n\t$0\n}",
41-
},
42-
gs: {
43-
name: "getter and setter",
44-
body:
45-
"get ${1:property}() {\n\t$0\n}\nset ${1:property}(${2:value}) {\n\t\n}",
46-
},
47-
met: {
48-
name: "method",
49-
body: "${1:name}($2) {\n\t$0\n}",
50-
},
1+
import { XSnippetDefinition } from "../../models/app.ts";
2+
3+
export const classes: XSnippetDefinition = {
514
meta: {
52-
name: "async method",
53-
body: "async ${1:name}($2) {\n\t$0\n}",
5+
title: "Classes",
6+
},
7+
snippets: {
8+
cs: {
9+
name: "class",
10+
body: "class $1 {\n\t$0\n}",
11+
},
12+
cse: {
13+
name: "class extends",
14+
body: "class $1 extends ${2:Base} {\n\t$0\n}",
15+
},
16+
csc: {
17+
name: "class with constructor",
18+
body: [
19+
"class $1 {",
20+
"\tconstructor($2) {",
21+
"\t\t$0",
22+
"\t}",
23+
"}",
24+
],
25+
},
26+
csec: {
27+
name: "class extends with constructor",
28+
body: [
29+
"class $1 extends ${2:Base} {",
30+
"\tconstructor($3) {",
31+
"\t\t$0",
32+
"\t}",
33+
"}",
34+
],
35+
},
36+
ctor: {
37+
name: "class constructor",
38+
body: "constructor($1) {$0}",
39+
},
40+
get: {
41+
name: "getter",
42+
body: "get ${1:property}() {\n\t$0\n}",
43+
},
44+
set: {
45+
name: "setter",
46+
body: "set ${1:property}(${2:value}) {\n\t$0\n}",
47+
},
48+
gs: {
49+
name: "getter and setter",
50+
body:
51+
"get ${1:property}() {\n\t$0\n}\nset ${1:property}(${2:value}) {\n\t\n}",
52+
},
53+
met: {
54+
name: "method",
55+
body: "${1:name}($2) {\n\t$0\n}",
56+
},
57+
meta: {
58+
name: "async method",
59+
body: "async ${1:name}($2) {\n\t$0\n}",
60+
},
5461
},
5562
};

0 commit comments

Comments
 (0)