Skip to content

Commit

Permalink
feat: improved the report format and custom logs when isDryRun (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrymatio authored Jul 24, 2023
1 parent 892f4f1 commit 8af9ba2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
51 changes: 37 additions & 14 deletions packages/gatsby-theme-aio/algolia/index-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const parseHtml = require('./helpers/parse-html');
const parseMdx = require('./helpers/parse-mdx');
const createAlgoliaRecord = require('./create-record');

function indexRecords() {
function indexRecords(isDryRun) {
return [
{
query: mdxQuery,
Expand Down Expand Up @@ -76,25 +76,48 @@ function indexRecords() {
});
}

// markdownFiles frontmatter report - start
const frontmatterReport = isDryRun => {
// markdownFiles frontmatter report - start
let warnCount = 0;
let passCount = 0;

console.log('\x1b[35m ==== FRONTMATTER REPORT - STARTING ==================== \x1b[0m');
console.info('\x1b[36m [cmd + click] on the file path to open \x1b[0m');
console.log('\x1b[35m ==== FRONTMATTER REPORT - STARTING ==================== \x1b[0m');
console.info('\x1b[36m [cmd + click] on the file path to open \x1b[0m');
markdownFiles.forEach(file => {
if (!file.description || !file.keywords || !file.title) {
warnCount++;
console.log('\n\x1b[43mWarn\x1b[0m - ' + file.fileAbsolutePath + ': ');
if (!file.title) console.log('\x1b[33m\tMissing Title \x1b[0m');
if (!file.description) console.log('\x1b[33m\tMissing Description \x1b[0m');
if (!file.keywords) console.log('\x1b[33m\tMissing Keywords \x1b[0m');
} else {
passCount++;
console.log('\x1b[42mPass\x1b[0m - ' + file.fileAbsolutePath);
}
});

markdownFiles.forEach(file => {
if (!file.description || !file.keywords || !file.title) {
console.log('\n\x1b[41mFail\x1b[0m - ' + file.fileAbsolutePath + ': ');
if (!file.title) console.log('\x1b[33m\tMissing Title \x1b[0m');
if (!file.description) console.log('\x1b[33m\tMissing Description \x1b[0m');
if (!file.keywords) console.log('\x1b[33m\tMissing Keywords \x1b[0m');
if (isDryRun) {
if (passCount)
console.info(
`- \x1b[42m${passCount} Pages have Title, Description, and Keywords\x1b[0m`
);
if (warnCount)
console.warn(`- \x1b[43m${warnCount} Pages are missing frontmatter\x1b[0m`);
} else {
console.log('\x1b[42mPass\x1b[0m - ' + file.fileAbsolutePath);
if (passCount)
console.info(
`- \x1b[42m${passCount} Pages have Title, Description, and Keywords\x1b[0m`
);
// Implement an Error or prompt to user when attempting to index frontmatter-less files
if (warnCount)
console.error(`\x1b[41m${warnCount} Pages are missing frontmatter\x1b[0m`);
}
});
console.log('\x1b[35m ==== FRONTMATTER REPORT - FINISHED ==================== \x1b[0m');

console.log('\x1b[35m ==== FRONTMATTER REPORT - FINISHED ==================== \x1b[0m');
// markdownFiles frontmatter report - end
};

// markdownFiles frontmatter report - end
frontmatterReport(isDryRun);

const algoliaRecords = [];

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-theme-aio/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = {
appId: process.env.GATSBY_ALGOLIA_APPLICATION_ID,
indexName: process.env.GATSBY_ALGOLIA_INDEX_ENV_PREFIX ? `${process.env.GATSBY_ALGOLIA_INDEX_ENV_PREFIX}-${process.env.GATSBY_ALGOLIA_INDEX_NAME}` : process.env.GATSBY_ALGOLIA_INDEX_NAME,
apiKey: process.env.ALGOLIA_WRITE_API_KEY,
queries: indexRecords(),
queries: indexRecords(isDryRun),
chunkSize: 1000,
algoliasearchOptions: {
timeouts: {
Expand Down

0 comments on commit 8af9ba2

Please sign in to comment.