Update builder script

This commit is contained in:
Angelos Chalaris
2019-08-13 12:12:28 +03:00
parent 7c84eea7cb
commit e54329cb28
22 changed files with 1143 additions and 615 deletions

View File

@ -1,5 +1,5 @@
/*
This is the builder script that generates the README and SNIPPETS_ARCHIVE files.
This is the builder script that generates the README files.
Run using `npm run builder`.
*/
// Load modules
@ -9,75 +9,33 @@ const { green, red } = require('kleur');
const util = require('./util');
const markdown = require('markdown-builder');
const { headers, misc, lists } = markdown;
const config = require('../config');
// Paths
const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
const STATIC_PARTS_PATH = './static-parts';
const SNIPPETS_PATH = `./${config.snippetPath}`;
const SNIPPETS_ARCHIVE_PATH = `./${config.snippetArchivePath}`;
const GLOSSARY_PATH = `./${config.glossaryPath}`;
const STATIC_PARTS_PATH = `./${config.staticPartsPath}`;
const makeExamples = data => {
data =
data.slice(0, data.lastIndexOf('```js')).trim() +
misc.collapsible(
'Examples',
data.slice(data.lastIndexOf('```js'), data.lastIndexOf('```')) +
data.slice(data.lastIndexOf('```'))
);
return `${data}\n<br>${misc.link('⬆ Back to top', misc.anchor('Contents'))}\n\n`;
};
if (util.isTravisCI() && /^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])) {
// Terminate if parent commit is a Travis build
if (
util.isTravisCI() &&
/^Travis build: \d+/g.test(process.env['TRAVIS_COMMIT_MESSAGE'])
) {
console.log(
`${green('NOBUILD')} README build terminated, parent commit is a Travis build!`
`${green(
'NOBUILD',
)} README build terminated, parent commit is a Travis build!`,
);
process.exit(0);
}
if (
util.isTravisCI() &&
(process.env['TRAVIS_EVENT_TYPE'] === 'cron' || process.env['TRAVIS_EVENT_TYPE'] === 'api')
) {
console.log(`${green('ARCHIVE')} Cron job or custom build, building archive README!`);
console.time('Builder');
let snippets = {};
// Synchronously read all snippets from snippets_archive folder and sort them as necessary (case-insensitive)
try {
const snippetFilenames = fs
.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'))
snippets[name] = fs.readFileSync(path.join(SNIPPETS_ARCHIVE_PATH, name), 'utf8');
} catch (err) {
console.log(`${red('ERROR!')} During snippet loading: ${err}`);
process.exit(1);
}
try {
// Add the start static part
let output =
misc.image('Logo', '/logo.png') +
headers.h1('Snippets Archive') +
"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." +
headers.h2('Table of Contents');
output += lists.ul(Object.entries(snippets), snippet =>
misc.link(`\`${snippet[0].slice(0, -3)}\``, misc.anchor(snippet[0].slice(0, -3)))
);
output += misc.hr();
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) {
console.log(`${red('ERROR!')} During README generation for snippets archive: ${err}`);
process.exit(1);
}
console.log(`${green('SUCCESS!')} README file generated for snippets archive!`);
console.timeEnd('Builder');
}
let snippets = {};
// Setup everything
let snippets = {},
snippetsArray = [],
startPart = '',
endPart = '',
output = '';
const EMOJIS = {
adapter: '🔌',
array: '📚',
@ -94,40 +52,47 @@ const EMOJIS = {
utility: '🔧'
};
let startPart = '',
endPart = '',
output = '',
tagDbData = {};
console.time('Builder');
// Synchronously read all snippets from snippets folder and sort them as necessary (case-insensitive)
snippets = util.readSnippets(SNIPPETS_PATH);
snippetsArray = Object.keys(snippets).reduce((acc, key) => {
acc.push(snippets[key]);
return acc;
}, []);
// Load static parts for the README file
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(`${red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
}
// Load tag data from the database
tagDbData = util.readTags();
console.log(tagDbData);
// Create the output for the README file
try {
const tags = util.prepTaggedData(tagDbData);
const tags = util.prepTaggedData(
Object.keys(snippets).reduce((acc, key) => {
acc[key] = snippets[key].attributes.tags;
return acc;
}, {}),
);
// Add the start static part
output += `${startPart}\n`;
// Loop over tags and snippets to create the table of contents
for (const tag of tags) {
const capitalizedTag = util.capitalize(tag, true);
const taggedSnippets = Object.entries(tagDbData).filter(v => v[1][0] === tag);
const taggedSnippets = snippetsArray.filter(
snippet => snippet.attributes.tags[0] === tag,
);
output += headers.h3((EMOJIS[tag] || '') + ' ' + capitalizedTag).trim();
output +=
@ -135,32 +100,43 @@ try {
'View contents',
lists.ul(taggedSnippets, snippet =>
misc.link(
`\`${snippet[0]}\``,
`${misc.anchor(snippet[0])}${snippet[1].includes('advanced') ? '-' : ''}`
)
)
`\`${snippet.title}\``,
`${misc.anchor(snippet.title)}${
snippet.attributes.tags.includes('advanced') ? '-' : ''
}`,
),
),
) + '\n';
}
// Loop over tags and snippets to create the list of snippets
for (const tag of tags) {
const capitalizedTag = util.capitalize(tag, true);
const taggedSnippets = Object.entries(tagDbData).filter(v => v[1][0] === tag);
const taggedSnippets = snippetsArray.filter(
snippet => snippet.attributes.tags[0] === tag,
);
output += misc.hr() + headers.h2((EMOJIS[tag] || '') + ' ' + capitalizedTag) + '\n';
output +=
misc.hr() + headers.h2((EMOJIS[tag] || '') + ' ' + capitalizedTag) + '\n';
for (const taggedSnippet of taggedSnippets) {
let snippet = snippets[taggedSnippet[0] + '.md'];
for (let snippet of taggedSnippets) {
if (snippet.attributes.tags.includes('advanced'))
output +=
headers.h3(
snippet.title + ' ' + misc.image('advanced', '/advanced.svg'),
) + '\n';
else output += headers.h3(snippet.title) + '\n';
// Add advanced tag
if (taggedSnippet[1].includes('advanced')) {
snippet = snippet.split(/\r?\n/);
// add label to snippet title (first line)
snippet[0] += ' ' + misc.image('advanced', '/advanced.svg');
snippet = snippet.join('\n');
}
output += snippet.attributes.text;
output += makeExamples(snippet);
output += `\`\`\`${config.language}\n${snippet.attributes.codeBlocks.es6}\n\`\`\``;
output += misc.collapsible(
'Examples',
`\`\`\`${config.language}\n${snippet.attributes.codeBlocks.example}\n\`\`\``,
);
output +=
'\n<br>' + misc.link('⬆ Back to top', misc.anchor('Contents')) + '\n';
}
}
@ -173,5 +149,104 @@ try {
process.exit(1);
}
console.log(`${green('SUCCESS!')} README file generated!`);
console.timeEnd('Builder');
// Snippets archive README file
output = '';
archivedSnippets = util.readSnippets(SNIPPETS_ARCHIVE_PATH);
archivedSnippetsArray = Object.keys(archivedSnippets).reduce((acc, key) => {
acc.push(archivedSnippets[key]);
return acc;
}, []);
// Load static parts for the README file
try {
startPart = fs.readFileSync(
path.join(STATIC_PARTS_PATH, 'snippets_archive_README-start.md'),
'utf8',
);
} catch (err) {
console.log(`${red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
}
// Create the output for the README file
try {
output += `${startPart}\n`;
// Loop over tags and snippets to create the table of contents
output += lists.ul(archivedSnippetsArray, snippet =>
misc.link(
`\`${snippet.title}\``,
`${misc.anchor(snippet.title)}`,
),
);
for (let snippet of archivedSnippetsArray) {
output += headers.h3(snippet.title) + '\n';
output += snippet.attributes.text;
output += `\`\`\`${config.language}\n${snippet.attributes.codeBlocks.es6}\n\`\`\``;
output += misc.collapsible(
'Examples',
`\`\`\`${config.language}\n${snippet.attributes.codeBlocks.example}\n\`\`\``,
);
output +=
'\n<br>' + misc.link('⬆ Back to top', misc.anchor('Contents')) + '\n';
}
// Write to the README file
fs.writeFileSync(path.join(SNIPPETS_ARCHIVE_PATH, 'README.md'), output);
} catch (err) {
console.log(`${red('ERROR!')} During README generation: ${err}`);
process.exit(1);
}
// Glossary README file
output = '';
glossarySnippets = util.readSnippets(GLOSSARY_PATH);
glossarySnippetsArray = Object.keys(glossarySnippets).reduce((acc, key) => {
acc.push(glossarySnippets[key]);
return acc;
}, []);
// Load static parts for the README file
try {
startPart = fs.readFileSync(
path.join(STATIC_PARTS_PATH, 'glossary_README-start.md'),
'utf8',
);
} catch (err) {
console.log(`${red('ERROR!')} During static part loading: ${err}`);
process.exit(1);
}
// Create the output for the README file
try {
output += `${startPart}\n`;
// Loop over tags and snippets to create the table of contents
output += lists.ul(glossarySnippetsArray, snippet =>
misc.link(
`\`${snippet.title}\``,
`${misc.anchor(snippet.title)}`,
),
);
for (let snippet of glossarySnippetsArray) {
output += headers.h3(snippet.title) + '\n';
output += snippet.attributes.text;
output +=
'\n<br>' + misc.link('⬆ Back to top', misc.anchor('Contents')) + '\n';
}
// Write to the README file
fs.writeFileSync(path.join(GLOSSARY_PATH, 'README.md'), output);
} catch (err) {
console.log(`${red('ERROR!')} During README generation: ${err}`);
process.exit(1);
}
console.log(`${green('SUCCESS!')} README files generated!`);
console.timeEnd('Builder');