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

Hotfix0.1.2 #48

Merged
merged 7 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,847 changes: 5,837 additions & 10 deletions packages/auth/package-lock.json

Large diffs are not rendered by default.

1,079 changes: 1,077 additions & 2 deletions packages/contentstack-command/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contentstack-export/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-export",
"description": "Contentstack CLI plugin to export content from stack",
"version": "0.1.1-beta.3",
"version": "0.1.2-beta.3",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
10 changes: 8 additions & 2 deletions packages/contentstack-export/src/lib/export/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ ExportStack.prototype.start = function (credentialConfig) {
client = stack.Client(config)
if (!config.preserveStackVersion && !config.hasOwnProperty("master_locale")) {
return new Promise((resolve, reject) => {
return client.stack({api_key: config.source_stack}).fetch()
var result = client.stack({ api_key: credentialConfig.source_stack, management_token: credentialConfig.management_token }).locale().query()
result.find()
.then(response => {
return resolve(response)
var masterLocalObj = response.items.filter(obj => {
if (obj.fallback_locale === null) {
return obj
}
});
return resolve(masterLocalObj)
}).catch(error => {
return reject(error)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-import",
"description": "Contentstack CLI plugin to import content into stack",
"version": "0.1.1-beta.4",
"version": "0.1.2-beta.4",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
144 changes: 74 additions & 70 deletions packages/contentstack-import/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let _ = require('lodash')

let login = require('./lib/util/login')
let util = require('./lib/util/index')
const stack = require('./lib/util/contentstack-management-sdk')


let { addlogs } = require('./lib/util/log')

Expand Down Expand Up @@ -61,7 +61,6 @@ exports.initial = function (configData) {
})
}


let singleImport = async (moduleName, types, config) => {
return new Promise(async (resolve, reject) => {
if (types.indexOf(moduleName) > -1) {
Expand All @@ -80,7 +79,7 @@ let singleImport = async (moduleName, types, config) => {
let ctPath = path.resolve(config.data, config.modules.content_types.dirName)
let fieldPath = path.join(ctPath + '/field_rules_uid.json')
if (fieldPath && fieldPath !== undefined) {
await field_rules_update(config, ctPath)
await util.field_rules_update(config, ctPath)
}
}
addlogs(config, moduleName + ' imported successfully!', 'success')
Expand All @@ -99,58 +98,58 @@ let singleImport = async (moduleName, types, config) => {
})
}

function field_rules_update(config, ctPath) {
return new Promise(function (resolve, reject) {
let client = stack.Client(config)
// function field_rules_update(config, ctPath) {
// return new Promise(function (resolve, reject) {
// let client = stack.Client(config)

fs.readFile(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
if (err) {
throw err;
}
var ct_field_visibility_uid = JSON.parse(data)
let ct_files = fs.readdirSync(ctPath)
if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
for (let index = 0; index < ct_field_visibility_uid.length; index++) {
if (ct_files.indexOf(ct_field_visibility_uid[index] + '.json') > -1) {
let schema = require(path.resolve(ctPath, ct_field_visibility_uid[index]))
// await field_rules_update(schema)
let fieldRuleLength = schema.field_rules.length
for (let k = 0; k < fieldRuleLength; k++) {
let fieldRuleConditionLength = schema.field_rules[k].conditions.length
for (let i = 0; i < fieldRuleConditionLength; i++) {
if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
let entryMapperPath = path.resolve(config.data, 'mapper', 'entries')
let entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json')
let fieldRulesValue = schema.field_rules[k].conditions[i].value
let fieldRulesArray = fieldRulesValue.split('.')
let updatedValue = []
for (let j = 0; j < fieldRulesArray.length; j++) {
let splitedFieldRulesValue = fieldRulesArray[j]
let oldUid = helper.readFile(path.join(entryUidMapperPath))
if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
updatedValue.push(oldUid[splitedFieldRulesValue])
} else {
updatedValue.push(fieldRulesArray[j])
}
}
schema.field_rules[k].conditions[i].value = updatedValue.join('.')
}
}
}
let ctObj = client.stack({ api_key: config.target_stack, management_token: config.management_token }).contentType(schema.uid)
Object.assign(ctObj, _.cloneDeep(schema))
ctObj.update()
.then(() => {
return resolve()
}).catch(function (error) {
return reject(error)
})
}
}
}
})
})
}
// fs.readFile(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
// if (err) {
// throw err;
// }
// var ct_field_visibility_uid = JSON.parse(data)
// let ct_files = fs.readdirSync(ctPath)
// if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
// for (let index = 0; index < ct_field_visibility_uid.length; index++) {
// if (ct_files.indexOf(ct_field_visibility_uid[index] + '.json') > -1) {
// let schema = require(path.resolve(ctPath, ct_field_visibility_uid[index]))
// // await field_rules_update(schema)
// let fieldRuleLength = schema.field_rules.length
// for (let k = 0; k < fieldRuleLength; k++) {
// let fieldRuleConditionLength = schema.field_rules[k].conditions.length
// for (let i = 0; i < fieldRuleConditionLength; i++) {
// if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
// let entryMapperPath = path.resolve(config.data, 'mapper', 'entries')
// let entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json')
// let fieldRulesValue = schema.field_rules[k].conditions[i].value
// let fieldRulesArray = fieldRulesValue.split('.')
// let updatedValue = []
// for (let j = 0; j < fieldRulesArray.length; j++) {
// let splitedFieldRulesValue = fieldRulesArray[j]
// let oldUid = helper.readFile(path.join(entryUidMapperPath))
// if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
// updatedValue.push(oldUid[splitedFieldRulesValue])
// } else {
// updatedValue.push(fieldRulesArray[j])
// }
// }
// schema.field_rules[k].conditions[i].value = updatedValue.join('.')
// }
// }
// }
// let ctObj = client.stack({ api_key: config.target_stack, management_token: config.management_token }).contentType(schema.uid)
// Object.assign(ctObj, _.cloneDeep(schema))
// ctObj.update()
// .then(() => {
// return resolve()
// }).catch(function (error) {
// return reject(error)
// })
// }
// }
// }
// })
// })
// }

let allImport = async (config, types) => {
return new Promise(async (resolve, reject) => {
Expand All @@ -159,11 +158,9 @@ let allImport = async (config, types) => {
let type = types[i]
var exportedModule = require('./lib/import/' + type)
if (i === 0 && !config.master_locale) {
var stackResponse = await stackDetails(config)
// console.log("Line no 101", stackResponse);
let master_locale = { code: stackResponse.master_locale }
var masterLocalResponse = await util.masterLocalDetails(config)
let master_locale = { code: masterLocalResponse.code }
config['master_locale'] = master_locale
config['stackName'] = stackResponse.name
}
await exportedModule.start(config).then(result => {
return
Expand All @@ -173,11 +170,12 @@ let allImport = async (config, types) => {
addlogs(config, chalk.green('The data of the ' + config.sourceStackName + ' stack has been imported into ' + config.destinationStackName + ' stack successfully!'), 'success')
addlogs(config, 'The log for this is stored at ' + path.join(config.data, 'logs', 'import'), 'success')
} else {
addlogs(config, chalk.green('Stack: ' + config.stackName + ' has been imported succesfully!'), 'success')
addlogs(config, chalk.green('Stack: ' + config.sourceStackName + ' has been imported succesfully!'), 'success')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success')
}
return resolve()
} catch (error) {
console.log("Line no 178+++++++", error);
addlogs(config, chalk.red('Failed to migrate stack: ' + config.target_stack + '. Please check error logs for more info'), 'error')
addlogs(config, error, 'error')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'error')
Expand All @@ -186,17 +184,23 @@ let allImport = async (config, types) => {
})
}

let stackDetails = async (credentialConfig) => {
let client = stack.Client(credentialConfig)
return new Promise((resolve, reject) => {
return client.stack({ api_key: credentialConfig.target_stack }).fetch()
.then(response => {
return resolve(response)
}).catch(error => {
return reject(error)
})
})
}
// let masterLocalDetails = async (credentialConfig) => {
// let client = stack.Client(credentialConfig)
// return new Promise((resolve, reject) => {
// var result = client.stack({ api_key: credentialConfig.target_stack, management_token: credentialConfig.management_token }).locale().query()
// result.find()
// .then(response => {
// var masterLocalObj = response.items.filter(obj => {
// if (obj.fallback_locale === null) {
// return obj
// }
// });
// return resolve(masterLocalObj)
// }).catch(error => {
// return reject(error)
// })
// })
// }

function createBackup(backupDirPath, config) {
return new Promise((resolve, reject) => {
Expand Down
22 changes: 18 additions & 4 deletions packages/contentstack-import/src/lib/import/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ importLanguages.prototype = {
}).then(function () {
// languages have imported successfully
self.update_locales(langUids)
helper.writeFile(langSuccessPath, self.success)
addlogs(config, chalk.green('Languages have been imported successfully!'), 'success')
return resolve()
.then(() => {
helper.writeFile(langSuccessPath, self.success)
addlogs(config, chalk.green('Languages have been imported successfully!'), 'success')
return resolve()
}).catch(function (error) {
return reject(error)
})

}).catch(function (error) {
// error while importing languages
helper.writeFile(langFailsPath, self.fails)
Expand All @@ -106,9 +111,15 @@ importLanguages.prototype = {
},
update_locales: function (langUids) {
let self = this
return new Promise(function (resolve, reject) {
Promise.all(
langUids.map(async langUid => {
let lang = self.languages[langUid]
let lang = {}
let requireKeys = config.modules.locales.requiredKeys
let _lang = self.languages[langUid]
requireKeys.forEach(e => {
lang[e] = _lang[e]
})
let langobj = client.stack({api_key: config.target_stack, management_token: config.management_token}).locale(lang.code)
Object.assign(langobj, _.cloneDeep(lang))
langobj.update()
Expand All @@ -117,8 +128,11 @@ importLanguages.prototype = {
})
)
.then(()=>{
return resolve()
}).catch((error) => {
return reject(error)
})
})
},
}

Expand Down
72 changes: 72 additions & 0 deletions packages/contentstack-import/src/lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var chalk = require('chalk')
var {addlogs} = require('./log')
var request = require('./request')
var defaultConfig = require('../../config/default')
const stack = require('./contentstack-management-sdk')
var config

exports.initialization = function(configData) {
Expand Down Expand Up @@ -106,6 +107,77 @@ exports.sanitizeStack = function (config) {
}
}

exports.masterLocalDetails = function(credentialConfig) {
let client = stack.Client(credentialConfig)
return new Promise((resolve, reject) => {
var result = client.stack({ api_key: credentialConfig.target_stack, management_token: credentialConfig.management_token }).locale().query()
result.find()
.then(response => {
var masterLocalObj = response.items.filter(obj => {
if (obj.fallback_locale === null) {
return obj
}
});
return resolve(masterLocalObj)
}).catch(error => {
return reject(error)
})
})
};

exports.field_rules_update = function(config, ctPath) {
return new Promise(function (resolve, reject) {
let client = stack.Client(config)

fs.readFile(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
if (err) {
throw err;
}
var ct_field_visibility_uid = JSON.parse(data)
let ct_files = fs.readdirSync(ctPath)
if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
for (let index = 0; index < ct_field_visibility_uid.length; index++) {
if (ct_files.indexOf(ct_field_visibility_uid[index] + '.json') > -1) {
let schema = require(path.resolve(ctPath, ct_field_visibility_uid[index]))
// await field_rules_update(schema)
let fieldRuleLength = schema.field_rules.length
for (let k = 0; k < fieldRuleLength; k++) {
let fieldRuleConditionLength = schema.field_rules[k].conditions.length
for (let i = 0; i < fieldRuleConditionLength; i++) {
if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
let entryMapperPath = path.resolve(config.data, 'mapper', 'entries')
let entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json')
let fieldRulesValue = schema.field_rules[k].conditions[i].value
let fieldRulesArray = fieldRulesValue.split('.')
let updatedValue = []
for (let j = 0; j < fieldRulesArray.length; j++) {
let splitedFieldRulesValue = fieldRulesArray[j]
let oldUid = helper.readFile(path.join(entryUidMapperPath))
if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
updatedValue.push(oldUid[splitedFieldRulesValue])
} else {
updatedValue.push(fieldRulesArray[j])
}
}
schema.field_rules[k].conditions[i].value = updatedValue.join('.')
}
}
}
let ctObj = client.stack({ api_key: config.target_stack, management_token: config.management_token }).contentType(schema.uid)
Object.assign(ctObj, _.cloneDeep(schema))
ctObj.update()
.then(() => {
return resolve()
}).catch(function (error) {
return reject(error)
})
}
}
}
})
})
}

exports.getConfig = function() {
return config
};