Add extractor script

Also runs on Travis
This commit is contained in:
Angelos Chalaris
2018-05-30 19:19:36 +03:00
parent bdd7eff932
commit 7113390fca
6 changed files with 6502 additions and 1 deletions

68
scripts/extract.js Normal file
View File

@ -0,0 +1,68 @@
/*
This is the extractor script that generates the snippets.json and snippetsArchive.json files.
Run using `npm run extractor`.
*/
// Load modules
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const util = require('./util');
// Paths
const SNIPPETS_PATH = './snippets';
const SNIPPETS_ARCHIVE_PATH = './snippets_archive';
const OUTPUT_PATH = './snippet_data';
// Check if running on Travis - only build for cron jobs and custom builds
if(util.isTravisCI() && process.env['TRAVIS_EVENT_TYPE'] !== 'cron' && process.env['TRAVIS_EVENT_TYPE'] !== 'api') {
console.log(`${chalk.green('NOBUILD')} snippet extraction terminated, not a cron or api build!`);
process.exit(0);
}
// Read data
let snippets = {}, archivedSnippets = {}, tagDbData = {};
console.time('Extractor');
snippets = util.readSnippets(SNIPPETS_PATH);
archivedSnippets = util.readSnippets(SNIPPETS_ARCHIVE_PATH);
tagDbData = util.readTags();
// Extract snippet data
let snippetData = {
data: Object.keys(snippets).map(key => {
return {
id: key.slice(0,-3),
type: 'snippet',
attributes: {
fileName: key,
text: util.getTextualContent(snippets[key]).trim(),
codeBlocks: util.getCodeBlocks(snippets[key]).map(v => v.replace(/```js([\s\S]*?)```/g, '$1').trim()),
tags: tagDbData[key.slice(0,-3)]
},
meta: {
archived: false,
hash: util.hashData(snippets[key])
}
}
})
};
// Extract archived snippet data
let snippetArchiveData = {
data: Object.keys(archivedSnippets).map(key => {
return {
id: key.slice(0,-3),
type: 'snippet',
attributes: {
fileName: key,
text: util.getTextualContent(archivedSnippets[key]).trim(),
codeBlocks: util.getCodeBlocks(archivedSnippets[key]).map(v => v.replace(/```js([\s\S]*?)```/g, '$1').trim()),
tags: []
},
meta: {
archived: true,
hash: util.hashData(archivedSnippets[key])
}
}
})
};
// Write files
fs.writeFileSync(path.join(OUTPUT_PATH, 'snippets.json'), JSON.stringify(snippetData, null, 2));
fs.writeFileSync(path.join(OUTPUT_PATH, 'snippetsArchive.json'), JSON.stringify(snippetArchiveData, null, 2));
// Display messages and time
console.log(`${chalk.green('SUCCESS!')} snippets.json and snippetsArchive.json files generated!`);
console.timeEnd('Extractor');

View File

@ -78,4 +78,34 @@ const capitalize = (str, lowerRest = false) =>
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
// Creates a hash for a value using the SHA-256 algorithm.
const hashData = val => crypto.createHash('sha256').update(val).digest('hex');
module.exports = {readSnippets, readTags, optimizeNodes, capitalize, objectFromPairs, isTravisCI, hashData, shuffle};
// Gets the code blocks for a snippet file.
const getCodeBlocks = str => {
const regex = /```[.\S\s]*?```/g;
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex += 1;
}
m.forEach((match, groupIndex) => {
results.push(match);
})
}
return results;
}
// Gets the textual content for a snippet file.
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;
}
m.forEach((match, groupIndex) => {
results.push(match);
})
}
return results[1];
}
module.exports = {readSnippets, readTags, optimizeNodes, capitalize, objectFromPairs, isTravisCI, hashData, shuffle, getCodeBlocks, getTextualContent};