ESlinted scripts

This commit is contained in:
Angelos Chalaris
2018-10-18 22:38:53 +03:00
parent 08e84b7336
commit 1eb609cd90
5 changed files with 63 additions and 45 deletions

View File

@ -45,10 +45,10 @@ if (
.readdirSync(SNIPPETS_ARCHIVE_PATH)
.sort((a, b) => a.toLowerCase() - b.toLowerCase());
// Store the data read from each snippet in the appropriate object
for (const name of snippetFilenames.filter(s => s !== 'README.md')) {
for (const name of snippetFilenames.filter(s => s !== 'README.md'))
snippets[name] = fs.readFileSync(path.join(SNIPPETS_ARCHIVE_PATH, name), 'utf8');
}
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
@ -65,13 +65,13 @@ if (
);
output += misc.hr();
for (const snippet of Object.entries(snippets)) {
for (const snippet of Object.entries(snippets))
output += makeExamples(snippet[1]);
}
// Write to the README file of the archive
fs.writeFileSync(path.join(SNIPPETS_ARCHIVE_PATH, 'README.md'), output);
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During README generation for snippets archive: ${err}`);
process.exit(1);
}
@ -110,7 +110,8 @@ snippets = util.readSnippets(SNIPPETS_PATH);
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');
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
}
@ -170,7 +171,8 @@ try {
output += `\n${endPart}\n`;
// Write to the README file
fs.writeFileSync('README.md', output);
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During README generation: ${err}`);
process.exit(1);
}

View File

@ -28,9 +28,9 @@ try {
// turn it into an object so we can add data to it to be used in a different scope
.map(name => ({ name }));
if (!fs.existsSync(TEMP_PATH)) {
if (!fs.existsSync(TEMP_PATH))
fs.mkdirSync(TEMP_PATH);
}
for (const snippet of snippets) {
snippet.data = fs.readFileSync(path.join(SNIPPETS_PATH, snippet.name), 'utf8');
@ -55,7 +55,7 @@ try {
`semistandard "${TEMP_PATH}" --fix & ` +
`prettier "${TEMP_PATH}/*.js" --single-quote --print-width=100 --write`;
cp.exec(cmd, {}, (err, stdout, stderr) => {
cp.exec(cmd, {}, () => {
// Loop through each snippet now that semistandard and prettier did their job
for (const snippet of snippets) {
// an array to store each linted code block (definition + example)
@ -77,7 +77,8 @@ try {
console.log(`${chalk.green('SUCCESS!')} Snippet files linted!`);
console.timeEnd('Linter');
});
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During linting: ${err}`);
process.exit(1);
}

View File

@ -30,22 +30,25 @@ tagDbStats = Object.entries(tagDbData).reduce((acc, val) => {
}, {});
// Update the listing of snippets in tag_database and log the statistics, along with missing scripts
try {
for (let snippet of Object.entries(snippets))
for (let snippet of Object.entries(snippets)) {
if (
tagDbData.hasOwnProperty(snippet[0].slice(0, -3)) &&
tagDbData[snippet[0].slice(0, -3)].join(',').trim()
)
) {
output += `${snippet[0].slice(0, -3)}:${tagDbData[snippet[0].slice(0, -3)]
.join(',')
.trim()}\n`;
}
else {
output += `${snippet[0].slice(0, -3)}:uncategorized\n`;
missingTags++;
console.log(`${chalk.yellow('Tagged uncategorized:')} ${snippet[0].slice(0, -3)}`);
}
}
// Write to tag_database
fs.writeFileSync('tag_database', output);
} catch (err) {
}
catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During tag_database generation: ${err}`);
process.exit(1);

View File

@ -31,7 +31,8 @@ const getFilesInDir = (directoryPath, withPath, exclude = null) => {
}, []);
}
return directoryFilenames;
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
@ -45,7 +46,8 @@ const readSnippets = snippetsPath => {
try {
for (let snippet of snippetFilenames)
snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8');
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
@ -68,7 +70,8 @@ const readTags = () => {
return data;
})
);
} catch (err) {
}
catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During tag database loading: ${err}`);
process.exit(1);
@ -82,9 +85,9 @@ const optimizeNodes = (data, regexp, replacer) => {
do {
output = output.replace(regexp, replacer);
count = 0;
while (regexp.exec(output) !== null) {
while (regexp.exec(output) !== null)
++count;
}
} while (count > 0);
return output;
};
@ -116,9 +119,9 @@ const getCodeBlocks = str => {
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
if (m.index === regex.lastIndex)
regex.lastIndex += 1;
}
m.forEach((match, groupIndex) => {
results.push(match);
});
@ -131,9 +134,9 @@ const getTextualContent = str => {
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
if (m.index === regex.lastIndex)
regex.lastIndex += 1;
}
m.forEach((match, groupIndex) => {
results.push(match);
});

View File

@ -47,12 +47,12 @@ const generateSnippetCard = (
${
addCornerTag
? `<div class="corner ${
snippetKey[1].includes('advanced')
? 'advanced'
: snippetKey[1].includes('beginner')
? 'beginner'
: 'intermediate'
}"></div>`
snippetKey[1].includes('advanced')
? 'advanced'
: snippetKey[1].includes('beginner')
? 'beginner'
: 'intermediate'
}"></div>`
: ''
}
${md
@ -109,9 +109,10 @@ sass.render(
if (!err2) console.log(`${chalk.green('SUCCESS!')} style.css file generated!`);
else console.log(`${chalk.red('ERROR!')} During style.css file generation: ${err}`);
});
} else {
console.log(`${chalk.red('ERROR!')} During style.css file generation: ${err}`);
}
else
console.log(`${chalk.red('ERROR!')} During style.css file generation: ${err}`);
}
);
// Set variables for paths
@ -147,7 +148,8 @@ try {
'static-page-start.html',
'static-page-end.html'
].map(filename => fs.readFileSync(path.join(staticPartsPath, filename), 'utf8'));
} catch (err) {
}
catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
@ -169,7 +171,7 @@ try {
.replace(/<p>/g, '')
.replace(/<\/p>/g, '') +
'</h4><ul>';
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) {
output += md
.render(
`[${taggedSnippet[0]}](./${
@ -179,6 +181,7 @@ try {
.replace(/<p>/g, '')
.replace(/<\/p>/g, '</li>')
.replace(/<a/g, `<li><a tags="${taggedSnippet[1].join(',')}"`);
}
output += '</ul>\n';
}
output += `<h4 class="static-link"><a href="./archive">Archive</a></h4>
@ -218,7 +221,7 @@ try {
/<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm,
(match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`
);
pagesOutput.push({ tag: tag, content: localOutput });
pagesOutput.push({ tag, content: localOutput });
}
// Minify output
pagesOutput.forEach(page => {
@ -231,7 +234,8 @@ try {
`${chalk.green('SUCCESS!')} ${page.tag === 'array' ? 'index' : page.tag}.html file generated!`
);
});
} catch (err) {
}
catch (err) {
// Handle errors (hopefully not!)
console.log(`${chalk.red('ERROR!')} During category page generation: ${err}`);
process.exit(1);
@ -251,7 +255,7 @@ const staticPageStartGenerator = (staticPart, heading, description) => {
.replace(/<p>/g, '')
.replace(/<\/p>/g, '') +
'</h4><ul>';
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) {
htmlCode += md
.render(
`[${taggedSnippet[0]}](./${
@ -261,6 +265,7 @@ const staticPageStartGenerator = (staticPart, heading, description) => {
.replace(/<p>/g, '')
.replace(/<\/p>/g, '</li>')
.replace(/<a/g, `<li><a tags="${taggedSnippet[1].join(',')}"`);
}
htmlCode += '</ul>\n';
}
htmlCode += `<h4 class="static-link"><a href="./archive">Archive</a></h4>
@ -271,13 +276,13 @@ const staticPageStartGenerator = (staticPart, heading, description) => {
</nav><main class="col-centered"><span id="top"><br/><br/></span><h2 class="category-name">${heading}</h2>
<p style="text-align: justify">${description}</p><br />`;
return htmlCode.replace(/\$page_name/g, util.capitalize(heading));
}
};
// Create the output for the archive.html file
try {
// Add the static part
let heading = "Snippets Archive";
let heading = 'Snippets Archive';
let description = "These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.";
let htmlCode = staticPageStartGenerator(staticPageStartPart, heading, description);
@ -316,7 +321,8 @@ try {
fs.writeFileSync(path.join(docsPath, 'archive.html'), minifiedArchivedOutput);
console.log(`${chalk.green('SUCCESS!')} archive.html file generated!`);
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During archive.html generation: ${err}`);
process.exit(1);
}
@ -324,8 +330,8 @@ try {
// Create the output for the glossary.html file
try {
// Add the static part
let heading = "Glossary";
let description = "Developers use a lot of terminology daily. Every once in a while, you might find a term you do not know. We know how frustrating that can get, so we provide you with a handy glossary of frequently used web development terms.";
let heading = 'Glossary';
let description = 'Developers use a lot of terminology daily. Every once in a while, you might find a term you do not know. We know how frustrating that can get, so we provide you with a handy glossary of frequently used web development terms.';
let htmlCode = staticPageStartGenerator(staticPageStartPart, heading, description);
glossaryOutput += htmlCode;
@ -333,7 +339,7 @@ try {
const filteredGlossarySnippets = filterSnippets(glossarySnippets, ['README.md']);
// Generate glossary snippets from md files
for (let snippet of Object.entries(filteredGlossarySnippets))
for (let snippet of Object.entries(filteredGlossarySnippets)) {
glossaryOutput +=
'<div class="card code-card"><div class="section card-content">' +
md
@ -341,6 +347,7 @@ try {
.replace(/<h3/g, `<h4 id="${snippet[0].toLowerCase()}"`)
.replace(/<\/h3>/g, '</h4>') +
'</div></div>';
}
glossaryOutput += `${staticPageEndPart}`;
@ -348,7 +355,8 @@ try {
const minifiedGlossaryOutput = minifyHTML(glossaryOutput);
fs.writeFileSync(path.join(docsPath, 'glossary.html'), minifiedGlossaryOutput);
console.log(`${chalk.green('SUCCESS!')} glossary.html file generated!`);
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During glossary.html generation: ${err}`);
process.exit(1);
}
@ -358,7 +366,8 @@ staticFiles.forEach(f => {
try {
fs.copyFileSync(path.join(staticPartsPath, f), path.join(docsPath, f));
console.log(`${chalk.green('SUCCESS!')} ${f} file copied!`);
} catch (err) {
}
catch (err) {
console.log(`${chalk.red('ERROR!')} During ${f} copying: ${err}`);
process.exit(1);
}