← back to Shopify Sample Shipping

theme-engine-undo.mjs

32 lines

#!/usr/bin/env node
// TK-11333 — undo the DEV-theme install: DELETE the "DW Sample-Shipping DEV" theme created by
// install-theme-engine.mjs. Reads verification/theme-engine-install.json for the id.
// DRY-RUN by default; --apply deletes. SAFETY: refuses to delete unless the recorded theme's
// name matches DEV_NAME and it is NOT the published/main theme.
import fs from 'node:fs';
import {query} from './query.mjs';
import {TOKEN, SHOP} from '../designerwallcoverings/scripts/lib/shopify.mjs';

const V = '2026-07';
const DEV_NAME = 'DW Sample-Shipping DEV';
const APPLY = process.argv.includes('--apply');
const REC = new URL('./verification/theme-engine-install.json', import.meta.url);
if (!fs.existsSync(REC)) { console.error('No install record at', REC.pathname, '— nothing to undo.'); process.exit(1); }
const rec = JSON.parse(fs.readFileSync(REC, 'utf8'));
const devId = rec.devThemeId, devGid = rec.devThemeGid;

// re-read live to confirm name + role before deleting
const r = await fetch(`https://${SHOP}/admin/api/${V}/themes/${devId}.json`, { headers: { 'X-Shopify-Access-Token': TOKEN }, signal: AbortSignal.timeout(30000) });
if (r.status === 404) { console.log('Theme', devId, 'already gone — nothing to undo.'); process.exit(0); }
if (!r.ok) throw Error('theme read ' + r.status);
const t = (await r.json()).theme;
console.log(`Recorded dev theme: ${devId} "${t.name}" [role:${t.role}]`);
if (t.role === 'main' || t.role === 'published') { console.error('SAFETY ABORT: recorded theme is PUBLISHED — refusing to delete.'); process.exit(1); }
if (t.name !== DEV_NAME) { console.error(`SAFETY ABORT: theme name "${t.name}" != "${DEV_NAME}" — refusing to delete (record may be stale).`); process.exit(1); }

if (!APPLY) { console.log(`\nDRY RUN — would DELETE unpublished theme ${devGid} "${t.name}". Add --apply to delete.`); process.exit(0); }
const del = (await query(`mutation($id:ID!){themeDelete(id:$id){deletedThemeId userErrors{field message}}}`, { id: devGid })).themeDelete;
if (del.userErrors.length) { console.error('themeDelete errors:', del.userErrors); process.exit(1); }
console.log('✅ Deleted dev theme', del.deletedThemeId);
fs.writeFileSync(REC, JSON.stringify({ ...rec, deletedAt: new Date().toISOString(), deleted: true }, null, 2) + '\n');