Skip to content

Commit

Permalink
Merge pull request #36 from contentstack/issue-CMA-1192
Browse files Browse the repository at this point in the history
Issue cma 1192
  • Loading branch information
shafeeqd959 authored Feb 3, 2022
2 parents f0e7fb0 + 37d6832 commit a77fc5b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
47 changes: 32 additions & 15 deletions lib/import/content_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
30 changes: 29 additions & 1 deletion lib/import/global_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit a77fc5b

Please sign in to comment.