computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
119 lines
3.0 KiB
2 years ago
|
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}`;
|
||
|
|
||
|
function populateRoadmapAds({
|
||
|
roadmapUrl,
|
||
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
startDate,
|
||
|
endDate,
|
||
|
isActive,
|
||
|
}) {
|
||
|
const isConfiguredActive = isActive.toLowerCase() === 'yes';
|
||
|
|
||
|
const currentDate = new Date();
|
||
|
const isDateInRange = currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
|
||
|
const shouldShowAd = isConfiguredActive && isDateInRange;
|
||
|
|
||
|
// get id from the roadmap URL
|
||
|
const roadmapId = roadmapUrl
|
||
|
.split('/')
|
||
|
.pop()
|
||
|
.replace(/\?.+?$/, '');
|
||
|
|
||
|
const roadmapFilePath = path.join(__dirname, '../src/data/roadmaps', `${roadmapId}/${roadmapId}.md`);
|
||
|
|
||
|
if (!fs.existsSync(roadmapFilePath)) {
|
||
|
console.error(`Roadmap file not found: ${roadmapFilePath}`);
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
|
console.log(`Updating roadmap: ${roadmapId}`);
|
||
|
const roadmapFileContent = fs.readFileSync(roadmapFilePath, 'utf8');
|
||
|
|
||
|
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
|
||
|
|
||
|
const existingFrontmatter = roadmapFileContent.match(frontMatterRegex)[1];
|
||
|
const contentWithoutFrontmatter = roadmapFileContent.replace(frontMatterRegex, ``).trim();
|
||
|
|
||
2 years ago
|
let fronmatterObj = yaml.load(existingFrontmatter);
|
||
2 years ago
|
|
||
|
if (shouldShowAd) {
|
||
2 years ago
|
const frontmatterValues = Object.entries(fronmatterObj);
|
||
|
|
||
|
// Insert sponsor data at 10 index i.e. after
|
||
|
// roadmap dimensions in the fronmatter
|
||
|
frontmatterValues.splice(10, 0, [
|
||
|
'sponsor',
|
||
|
{
|
||
|
url: redirectUrl,
|
||
|
title: adTitle,
|
||
|
imageUrl,
|
||
|
description: adDescription,
|
||
|
event: {
|
||
|
category: 'SponsorClick',
|
||
|
action: `${company} Redirect`,
|
||
|
label: `Clicked ${company} Link`,
|
||
|
},
|
||
2 years ago
|
},
|
||
2 years ago
|
]);
|
||
|
|
||
|
fronmatterObj = Object.fromEntries(frontmatterValues);
|
||
2 years ago
|
} else {
|
||
2 years ago
|
delete fronmatterObj.sponsor;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const newFrontmatter = yaml.dump(fronmatterObj, { lineWidth: 10000, forceQuotes: true, quotingType: '"' });
|
||
2 years ago
|
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`;
|
||
|
|
||
|
fs.writeFileSync(roadmapFilePath, newContent, 'utf8');
|
||
|
}
|
||
|
|
||
|
fetch(sheetUrl)
|
||
|
.then((res) => res.json())
|
||
|
.then((rawData) => {
|
||
|
const rows = rawData.values;
|
||
|
|
||
|
rows.map((row) => {
|
||
|
// prettier-ignore
|
||
|
const [
|
||
|
roadmapUrl,
|
||
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
startDate,
|
||
|
endDate,
|
||
|
isActive,
|
||
|
] = row;
|
||
|
|
||
|
populateRoadmapAds({
|
||
|
roadmapUrl,
|
||
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
startDate,
|
||
|
endDate,
|
||
|
isActive,
|
||
|
});
|
||
|
});
|
||
|
});
|