Ran linter

This commit is contained in:
Angelos Chalaris
2019-03-02 10:10:14 +02:00
parent ddd852962d
commit 3868acab34
32 changed files with 583 additions and 708 deletions

View File

@ -1,19 +1,21 @@
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const util = require("./util.js");
const markdown = require("markdown-builder");
const snippets = require("../data/snippet_data.json")
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const util = require('./util.js');
const markdown = require('markdown-builder');
const snippets = require('../data/snippet_data.json');
const { headers, misc, lists } = markdown;
const TAG_NAMES = [...new Set(snippets.reduce((acc,v) => [...acc,v.tags[0]],[]))].sort((a,b) => a.localeCompare(b));
const TAG_NAMES = [...new Set(snippets.reduce((acc, v) => [...acc, v.tags[0]], []))].sort((a, b) =>
a.localeCompare(b)
);
console.log(TAG_NAMES);
const STATIC_PARTS_PATH = "./static-parts";
const STATIC_PARTS_PATH = './static-parts';
let startPart = "";
let endPart = "";
let output = "";
let startPart = '';
let endPart = '';
let output = '';
const detailsTOC = (title, snippetsArray) =>
`\n${misc
@ -23,13 +25,13 @@ const detailsTOC = (title, snippetsArray) =>
.ul(snippetsArray, snippet =>
misc.link(
snippet.title
.replace("\n", "")
.split("```")[0]
.replace('\n', '')
.split('```')[0]
.trim(),
misc.anchor(
snippet.title
.replace("\n", "")
.split("```")[0]
.replace('\n', '')
.split('```')[0]
.trim()
)
)
@ -38,19 +40,13 @@ const detailsTOC = (title, snippetsArray) =>
)
.trim()}\n\n`;
console.time("Builder");
console.time('Builder');
try {
startPart = fs.readFileSync(
path.join(STATIC_PARTS_PATH, "README-start.md"),
"utf8"
);
endPart = fs.readFileSync(
path.join(STATIC_PARTS_PATH, "README-end.md"),
"utf8"
);
startPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-start.md'), 'utf8');
endPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-end.md'), 'utf8');
} catch (err) {
console.log(`${chalk.red("ERROR!")} During static part loading: ${err}`);
console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
}
@ -60,13 +56,13 @@ try {
const snippetsInTag = {};
TAG_NAMES.forEach(tag => snippetsInTag[tag] = snippets.filter(v => v.tags[0] == tag));
TAG_NAMES.forEach(tag => (snippetsInTag[tag] = snippets.filter(v => v.tags[0] == tag)));
// write Table of Contents
TAG_NAMES.forEach(tag => {
const taggedSnippets = snippetsInTag[tag];
output += headers.h3(util.capitalize(tag));
output += detailsTOC("View contents", taggedSnippets);
output += detailsTOC('View contents', taggedSnippets);
});
// delimeter after TOC
@ -78,27 +74,24 @@ try {
const taggedSnippets = snippetsInTag[tag];
taggedSnippets.forEach(snippet => {
output += headers.h3(snippet.title).trim();
output += `\n\n${snippet.text}${snippet.codeBlocks.slice(0,-1).join('\n\n')}`;
output += `\n\n${snippet.text}${snippet.codeBlocks.slice(0, -1).join('\n\n')}`;
if (snippet.notes && snippet.notes.length) {
output += headers.h4('Notes');
output += `\n${snippet.notes}`;
}
output += misc.collapsible('Examples', snippet.codeBlocks.slice(-1));
output += `\n<br>${misc.link(
"⬆ Back to top",
misc.anchor("Table of Contents")
)}\n\n`
})
output += `\n<br>${misc.link('⬆ Back to top', misc.anchor('Table of Contents'))}\n\n`;
});
});
// add static part for end
output += `\n${endPart}\n`;
fs.writeFileSync("README.md", output);
fs.writeFileSync('README.md', output);
} catch (err) {
console.log(`${chalk.red("ERROR!")} During README generation: ${err}`);
console.log(`${chalk.red('ERROR!')} During README generation: ${err}`);
process.exit(1);
}
console.log(`${chalk.green("SUCCESS!")} README file generated!`);
console.timeEnd("Builder");
console.log(`${chalk.green('SUCCESS!')} README file generated!`);
console.timeEnd('Builder');

View File

@ -1,6 +1,6 @@
const fs = require("fs-extra")
const path = require("path")
const chalk = require("chalk")
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const {
attempt,
readSnippets,
@ -8,36 +8,35 @@ const {
getSection,
getTitle,
getTextualContent
} = require("./util");
} = require('./util');
console.time("Extractor");
console.time('Extractor');
attempt("snippet_data.json generation", () => {
attempt('snippet_data.json generation', () => {
const output = Object.entries(readSnippets()).map(([name, contents]) => {
const title = getTitle(contents);
const text = getTextualContent(contents);
const codeBlocks = getCodeBlocks(contents);
const notes = getSection("#### Notes", contents, false)
.split("\n")
.map(v => v.replace(/[*-] /g, ""))
.filter(v => v.trim() !== "")
const notes = getSection('#### Notes', contents, false)
.split('\n')
.map(v => v.replace(/[*-] /g, ''))
.filter(v => v.trim() !== '');
return {
name,
title,
text,
codeBlocks,
expertise: parseInt(
(contents.match(/<!--\s*expertise:\s*\(*(.+)\)*/) || [])[1],
10
),
tags: (contents.match(/<!--\s*tags:\s*\(*(.+)\)*\s*-->/) || [])[1].split(",").map(v => v.trim()),
expertise: parseInt((contents.match(/<!--\s*expertise:\s*\(*(.+)\)*/) || [])[1], 10),
tags: (contents.match(/<!--\s*tags:\s*\(*(.+)\)*\s*-->/) || [])[1]
.split(',')
.map(v => v.trim()),
notes
}
})
};
});
fs.writeFileSync("./data/snippet_data.json", JSON.stringify(output, null, 2))
})
fs.writeFileSync('./data/snippet_data.json', JSON.stringify(output, null, 2));
});
console.log(`${chalk.green("SUCCESS!")} snippet_data.json file generated!`);
console.timeEnd("Extractor");
console.log(`${chalk.green('SUCCESS!')} snippet_data.json file generated!`);
console.timeEnd('Extractor');

View File

@ -1,31 +1,29 @@
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const SNIPPETS_PATH = "./snippets";
const SNIPPETS_PATH = './snippets';
const attempt = (task, cb) => {
try {
return cb();
} catch (e) {
console.log(`${chalk.red("ERROR!")} During ${task}: ${e}`);
console.log(`${chalk.red('ERROR!')} During ${task}: ${e}`);
process.exit(1);
return null;
}
};
const capitalize = ([first, ...rest], lowerRest = false) =>
first.toUpperCase() + (lowerRest ? rest.join("").toLowerCase() : rest.join(""));
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
const readSnippets = () =>
attempt("read snippets", () =>
attempt('read snippets', () =>
fs
.readdirSync(SNIPPETS_PATH)
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.reduce((acc, name) => {
acc[name] = fs
.readFileSync(path.join(SNIPPETS_PATH, name), "utf8")
.replace(/\r\n/g, "\n");
acc[name] = fs.readFileSync(path.join(SNIPPETS_PATH, name), 'utf8').replace(/\r\n/g, '\n');
return acc;
}, {})
);
@ -41,16 +39,16 @@ const getCodeBlocks = str => {
m.forEach(match => results.push(match));
}
return results;
}
};
const getSection = (searchString, contents, includeSubsections = true) => {
const indexOfSearch = contents.indexOf(searchString);
if (indexOfSearch < 0) return "";
if (indexOfSearch < 0) return '';
let endSearch = "\\n#"
let endSearch = '\\n#';
if (includeSubsections) {
let i;
for (i = 0; searchString[i] === "#" && i < searchString.length; i++);
for (i = 0; searchString[i] === '#' && i < searchString.length; i++);
if (i > 0) {
endSearch += `{${i - 1},${i}}[^#]`;
@ -63,15 +61,14 @@ const getSection = (searchString, contents, includeSubsections = true) => {
const sliceEnd = endIndex === -1 ? undefined : endIndex + sliceStart;
return contents.slice(sliceStart, sliceEnd).trim();
}
};
const getTextualContent = str => {
const regex = /###.*\n*([\s\S]*?)```/g;
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex)
regex.lastIndex += 1;
if (m.index === regex.lastIndex) regex.lastIndex += 1;
m.forEach((match, groupIndex) => {
results.push(match);
@ -80,8 +77,7 @@ const getTextualContent = str => {
return results[1];
};
const getTitle = (contents) =>
contents.split('\n')[0].replace(/^#+\s+/g,'');
const getTitle = contents => contents.split('\n')[0].replace(/^#+\s+/g, '');
module.exports = {
attempt,
@ -92,4 +88,4 @@ module.exports = {
getCodeBlocks,
getSection,
getTitle
};
};