565d2ed4bc
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
104 lines
3.6 KiB
JavaScript
104 lines
3.6 KiB
JavaScript
// One-time: removes leftover '.Project Files' and 'Project Files' folders
|
|
// that may still exist alongside the renamed '00 Project Files'
|
|
// Run: node scripts/cleanup-old-project-files-folders.mjs
|
|
import { readFileSync } from 'fs';
|
|
import { resolve, dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { createClient } from '@supabase/supabase-js';
|
|
|
|
const __dir = dirname(fileURLToPath(import.meta.url));
|
|
const envFile = resolve(__dir, '../.env.backfill');
|
|
|
|
const env = {};
|
|
readFileSync(envFile, 'utf8').split('\n').forEach(line => {
|
|
const m = line.match(/^([^#=]+)=(.*)$/);
|
|
if (m) env[m[1].trim()] = m[2].trim().replace(/^["']|["']$/g, '');
|
|
});
|
|
|
|
const SUPABASE_URL = env.VITE_SUPABASE_URL || env.SUPABASE_URL;
|
|
const SERVICE_ROLE_KEY = env.SUPABASE_SERVICE_ROLE_KEY;
|
|
const FB_URL = (env.FILEBROWSER_URL || '').replace(/\/+$/, '');
|
|
const FB_TOKEN = env.FILEBROWSER_TOKEN;
|
|
const CLIENT_ROOT = env.FILEBROWSER_CLIENT_ROOT || '/fourgebranding/Clients';
|
|
const FB_SOURCE = 'files';
|
|
|
|
if (!SUPABASE_URL || !SERVICE_ROLE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
|
|
if (!FB_URL || !FB_TOKEN) { console.error('Missing FileBrowser env'); process.exit(1); }
|
|
|
|
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, { auth: { persistSession: false } });
|
|
|
|
function safeName(v) {
|
|
return String(v || '').trim()
|
|
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
|
|
.replace(/\s+/g, ' ')
|
|
.replace(/^-+|-+$/g, '');
|
|
}
|
|
|
|
function joinPath(...parts) {
|
|
const clean = parts.join('/').split('/').filter(p => p && p !== '.');
|
|
return `/${clean.join('/')}`;
|
|
}
|
|
|
|
async function fbList(path) {
|
|
const qs = new URLSearchParams({ source: FB_SOURCE, path }).toString();
|
|
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
|
|
headers: { Authorization: `Bearer ${FB_TOKEN}` },
|
|
});
|
|
if (!res.ok) return null;
|
|
const data = await res.json().catch(() => null);
|
|
return data?.folders || [];
|
|
}
|
|
|
|
async function fbDelete(path) {
|
|
const qs = new URLSearchParams({ source: FB_SOURCE, path }).toString();
|
|
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
|
|
method: 'DELETE',
|
|
headers: { Authorization: `Bearer ${FB_TOKEN}` },
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`${res.status}: ${text.slice(0, 100)}`);
|
|
}
|
|
}
|
|
|
|
const OLD_NAMES = ['.Project Files', 'Project Files'];
|
|
|
|
async function main() {
|
|
const { data: projects, error } = await admin
|
|
.from('projects')
|
|
.select('id, name, company:companies(name)');
|
|
|
|
if (error) { console.error('Query failed:', error.message); process.exit(1); }
|
|
console.log(`Checking ${projects.length} projects...\n`);
|
|
|
|
let cleaned = 0;
|
|
|
|
for (const p of projects) {
|
|
const company = safeName(p.company?.name || '');
|
|
const project = safeName(p.name || '');
|
|
if (!company || !project) continue;
|
|
|
|
const projectDir = joinPath(CLIENT_ROOT, company, 'Projects', project);
|
|
const entries = await fbList(projectDir);
|
|
if (!entries) { console.log(` ? could not list: ${company} / ${project}`); continue; }
|
|
|
|
const names = entries.map(e => e.name);
|
|
for (const oldName of OLD_NAMES) {
|
|
if (names.includes(oldName)) {
|
|
const oldPath = joinPath(projectDir, oldName);
|
|
try {
|
|
await fbDelete(oldPath);
|
|
console.log(` ✓ deleted "${oldName}": ${company} / ${project}`);
|
|
cleaned++;
|
|
} catch (err) {
|
|
console.error(` ✗ failed to delete "${oldName}" in ${company} / ${project}: ${err.message}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`\nDone. Cleaned ${cleaned} old folder(s).`);
|
|
}
|
|
|
|
main().catch(err => { console.error(err); process.exit(1); });
|