Linted scripts

This commit is contained in:
Angelos Chalaris
2018-09-15 13:04:31 +03:00
parent 83c6ab3e83
commit 17d64598e8
3 changed files with 70 additions and 82 deletions

View File

@ -15,27 +15,17 @@ const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive'; const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
const STATIC_PARTS_PATH = './static-parts'; const STATIC_PARTS_PATH = './static-parts';
if ( if (util.isTravisCI() && /^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])) {
util.isTravisCI() &&
/^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])
) {
console.log( console.log(
`${chalk.green( `${chalk.green('NOBUILD')} README build terminated, parent commit is a Travis build!`
'NOBUILD'
)} README build terminated, parent commit is a Travis build!`
); );
process.exit(0); process.exit(0);
} }
if ( if (
util.isTravisCI() && util.isTravisCI() &&
(process.env['TRAVIS_EVENT_TYPE'] === 'cron' || (process.env['TRAVIS_EVENT_TYPE'] === 'cron' || process.env['TRAVIS_EVENT_TYPE'] === 'api')
process.env['TRAVIS_EVENT_TYPE'] === 'api')
) { ) {
console.log( console.log(`${chalk.green('ARCHIVE')} Cron job or custom build, building archive README!`);
`${chalk.green(
'ARCHIVE'
)} Cron job or custom build, building archive README!`
);
console.time('Builder'); console.time('Builder');
let snippets = {}; let snippets = {};
// Synchronously read all snippets from snippets_archive folder and sort them as necessary (case-insensitive) // Synchronously read all snippets from snippets_archive folder and sort them as necessary (case-insensitive)
@ -45,10 +35,7 @@ if (
.sort((a, b) => a.toLowerCase() - b.toLowerCase()); .sort((a, b) => a.toLowerCase() - b.toLowerCase());
// Store the data read from each snippet in the appropriate object // 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( snippets[name] = fs.readFileSync(path.join(SNIPPETS_ARCHIVE_PATH, name), 'utf8');
path.join(SNIPPETS_ARCHIVE_PATH, name),
'utf8'
);
} }
} catch (err) { } catch (err) {
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`); console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
@ -76,23 +63,18 @@ if (
data.slice(data.lastIndexOf('```js'), data.lastIndexOf('```')) + data.slice(data.lastIndexOf('```js'), data.lastIndexOf('```')) +
data.slice(data.lastIndexOf('```')) data.slice(data.lastIndexOf('```'))
); );
output += data + '\n<br>' + misc.link('⬆ Back to top', misc.anchor('Table of Contents')) + '\n\n'; output +=
data + '\n<br>' + misc.link('⬆ Back to top', misc.anchor('Table of Contents')) + '\n\n';
} }
// Write to the README file of the archive // Write to the README file of the archive
fs.writeFileSync(path.join(SNIPPETS_ARCHIVE_PATH, 'README.md'), output); fs.writeFileSync(path.join(SNIPPETS_ARCHIVE_PATH, 'README.md'), output);
} catch (err) { } catch (err) {
console.log( console.log(`${chalk.red('ERROR!')} During README generation for snippets archive: ${err}`);
`${chalk.red(
'ERROR!'
)} During README generation for snippets archive: ${err}`
);
process.exit(1); process.exit(1);
} }
console.log( console.log(`${chalk.green('SUCCESS!')} README file generated for snippets archive!`);
`${chalk.green('SUCCESS!')} README file generated for snippets archive!`
);
console.timeEnd('Builder'); console.timeEnd('Builder');
} }
let snippets = {}; let snippets = {};
@ -124,14 +106,8 @@ snippets = util.readSnippets(SNIPPETS_PATH);
// Load static parts for the README file // Load static parts for the README file
try { try {
startPart = fs.readFileSync( startPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-start.md'), 'utf8');
path.join(STATIC_PARTS_PATH, 'README-start.md'), endPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-end.md'), 'utf8');
'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}`); console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
process.exit(1); process.exit(1);
@ -170,9 +146,10 @@ try {
const taggedSnippets = Object.entries(tagDbData).filter(v => v[1][0] === tag); const taggedSnippets = Object.entries(tagDbData).filter(v => v[1][0] === tag);
output += headers.h3((EMOJIS[tag] || '') + ' ' + capitalizedTag).trim(); output += headers.h3((EMOJIS[tag] || '') + ' ' + capitalizedTag).trim();
output += misc.collapsible( output +=
misc.collapsible(
'View contents', 'View contents',
lists.ul(taggedSnippets, (snippet) => lists.ul(taggedSnippets, snippet =>
misc.link( misc.link(
`\`${snippet[0]}\``, `\`${snippet[0]}\``,
`${misc.anchor(snippet[0])}${snippet[1].includes('advanced') ? '-' : ''}` `${misc.anchor(snippet[0])}${snippet[1].includes('advanced') ? '-' : ''}`
@ -199,14 +176,18 @@ try {
snippet = snippet.join('\n'); snippet = snippet.join('\n');
} }
snippet = snippet.slice(0, snippet.lastIndexOf('```js')).trim() + snippet =
snippet.slice(0, snippet.lastIndexOf('```js')).trim() +
misc.collapsible( misc.collapsible(
'Examples', 'Examples',
snippet.slice(snippet.lastIndexOf('```js'), snippet.lastIndexOf('```')) + snippet.slice(snippet.lastIndexOf('```js'), snippet.lastIndexOf('```')) +
snippet.slice(snippet.lastIndexOf('```')) snippet.slice(snippet.lastIndexOf('```'))
); );
output += `${snippet}\n<br>${misc.link('⬆ Back to top', misc.anchor('Table of Contents'))}\n\n`; output += `${snippet}\n<br>${misc.link(
'⬆ Back to top',
misc.anchor('Table of Contents')
)}\n\n`;
} }
} }

View File

@ -165,15 +165,17 @@ try {
'</h4>'; '</h4>';
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 output += md
.render(`[${taggedSnippet[0]}](./${tag == 'array' ?'index' : tag}#${taggedSnippet[0].toLowerCase()})\n`) .render(
`[${taggedSnippet[0]}](./${
tag == 'array' ? 'index' : tag
}#${taggedSnippet[0].toLowerCase()})\n`
)
.replace(/<p>/g, '') .replace(/<p>/g, '')
.replace(/<\/p>/g, '') .replace(/<\/p>/g, '')
.replace(/<a/g, `<a tags="${taggedSnippet[1].join(',')}"`) .replace(/<a/g, `<a tags="${taggedSnippet[1].join(',')}"`);
;
output += '\n'; output += '\n';
} }
output += output += '</nav><main class="col-centered">';
'</nav><main class="col-centered">';
output += '<span id="top"><br/><br/></span>'; output += '<span id="top"><br/><br/></span>';
// Loop over tags and snippets to create the list of snippets // Loop over tags and snippets to create the list of snippets
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))] for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
@ -196,17 +198,20 @@ try {
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))
localOutput += localOutput +=
'<div class="card code-card">' + '<div class="card code-card">' +
`<div class="corner ${taggedSnippet[1].includes('advanced') ? 'advanced' : taggedSnippet[1].includes('beginner') ? 'beginner' : 'intermediate'}"></div>` + `<div class="corner ${
taggedSnippet[1].includes('advanced')
? 'advanced'
: taggedSnippet[1].includes('beginner')
? 'beginner'
: 'intermediate'
}"></div>` +
md md
.render(`\n${snippets[taggedSnippet[0] + '.md']}`) .render(`\n${snippets[taggedSnippet[0] + '.md']}`)
.replace( .replace(
/<h3/g, /<h3/g,
`<div class="section card-content"><h4 id="${taggedSnippet[0].toLowerCase()}"` `<div class="section card-content"><h4 id="${taggedSnippet[0].toLowerCase()}"`
) )
.replace( .replace(/<\/h3>/g, '</h4>')
/<\/h3>/g,
'</h4>'
)
.replace( .replace(
/<pre><code class="language-js">/m, /<pre><code class="language-js">/m,
'</div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre><code class="language-js">' '</div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre><code class="language-js">'
@ -220,7 +225,10 @@ try {
)}</pre>` )}</pre>`
) )
.replace(/<\/div>\s*<pre class="/g, '</div><pre class="section card-code ') .replace(/<\/div>\s*<pre class="/g, '</div><pre class="section card-code ')
.replace(/<\/pre>\s+<pre class="/g, '</pre><label class="collapse">examples</label><pre class="section card-examples ') + .replace(
/<\/pre>\s+<pre class="/g,
'</pre><label class="collapse">examples</label><pre class="section card-examples '
) +
'</div>'; '</div>';
// Add the ending static part // Add the ending static part
localOutput += `\n${endPart + '\n'}`; localOutput += `\n${endPart + '\n'}`;
@ -262,8 +270,13 @@ try {
removeStyleLinkTypeAttributes: false, removeStyleLinkTypeAttributes: false,
trimCustomFragments: true trimCustomFragments: true
}); });
fs.writeFileSync(path.join(docsPath, (page.tag == 'array' ? 'index' : page.tag) + '.html'), page.content); fs.writeFileSync(
console.log(`${chalk.green('SUCCESS!')} ${page.tag == 'array' ? 'index' : page.tag}.html file generated!`); path.join(docsPath, (page.tag == 'array' ? 'index' : page.tag) + '.html'),
page.content
);
console.log(
`${chalk.green('SUCCESS!')} ${page.tag == 'array' ? 'index' : page.tag}.html file generated!`
);
}); });
} catch (err) { } catch (err) {
// Handle errors (hopefully not!) // Handle errors (hopefully not!)
@ -377,14 +390,8 @@ try {
'<div class="card code-card">' + '<div class="card code-card">' +
md md
.render(`\n${filteredArchivedSnippets[snippet[0]]}`) .render(`\n${filteredArchivedSnippets[snippet[0]]}`)
.replace( .replace(/<h3/g, `<div class="section card-content"><h4 id="${snippet[0].toLowerCase()}"`)
/<h3/g, .replace(/<\/h3>/g, '</h4>')
`<div class="section card-content"><h4 id="${snippet[0].toLowerCase()}"`
)
.replace(
/<\/h3>/g,
'</h4>'
)
.replace( .replace(
/<pre><code class="language-js">/m, /<pre><code class="language-js">/m,
'</div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre><code class="language-js">' '</div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre><code class="language-js">'
@ -398,7 +405,10 @@ try {
)}</pre>` )}</pre>`
) )
.replace(/<\/div>\s*<pre class="/g, '</div><pre class="section card-code ') .replace(/<\/div>\s*<pre class="/g, '</div><pre class="section card-code ')
.replace(/<\/pre>\s+<pre class="/g, '</pre><label class="collapse">examples</label><pre class="section card-examples ') + .replace(
/<\/pre>\s+<pre class="/g,
'</pre><label class="collapse">examples</label><pre class="section card-examples '
) +
'</div>'; '</div>';
// Optimize punctuation nodes // Optimize punctuation nodes
@ -468,14 +478,8 @@ try {
'<div class="card code-card"><div class="section card-content">' + '<div class="card code-card"><div class="section card-content">' +
md md
.render(`\n${filteredGlossarySnippets[snippet[0]]}`) .render(`\n${filteredGlossarySnippets[snippet[0]]}`)
.replace( .replace(/<h3/g, `<h4 id="${snippet[0].toLowerCase()}"`)
/<h3/g, .replace(/<\/h3>/g, '</h4>') +
`<h4 id="${snippet[0].toLowerCase()}"`
)
.replace(
/<\/h3>/g,
'</h4>'
) +
'</div></div>'; '</div></div>';
glossaryOutput += `${glossaryEndPart}`; glossaryOutput += `${glossaryEndPart}`;
@ -523,7 +527,10 @@ try {
} }
// Copy contributing.html // Copy contributing.html
try { try {
fs.copyFileSync(path.join(staticPartsPath, 'contributing.html'), path.join(docsPath, 'contributing.html')); fs.copyFileSync(
path.join(staticPartsPath, 'contributing.html'),
path.join(docsPath, 'contributing.html')
);
console.log(`${chalk.green('SUCCESS!')} contributing.html file copied!`); console.log(`${chalk.green('SUCCESS!')} contributing.html file copied!`);
} catch (err) { } catch (err) {
console.log(`${chalk.red('ERROR!')} During contributing.html copying: ${err}`); console.log(`${chalk.red('ERROR!')} During contributing.html copying: ${err}`);