1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-22 02:27:43 +01:00
developer-roadmap/bin/update-sponsors.cjs

133 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-03-01 02:19:40 +00:00
const path = require('path');
const fs = require('fs');
const yaml = require('js-yaml');
const apiKey = process.env.SPONSOR_SHEET_API_KEY;
const sheetId = process.env.SPONSOR_SHEET_ID;
if (!apiKey || !sheetId) {
console.error('Missing API key or sheet ID');
process.exit(1);
}
const sheetRange = 'A3:I1001';
const sheetUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${sheetRange}?key=${apiKey}`;
2023-03-13 18:04:20 +00:00
function populatePageAds({
pageUrl,
2023-03-01 02:19:40 +00:00
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
startDate,
endDate,
isActive,
}) {
const isConfiguredActive = isActive.toLowerCase() === 'yes';
const currentDate = new Date();
2023-03-30 01:23:01 +01:00
const isDateInRange =
currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
2023-03-01 02:19:40 +00:00
const shouldShowAd = isConfiguredActive && isDateInRange;
2023-03-30 01:23:01 +01:00
const urlPart = pageUrl
.replace('https://roadmap.sh/', '')
.replace(/\?.+?$/, '');
2023-03-01 02:19:40 +00:00
2023-03-30 01:23:01 +01:00
const parentDir = urlPart.startsWith('best-practices/')
? 'best-practices'
: 'roadmaps';
2023-03-13 18:04:20 +00:00
const pageId = urlPart.replace(`${parentDir}/`, '');
2023-03-01 02:19:40 +00:00
2023-03-30 01:23:01 +01:00
const pageFilePath = path.join(
__dirname,
`../src/data/${parentDir}`,
`${pageId}/${pageId}.md`
);
2023-03-13 18:04:20 +00:00
if (!fs.existsSync(pageFilePath)) {
console.error(`Page file not found: ${pageFilePath}`);
2023-03-01 02:19:40 +00:00
process.exit(1);
}
2023-03-13 18:04:20 +00:00
console.log(`Updating page: ${urlPart}`);
const pageFileContent = fs.readFileSync(pageFilePath, 'utf8');
2023-03-01 02:19:40 +00:00
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
2023-03-13 18:04:20 +00:00
const existingFrontmatter = pageFileContent.match(frontMatterRegex)[1];
2023-03-30 01:23:01 +01:00
const contentWithoutFrontmatter = pageFileContent
.replace(frontMatterRegex, ``)
.trim();
2023-03-01 02:19:40 +00:00
2023-03-01 23:04:32 +00:00
let frontmatterObj = yaml.load(existingFrontmatter);
delete frontmatterObj.sponsor;
2023-03-01 02:19:40 +00:00
if (shouldShowAd) {
2023-03-01 23:04:32 +00:00
const frontmatterValues = Object.entries(frontmatterObj);
const roadmapLabel = frontmatterObj.briefTitle;
2023-03-01 02:28:46 +00:00
2023-03-01 23:04:32 +00:00
// Insert sponsor data at 10 index i.e. after
2023-03-13 18:04:20 +00:00
// roadmap dimensions in the frontmatter
2023-03-01 02:28:46 +00:00
frontmatterValues.splice(10, 0, [
'sponsor',
{
url: redirectUrl,
title: adTitle,
imageUrl,
description: adDescription,
event: {
category: 'SponsorClick',
action: `${company} Redirect`,
2023-03-01 23:04:32 +00:00
label: `${roadmapLabel} / ${company} Link`,
2023-03-01 02:28:46 +00:00
},
2023-03-01 02:19:40 +00:00
},
2023-03-01 02:28:46 +00:00
]);
2023-03-01 23:04:32 +00:00
frontmatterObj = Object.fromEntries(frontmatterValues);
2023-03-01 02:19:40 +00:00
}
2023-03-30 01:23:01 +01:00
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
forceQuotes: true,
quotingType: '"',
});
2023-03-01 02:19:40 +00:00
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`;
2023-03-13 18:04:20 +00:00
fs.writeFileSync(pageFilePath, newContent, 'utf8');
2023-03-01 02:19:40 +00:00
}
fetch(sheetUrl)
.then((res) => res.json())
.then((rawData) => {
const rows = rawData.values;
rows.map((row) => {
// prettier-ignore
const [
2023-03-13 18:04:20 +00:00
pageUrl,
2023-03-01 02:19:40 +00:00
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
startDate,
endDate,
isActive,
] = row;
2023-03-13 18:04:20 +00:00
populatePageAds({
pageUrl,
2023-03-01 02:19:40 +00:00
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
startDate,
endDate,
isActive,
});
});
});