Files
api/prisma/update-plugin-store-descriptions.ts
T

73 lines
2.1 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { existsSync, mkdirSync, copyFileSync } from 'fs';
import { join } from 'path';
import { PLUGIN_CATALOG_SEED } from '../src/plugins/plugin-catalog.seed';
const prisma = new PrismaClient();
async function main() {
console.log('Updating plugin store catalog descriptions and images in database...');
const sourceDir = join(__dirname, 'plugin-images');
const targetDir = join(process.cwd(), 'uploads', 'plugins');
if (!existsSync(targetDir)) {
mkdirSync(targetDir, { recursive: true });
console.log(`Created directory: ${targetDir}`);
}
let updatedCount = 0;
for (const seed of PLUGIN_CATALOG_SEED) {
// Check if there is a cache-busted _v2 version (e.g. enable_automation_v2.png)
const baseFilename = `${seed.settingKey}.png`;
const v2Filename = `${seed.settingKey}_v2.png`;
let activeFilename = baseFilename;
if (existsSync(join(sourceDir, v2Filename))) {
activeFilename = v2Filename;
copyFileSync(join(sourceDir, v2Filename), join(targetDir, v2Filename));
}
let hasImage = false;
const baseSrc = join(sourceDir, baseFilename);
if (existsSync(baseSrc)) {
copyFileSync(baseSrc, join(targetDir, baseFilename));
hasImage = true;
}
if (activeFilename === v2Filename) {
hasImage = true;
}
const imageUrl = `/uploads/plugins/${activeFilename}`;
const res = await prisma.pluginCatalogItem.updateMany({
where: { settingKey: seed.settingKey },
data: {
description: seed.description,
titleFa: seed.titleFa,
...(hasImage ? { images: [imageUrl] } : {}),
},
});
if (res.count > 0) {
console.log(`✓ Updated [${seed.settingKey}] - ${seed.titleFa} -> ${imageUrl}`);
updatedCount += res.count;
} else {
console.log(`- Item [${seed.settingKey}] not found in DB`);
}
}
console.log(`\nSuccessfully updated ${updatedCount} plugin store items with descriptions and images!`);
}
main()
.catch((e) => {
console.error('Error updating descriptions and images:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});