Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global field #26

Merged
merged 13 commits into from
Apr 17, 2020
7 changes: 3 additions & 4 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ module.exports = {
stacks: '/stacks/',
labels: '/labels/'
},
preserveStackVersion: false
// if exisstingContentDir exists, no backup folder will be created
// rather, its value(path) will be used instead
// useBackedupDir: './_backup_413',
preserveStackVersion: false,
concurrency: 3
// , useBackedupDir: './_backup_409'
// is the no. of files to be copied/backed up concurrently
// backupConcurrency: 10,
};
53 changes: 51 additions & 2 deletions lib/import/content_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ var log = require('../util/log');
var supress = require('../util/extensionsUidReplace');
var config = util.getConfig();

var concurrencyNo = config.concurrency;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change var name to concurrency

Copy link
Contributor Author

@rohitmishra209 rohitmishra209 Apr 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to reqConcurrency

var contentTypeConfig = config.modules.content_types;
var globalFieldConfig = config.modules.globalfields;
var globalfieldsFolderPath = path.resolve(config.data, globalFieldConfig.dirName);
var contentTypesFolderPath = path.resolve(config.data, contentTypeConfig.dirName);
var mapperFolderPath = path.join(config.data, 'mapper', 'content_types');
var globalFieldMapperFolderpath = helper.readFile(path.join(config.data, 'mapper', 'global_fields', 'success.json'));
var globalFieldUpdateFile = path.join(config.data, 'mapper', 'global_fields', 'success.json');
var skipFiles = ['__master.json', '__priority.json', 'schema.json'];
var fileNames = fs.readdirSync(path.join(contentTypesFolderPath));
var field_rules_ct = [];
Expand All @@ -28,6 +33,7 @@ var field_rules_ct = [];
function importContentTypes() {
var self = this;
this.contentTypes = [];
this.globalfields = helper.readFile(path.resolve(globalfieldsFolderPath, globalFieldConfig.fileName));
for (var index in fileNames) {
if (skipFiles.indexOf(fileNames[index]) === -1) {
this.contentTypes.push(helper.readFile(path.join(contentTypesFolderPath, fileNames[index])));
Expand Down Expand Up @@ -69,7 +75,7 @@ importContentTypes.prototype = {
}).catch(reject);
}, {
// seed 3 content types at a time
concurrency: 3
concurrency: concurrencyNo
}).then(function() {
// content type seeidng completed
self.requestOptions.method = 'PUT';
Expand All @@ -87,7 +93,10 @@ importContentTypes.prototype = {
}
log.success('Content types have been imported successfully!');
// content types have been successfully imported
return resolve();
return self.updateGlobalfields().then(function() {
return resolve();
});

}).catch(reject);
}).catch(reject);
});
Expand Down Expand Up @@ -132,6 +141,46 @@ importContentTypes.prototype = {
return reject(error);
});
});
},
updateGlobalfields: function() {
var self = this;
return new Promise(function(resolve, reject) {
return Promise.map(_globalField_pending, function (globalfield) {
var lenGlobalField = (self.globalfields).length;
for(var i=0; i < lenGlobalField; i++) {
if(self.globalfields[i].uid == globalfield) {
self.requestGlobalfieldOptions = {
uri: config.host + config.apis.globalfields+globalfield,
headers: config.headers,
method: 'PUT',
json: {
global_field: self.globalfields[i]
}
};
return request(self.requestGlobalfieldOptions).then(function (response) {
var updateObjpos = _.findIndex(globalFieldMapperFolderpath, function(successobj) {
var global_field_uid = response.body.global_field.uid;
return global_field_uid == successobj;
});
globalFieldMapperFolderpath.splice(updateObjpos, 1, self.globalfields[i]);
helper.writeFile(globalFieldUpdateFile, globalFieldMapperFolderpath);
return;
}).catch(function (error) {
// eslint-disable-next-line no-console
log.error('Globalfield fail to import '+ JSON.stringify(error.errors));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

failed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return;
});
}
}
}, {
concurrency: concurrencyNo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only concurrency works

}).then(function() {
return resolve();
}).catch(function(error) {
// failed to update modified schemas back to their original form
return reject(error);
});
});
}
};

Expand Down
27 changes: 15 additions & 12 deletions lib/import/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var extension_supress = require('../util/extensionsUidReplace');
var util = require('../util/');
var config = util.getConfig();

// var config = getConfig();
var concurrencyNo = config.concurrency;
var eConfig = config.modules.entries;
var ePath = path.resolve(config.data, eConfig.dirName);
var ctPath = path.resolve(config.data, config.modules.content_types.dirName);
Expand Down Expand Up @@ -135,7 +135,7 @@ importEntries.prototype = {
return;
}
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
return self.unSupressFields().then(function() {
return self.removeBuggedEntries().then(async function() {
Expand Down Expand Up @@ -190,6 +190,9 @@ importEntries.prototype = {
return resolve();
}
for (var eUid in entries) {
// console.log("self contentstypess>>>", self.ctSchemas[ctUid])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comments if not required

// console.log("self contentstypess>>>", entries[eUid])

// will replace all old asset uid/urls with new ones
entries[eUid] = lookupReplaceAssets({
content_type: self.ctSchemas[ctUid],
Expand Down Expand Up @@ -288,7 +291,7 @@ importEntries.prototype = {
});
// create/update 5 entries at a time
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
helper.writeFile(successEntryLogPath, self.success[ctUid]);
helper.writeFile(failedEntryLogPath, self.fails[ctUid]);
Expand All @@ -299,7 +302,7 @@ importEntries.prototype = {
});
// process one batch at a time
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
log.success('Entries created successfully in ' + ctUid + ' content type in ' + lang +
' locale!');
Expand All @@ -312,7 +315,7 @@ importEntries.prototype = {
eFilePath + '\' does not exist!');
}
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
log.success('Entries created successfully in \'' + lang + '\' language');
return resolve();
Expand All @@ -331,7 +334,7 @@ importEntries.prototype = {
return Promise.map(self.createdEntriesWOUid, function(entry) {
return self.fetchEntry(entry);
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
helper.writeFile(failedWOPath, self.failedWO);
log.success('Mapped entries without mapped uid successfully!');
Expand Down Expand Up @@ -441,7 +444,7 @@ importEntries.prototype = {
return;
});
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
// batch completed successfully
helper.writeFile(path.join(eFolderPath, 'success.json'), entries);
Expand All @@ -455,7 +458,7 @@ importEntries.prototype = {
throw error;
});
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
// finished updating entries with references
log.success('Imported entries of Content Type: \'' + ctUid + '\' in language: \'' + lang +
Expand All @@ -468,7 +471,7 @@ importEntries.prototype = {
throw error;
});
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function() {
// completed updating entry references
log.success('Imported entries in \'' + lang + '\' language successfully!');
Expand Down Expand Up @@ -531,7 +534,7 @@ importEntries.prototype = {
});
// update 5 content types at a time
}, {
concurrency: 3
concurrency: concurrencyNo
}).then(function() {
return resolve();
}).catch(function(error) {
Expand Down Expand Up @@ -610,7 +613,7 @@ importEntries.prototype = {
return;
});
}, {
concurrency: 3
concurrency: concurrencyNo
}).then(function() {
for (var i = 0; i < modifiedSchemas.length; i++) {
if (modifiedSchemasUids.indexOf(modifiedSchemas[i].uid) !== -1) {
Expand Down Expand Up @@ -667,7 +670,7 @@ importEntries.prototype = {
});

}, {
concurrency: 3
concurrency: concurrencyNo
}).then(function() {

for (var i = 0; i < bugged.length; i++) {
Expand Down
8 changes: 5 additions & 3 deletions lib/import/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ importEnvironments.prototype = {
helper.writeFile(envUidMapperPath, self.envUidMapper);
return;
}).catch(function (error) {
self.fails.push(env);
// eslint-disable-next-line no-console
log.error('Environment: \'' + env.name + '\' failed to be imported\n ' + JSON.stringify(error.errors));
if(error.errors.name[0]) {
log.success('Environment: \'' + env.name + '\' already exists');
} else {
log.error('Environment: \'' + env.name + '\' failed to be imported\n ' + JSON.stringify(error.errors));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imported >> import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
return;
});
} else {
Expand Down
5 changes: 3 additions & 2 deletions lib/import/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var path = require('path');
var Promise = require('bluebird');
var chalk = require('chalk');


var concurrencyNo = config.concurrency;
var request = require('../util/request');
var helper = require('../util/fs');
var log = require('../util/log');
Expand Down Expand Up @@ -84,7 +86,7 @@ importExtensions.prototype = {
}
// import 2 extensions at a time
}, {
concurrency: 2
concurrency: concurrencyNo
}).then(function () {
// extensions have imported successfully
helper.writeFile(extSuccessPath, self.success);
Expand All @@ -94,7 +96,6 @@ importExtensions.prototype = {
// error while importing extensions
helper.writeFile(extFailsPath, self.fails);
log.error('Extension import failed');
console.log("errrr", JSON.stringify(error));
return reject(error);
});
});
Expand Down
23 changes: 19 additions & 4 deletions lib/import/global_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var helper = require('../util/fs');
var log = require('../util/log');
var util = require('../util/');
var extension_supress = require('../util/extensionsUidReplace');
var removeReferenceFields = require('../util/removeReferenceFields');

var config = util.getConfig();
var globalfieldsConfig = config.modules.globalfields;
var globalfieldsFolderPath = path.resolve(config.data, globalfieldsConfig.dirName);
Expand All @@ -26,6 +28,8 @@ if (!fs.existsSync(globalfieldsMapperPath)) {
mkdirp.sync(globalfieldsMapperPath);
}

global._globalField_pending = [];

function importGlobalFields () {
this.fails = [];
this.success = [];
Expand All @@ -52,8 +56,17 @@ importGlobalFields.prototype = {
}
var snipUids = Object.keys(self.globalfields);
return Promise.map(snipUids, function (snipUid) {
var flag = {
supressed: false
};
var snip = self.globalfields[snipUid];
extension_supress(snip.schema);
removeReferenceFields(snip.schema, flag);

if(flag.supressed) {
// eslint-disable-next-line no-undef
_globalField_pending.push(snip.uid);
}

if (!self.snipUidMapper.hasOwnProperty(snipUid)) {
var requestOption = {
Expand All @@ -74,9 +87,10 @@ importGlobalFields.prototype = {
log.success(global_field_uid +' '+' globalfield created successfully');
return;
}).catch(function (error) {
if(error.error_code === 115) {
// eslint-disable-next-line no-console
log.error('Globalfield fail to import '+ JSON.stringify(error.errors));
if(error.errors.title[0]) {
log.error('Globalfield already exists ');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add global field details in error logs

} else {
log.error('Globalfield fail to import '+ JSON.stringify(error.errors));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail >> failed

}
self.fails.push(snip);
return;
Expand All @@ -90,8 +104,9 @@ importGlobalFields.prototype = {
}, {
concurrency: 2
}).then(function () {
// console.log("dattatatat", _globalField_pending);
// globalfields have imported successfully
helper.writeFile(globalfieldsSuccessPath, self.success);
helper.writeFile(globalfieldsSuccessPath, self.success);
// log.success('globalfields have been imported successfully!');
return resolve();
}).catch(function (error) {
Expand Down
6 changes: 4 additions & 2 deletions lib/import/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ var fs = require('fs');
var path = require('path');
var Promise = require('bluebird');


var request = require('../util/request');
var helper = require('../util/fs');
var log = require('../util/log');
var util = require('../util/');

var config = util.getConfig();
var concurrencyNo = config.concurrency;
var labelConfig = config.modules.labels;
var labelsFolderPath = path.resolve(config.data, labelConfig.dirName);
var labelMapperPath = path.resolve(config.data, 'mapper', 'labels');
Expand Down Expand Up @@ -83,7 +85,7 @@ importLabels.prototype = {
}
// import 1 labels at a time
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function () {
// eslint-disable-next-line no-undef
return self.updateLabels().then(function () {
Expand Down Expand Up @@ -136,7 +138,7 @@ importLabels.prototype = {
});
}
}, {
concurrency: 1
concurrency: concurrencyNo
}).then(function () {
return resolve();

Expand Down
3 changes: 2 additions & 1 deletion lib/import/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var log = require('../util/log');
var util = require('../util/');

var config = util.getConfig();
var concurrencyNo = config.concurrency;
var langConfig = config.modules.locales;
var langFolderPath = path.resolve(config.data, langConfig.dirName);
var langMapperPath = path.resolve(config.data, 'mapper', 'languages');
Expand Down Expand Up @@ -87,7 +88,7 @@ importLanguages.prototype = {
}
// import 2 languages at a time
}, {
concurrency: 2
concurrency: concurrencyNo
}).then(function () {
// languages have imported successfully
helper.writeFile(langSuccessPath, self.success);
Expand Down
3 changes: 2 additions & 1 deletion lib/import/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var log = require('../util/log');
var util = require('../util/');

var config = util.getConfig();
var concurrencyNo = config.concurrency;
var webhooksConfig = config.modules.webhooks;
var webhooksFolderPath = path.resolve(config.data, webhooksConfig.dirName);
var webMapperPath = path.resolve(config.data, 'mapper', 'webhooks');
Expand Down Expand Up @@ -79,7 +80,7 @@ importWebhooks.prototype = {
}
// import 2 webhooks at a time
}, {
concurrency: 2
concurrency: concurrencyNo
}).then(function () {
// webhooks have imported successfully
helper.writeFile(webSuccessPath, self.success);
Expand Down
Loading