Skip to content

Release/v0.4.1 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<br>

🚧 *Still a work in progress. Some snippets may be changed or removed, and many more will be added.*
🚧 *Still a work in progress. Some snippets may be changed or removed, and more will be added.*

## Features
- Contains over **180** snippets
Expand Down Expand Up @@ -61,7 +61,7 @@ It's highly recommended to use these snippets along with Prettier/ESLint to have

### Assignments

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -180,7 +180,7 @@ const [$0] = ${1:array}

### Flow control

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -368,7 +368,7 @@ try {

### Functions

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -497,7 +497,7 @@ async ($1) => {

### Loops

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -606,7 +606,7 @@ while (${1:true}) {

### Classes

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -762,7 +762,7 @@ async ${1:name}($2) {

### Promises

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -907,7 +907,7 @@ Promise.any($1)

### Modules

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1102,7 +1102,7 @@ export const ${1:name} = ($2) => {$0}

### Array methods

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1263,7 +1263,7 @@ $1.filter(Boolean)

### Objects

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1322,7 +1322,7 @@ Object.values($0)

### Returns

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1371,7 +1371,7 @@ return ({$0})

### Operators, Expressions, Literals
Grouping them all together for now
<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1610,7 +1610,7 @@ ${$1}$0

### Console

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1791,7 +1791,7 @@ console.warn('$1 ->', ${2:$1})

### Timers

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1858,7 +1858,7 @@ process.nextTick(() => {

### JSON

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -1917,7 +1917,7 @@ typeof ${1:value} === 'string' ? value : JSON.stringify($1)

### DOM

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2014,7 +2014,7 @@ ${1:emitter}.on('${2:event}', (${3:arguments}) => {

### Dates

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2049,7 +2049,7 @@ Date.now()

### Testing

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2187,7 +2187,7 @@ afterEach(() => {

### Types

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2282,7 +2282,7 @@ isNan($0)

### Misc

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2353,7 +2353,7 @@ import.meta.env.$0

### Uncategorized
Will be sorted into appropriate categories in the future.
<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2452,7 +2452,7 @@ Available only in .ts and .tsx files

### Declarations

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down Expand Up @@ -2511,7 +2511,7 @@ const $1: ${2:object} = { $0 }

### Types

<table>
<table width="100%">

<tr>
<td>Prefix</td>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "modern-js-snippets",
"displayName": "Modern JavaScript Snippets ⚡",
"version": "0.4.0",
"version": "0.4.1",
"license": "MIT",
"description": "Code snippets for modern JavaScript & TypeScript",
"icon": "assets/icon.png",
Expand Down
58 changes: 34 additions & 24 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
import { parse } from "./deps.ts";
import { generateDocs, populateDocsBlock } from "./docs-gen/snippets.ts";
import { languages } from "./snippets/app.ts";
import { variants } from "./snippets/app.ts";
import {
convertToVscSnippet,
generateSnippets,
groupSnippets,
} from "./utils/snippets.ts";

const flags = parse(Deno.args, {
boolean: ["snippets", "docs"],
default: { snippets: false, docs: false },
});
const main = () => {
const flags = parse(Deno.args, {
boolean: ["snippets", "docs"],
default: { snippets: false, docs: false },
});

if (!flags.snippets && !flags.docs) {
return console.log(
"Please specify at least one flag: --snippets or --docs",
);
}

// Convert XDefinitions to VscDefinitions, for every variant
const variantsAsVsc = variants.map((variant) => {
const snippetDefinitions = variant.snippetDefinitions
.map((def) => ({
...def,
snippets: convertToVscSnippet(def.snippets),
}));

return { ...variant, snippetDefinitions };
});

if (!flags.snippets && !flags.docs) {
console.log("Please specify at least one flag: --snippets or --docs");
} else {
if (flags.snippets) {
console.log("\nGenerating snippets...");
languages.forEach((language) => {
const categorizedVscSnippets = language
.snippetDefinitions.map(
(item) => {
const snippets = convertToVscSnippet(item.snippets);
return { ...item, snippets };
},
);

const variantVscSnippet = groupSnippets(
categorizedVscSnippets.map((item) => item.snippets),

variantsAsVsc.forEach((variant) => {
const vscSnippetDict = groupSnippets(
variant.snippetDefinitions.map((def) => def.snippets),
);
generateSnippets(language.fileExtension, variantVscSnippet);
generateSnippets(variant.fileExtension, vscSnippetDict);
});
}

// TODO: probably better to make it generate from vsc json
// pass in meta, and snippets converted to vsc format
// important to know it generates docs off of defined xSnippets
// so .code-snippets could be out of date if you haven't run --snippets
if (flags.docs) {
console.log("\nGenerating docs...");
const docs = generateDocs(languages);
const docs = generateDocs(variantsAsVsc);
populateDocsBlock(docs);
}
}
};

main();
22 changes: 11 additions & 11 deletions src/docs-gen/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { replaceInFile } from "../deps.ts";
import { XSnippetDefinition, XSnippetVariant } from "../models/app.ts";
import { VscSnippetDefinition, VscSnippetVariant } from "../models/app.ts";
import { parseMultiline, replaceSymbol } from "../utils/general.ts";
import {
$col,
$colCode,
Expand All @@ -18,11 +19,10 @@ type SnippetRow = {
};

const snippetRow = ({ prefix, name, body }: SnippetRow) => {
const parsedBody = Array.isArray(body) ? body.join("\n") : body;
const cols = joinByNewLine([
$colCode(prefix),
$col(name),
$colCodeBlock(parsedBody),
$colCodeBlock(parseMultiline(body)),
]);

return $row(cols);
Expand All @@ -35,30 +35,30 @@ const generateSnippetTable = (items: SnippetRow[]) => {
return $table(headings, rows);
};

const generateSnippetSection = (
{ meta, snippets }: XSnippetDefinition,
) => {
const generateSnippetSection = ({ meta, snippets }: VscSnippetDefinition) => {
const title = `### ${meta.title}`;
const description = meta.description ?? "";
const table = generateSnippetTable(
Object.entries(snippets).map(([prefix, value]) => ({
...value,
prefix,
Object.entries(snippets).map(([name, { body, prefix, description }]) => ({
name: replaceSymbol(name),
body,
prefix: parseMultiline(prefix),
description,
})),
);

return joinByNewLine([title, description, table, ""]);
};

const generateVariantSection = (variant: XSnippetVariant) => {
const generateVariantSection = (variant: VscSnippetVariant) => {
const title = `## ${variant.label}`;
const description = variant.description ?? "";
const sections = variant.snippetDefinitions.map(generateSnippetSection);

return joinByNewLine([title, description, "", ...sections]);
};

export const generateDocs = (variants: XSnippetVariant[]) => {
export const generateDocs = (variants: VscSnippetVariant[]) => {
return joinByDoubleNewLine(variants.map(generateVariantSection));
};

Expand Down
2 changes: 1 addition & 1 deletion src/docs-gen/table-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const $headerRow = (headers: string[]) => {

export const $table = (headings: string[], rows: string[]) => {
return joinByNewLine([
"<table>",
'<table width="100%">',
$headerRow(headings),
joinByNewLine(rows),
"</table>",
Expand Down
Loading