Skip to content
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

Support code snippets from the BE #160

Closed
wants to merge 11 commits into from
47 changes: 18 additions & 29 deletions scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@ const fetch = require('node-fetch');
const fs = require('fs-extra');
const path = require('path');

const SNIPPET_TYPES = ['expTests', 'filters', 'functions', 'tags'];

const PREFIX = {
expTests: '',
filters: '|',
functions: '~',
tags: '~',
};

// Skip snippet generation if format is incompatible
const SKIP_SNIPPET_GENERATION = [
'set',
'for',
'if',
'flip',
'import',
'include',
'from',
'do',
'module_attribute',
];

const OMIT_SNIPPET = [
'dnd_area',
'dnd_section',
Expand Down Expand Up @@ -101,34 +90,35 @@ const buildSnippetDescription = (docEntry) => {
return description;
};

const getFirstDefaultSnippet = (docEntry) => {
return docEntry.snippets.shift().code;
};

const createSnippet = (docEntry, type) => {
const createSnippet = (docEntry, type, existingSnippet) => {
if (OMIT_SNIPPET.includes(docEntry.name)) {
return;
}

let snippetEntry = {
return {
body: [
SKIP_SNIPPET_GENERATION.includes(docEntry.name)
? getFirstDefaultSnippet(docEntry)
: buildSnippetBody(docEntry, type),
existingSnippet ? existingSnippet : buildSnippetBody(docEntry, type),
],
description: buildSnippetDescription(docEntry, type),
prefix: PREFIX[type] + docEntry.name,
};

return snippetEntry;
};

const createFile = async (data, type) => {
const docEntries = Object.values(data);
const snippetData = data[type];
const docEntries = Object.values(snippetData);

let snippets = {};
for (let entry of docEntries) {
snippets[entry['name']] = createSnippet(entry, type);
if (type === 'tags' && data.codeSnippets[entry['name']]) {
snippets[entry['name']] = createSnippet(
entry,
type,
data.codeSnippets[entry['name']]
);
} else {
snippets[entry['name']] = createSnippet(entry, type);
}
}

try {
Expand All @@ -149,10 +139,9 @@ const createFile = async (data, type) => {

const createSnippetFiles = async () => {
const data = await fetchHubldocs();
const snippetTypes = Object.keys(data);

for (let type of snippetTypes) {
createFile(data[type], type);
for (let type of SNIPPET_TYPES) {
createFile(data, type);
}
};

Expand Down
Loading