Files
backstage/scripts/plugin-directory-audit.js
T
Andre Wanlin d00a492570 Changes based on feedback
Signed-off-by: Andre Wanlin <awanlin@spotify.com>
2026-02-17 17:15:07 -06:00

148 lines
4.5 KiB
JavaScript

#!/usr/bin/env node
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable @backstage/no-undeclared-imports */
// This script is used to audit the list of plugins in the Plugin Directory: https://backstage.io/plugins
const { resolve } = require('node:path');
const fs = require('fs-extra');
const yaml = require('js-yaml');
async function getNpmPackage(npmPackageName) {
const response = await fetch(`https://registry.npmjs.com/${npmPackageName}`);
const json = await response.json();
return json;
}
function getAge(npmModified) {
const ageDif = Date.now() - new Date(npmModified).getTime();
return Math.round(ageDif / (1000 * 60 * 60 * 24));
}
async function main(args) {
const rootPath = resolve(__dirname, '..');
const pluginDataPath = resolve(rootPath, 'microsite/data/plugins');
const auditMode = args.includes('--audit');
const pluginDataFiles = fs.readdirSync(pluginDataPath);
const pluginsData = [];
const updatedPlugins = [];
for (const pluginDataFile of pluginDataFiles) {
const pluginDataFilePath = resolve(pluginDataPath, pluginDataFile);
const pluginDataYaml = yaml.load(
fs.readFileSync(pluginDataFilePath, { encoding: 'utf-8' }),
);
console.log(
`Auditing - ${pluginDataYaml.title} by ${pluginDataYaml.author} - ${pluginDataYaml.npmPackageName}`,
);
const npmPackage = await getNpmPackage(pluginDataYaml.npmPackageName);
const modifiedTime = npmPackage.time?.modified;
const age = getAge(npmPackage.time?.modified);
if (!modifiedTime || isNaN(age)) {
console.warn(
`Skipping ${pluginDataYaml.title}: Could not calculate age (Data: ${modifiedTime})`,
);
continue; // Skip to the next plugin in the loop
}
const pluginData = {
npmPackageName: pluginDataYaml.npmPackageName,
npmCreated: npmPackage.time?.created,
npmModified: npmPackage.time?.modified,
age: age,
currentStatus: pluginDataYaml.status,
};
// Update plugin YAML if in audit mode
if (auditMode) {
let newStatus = pluginDataYaml.status;
let statusChanged = false;
// If age < 365 and status is inactive or archived, change to active
if (
age < 365 &&
(pluginDataYaml.status === 'inactive' ||
pluginDataYaml.status === 'archived')
) {
newStatus = 'active';
statusChanged = true;
}
// If status is inactive, change to archived
else if (age > 365 && pluginDataYaml.status === 'inactive') {
newStatus = 'archived';
statusChanged = true;
}
// If age > 365 and status is active, change to inactive
else if (age > 365 && pluginDataYaml.status === 'active') {
newStatus = 'inactive';
statusChanged = true;
}
if (statusChanged) {
pluginDataYaml.status = newStatus;
pluginDataYaml.staleSince = new Date().toISOString().split('T')[0];
// Write updated YAML back to file
const yamlContent = yaml.dump(pluginDataYaml, {
lineWidth: -1,
quotingType: "'",
forceQuotes: false,
});
fs.writeFileSync(pluginDataFilePath, `---\n${yamlContent}`);
updatedPlugins.push({
file: pluginDataFile,
plugin: pluginDataYaml.title,
oldStatus: pluginData.currentStatus,
newStatus: newStatus,
age: age,
});
console.log(
` ✓ Updated ${pluginDataFile}: ${pluginData.currentStatus}${newStatus} (age: ${age} days)`,
);
}
pluginData.newStatus = newStatus;
}
pluginsData.push(pluginData);
}
console.table(pluginsData);
if (auditMode && updatedPlugins.length > 0) {
console.log('\n=== Summary of Updates ===');
console.table(updatedPlugins);
console.log(`\nTotal plugins updated: ${updatedPlugins.length}`);
} else if (auditMode) {
console.log('\nNo plugins required updates.');
}
}
main(process.argv.slice(2)).catch(error => {
console.error(error.stack || error);
process.exit(1);
});