From bc513f288c0a1a45003e528b9e52c3f30fac97ac Mon Sep 17 00:00:00 2001 From: raj pandey Date: Mon, 29 Jul 2024 17:02:03 +0530 Subject: [PATCH] Added script to update the enviornments and version bump --- package-lock.json | 199 +++++++----------- .../06-update-environment-sample-config.json | 7 + .../examples/06-update-environment.js | 171 +++++++++++++++ packages/contentstack-migration/package.json | 2 +- packages/contentstack/package.json | 2 +- pnpm-lock.yaml | 2 +- 6 files changed, 257 insertions(+), 126 deletions(-) create mode 100644 packages/contentstack-migration/examples/06-update-environment-sample-config.json create mode 100644 packages/contentstack-migration/examples/06-update-environment.js diff --git a/package-lock.json b/package-lock.json index b4aa190a11..162e304dbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -87,9 +87,9 @@ } }, "node_modules/@babel/compat-data": { - "version": "7.24.9", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.24.9.tgz", - "integrity": "sha512-e701mcfApCJqMMueQI0Fb68Amflj83+dvAvHawoBpAz+GDjCIyGHzNwnefjsWJ3xiYAqqiQFoWbspGYBdb2/ng==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.25.0.tgz", + "integrity": "sha512-P4fwKI2mjEb3ZU5cnMJzvRsRKGBUcs8jvxIoRmr6ufAY9Xk2Bz7JubRTTivkw55c7WQJfTECeqYVa+HZ0FzREg==", "dev": true, "engines": { "node": ">=6.9.0" @@ -135,9 +135,9 @@ } }, "node_modules/@babel/eslint-parser": { - "version": "7.24.8", - "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.24.8.tgz", - "integrity": "sha512-nYAikI4XTGokU2QX7Jx+v4rxZKhKivaQaREZjuW3mrJrbdWJ5yUfohnoUULge+zEEaKjPYNxhoRgUKktjXtbwA==", + "version": "7.25.1", + "resolved": "https://registry.npmjs.org/@babel/eslint-parser/-/eslint-parser-7.25.1.tgz", + "integrity": "sha512-Y956ghgTT4j7rKesabkh5WeqgSFZVFwaPR0IWFm7KFHFmmJ4afbG49SmfW4S+GyRPx0Dy5jxEWA5t0rpxfElWg==", "dev": true, "dependencies": { "@nicolo-ribaudo/eslint-scope-5-internals": "5.1.1-v1", @@ -171,12 +171,12 @@ } }, "node_modules/@babel/generator": { - "version": "7.24.10", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.24.10.tgz", - "integrity": "sha512-o9HBZL1G2129luEUlG1hB4N/nlYNWHnpwlND9eOMclRqqu1YDy2sSYVCFUZwl8I1Gxh+QSRrP2vD7EpUmFVXxg==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.25.0.tgz", + "integrity": "sha512-3LEEcj3PVW8pW2R1SR1M89g/qrYk/m/mB/tLqn7dn4sbBUQyTqnlod+II2U4dqiGtUmkcnAmkMDralTFZttRiw==", "dev": true, "dependencies": { - "@babel/types": "^7.24.9", + "@babel/types": "^7.25.0", "@jridgewell/gen-mapping": "^0.3.5", "@jridgewell/trace-mapping": "^0.3.25", "jsesc": "^2.5.1" @@ -210,43 +210,6 @@ "semver": "bin/semver.js" } }, - "node_modules/@babel/helper-environment-visitor": { - "version": "7.24.7", - "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.24.7.tgz", - "integrity": "sha512-DoiN84+4Gnd0ncbBOM9AZENV4a5ZiL39HYMyZJGZ/AZEykHYdJw0wW3kdcsh9/Kn+BRXHLkkklZ51ecPKmI1CQ==", - "dev": true, - "dependencies": { - "@babel/types": "^7.24.7" - }, - "engines": { - "node": ">=6.9.0" - } - }, - "node_modules/@babel/helper-function-name": { - "version": "7.24.7", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.24.7.tgz", - "integrity": "sha512-FyoJTsj/PEUWu1/TYRiXTIHc8lbw+TDYkZuoE43opPS5TrI7MyONBE1oNvfguEXAD9yhQRrVBnXdXzSLQl9XnA==", - "dev": true, - "dependencies": { - "@babel/template": "^7.24.7", - "@babel/types": "^7.24.7" - }, - "engines": { - "node": ">=6.9.0" - } - }, - "node_modules/@babel/helper-hoist-variables": { - "version": "7.24.7", - "resolved": "https://registry.npmjs.org/@babel/helper-hoist-variables/-/helper-hoist-variables-7.24.7.tgz", - "integrity": "sha512-MJJwhkoGy5c4ehfoRyrJ/owKeMl19U54h27YYftT0o2teQ3FJ3nQUf/I3LlJsX4l3qlw7WRXUmiyajvHXoTubQ==", - "dev": true, - "dependencies": { - "@babel/types": "^7.24.7" - }, - "engines": { - "node": ">=6.9.0" - } - }, "node_modules/@babel/helper-module-imports": { "version": "7.24.7", "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.24.7.tgz", @@ -261,16 +224,15 @@ } }, "node_modules/@babel/helper-module-transforms": { - "version": "7.24.9", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.24.9.tgz", - "integrity": "sha512-oYbh+rtFKj/HwBQkFlUzvcybzklmVdVV3UU+mN7n2t/q3yGHbuVdNxyFvSBO1tfvjyArpHNcWMAzsSPdyI46hw==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.25.0.tgz", + "integrity": "sha512-bIkOa2ZJYn7FHnepzr5iX9Kmz8FjIz4UKzJ9zhX3dnYuVW0xul9RuR3skBfoLu+FPTQw90EHW9rJsSZhyLQ3fQ==", "dev": true, "dependencies": { - "@babel/helper-environment-visitor": "^7.24.7", "@babel/helper-module-imports": "^7.24.7", "@babel/helper-simple-access": "^7.24.7", - "@babel/helper-split-export-declaration": "^7.24.7", - "@babel/helper-validator-identifier": "^7.24.7" + "@babel/helper-validator-identifier": "^7.24.7", + "@babel/traverse": "^7.25.0" }, "engines": { "node": ">=6.9.0" @@ -301,18 +263,6 @@ "node": ">=6.9.0" } }, - "node_modules/@babel/helper-split-export-declaration": { - "version": "7.24.7", - "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.24.7.tgz", - "integrity": "sha512-oy5V7pD+UvfkEATUKvIjvIAH/xCzfsFVw7ygW2SI6NClZzquT+mwdTfgfdbUiceh6iQO0CHtCPsyze/MZ2YbAA==", - "dev": true, - "dependencies": { - "@babel/types": "^7.24.7" - }, - "engines": { - "node": ">=6.9.0" - } - }, "node_modules/@babel/helper-string-parser": { "version": "7.24.8", "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.24.8.tgz", @@ -341,13 +291,13 @@ } }, "node_modules/@babel/helpers": { - "version": "7.24.8", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.24.8.tgz", - "integrity": "sha512-gV2265Nkcz7weJJfvDoAEVzC1e2OTDpkGbEsebse8koXUJUXPsCMi7sRo/+SPMuMZ9MtUPnGwITTnQnU5YjyaQ==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.25.0.tgz", + "integrity": "sha512-MjgLZ42aCm0oGjJj8CtSM3DB8NOOf8h2l7DCTePJs29u+v7yO/RBX9nShlKMgFnRks/Q4tBAe7Hxnov9VkGwLw==", "dev": true, "dependencies": { - "@babel/template": "^7.24.7", - "@babel/types": "^7.24.8" + "@babel/template": "^7.25.0", + "@babel/types": "^7.25.0" }, "engines": { "node": ">=6.9.0" @@ -440,9 +390,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.24.8", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.24.8.tgz", - "integrity": "sha512-WzfbgXOkGzZiXXCqk43kKwZjzwx4oulxZi3nq2TYL9mOjQv6kYwul9mz6ID36njuL7Xkp6nJEfok848Zj10j/w==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.25.0.tgz", + "integrity": "sha512-CzdIU9jdP0dg7HdyB+bHvDJGagUv+qtzZt5rYCWwW6tITNqV9odjp6Qu41gkG0ca5UfdDUWrKkiAnHHdGRnOrA==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -629,9 +579,9 @@ } }, "node_modules/@babel/runtime": { - "version": "7.24.8", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.24.8.tgz", - "integrity": "sha512-5F7SDGs1T72ZczbRwbGO9lQi0NLjQxzl6i4lJxLxfW9U5UluCSyEJeniWvnhl3/euNiqQVbo8zruhsDfid0esA==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.25.0.tgz", + "integrity": "sha512-7dRy4DwXwtzBrPbZflqxnvfxLF8kdZXPkhymtDeFoFqE6ldzjQFgYTtYIFARcLEYDrqfBfYcZt1WqFxRoyC9Rw==", "dev": true, "dependencies": { "regenerator-runtime": "^0.14.0" @@ -641,33 +591,30 @@ } }, "node_modules/@babel/template": { - "version": "7.24.7", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.24.7.tgz", - "integrity": "sha512-jYqfPrU9JTF0PmPy1tLYHW4Mp4KlgxJD9l2nP9fD6yT/ICi554DmrWBAEYpIelzjHf1msDP3PxJIRt/nFNfBig==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.25.0.tgz", + "integrity": "sha512-aOOgh1/5XzKvg1jvVz7AVrx2piJ2XBi227DHmbY6y+bM9H2FlN+IfecYu4Xl0cNiiVejlsCri89LUsbj8vJD9Q==", "dev": true, "dependencies": { "@babel/code-frame": "^7.24.7", - "@babel/parser": "^7.24.7", - "@babel/types": "^7.24.7" + "@babel/parser": "^7.25.0", + "@babel/types": "^7.25.0" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/traverse": { - "version": "7.24.8", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.24.8.tgz", - "integrity": "sha512-t0P1xxAPzEDcEPmjprAQq19NWum4K0EQPjMwZQZbHt+GiZqvjCHjj755Weq1YRPVzBI+3zSfvScfpnuIecVFJQ==", + "version": "7.25.1", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.25.1.tgz", + "integrity": "sha512-LrHHoWq08ZpmmFqBAzN+hUdWwy5zt7FGa/hVwMcOqW6OVtwqaoD5utfuGYU87JYxdZgLUvktAsn37j/sYR9siA==", "dev": true, "dependencies": { "@babel/code-frame": "^7.24.7", - "@babel/generator": "^7.24.8", - "@babel/helper-environment-visitor": "^7.24.7", - "@babel/helper-function-name": "^7.24.7", - "@babel/helper-hoist-variables": "^7.24.7", - "@babel/helper-split-export-declaration": "^7.24.7", - "@babel/parser": "^7.24.8", - "@babel/types": "^7.24.8", + "@babel/generator": "^7.25.0", + "@babel/parser": "^7.25.0", + "@babel/template": "^7.25.0", + "@babel/types": "^7.25.0", "debug": "^4.3.1", "globals": "^11.1.0" }, @@ -685,9 +632,9 @@ } }, "node_modules/@babel/types": { - "version": "7.24.9", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.24.9.tgz", - "integrity": "sha512-xm8XrMKz0IlUdocVbYJe0Z9xEgidU7msskG8BbhnTPK/HZ2z/7FP7ykqPgrUH+C+r414mNfNWam1f2vqOjqjYQ==", + "version": "7.25.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.25.0.tgz", + "integrity": "sha512-LcnxQSsd9aXOIgmmSpvZ/1yo46ra2ESYyqLcryaBZOghxy5qqOBjvCWP5JfkI8yl9rlxRgdLTTMCQQRcN2hdCg==", "dev": true, "dependencies": { "@babel/helper-string-parser": "^7.24.8", @@ -2256,12 +2203,12 @@ } }, "node_modules/@oclif/plugin-plugins/node_modules/@oclif/core": { - "version": "4.0.14", - "resolved": "https://registry.npmjs.org/@oclif/core/-/core-4.0.14.tgz", - "integrity": "sha512-oXXlUuDw6gWjIBI+mR5i4/SJRv9fz8N/d1xr1yMhW1incx/u0oYO2CpUNKn0sGDyONMdyOIVPMqgsU+as7jdrQ==", + "version": "4.0.17", + "resolved": "https://registry.npmjs.org/@oclif/core/-/core-4.0.17.tgz", + "integrity": "sha512-zfdSRip9DVMOklMojWCLZEB4iOzy7LDTABCDzCXqmpZGS+o1e1xts4jGhnte3mi0WV0YthNfYqF16tqk6CWITA==", "dependencies": { "ansi-escapes": "^4.3.2", - "ansis": "^3.3.1", + "ansis": "^3.3.2", "clean-stack": "^3.0.1", "cli-spinners": "^2.9.2", "debug": "^4.3.5", @@ -3059,13 +3006,18 @@ } }, "node_modules/@slack/logger/node_modules/@types/node": { - "version": "20.14.12", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.12.tgz", - "integrity": "sha512-r7wNXakLeSsGT0H1AU863vS2wa5wBOK4bWMjZz2wj+8nBx+m5PeIn0k8AloSLpRuiwdRQZwarZqHE4FNArPuJQ==", + "version": "22.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.0.0.tgz", + "integrity": "sha512-VT7KSYudcPOzP5Q0wfbowyNLaVR8QWUdw+088uFWwfvpY6uCWaXpqV6ieLAu9WBcnTa7H4Z5RLK8I5t2FuOcqw==", "dependencies": { - "undici-types": "~5.26.4" + "undici-types": "~6.11.1" } }, + "node_modules/@slack/logger/node_modules/undici-types": { + "version": "6.11.1", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.11.1.tgz", + "integrity": "sha512-mIDEX2ek50x0OlRgxryxsenE5XaQD4on5U2inY7RApK3SOJpofyw7uW2AyfMKkhAxXIceo2DeWGVGwyvng1GNQ==" + }, "node_modules/@slack/oauth": { "version": "2.6.2", "resolved": "https://registry.npmjs.org/@slack/oauth/-/oauth-2.6.2.tgz", @@ -6470,9 +6422,9 @@ } }, "node_modules/debug": { - "version": "4.3.5", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz", - "integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==", + "version": "4.3.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.6.tgz", + "integrity": "sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==", "dependencies": { "ms": "2.1.2" }, @@ -6923,9 +6875,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.1.tgz", - "integrity": "sha512-FKbOCOQ5QRB3VlIbl1LZQefWIYwszlBloaXcY2rbfpu9ioJnNh3TK03YtIDKDo3WKBi8u+YV4+Fn2CkEozgf4w==", + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.2.tgz", + "integrity": "sha512-kc4r3U3V3WLaaZqThjYz/Y6z8tJe+7K0bbjUVo3i+LWIypVdMx5nXCkwRe6SWbY6ILqLdc1rKcKmr3HoH7wjSQ==", "dev": true }, "node_modules/elegant-spinner": { @@ -7604,9 +7556,9 @@ } }, "node_modules/eslint-config-oclif-typescript/node_modules/eslint-plugin-mocha": { - "version": "10.4.3", - "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-10.4.3.tgz", - "integrity": "sha512-emc4TVjq5Ht0/upR+psftuz6IBG5q279p+1dSRDeHf+NS9aaerBi3lXKo1SEzwC29hFIW21gO89CEWSvRsi8IQ==", + "version": "10.5.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-mocha/-/eslint-plugin-mocha-10.5.0.tgz", + "integrity": "sha512-F2ALmQVPT1GoP27O1JTZGrV9Pqg8k79OeIuvw63UxMtQKREZtmkK1NFgkZQ2TW7L2JSSFKHFPTtHu5z8R9QNRw==", "dev": true, "dependencies": { "eslint-utils": "^3.0.0", @@ -14323,9 +14275,9 @@ "dev": true }, "node_modules/minipass-json-stream": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/minipass-json-stream/-/minipass-json-stream-1.0.1.tgz", - "integrity": "sha512-ODqY18UZt/I8k+b7rl2AENgbWE8IDYam+undIJONvigAz8KR5GWblsFTEfQs0WODsjbSXWlm+JHEv8Gr6Tfdbg==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/minipass-json-stream/-/minipass-json-stream-1.0.2.tgz", + "integrity": "sha512-myxeeTm57lYs8pH2nxPzmEEg8DGIgW+9mv6D4JZD2pa81I/OBjeU7PtICXV6c9eRGTA5JMDsuIPUZRCyBMYNhg==", "dev": true, "dependencies": { "jsonparse": "^1.3.1", @@ -22881,9 +22833,9 @@ "dev": true }, "node_modules/uglify-js": { - "version": "3.19.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.0.tgz", - "integrity": "sha512-wNKHUY2hYYkf6oSFfhwwiHo4WCHzHmzcXsqXYTN9ja3iApYIFbb2U6ics9hBcYLHcYGQoAlwnZlTrf3oF+BL/Q==", + "version": "3.19.1", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.1.tgz", + "integrity": "sha512-y/2wiW+ceTYR2TSSptAhfnEtpLaQ4Ups5zrjB2d3kuVxHj16j/QJwPl5PvuGy9uARb39J0+iKxcRPvtpsx4A4A==", "dev": true, "optional": true, "bin": { @@ -22916,7 +22868,8 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true }, "node_modules/unique-filename": { "version": "1.1.1", @@ -24869,7 +24822,7 @@ "@contentstack/cli-command": "~1.2.19", "@contentstack/cli-config": "~1.6.5", "@contentstack/cli-launch": "~1.0.19", - "@contentstack/cli-migration": "~1.6.0", + "@contentstack/cli-migration": "~1.6.1", "@contentstack/cli-utilities": "~1.7.0", "@contentstack/management": "~1.15.3", "@oclif/core": "^3.26.5", @@ -24979,9 +24932,9 @@ "dev": true }, "packages/contentstack-audit/node_modules/@types/node": { - "version": "20.14.12", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.12.tgz", - "integrity": "sha512-r7wNXakLeSsGT0H1AU863vS2wa5wBOK4bWMjZz2wj+8nBx+m5PeIn0k8AloSLpRuiwdRQZwarZqHE4FNArPuJQ==", + "version": "20.14.13", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.13.tgz", + "integrity": "sha512-+bHoGiZb8UiQ0+WEtmph2IWQCjIqg8MDZMAV+ppRRhUZnquF5mQkP/9vpSwJClEiSM/C7fZZExPzfU0vJTyp8w==", "dev": true, "dependencies": { "undici-types": "~5.26.4" @@ -26993,7 +26946,7 @@ }, "packages/contentstack-migration": { "name": "@contentstack/cli-migration", - "version": "1.6.0", + "version": "1.6.1", "license": "MIT", "dependencies": { "@contentstack/cli-command": "~1.2.19", diff --git a/packages/contentstack-migration/examples/06-update-environment-sample-config.json b/packages/contentstack-migration/examples/06-update-environment-sample-config.json new file mode 100644 index 0000000000..4dc26dfe74 --- /dev/null +++ b/packages/contentstack-migration/examples/06-update-environment-sample-config.json @@ -0,0 +1,7 @@ +{ + "environmentMapping": { + "source_environment_name": "destination_environment_name" + }, + "source_stack_exported_data_path": "path", + "destination_stack_exported_data_path": "path" +} diff --git a/packages/contentstack-migration/examples/06-update-environment.js b/packages/contentstack-migration/examples/06-update-environment.js new file mode 100644 index 0000000000..59d364d0fb --- /dev/null +++ b/packages/contentstack-migration/examples/06-update-environment.js @@ -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); +}; diff --git a/packages/contentstack-migration/package.json b/packages/contentstack-migration/package.json index 4d688a0623..d7cbfa20d9 100644 --- a/packages/contentstack-migration/package.json +++ b/packages/contentstack-migration/package.json @@ -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": { diff --git a/packages/contentstack/package.json b/packages/contentstack/package.json index ad4d614a13..96b5ef544c 100755 --- a/packages/contentstack/package.json +++ b/packages/contentstack/package.json @@ -36,7 +36,7 @@ "@contentstack/cli-command": "~1.2.19", "@contentstack/cli-config": "~1.6.5", "@contentstack/cli-launch": "~1.0.19", - "@contentstack/cli-migration": "~1.6.0", + "@contentstack/cli-migration": "~1.6.1", "@contentstack/cli-utilities": "~1.7.0", "@contentstack/management": "~1.15.3", "@oclif/core": "^3.26.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 15f8d5a8d2..829accda2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -24,7 +24,7 @@ importers: '@contentstack/cli-command': ~1.2.19 '@contentstack/cli-config': ~1.6.5 '@contentstack/cli-launch': ~1.0.19 - '@contentstack/cli-migration': ~1.6.0 + '@contentstack/cli-migration': ~1.6.1 '@contentstack/cli-utilities': ~1.7.0 '@contentstack/management': ~1.15.3 '@oclif/core': ^3.26.5