Update and housekeeping

This commit is contained in:
Angelos Chalaris
2019-08-21 13:00:53 +03:00
parent 255df75ad3
commit 58cf802828
40 changed files with 754 additions and 630 deletions

View File

@ -16,15 +16,8 @@ const SNIPPETS_PATH = `./${config.snippetPath}`;
const STATIC_PARTS_PATH = `./${config.staticPartsPath}`;
// 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!`,
);
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!`);
process.exit(0);
}
@ -47,14 +40,8 @@ snippetsArray = Object.keys(snippets).reduce((acc, key) => {
// 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);
@ -66,7 +53,7 @@ try {
Object.keys(snippets).reduce((acc, key) => {
acc[key] = snippets[key].attributes.tags;
return acc;
}, {}),
}, {})
);
output += `${startPart}\n`;
@ -74,9 +61,7 @@ try {
// Loop over tags and snippets to create the table of contents
for (const tag of tags) {
const capitalizedTag = util.capitalize(tag, true);
const taggedSnippets = snippetsArray.filter(
snippet => snippet.attributes.tags[0] === tag,
);
const taggedSnippets = snippetsArray.filter(snippet => snippet.attributes.tags[0] === tag);
output += headers.h3((EMOJIS[tag] || '') + ' ' + capitalizedTag).trim();
output +=
@ -87,44 +72,36 @@ try {
`\`${snippet.title}\``,
`${misc.anchor(snippet.title)}${
snippet.attributes.tags.includes('advanced') ? '-' : ''
}`,
),
),
}`
)
)
) + '\n';
}
for (const tag of tags) {
const capitalizedTag = util.capitalize(tag, true);
const taggedSnippets = snippetsArray.filter(
snippet => snippet.attributes.tags[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 (let snippet of taggedSnippets) {
if (snippet.attributes.tags.includes('advanced'))
output +=
headers.h3(
snippet.title + ' ' + misc.image('advanced', '/advanced.svg'),
) + '\n';
output += headers.h3(snippet.title + ' ' + misc.image('advanced', '/advanced.svg')) + '\n';
else output += headers.h3(snippet.title) + '\n';
output += snippet.attributes.text;
if (snippet.attributes.codeBlocks.style !== '')
output += `\`\`\`${config.optionalLanguage}\n${snippet.attributes.codeBlocks.style}\n\`\`\`\n\n`;
output += `\`\`\`${config.language}\n${snippet.attributes.codeBlocks.code}\n\`\`\``;
output += misc.collapsible(
'Examples',
`\`\`\`${config.language}\n${snippet.attributes.codeBlocks.example}\n\`\`\``,
`\`\`\`${config.language}\n${snippet.attributes.codeBlocks.example}\n\`\`\``
);
output +=
'\n<br>' + misc.link('⬆ Back to top', misc.anchor('Contents')) + '\n';
output += '\n<br>' + misc.link('⬆ Back to top', misc.anchor('Contents')) + '\n';
}
}

View File

@ -19,11 +19,7 @@ if (
process.env['TRAVIS_EVENT_TYPE'] !== 'cron' &&
process.env['TRAVIS_EVENT_TYPE'] !== 'api'
) {
console.log(
`${green(
'NOBUILD',
)} snippet extraction terminated, not a cron or api build!`,
);
console.log(`${green('NOBUILD')} snippet extraction terminated, not a cron or api build!`);
process.exit(0);
}
@ -43,8 +39,8 @@ const completeData = {
data: [...snippetsArray],
meta: {
specification: 'http://jsonapi.org/format/',
type: 'snippetArray',
},
type: 'snippetArray'
}
};
let listingData = {
data: completeData.data.map(v => ({
@ -53,28 +49,20 @@ let listingData = {
title: v.title,
attributes: {
text: v.attributes.text,
tags: v.attributes.tags,
tags: v.attributes.tags
},
meta: {
hash: v.meta.hash,
},
hash: v.meta.hash
}
})),
meta: {
specification: 'http://jsonapi.org/format/',
type: 'snippetListingArray',
},
type: 'snippetListingArray'
}
};
// Write files
fs.writeFileSync(
path.join(OUTPUT_PATH, 'snippets.json'),
JSON.stringify(completeData, null, 2),
);
fs.writeFileSync(
path.join(OUTPUT_PATH, 'snippetList.json'),
JSON.stringify(listingData, null, 2),
);
fs.writeFileSync(path.join(OUTPUT_PATH, 'snippets.json'), JSON.stringify(completeData, null, 2));
fs.writeFileSync(path.join(OUTPUT_PATH, 'snippetList.json'), JSON.stringify(listingData, null, 2));
// Display messages and time
console.log(
`${green('SUCCESS!')} snippets.json and snippetList.json files generated!`,
);
console.log(`${green('SUCCESS!')} snippets.json and snippetList.json files generated!`);
console.timeEnd('Extractor');

View File

