89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
/**
|
|
* Patch `product.abiirPdp` for every Shopify-synced product in Sanity using the same
|
|
* storefront defaults as Hydrogen (`buildAbiirPdpSanityPayloadForHandle` in `app/lib/abiirPdpDefaults.ts`).
|
|
*
|
|
* Default: only products missing `subtitle` OR `shortDescription` (safe re-run).
|
|
* Pass `--force` to overwrite existing `abiirPdp` on all products.
|
|
*
|
|
* From Hydrogen/web (Node 22+ for TypeScript import):
|
|
* node --env-file=.env --experimental-strip-types ./scripts/sanity-seed-pdp-all.mjs
|
|
* node --env-file=.env --experimental-strip-types ./scripts/sanity-seed-pdp-all.mjs -- --force
|
|
*
|
|
* npm: npm run sanity:rollout:seed-pdp-all
|
|
*/
|
|
|
|
import {createClient} from '@sanity/client';
|
|
import {buildAbiirPdpSanityPayloadForHandle} from '../app/lib/abiirPdpDefaults.ts';
|
|
|
|
function req(name) {
|
|
const v = process.env[name];
|
|
if (!v || !String(v).trim()) {
|
|
console.error(`Missing required env: ${name}`);
|
|
process.exit(1);
|
|
}
|
|
return String(v).trim();
|
|
}
|
|
|
|
const projectId = req('SANITY_PROJECT_ID');
|
|
const dataset = req('SANITY_DATASET');
|
|
const token = req('SANITY_API_WRITE_TOKEN');
|
|
const apiVersion = process.env.SANITY_API_VERSION?.trim() || '2026-04-16';
|
|
|
|
const client = createClient({
|
|
projectId,
|
|
dataset,
|
|
apiVersion,
|
|
token,
|
|
useCdn: false,
|
|
});
|
|
|
|
const argv = process.argv.slice(2).filter((a) => a !== '--');
|
|
const force = argv.includes('--force');
|
|
|
|
async function main() {
|
|
const rows = await client.fetch(
|
|
`*[_type == "product" && defined(store.slug.current)]{
|
|
"id": _id,
|
|
"handle": store.slug.current,
|
|
"subtitle": abiirPdp.subtitle,
|
|
"shortDescription": abiirPdp.shortDescription
|
|
}`,
|
|
);
|
|
|
|
if (!Array.isArray(rows) || rows.length === 0) {
|
|
console.error('No product documents with store.slug.current. Sync products from Shopify first.');
|
|
process.exit(1);
|
|
}
|
|
|
|
let patched = 0;
|
|
let skipped = 0;
|
|
|
|
for (const row of rows) {
|
|
const handle = String(row.handle ?? '').trim();
|
|
if (!handle || !row.id) continue;
|
|
|
|
const hasEditorial =
|
|
Boolean(String(row.subtitle ?? '').trim()) &&
|
|
Boolean(String(row.shortDescription ?? '').trim());
|
|
|
|
if (!force && hasEditorial) {
|
|
skipped += 1;
|
|
continue;
|
|
}
|
|
|
|
const abiirPdp = buildAbiirPdpSanityPayloadForHandle(handle);
|
|
await client.patch(row.id).set({abiirPdp}).commit({autoGenerateArrayKeys: true});
|
|
patched += 1;
|
|
console.log(`${force ? 'FORCE ' : ''}OK "${handle}" → ${row.id}`);
|
|
}
|
|
|
|
console.log(
|
|
`\nDone. Patched ${patched}, skipped ${skipped} (already had subtitle+shortDescription). ${force ? '(--force)' : 'Use --force to replace all.'}`,
|
|
);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|