Skip to content

Instantly share code, notes, and snippets.

@e0ipso
Created December 19, 2024 11:22
Show Gist options
  • Save e0ipso/7a86f75f6bfa16728c5c928440f85feb to your computer and use it in GitHub Desktop.
Save e0ipso/7a86f75f6bfa16728c5c928440f85feb to your computer and use it in GitHub Desktop.
Table of Drupal modules
// Function to fetch data from a URL
async function fetchData(url) {
try {
const response = await globalThis.fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch data: ${response.statusText}`);
}
const data = await response.json();
return data.list || [];
} catch (error) {
console.error(`Error fetching data from URL ${url}:`, error.message);
return [];
}
}
// Function to fetch maintainers for a specific module
async function fetchMaintainers(machineName, firstUserId) {
const url = `https://www.drupal.org/project/${machineName}/maintainers.json`;
try {
const response = await globalThis.fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch maintainers: ${response.statusText}`);
}
const maintainers = await response.json();
return '<ul>' + Object.entries(maintainers)
.sort(([a], [b]) => (a === firstUserId ? 1 : b === firstUserId ? -1 : a - b))
.map(([userId, details]) => {
return `<li>[${details.name}](https://www.drupal.org/user/${userId})</li>`;
})
.join('') + '</ul>';
} catch (error) {
console.error(`Error fetching maintainers for ${machineName}:`, error.message);
return 'N/A';
}
}
// Main function to fetch and display module data
async function main(authorId) {
// Define the API URLs
const urls = [
`https://www.drupal.org/api-d7/node.json?type=project_module&author[value]=${authorId}`,
`https://www.drupal.org/api-d7/node.json?type=project_module&author[value]=${authorId}&page=1`
];
let allModules = [];
try {
// Fetch data from all URLs
for (const url of urls) {
const modules = await fetchData(url);
if (modules.length === 0) {
console.warn(`No modules retrieved from ${url}`);
}
allModules = allModules.concat(modules);
}
if (allModules.length === 0) {
console.error("No modules data retrieved from any URL. Exiting.");
return;
}
// Map modules to a structured format
const formattedModules = await Promise.all(
allModules.map(async module => {
try {
const aggregatedUsage = Object.values(module.project_usage || {}).reduce(
(sum, value) => sum + (parseInt(value) || 0),
0
);
if (aggregatedUsage === 0) return null;
const maintainers = await fetchMaintainers(module.field_project_machine_name, authorId);
const versions = Object.keys(module.project_usage || {}).map(version =>
version.startsWith('7.') ? 'Legacy' : 'Modern'
);
const versionType = [...new Set(versions)].join(' & ');
return {
name: `[${module.title}](https://www.drupal.org/project/${module.field_project_machine_name})`,
usage: typeof aggregatedUsage === 'number' && !isNaN(aggregatedUsage) ? aggregatedUsage : 0,
maintainers: maintainers,
stars: module.flag_project_star_user?.length || 0,
usageUrl: `https://www.drupal.org/project/usage/${module.field_project_machine_name}`,
versionType: versionType
};
} catch (error) {
console.error(`Error processing module: ${module.title || 'Unknown'}`, error.message);
return null;
}
})
);
// Filter out null entries and sort modules by usage (descending)
const filteredModules = formattedModules.filter(module => module !== null);
filteredModules.sort((a, b) => b.usage - a.usage);
// Create a table for displaying the data
const table = {
head: ['Module Name 🌐', 'Maintainers πŸ‘₯', 'Usage πŸ“Š', 'Version πŸ› οΈ'],
rows: []
};
// Add data to the table
filteredModules.forEach(module => {
table.rows.push([
module.name,
module.maintainers,
`[${module.usage}](${module.usageUrl}) (⭐${module.stars})`,
module.versionType
]);
});
// Print the table in Markdown format
console.log(`| ${table.head.join(' | ')} |`);
console.log(`| ${table.head.map(() => '---').join(' | ')} |`);
table.rows.forEach(row => console.log(`| ${row.join(' | ')} |`));
} catch (error) {
console.error("An unexpected error occurred:", error.message);
}
}
// Run the script
main(550110);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment