1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-21 09:42:29 +01:00
developer-roadmap/scripts/update-sponsors.cjs

168 lines
4.4 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-04-03 19:15:10 +01:00
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) => {
2023-05-02 20:47:55 +01:00
console.log('Removing sponsors from: ', content);
2023-04-03 19:15:10 +01:00
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);
2023-05-02 20:47:55 +01:00
delete frontmatterObj.sponsors;
2023-04-03 19:15:10 +01:00
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
forceQuotes: true,
quotingType: "'",
});
const newContent = `---\n${newFrontmatter}---\n${contentWithoutFrontmatter}`;
fs.writeFileSync(pageFilePath, newContent, 'utf8');
});
}
function addPageSponsor({
2023-03-13 18:04:20 +00:00
pageUrl,
2023-03-01 02:19:40 +00:00
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
}) {
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);
2023-05-02 20:47:55 +01:00
const sponsors = frontmatterObj.sponsors || [];
2023-03-01 02:19:40 +00:00
2023-04-03 19:15:10 +01:00
const frontmatterValues = Object.entries(frontmatterObj);
const roadmapLabel = frontmatterObj.briefTitle;
2023-05-02 20:47:55 +01:00
sponsors.push({
url: redirectUrl,
title: adTitle,
imageUrl,
description: adDescription,
page: roadmapLabel,
company,
});
2023-04-03 19:15:10 +01:00
// Insert sponsor data at 10 index i.e. after
// roadmap dimensions in the frontmatter
2023-05-02 20:47:55 +01:00
frontmatterValues.splice(10, 0, ['sponsors', sponsors]);
2023-03-01 02:28:46 +00:00
2023-04-03 19:15:10 +01: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,
2023-04-01 01:37:24 +01:00
quotingType: "'",
2023-03-30 01:23:01 +01:00
});
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
}
2023-04-03 19:15:10 +01:00
// Remove sponsors from all roadmaps
removeAllSponsors('roadmaps');
removeAllSponsors('best-practices');
2023-04-03 21:37:13 +01:00
console.log('------------------------');
console.log('Adding sponsors');
console.log('------------------------');
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-04-03 19:15:10 +01:00
const isConfiguredActive = isActive?.toLowerCase() === 'yes';
const currentDate = new Date();
2023-04-03 21:37:13 +01:00
const isDateInRange =
currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
2023-04-03 19:15:10 +01:00
if (!isConfiguredActive || !isDateInRange) {
return;
}
addPageSponsor({
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,
});
});
});