Skip to content

Commit

Permalink
Merge pull request #1483 from contentstack/fix/DX-996
Browse files Browse the repository at this point in the history
DX - 996 - Added script to update the enviornments and version bump
  • Loading branch information
cs-raj authored Aug 6, 2024
2 parents 98ef8e4 + 630388e commit da72643
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 18 deletions.
19 changes: 4 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"environmentMapping": {
"source_environment_name": "destination_environment_name"
},
"source_stack_exported_data_path": "path",
"destination_stack_exported_data_path": "path"
}
171 changes: 171 additions & 0 deletions packages/contentstack-migration/examples/06-update-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
let fs = require('fs');
let { existsSync } = require('fs');
let path = require('path');
let crypto = require('crypto');
const { pathValidator, FsUtility, sanitizePath } = require('@contentstack/cli-utilities');

module.exports = async ({ migration, config }) => {
let updateEnvironments = {
title: 'Update Environments into the entries of the Stack',
successMessage: 'Environments of the Entries are Updated',
failMessage: 'Failed to execute successfully',
task: async (params) => {
let envMapper = {};
function checkWritePermissionToDirectory(directory) {
try {
fs.accessSync(directory, fs.constants.W_OK);
return true;
} catch (err) {
console.log(`Permission Denied! You do not have the necessary write access for this directory.`);
return false;
}
}

async function verifySourceAndDestinationStackData() {
try {
let source =
fs.existsSync(`${config.source_stack_exported_data_path}/entries`) &&
fs.existsSync(`${config.source_stack_exported_data_path}/environments`);
let destination =
fs.existsSync(`${config.destination_stack_exported_data_path}/entries`) &&
fs.existsSync(`${config.destination_stack_exported_data_path}/environments`);

if (!source || !destination) {
throw new Error(`The Source or Destination Directory Path are not valid`);
} else {
console.log(`You have permission to write to directory`);
}
} catch (err) {
console.log(
`The 'environments' or 'entries' folder doesn't exist either in source or destination stack. Please Check!`,
);
throw err;
}
}

async function createEnvironmentUidMapper() {
try {
let sourceEnv = Object.values(
JSON.parse(
fs.readFileSync(`${config.source_stack_exported_data_path}/environments/environments.json`, 'utf-8'),
),
);
let destinationEnv = Object.values(
JSON.parse(
fs.readFileSync(`${config.destination_stack_exported_data_path}/environments/environments.json`, 'utf-8'),
),
);

for (const [sourceName, destName] of Object.entries(config.environmentMapping)) {
const sourceUid = sourceEnv.find((env) => env.name === sourceName)?.uid;
const destUid = destinationEnv.find((env) => env.name === destName)?.uid;

if (sourceUid && destUid) {
envMapper[sourceUid] = destUid;
} else {
console.log(`No Mapper Provided for the environment ${sourceName} or ${destName}`);
}
}
} catch (err) {
throw err;
}
}

async function readAndUpdateEntries() {
let ctUidSource = Object.values(
JSON.parse(
fs.readFileSync(
path.join(`${sanitizePath(config.source_stack_exported_data_path)}/content_types/schema.json`),
'utf-8',
),
),
).map((ct) => ct.uid);

let sourceLocale = Object.values(
JSON.parse(
fs.readFileSync(
path.join(`${sanitizePath(config.source_stack_exported_data_path)}/locales/locales.json`),
'utf-8',
),
),
).map((locale) => locale.code);
let sourceMasterLocale = Object.values(
JSON.parse(
fs.readFileSync(
path.join(`${sanitizePath(config.source_stack_exported_data_path)}/locales/master-locale.json`),
'utf-8',
),
),
).map((locale) => locale.code);
let locales = [...sourceLocale, ...sourceMasterLocale];

for (let ct of ctUidSource) {
for (let locale of locales) {
let sourceEntries;
if (
existsSync(pathValidator(path.resolve(config.source_stack_exported_data_path, `entries/${ct}/${locale}`)))
) {
sourceEntries = fs.readFileSync(
pathValidator(
path.resolve(config.source_stack_exported_data_path, `entries/${ct}/${locale}/index.json`),
),
'utf-8',
);
if (sourceEntries) {
sourceEntries = await fs.readFileSync(
pathValidator(
path.resolve(
config.source_stack_exported_data_path,
`entries/${ct}/${locale}/${Object.values(JSON.parse(sourceEntries))[0]}`,
),
),
'utf8',
);
sourceEntries = JSON.parse(sourceEntries);

Object.keys(sourceEntries).forEach((entry) => {
sourceEntries[entry].publish_details = sourceEntries[entry].publish_details?.map((details) => {
details.environment = envMapper[details.environment];
return details;
});
let existingEntries = fs.readFileSync(
pathValidator(
path.resolve(config.source_stack_exported_data_path, `entries/${ct}/${locale}/index.json`),
),
{ encoding: 'utf8', flag: 'a+' },
);
fs.writeFileSync(
pathValidator(
path.resolve(
config.source_stack_exported_data_path,
`entries/${ct}/${locale}/${Object.values(JSON.parse(existingEntries))[0]}`,
),
),
JSON.stringify(sourceEntries, null, 2),
);
});
} else {
console.log(`No Entries Exist for Content-type ${ct} in loclae ${locale}`);
}
}
}
}
}

async function start() {
try {
if (await checkWritePermissionToDirectory(config.source_stack_exported_data_path)) {
await verifySourceAndDestinationStackData();
await createEnvironmentUidMapper();
await readAndUpdateEntries();
}
} catch (err) {
throw err;
}
}

await start();
},
};
migration.addTask(updateEnvironments);
};
2 changes: 1 addition & 1 deletion packages/contentstack-migration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-migration",
"version": "1.6.0",
"version": "1.6.1",
"author": "@contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@contentstack/cli-command": "~1.2.19",
"@contentstack/cli-config": "~1.6.5",
"@contentstack/cli-launch": "~1.1.0",
"@contentstack/cli-migration": "~1.6.0",
"@contentstack/cli-migration": "~1.6.1",
"@contentstack/cli-utilities": "~1.7.1",
"@contentstack/management": "~1.17.0",
"@oclif/core": "^3.26.5",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da72643

Please sign in to comment.