diff --git a/contentful/content-management/README.md b/contentful/content-management/README.md index ffc945b6e..d77413155 100644 --- a/contentful/content-management/README.md +++ b/contentful/content-management/README.md @@ -25,6 +25,9 @@ ____ - importing content can be achieved using the `import-content` script, which will import content from a json file into a specified environment. The json file should be in the format of the `export-processor` export outputs - `import-content` uses the `contentful-import` npm package to do the importing +- By default a content import will add to an environment and not remove unrelated data + - It will overwrite entries with the same id as one in the import file, and leave other entries alone + - To delete all existing data prior to import so that the environment exactly matches the import file, set `DELETE_ALL_DATA` to true in `.env` ### Usage @@ -39,11 +42,12 @@ Required Environment variables `MANAGEMENT_TOKEN` `ENVIRONMENT` (environmentId default is 'master') `SKIP_CONTENT_MODEL` (optional, default is false) +`DELETE_ALL_DATA` (optional, default is false) ## Deleting all content You may wish to remove all content from an environment, for example, before importing from a backup. -This can be done with the `delete-all-content` script, which will clear down the entire environment. +This is an environment variable option in the import process, or it can be run standalone with the `delete-all-content` script, which will clear down the entire environment. ### Setup diff --git a/contentful/content-management/delete-all-content.js b/contentful/content-management/delete-all-content.js index 1d5f76ea9..3facfa79b 100644 --- a/contentful/content-management/delete-all-content.js +++ b/contentful/content-management/delete-all-content.js @@ -5,8 +5,6 @@ const deleteContentfulContent = require("./helpers/delete-all-content-and-conten async function deleteAllContentfulData() { const client = await getClient(); - await validateEnvironment(client); - const contentTypesToDelete = process.env.CONTENT_TYPES_TO_DELETE?.split(","); console.log(`Deleting ${process.env.CONTENT_TYPES_TO_DELETE ?? 'all data'} from the following environment: ${process.env.ENVIRONMENT}`) diff --git a/contentful/content-management/helpers/validate-environment.js b/contentful/content-management/helpers/validate-environment.js index 0a2a576b6..bf84ed471 100644 --- a/contentful/content-management/helpers/validate-environment.js +++ b/contentful/content-management/helpers/validate-environment.js @@ -1,20 +1,3 @@ -const contentful = require("contentful-management"); - -function createClient() { - return contentful.createClient( - { - accessToken: process.env.MANAGEMENT_TOKEN, - }, - { - type: "plain", - defaults: { - spaceId: process.env.SPACE_ID, - environmentId: process.env.ENVIRONMENT, - }, - } - ); -} - /** * Verifies that the environment specified in .env is a valid contentful environment * This is important because if it isn't, the management api fails silently @@ -22,10 +5,7 @@ function createClient() { * * @param {ClientAPI | null} client */ -module.exports = async function validateEnvironment(client = null) { - if (!client) { - client = createClient(); - } +module.exports = async function validateEnvironment(client) { const environments = await client.environment.getMany({ spaceId: process.env.SPACE_ID, }); diff --git a/contentful/content-management/import-content.js b/contentful/content-management/import-content.js index afa0c78b4..aeebecc42 100644 --- a/contentful/content-management/import-content.js +++ b/contentful/content-management/import-content.js @@ -2,7 +2,8 @@ require('dotenv').config(); const contentfulImport = require('contentful-import'); const fs = require('fs'); -const validateEnvironment = require("./helpers/validate-environment"); +const getClient = require("./helpers/get-client"); +const deleteContentfulContent = require("./helpers/delete-all-content-and-content-types"); async function importContentfulData() { const options = { @@ -12,14 +13,18 @@ async function importContentfulData() { environmentId: process.env.ENVIRONMENT, skipContentModel: process.env.SKIP_CONTENT_MODEL === 'true' ? true : false }; - - await validateEnvironment(); + const client = await getClient(); if (!fs.existsSync(options.contentFile)) { throw new Error(`File not found: ${options.contentFile}`); } - console.log("Attempting import with the following options:", options) + if (process.env.DELETE_ALL_DATA == 'true') { + console.log(`Deleting all existing data from ${options.environmentId}`); + await deleteContentfulContent({ client: client }); + } + + console.log("Starting import with the following options:", options) try { await contentfulImport(options);