From f5b9c9759f8d05cf0b688de82f64e754f0faa41e Mon Sep 17 00:00:00 2001 From: Padmaja Date: Wed, 29 Nov 2023 16:51:42 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Script=20for=20replacing=20secre?= =?UTF-8?q?t=20site=20reference=20on=20assets=20(#1968)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scripts/issue-1626/fix-asset-urls.mjs | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 sanityv3/scripts/issue-1626/fix-asset-urls.mjs diff --git a/sanityv3/scripts/issue-1626/fix-asset-urls.mjs b/sanityv3/scripts/issue-1626/fix-asset-urls.mjs new file mode 100644 index 000000000..943cf89e1 --- /dev/null +++ b/sanityv3/scripts/issue-1626/fix-asset-urls.mjs @@ -0,0 +1,110 @@ +//import { type SourceOptions } from 'sanity' +//import { getCliClient } from 'sanity/cli' +//import { sanityClient } from '../../sanity.client' +import { createClient } from '@sanity/client' + +const dataset = 'global-development' +const client = createClient({ + apiVersion: '2023-08-29', + projectId: process.env.SANITY_STUDIO_API_PROJECT_ID || 'h61q9gi9', + token: process.env.SANITY_STUDIO_MUTATION_TOKEN, + dataset: dataset, +}) + +const config = client.config() +/* +const client = getCliClient() +const config = client.config() + +type ProjectDetails = Pick + +interface AssetDocument { + _id: string + url: string + path: string +}*/ + +/** + * Ensures the required configuration properties are set. + */ +function isConfigValid(config) { + return typeof config.projectId !== 'undefined' && typeof config.dataset !== 'undefined' +} + +/** + * Creates a function that transforms a path string (segments seperated by a `/` + * character), replacing the `${projectId}/${dataset}` segments at the given + * position with the given project details. + */ +function createFixFunction(position) { + return (input, { projectId, dataset }) => { + const input2 = input.split('/').slice() // create a copy of the array + input2.splice(position, 2, projectId, dataset) + return input2.join('/') + //return input.split('/').toSpliced(position, 2, projectId, dataset).join('/') + } +} + +/** + * Replaces the `${projectId}/${dataset}` segments in the given path. + */ +const fixPath = createFixFunction(1) + +/** + * Replaces the `${projectId}/${dataset}` segments in the given URL. + */ +const fixUrl = createFixFunction(4) + +/** + * Queries Sanity dataset for assets that have a `url` or `path` field that + * doesn't match the provided project details. + */ +function fetchBrokenAssets({ projectId, dataset }) { + return client.fetch( + ` + *[ + _type in ["sanity.imageAsset", "sanity.fileAsset"] + && ( + count(string::split(url, $expectedPath)) != 2 + || count(string::split(path, $expectedPath)) != 2 + ) + ] { + _id, + url, + path + }`, + { + expectedPath: `/${projectId}/${dataset}/`, + }, + ) +} + +/** + * Queries Sanity dataset for assets that have a `url` or `path` field that + * doesn't match the provided project details, and patches them with fields + * that do match. + */ +async function fixAssets(projectDetails) { + const assets = await fetchBrokenAssets(projectDetails) + + const transaction = assets.reduce((transaction, asset) => { + return transaction.patch(asset._id, (patch) => + patch.set({ + url: fixUrl(asset.url, projectDetails), + path: fixPath(asset.path, projectDetails), + }), + ) + }, client.transaction()) + + console.log(JSON.stringify(transaction)) + const result = await transaction.commit() + + console.log(`Fixed ${result.documentIds.length} documents.`) + console.log(result.documentIds) +} + +if (!isConfigValid(config)) { + throw new Error('Please specify your project details.') +} + +fixAssets(config)