Last active
August 3, 2023 11:34
-
-
Save horttanainen/2355aff96a0723dda4dcdb7045016a18 to your computer and use it in GitHub Desktop.
Migrate Contentful entry and it's child entries from environment to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createClient } from 'contentful-management' | |
import contentfulExport, { Options } from 'contentful-export' | |
//@ts-ignore | |
import contentfulImport from 'contentful-import' | |
// This code was based on Hany Darwish's solution: https://github.com/contentful/contentful-cli/issues/614#issuecomment-955794191 | |
// MODIFY THESE VALUES BEFORE RUNNING | |
// | |
// Run like this ts-node --project migrate-entry-tsconfig.json migrate-contentful-entry.ts | |
const SPACE_ID = 'SPACE ID HERE' | |
const MANAGEMENT_TOKEN = 'TOKEN HERE' | |
const SOURCE_ENV_ID = 'dev' | |
const TARGET_ENV_ID = 'test' | |
const ENTRY_ID = 'ENTRY TO MIGRATE' | |
const CONTENT_FILE_URL = 'export.json' | |
const maxDepth = 10 | |
const options: Options = { | |
spaceId: SPACE_ID, | |
environmentId: SOURCE_ENV_ID, | |
managementToken: MANAGEMENT_TOKEN, | |
contentFile: CONTENT_FILE_URL, | |
contentOnly: true, | |
downloadAssets: false, | |
skipContent: false, | |
skipRoles: true, | |
skipWebhooks: true, | |
} | |
async function importEntries() { | |
const importOptions = { | |
contentFile: CONTENT_FILE_URL, | |
spaceId: SPACE_ID, | |
managementToken: MANAGEMENT_TOKEN, | |
environmentId: TARGET_ENV_ID, | |
} | |
await contentfulImport(importOptions) | |
} | |
async function main() { | |
const client = createClient({ | |
accessToken: options.managementToken, | |
}) | |
const space = await client.getSpace(options.spaceId) | |
const environment = await space.getEnvironment(SOURCE_ENV_ID) | |
const entryReferences = await environment.getEntryReferences(ENTRY_ID, { | |
include: maxDepth, | |
}) | |
const entries = | |
entryReferences.includes?.Entry?.map((e) => e.sys.id).join(',') || '' | |
const assets = | |
entryReferences.includes?.Asset?.map((e) => e.sys.id).join(',') || '' | |
options.queryEntries = [`sys.id[in]=${ENTRY_ID},${entries}`] | |
options.queryAssets = [`sys.id[in]=${assets}`] | |
await contentfulExport(options) | |
await importEntries() | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((err) => { | |
console.log(err) | |
process.exit(1) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"target": "es5", | |
"allowJs": true, | |
"skipLibCheck": true, | |
"strict": true, | |
"noEmit": true, | |
"esModuleInterop": true, | |
"noUnusedParameters": true, | |
"noUnusedLocals": true | |
}, | |
"include": ["migrate-contentful-entry.ts"], | |
"exclude": ["node_modules"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment