forked from roadmapsh/deprecated-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-sponsors.cjs
171 lines (141 loc) · 4.48 KB
/
update-sponsors.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 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) => {
console.log('Removing sponsor from: ', content);
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);
delete frontmatterObj.sponsor;
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
forceQuotes: true,
quotingType: "'",
});
const newContent = `---\n${newFrontmatter}---\n${contentWithoutFrontmatter}`;
fs.writeFileSync(pageFilePath, newContent, 'utf8');
});
}
function addPageSponsor({
pageUrl,
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
}) {
const urlPart = pageUrl
.replace('https://roadmap.sh/', '')
.replace(/\?.+?$/, '');
const parentDir = urlPart.startsWith('best-practices/')
? 'best-practices'
: 'roadmaps';
const pageId = urlPart.replace(`${parentDir}/`, '');
const pageFilePath = path.join(
__dirname,
`../src/data/${parentDir}`,
`${pageId}/${pageId}.md`
);
if (!fs.existsSync(pageFilePath)) {
console.error(`Page file not found: ${pageFilePath}`);
process.exit(1);
}
console.log(`Updating page: ${urlPart}`);
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);
delete frontmatterObj.sponsor;
const frontmatterValues = Object.entries(frontmatterObj);
const roadmapLabel = frontmatterObj.briefTitle;
// Insert sponsor data at 10 index i.e. after
// roadmap dimensions in the frontmatter
frontmatterValues.splice(10, 0, [
'sponsor',
{
url: redirectUrl,
title: adTitle,
imageUrl,
description: adDescription,
event: {
category: 'SponsorClick',
action: `${company} Redirect`,
label: `${roadmapLabel} / ${company} Link`,
},
},
]);
frontmatterObj = Object.fromEntries(frontmatterValues);
const newFrontmatter = yaml.dump(frontmatterObj, {
lineWidth: 10000,
forceQuotes: true,
quotingType: "'",
});
const newContent = `---\n${newFrontmatter}---\n\n${contentWithoutFrontmatter}`;
fs.writeFileSync(pageFilePath, newContent, 'utf8');
}
// Remove sponsors from all roadmaps
removeAllSponsors('roadmaps');
removeAllSponsors('best-practices');
console.log('------------------------');
console.log('Adding sponsors');
console.log('------------------------');
fetch(sheetUrl)
.then((res) => res.json())
.then((rawData) => {
const rows = rawData.values;
rows.map((row) => {
// prettier-ignore
const [
pageUrl,
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
startDate,
endDate,
isActive,
] = row;
const isConfiguredActive = isActive?.toLowerCase() === 'yes';
const currentDate = new Date();
const isDateInRange =
currentDate >= new Date(startDate) && currentDate <= new Date(endDate);
if (!isConfiguredActive || !isDateInRange) {
return;
}
addPageSponsor({
pageUrl,
company,
redirectUrl,
imageUrl,
adTitle,
adDescription,
startDate,
endDate,
isActive,
});
});
});