forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-showcase.js
More file actions
170 lines (145 loc) · 4.51 KB
/
Copy pathupdate-showcase.js
File metadata and controls
170 lines (145 loc) · 4.51 KB
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
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
// Load environment variables
dotenv.config();
// Constants for the projects table
const PROJECTS_BASE_ID = "appyWBGCHaZoTzKTN";
const PROJECTS_TABLE_NAME = "tbl7PMpN6pHpF8rMX";
const PROJECTS_VIEW_NAME = "viwIiHvahcPMYpx4M";
if (!process.env.AIRTABLE_KEY) {
console.error("AIRTABLE_KEY environment variable is required");
process.exit(1);
}
/**
* Load records from Airtable API
*/
async function loadRecords({
apiKey,
baseId,
tableName,
viewId,
offset = null,
}) {
const url = `https://api.airtable.com/v0/${baseId}/${tableName}?view=${viewId}${
offset ? `&offset=${offset}` : ""
}`;
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
return response.json();
}
/**
* Fetch all records from Airtable
*/
async function fetchAirtableRecords({ apiKey, baseId, tableName, viewId }) {
let records = [];
let offset = null;
do {
const res = await loadRecords({
apiKey,
baseId,
tableName,
viewId,
offset,
});
if (!res.records) {
console.error("No records found in Airtable response.");
break;
}
offset = res.offset;
records.push(...res.records);
} while (offset);
return records;
}
/**
* Process projects data into the correct format
*/
function processProjectsData(records) {
return records.map((record) => {
const fields = record.fields;
// Extract tags (looking for tags__001 through tags__007)
const tags = [];
for (let i = 1; i <= 7; i++) {
const tagKey = `tags__${String(i).padStart(3, "0")}`;
if (fields[tagKey]) {
tags.push(fields[tagKey]);
}
}
// Extract screenshots
const screenshots = [];
for (let i = 1; i <= 3; i++) {
const screenshotKey = `screenshots__${String(i).padStart(3, "0")}`;
if (fields[screenshotKey]) {
screenshots.push(fields[screenshotKey]);
}
}
// Extract authOrigins
const authOrigins = [];
for (let i = 1; i <= 2; i++) {
const authOriginKey = `authOrigins__${String(i).padStart(3, "0")}`;
if (fields[authOriginKey]) {
authOrigins.push(fields[authOriginKey]);
}
}
// Convert usesInternetIdentity from string to boolean
const usesInternetIdentity = fields.usesInternetIdentity === "True";
// Build the project object in the correct format
const project = {
id: fields.id,
name: fields.name,
};
// Add optional fields only if they exist
if (fields.display) project.display = fields.display;
if (fields.website) project.website = fields.website;
if (fields.twitter) project.twitter = fields.twitter;
if (fields.github) project.github = fields.github;
if (fields.youtube) project.youtube = fields.youtube;
if (fields.submittableId) project.submittableId = fields.submittableId;
if (tags.length > 0) project.tags = tags;
if (fields.stats) project.stats = fields.stats;
if (fields.description) project.description = fields.description;
if (fields.usesInternetIdentity !== undefined)
project.usesInternetIdentity = usesInternetIdentity;
if (authOrigins.length > 0) project.authOrigins = authOrigins;
if (fields.logo) project.logo = fields.logo;
if (screenshots.length > 0) project.screenshots = screenshots;
if (fields.video) project.video = fields.video;
if (fields.videoContentType)
project.videoContentType = fields.videoContentType;
if (fields.oneLiner) project.oneLiner = fields.oneLiner;
return project;
});
}
/**
* Main function to update showcase.json
*/
async function updateShowcase() {
try {
console.log("Fetching projects from Airtable...");
// Load projects from Airtable
const records = await fetchAirtableRecords({
apiKey: process.env.AIRTABLE_KEY,
baseId: PROJECTS_BASE_ID,
tableName: PROJECTS_TABLE_NAME,
viewId: PROJECTS_VIEW_NAME,
});
// Process projects data
const projects = processProjectsData(records);
// Write showcase.json to the project root
const showcasePath = path.join(__dirname, "showcase.json");
fs.writeFileSync(showcasePath, JSON.stringify(projects, null, 2));
console.log(
`Successfully wrote ${projects.length} projects to showcase.json`
);
return true;
} catch (error) {
console.error(`Error updating showcase.json: ${error.message}`);
return false;
}
}
// Run the update showcase function
updateShowcase();