@ -1,12 +1,11 @@
// Checks if current environment is Travis CI, Cron builds, API builds
const isTravisCI = () => 'TRAVIS' in process.env && 'CI' in process.env;
const isTravisCronOrAPI = () =>
process.env['TRAVIS_EVENT_TYPE'] === 'cron' ||
process.env['TRAVIS_EVENT_TYPE'] === 'api';
process.env['TRAVIS_EVENT_TYPE'] === 'cron' || process.env['TRAVIS_EVENT_TYPE'] === 'api';
const isNotTravisCronOrAPI = () => !isTravisCronOrAPI();
module.exports = {
isTravisCI,
isTravisCronOrAPI,
isNotTravisCronOrAPI,
isNotTravisCronOrAPI
};

View File

@ -22,8 +22,7 @@ const optimizeNodes = (data, regexp, replacer) => {
};
// Capitalizes the first letter of a string
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() +
(lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
const prepTaggedData = tagDbData =>
[...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v)
@ -32,22 +31,17 @@ const prepTaggedData = tagDbData =>
? 1
: capitalize(b, true) === 'Uncategorized'
? -1
: a.localeCompare(b),
: a.localeCompare(b)
);
const makeExamples = data => {
data =
data.slice(0, data.lastIndexOf(`\`\`\`${config.language}`)).trim() +
misc.collapsible(
'Examples',
data.slice(
data.lastIndexOf(`\`\`\`${config.language}`),
data.lastIndexOf('```'),
) + data.slice(data.lastIndexOf('```')),
data.slice(data.lastIndexOf(`\`\`\`${config.language}`), data.lastIndexOf('```')) +
data.slice(data.lastIndexOf('```'))
);
return `${data}\n<br>${misc.link(
'⬆ Back to top',
misc.anchor('Contents'),
)}\n\n`;
return `${data}\n<br>${misc.link('⬆ Back to top', misc.anchor('Contents'))}\n\n`;
};
module.exports = {
@ -56,5 +50,5 @@ module.exports = {
optimizeNodes,
capitalize,
prepTaggedData,
makeExamples,
makeExamples
};

View File

@ -1,22 +1,18 @@
const {
isTravisCI,
isTravisCronOrAPI,
isNotTravisCronOrAPI,
} = require('./environmentCheck');
const { isTravisCI, isTravisCronOrAPI, isNotTravisCronOrAPI } = require('./environmentCheck');
const {
getMarkDownAnchor,
objectFromPairs,
optimizeNodes,
capitalize,
prepTaggedData,
makeExamples,
makeExamples
} = require('./helpers');
const {
getFilesInDir,
hashData,
getCodeBlocks,
getTextualContent,
readSnippets,
readSnippets
} = require('./snippetParser');
module.exports = {
@ -33,5 +29,5 @@ module.exports = {
hashData,
getCodeBlocks,
getTextualContent,
readSnippets,
readSnippets
};

View File

@ -20,10 +20,7 @@ const getFilesInDir = (directoryPath, withPath, exclude = null) => {
if (withPath) {
// a hacky way to do conditional array.map
return directoryFilenames.reduce((fileNames, fileName) => {
if (
exclude == null ||
!exclude.some(toExclude => fileName === toExclude)
)
if (exclude == null || !exclude.some(toExclude => fileName === toExclude))
fileNames.push(`${directoryPath}/${fileName}`);
return fileNames;
}, []);
@ -52,25 +49,24 @@ const getCodeBlocks = str => {
results.push(match);
});
}
const replacer = new RegExp(
`\`\`\`${config.language}([\\s\\S]*?)\`\`\``,
'g',
const replacer = new RegExp(`\`\`\`${config.language}([\\s\\S]*?)\`\`\``, 'g');
const optionalReplacer = new RegExp(`\`\`\`${config.optionalLanguage}([\\s\\S]*?)\`\`\``, 'g');
results = results.map(v =>
v
.replace(replacer, '$1')
.replace(optionalReplacer, '$1')
.trim()
);
const optionalReplacer = new RegExp(
`\`\`\`${config.optionalLanguage}([\\s\\S]*?)\`\`\``,
'g',
);
results = results.map(v => v.replace(replacer, '$1').replace(optionalReplacer, '$1').trim());
if(results.length > 2)
if (results.length > 2)
return {
style: results[0],
code: results[1],
example: results[2],
example: results[2]
};
return {
style: '',
code: results[0],
example: results[1],
example: results[1]
};
};
// Gets the textual content for a snippet file.
@ -95,9 +91,7 @@ const readSnippets = snippetsPath => {
let snippets = {};
try {
for (let snippet of snippetFilenames) {
let data = frontmatter(
fs.readFileSync(path.join(snippetsPath, snippet), 'utf8'),
);
let data = frontmatter(fs.readFileSync(path.join(snippetsPath, snippet), 'utf8'));
snippets[snippet] = {
id: snippet.slice(0, -3),
title: data.attributes.title,
@ -106,11 +100,11 @@ const readSnippets = snippetsPath => {
fileName: snippet,
text: getTextualContent(data.body),
codeBlocks: getCodeBlocks(data.body),
tags: data.attributes.tags.split(',').map(t => t.trim()),
tags: data.attributes.tags.split(',').map(t => t.trim())
},
meta: {
hash: hashData(data.body),
},
hash: hashData(data.body)
}
};
}
} catch (err) {
@ -125,5 +119,5 @@ module.exports = {
hashData,
getCodeBlocks,
getTextualContent,
readSnippets,
readSnippets
};