565d2ed4bc
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
105 lines
3.6 KiB
JavaScript
105 lines
3.6 KiB
JavaScript
// One-time: renames .00 Project Files → 00 Project Files for all existing project folders
|
|
// Run: node scripts/rename-project-files-folder.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 fbRename(fromPath, toPath) {
|
|
const qs = new URLSearchParams({ source: FB_SOURCE }).toString();
|
|
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
|
|
method: 'PATCH',
|
|
headers: { Authorization: `Bearer ${FB_TOKEN}`, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
action: 'rename',
|
|
items: [{ fromSource: FB_SOURCE, fromPath, toSource: FB_SOURCE, toPath }],
|
|
overwrite: false,
|
|
}),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`${res.status}: ${text.slice(0, 120)}`);
|
|
}
|
|
}
|
|
|
|
async function fbMkdir(path) {
|
|
const qs = new URLSearchParams({ source: FB_SOURCE, path, isDir: 'true' }).toString();
|
|
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${FB_TOKEN}` },
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
if (!text.includes('already') && !text.includes('exist')) throw new Error(`mkdir ${path}: ${res.status}`);
|
|
}
|
|
}
|
|
|
|
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(`Found ${projects.length} projects`);
|
|
|
|
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 oldPath = joinPath(projectDir, 'Project Files');
|
|
const newPath = joinPath(projectDir, '00 Project Files');
|
|
|
|
try {
|
|
await fbRename(oldPath, newPath);
|
|
console.log(` ✓ renamed: ${company} / ${project}`);
|
|
} catch (err) {
|
|
// If rename fails (source doesn't exist), ensure new folder exists
|
|
try {
|
|
await fbMkdir(newPath);
|
|
console.log(` + created: ${company} / ${project} (no dot folder found)`);
|
|
} catch (e2) {
|
|
console.log(` ~ exists: ${company} / ${project}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('\nDone.');
|
|
}
|
|
|
|
main().catch(err => { console.error(err); process.exit(1); });
|