From cf4f122356df39f4fe2684be04bb7b65f310c604 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 1 Feb 2022 11:16:50 +0530 Subject: [PATCH 1/3] refactor: added updateGlobalField and wrote pending global fields to file --- lib/import/global_fields.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/import/global_fields.js b/lib/import/global_fields.js index 1c33619..e219328 100644 --- a/lib/import/global_fields.js +++ b/lib/import/global_fields.js @@ -25,6 +25,7 @@ var globalfieldsFolderPath = path.resolve(config.data, globalfieldsConfig.dirNam var globalfieldsMapperPath = path.resolve(config.data, 'mapper', 'global_fields'); var globalfieldsUidMapperPath = path.resolve(config.data, 'mapper', 'global_fields', 'uid-mapping.json'); var globalfieldsSuccessPath = path.resolve(config.data, 'mapper', 'global_fields', 'success.json'); +var globalFieldsPending = path.resolve(config.data, 'mapper', 'global_fields', 'pending_global_fields.js') var globalfieldsFailsPath = path.resolve(config.data, 'mapper', 'global_fields', 'fails.json'); if (!fs.existsSync(globalfieldsMapperPath)) { @@ -55,6 +56,7 @@ importGlobalFields.prototype = { return new Promise(function (resolve, reject) { if(self.globalfields == undefined) { log.success(chalk.blue('No globalfields Found')); + helper.writeFile(globalFieldsPending, _globalField_pending) return resolve(); } var snipUids = Object.keys(self.globalfields); @@ -89,10 +91,12 @@ importGlobalFields.prototype = { helper.writeFile(globalfieldsUidMapperPath, self.snipUidMapper); log.success(chalk.green(global_field_uid +' globalfield created successfully')); return; - }).catch(function (error) { + }).catch(async function (error) { if(error.errors.title || error.errors.uid) { // eslint-disable-next-line no-undef log.error(chalk.blue(snip.uid + ' globalfield already exists')); + log.error(chalk.blue('trying to update ' + snip.uid)) + await self.updateGlobalfield({...snip}) return self.setupMapperForExistingGlobalFields({ ...snip }).then(() => { log.success(chalk.blue(`Existing globalfield ${snip.title} mapped successfully`)); }).catch((error) => { @@ -116,6 +120,7 @@ importGlobalFields.prototype = { }).then(function () { // globalfields have imported successfully helper.writeFile(globalfieldsSuccessPath, self.success); + helper.writeFile(globalFieldsPending, _globalField_pending) log.success(chalk.green('globalfields have been imported successfully!')); return resolve(); }).catch(function (error) { @@ -147,6 +152,29 @@ importGlobalFields.prototype = { }) }) }, + updateGlobalfield: function (globalField) { + return new Promise((resolve) => { + let self = this + let requestOption = { + uri: config.host + config.apis.globalfields + globalField.uid, + headers: config.headers, + method: 'PUT', + body: {"global_field": globalField} + } + return request(requestOption).then((response) => { + if (!response.body.notice) { + log.error(chalk.red(`failed to updated ${globalField.uid}`)) + } else { + log.success(chalk.green(`updated ${globalField.uid} successfully`)) + } + resolve() + }).catch((error) => { + log.debug(error) + log.error(chalk.red(`failed to updated ${globalField.uid}`)) + resolve() + }) + }) + } }; module.exports = new importGlobalFields(); From 325082df596bb2da84a4d296251358bb1e28a47c Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 1 Feb 2022 11:21:42 +0530 Subject: [PATCH 2/3] refactor: updated pending global field path, and added uidToTitleMap --- lib/import/content_types.js | 47 +++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/import/content_types.js b/lib/import/content_types.js index 4e1956e..bcc649a 100755 --- a/lib/import/content_types.js +++ b/lib/import/content_types.js @@ -5,6 +5,7 @@ * MIT Licensed */ var mkdirp = require('mkdirp'); +let fsPromises = require('fs').promises var fs = require('fs'); var path = require('path'); var _ = require('lodash'); @@ -19,6 +20,7 @@ var util = require('../util/'); var log = require('../util/log'); var supress = require('../util/extensionsUidReplace'); +var globalFieldPendingPath var config = util.getConfig(); var reqConcurrency = config.concurrency; var contentTypeConfig = config.modules.content_types; @@ -54,6 +56,7 @@ function importContentTypes() { this.createdContentTypeUids = helper.readFile(path.join(mapperFolderPath, 'success.json')) || []; } this.contentTypeUids = _.difference(this.contentTypeUids, this.createdContentTypeUids); + this.uidToTitleMap = this.mapUidToTitle(this.contentTypes) // remove contet types, already created _.remove(this.contentTypes, function(contentType) { return self.contentTypeUids.indexOf(contentType.uid) === -1; @@ -70,10 +73,10 @@ function importContentTypes() { importContentTypes.prototype = { start: function() { var self = this; - + globalFieldPendingPath = helper.readFile(path.join(config.data, 'mapper', 'global_fields', 'pending_global_fields.js')) return new Promise(function(resolve, reject) { return Promise.map(self.contentTypeUids, function(contentTypeUid) { - return self.seedContentTypes(contentTypeUid).then(function() { + return self.seedContentTypes(contentTypeUid, self.uidToTitleMap[contentTypeUid]).then(function() { return; }).catch(function(error) { reject(error); @@ -91,29 +94,36 @@ importContentTypes.prototype = { }).catch(function () { return; }); - }).then(function() { + }).then(async function() { // eslint-disable-next-line quotes if(field_rules_ct.length > 0) { - fs.writeFile(contentTypesFolderPath + '/field_rules_uid.json', JSON.stringify(field_rules_ct), function(err) { - if (err) throw err; - }); + // fs.writeFile(contentTypesFolderPath + '/field_rules_uid.json', JSON.stringify(field_rules_ct), function(err) { + // if (err) throw err; + // }); + await fsPromises.writeFile(contentTypesFolderPath + '/field_rules_uid.json', JSON.stringify(field_rules_ct)) } - log.success(chalk.green('Content types have been imported successfully!')); // content types have been successfully imported - return self.updateGlobalfields().then(function() { - return resolve(); - }).catch(reject); - + if (globalFieldPendingPath && globalFieldPendingPath.length !== 0) { + return self.updateGlobalfields().then(function() { + log.success(chalk.green('Content types have been imported successfully!')); + return resolve(); + }).catch(error => { + log.error(chalk.red('Error in GlobalFields')); + return reject() + }); + } + log.success(chalk.green('Content types have been imported successfully!')) + return resolve() }).catch(reject); }).catch(reject); }); }, - seedContentTypes: function(uid) { + seedContentTypes: function(uid, title) { var self = this; return new Promise(function(resolve, reject) { var body = _.cloneDeep(self.schemaTemplate); body.content_type.uid = uid; - body.content_type.title = uid; + body.content_type.title = title; var requestObject = _.cloneDeep(self.requestOptions); requestObject.json = body; return request(requestObject) @@ -158,7 +168,7 @@ importContentTypes.prototype = { var self = this; return new Promise(function(resolve, reject) { // eslint-disable-next-line no-undef - return Promise.map(_globalField_pending, function (globalfield) { + return Promise.map(globalFieldPendingPath, function (globalfield) { var lenGlobalField = (self.globalfields).length; for(var i=0; i < lenGlobalField; i++) { if(self.globalfields[i].uid == globalfield) { @@ -194,7 +204,14 @@ importContentTypes.prototype = { return reject(error); }); }); - } + }, + mapUidToTitle: function (contentTypes) { + let result = {} + contentTypes.forEach(ct => { + result[ct.uid] = ct.title + }) + return result + }, }; module.exports = new importContentTypes(); From 37d68326e51912c3114a76ee01bee1c8f84fc666 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 3 Feb 2022 12:52:19 +0530 Subject: [PATCH 3/3] refactor: version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8c2df8f..917bd34 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "contentstack-import", - "version": "1.7.6", + "version": "1.7.7", "description": "Import data into Contentstack", "homepage": "https://www.contentstack.com/docs/tools-and-frameworks/content-migration/within-contentstack", "author": {