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.
168 lines
4.4 KiB
168 lines
4.4 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}`;
|
||
|
|
||
2 years ago
|
function removeAllSponsors(baseContentDir) {
|
||
|
console.log('------------------------');
|
||
|
console.log('Removing sponsors from: ', baseContentDir);
|
||
|
console.log('------------------------');
|
||
|
const dataDirPath = path.join(__dirname, '../src/data');
|
||
|
const contentDirPath = path.join(dataDirPath, baseContentDir);
|
||
|
|
||
|
const contentDir = fs.readdirSync(contentDirPath);
|
||
|
contentDir.forEach((content) => {
|
||
2 years ago
|
console.log('Removing sponsors from: ', content);
|
||
2 years ago
|
|
||
|
const pageFilePath = path.join(contentDirPath, content, `${content}.md`);
|
||
|
const pageFileContent = fs.readFileSync(pageFilePath, 'utf8');
|
||
|
|
||
|
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
|
||
|
|
||
|
const existingFrontmatter = pageFileContent.match(frontMatterRegex)[1];
|
||
|
const contentWithoutFrontmatter = pageFileContent
|
||
|
.replace(frontMatterRegex, ``)
|
||
|
.trim();
|
||
|
|
||
|
let frontmatterObj = yaml.load(existingFrontmatter);
|
||
2 years ago
|
delete frontmatterObj.sponsors;
|
||
2 years ago
|
|
||
|
const newFrontmatter = yaml.dump(frontmatterObj, {
|
||
|
lineWidth: 10000,
|
||
|
forceQuotes: true,
|
||
|
quotingType: "'",
|
||
|
});
|
||
|
const newContent = `---\n${newFrontmatter}---\n${contentWithoutFrontmatter}`;
|
||
|
|
||
|
fs.writeFileSync(pageFilePath, newContent, 'utf8');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function addPageSponsor({
|
||
2 years ago
|
pageUrl,
|
||
2 years ago
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
}) {
|
||
2 years ago
|
const urlPart = pageUrl
|
||
|
.replace('https://roadmap.sh/', '')
|
||
|
.replace(/\?.+?$/, '');
|
||
2 years ago
|
|
||
2 years ago
|
const parentDir = urlPart.startsWith('best-practices/')
|
||
|
? 'best-practices'
|
||
|
: 'roadmaps';
|
||
2 years ago
|
const pageId = urlPart.replace(`${parentDir}/`, '');
|
||
2 years ago
|
|
||
2 years ago
|
const pageFilePath = path.join(
|
||
|
__dirname,
|
||
|
`../src/data/${parentDir}`,
|
||
|
`${pageId}/${pageId}.md`
|
||
|
);
|
||
2 years ago
|
|
||
|
if (!fs.existsSync(pageFilePath)) {
|
||
|
console.error(`Page file not found: ${pageFilePath}`);
|
||
2 years ago
|
process.exit(1);
|
||
|
}
|
||
|
|
||
2 years ago
|
console.log(`Updating page: ${urlPart}`);
|
||
|
const pageFileContent = fs.readFileSync(pageFilePath, 'utf8');
|
||
2 years ago
|
|
||
|
const frontMatterRegex = /---\n([\s\S]*?)\n---/;
|
||
|
|
||
2 years ago
|
const existingFrontmatter = pageFileContent.match(frontMatterRegex)[1];
|
||
2 years ago
|
const contentWithoutFrontmatter = pageFileContent
|
||
|
.replace(frontMatterRegex, ``)
|
||
|
.trim();
|
||
2 years ago
|
|
||
2 years ago
|
let frontmatterObj = yaml.load(existingFrontmatter);
|
||
2 years ago
|
const sponsors = frontmatterObj.sponsors || [];
|
||
2 years ago
|
|
||
2 years ago
|
const frontmatterValues = Object.entries(frontmatterObj);
|
||
|
const roadmapLabel = frontmatterObj.briefTitle;
|
||
|
|
||
2 years ago
|
sponsors.push({
|
||
|
url: redirectUrl,
|
||
|
title: adTitle,
|
||
|
imageUrl,
|
||
|
description: adDescription,
|
||
|
page: roadmapLabel,
|
||
|
company,
|
||
|
});
|
||
|
|
||
2 years ago
|
// Insert sponsor data at 10 index i.e. after
|
||
|
// roadmap dimensions in the frontmatter
|
||
2 years ago
|
frontmatterValues.splice(10, 0, ['sponsors', sponsors]);
|
||
2 years ago
|
|
||
2 years ago
|
frontmatterObj = Object.fromEntries(frontmatterValues);
|
||
2 years ago
|
|
||
2 years ago
|
const newFrontmatter = yaml.dump(frontmatterObj, {
|
||
|
lineWidth: 10000,
|
||
|
forceQuotes: true,
|
||
2 years ago
|
quotingType: "'",
|
||
2 years ago
|
});
|
||
2 years ago
|
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`;
|
||
|
|
||
2 years ago
|
fs.writeFileSync(pageFilePath, newContent, 'utf8');
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// Remove sponsors from all roadmaps
|
||
|
removeAllSponsors('roadmaps');
|
||
|
removeAllSponsors('best-practices');
|
||
|
|
||
2 years ago
|
console.log('------------------------');
|
||
|
console.log('Adding sponsors');
|
||
|
console.log('------------------------');
|
||
2 years ago
|
fetch(sheetUrl)
|
||
|
.then((res) => res.json())
|
||
|
.then((rawData) => {
|
||
|
const rows = rawData.values;
|
||
|
|
||
|
rows.map((row) => {
|
||
|
// prettier-ignore
|
||
|
const [
|
||
2 years ago
|
pageUrl,
|
||
2 years ago
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
startDate,
|
||
|
endDate,
|
||
|
isActive,
|
||
|
] = row;
|
||
|
|
||
2 years ago
|
const isConfiguredActive = isActive?.toLowerCase() === 'yes';
|
||
|
const currentDate = new Date();
|
||
2 years ago
|
const isDateInRange =
|
||
|
currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
|
||
2 years ago
|
|
||
|
if (!isConfiguredActive || !isDateInRange) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
addPageSponsor({
|
||
2 years ago
|
pageUrl,
|
||
2 years ago
|
company,
|
||
|
redirectUrl,
|
||
|
imageUrl,
|
||
|
adTitle,
|
||
|
adDescription,
|
||
|
startDate,
|
||
|
endDate,
|
||
|
isActive,
|
||
|
});
|
||
|
});
|
||
|
});
|