Skip to content

Commit

Permalink
Merge pull request #30 from contentstack/clone-stack
Browse files Browse the repository at this point in the history
Merge develop branch into clone-stack branch
  • Loading branch information
rohitmishra209 authored Mar 15, 2021
2 parents 7330a91 + 8c72651 commit d8ec3fc
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 101 deletions.
9 changes: 6 additions & 3 deletions packages/contentstack-clone/src/lib/util/clone-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CloneHandler {
let orgdetails = this.getOrganizationChoices(oraMessage)
orgdetails
.then(async (orgList)=>{
var stackMessage = 'Select the source stack'
var stackMessage = 'Choose the destination stack:'
var orgSelected = await inquirer.prompt(orgList)
let stackDetails = this.getStack(orgSelected, stackMessage)
stackDetails
Expand Down Expand Up @@ -122,7 +122,7 @@ class CloneHandler {
let orgChoice = {
type: 'list',
name: 'Organization',
message: oraMessage,
message: oraMessage !== undefined ? oraMessage : "Choose an organization",
choices: [],
}
return new Promise(async (resolve, reject) => {
Expand All @@ -148,6 +148,7 @@ class CloneHandler {
type: 'list',
name: 'stack',
message: stkMessage,
message: stkMessage !== undefined ? stkMessage : "Select the stack",
choices: [],
}
const spinner = ora('Fetching stacks').start()
Expand Down Expand Up @@ -198,7 +199,9 @@ class CloneHandler {
type: 'list',
name: 'type',
message: 'Choose the type of data to clone:',
choices: ["Structure (all modules except entries & assets)", "Structure with content (all modules including entries & assets)"]
choices: ["Structure (all modules except entries & assets)",
"Structure with content (all modules including entries & assets)"
]
}]
var selectedValue = await inquirer.prompt(cloneTypeSelection)
let cloneType = selectedValue.type
Expand Down
253 changes: 156 additions & 97 deletions packages/contentstack-import/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,136 +9,195 @@ let Bluebird = require('bluebird')
let fs = require('fs')
let path = require('path')
const chalk = require('chalk')
const helper = require('./lib/util/fs')
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')

exports.initial = function (configData) {
let { addlogs } = require('./lib/util/log')

exports.initial = function (configData) {
return new Promise(function (resolve, reject) {
let config = util.initialization(configData)
config.oldPath = config.data
if (config && config !== undefined) {
login(config)
.then(function () {
if (fs.existsSync(config.data)) {
let migrationBackupDirPath = path.join(process.cwd(), '_backup_' + Math.floor((Math.random() * 1000)))
return createBackup(migrationBackupDirPath, config).then((basePath) => {
config.data = basePath
return util.sanitizeStack(config)
}).catch(e=>{
console.error(e)
process.exit(1)
})
.then(() => {
let types = config.modules.types
if (config.moduleName && config.moduleName !== undefined) {
singleImport(config.moduleName, types, config).then(() => {
return resolve()
})
} else {
allImport(config, types).then(() => {
return resolve()
})
}
}).catch(e=>{
console.error(e)
return reject(e)
// process.exit(1)
})
} else {
let filename = path.basename(config.data)
addlogs(config, chalk.red(filename + " Folder does not Exist"), 'error')
return
let config = util.initialization(configData)
config.oldPath = config.data
if (config && config !== undefined) {
login(config)
.then(function () {
if (fs.existsSync(config.data)) {
let migrationBackupDirPath = path.join(process.cwd(), '_backup_' + Math.floor((Math.random() * 1000)))
return createBackup(migrationBackupDirPath, config).then((basePath) => {
config.data = basePath
return util.sanitizeStack(config)
}).catch(e => {
console.error(e)
process.exit(1)
})
.then(() => {
let types = config.modules.types
if (config.moduleName && config.moduleName !== undefined) {
singleImport(config.moduleName, types, config).then(() => {
return resolve()
})
} else {
allImport(config, types).then(() => {
return resolve()
})
}
}).catch(e => {
console.error(e)
return reject(e)
})
} else {
let filename = path.basename(config.data)
addlogs(config, chalk.red(filename + " Folder does not Exist"), 'error')
return
}
}).catch(error => {
return
})
}
}).catch(error => {
return
})
}
})
})
}


let singleImport = async (moduleName, types, config) => {
return new Promise(async (resolve, reject) => {
if (types.indexOf(moduleName) > -1) {
if (!config.master_locale) {
await stackDetails(config).then(stackResponse => {
let master_locale = { code: stackResponse.master_locale }
config['master_locale'] = master_locale
return
}).catch(error => {
console.log("Error to fetch the stack details" + error);
if (types.indexOf(moduleName) > -1) {
if (!config.master_locale) {
await stackDetails(config).then(stackResponse => {
let master_locale = { code: stackResponse.master_locale }
config['master_locale'] = master_locale
return
}).catch(error => {
console.log("Error to fetch the stack details" + error);
})
}
let exportedModule = require('./lib/import/' + moduleName)
exportedModule.start(config).then(async function () {
if (moduleName === 'content-types') {
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)
}
}
addlogs(config, moduleName + ' imported successfully!', 'success')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success')
return resolve()
}).catch(function (error) {
addlogs(config, 'Failed to migrate ' + moduleName, 'error')
addlogs(config, error, 'error')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'error')
return reject()
})
} else {
addlogs(config, 'Please provide valid module name.', 'error')
return reject()
}
let exportedModule = require('./lib/import/' + moduleName)
exportedModule.start(config).then(function () {
addlogs(config, moduleName + ' imported successfully!', 'success')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success')
return resolve()
}).catch(function (error) {
addlogs(config, 'Failed to migrate ' + moduleName, 'error')
addlogs(config, error, 'error')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'error')
return reject()
})
}

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)
})
}
}
}
})
} else {
addlogs(config, 'Please provide valid module name.', 'error')
return reject()
}
})
})
}

let allImport = async (config, types) => {
return new Promise(async (resolve, reject) => {
try {
for (let i = 0; i < types.length; i++) {
let type = types[i]
var exportedModule = require('./lib/import/' + type)
if (i === 0 && !config.master_locale) {
await stackDetails(config).then(stackResponse => {
try {
for (let i = 0; i < types.length; i++) {
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 }
config['master_locale'] = master_locale
config['stackName'] = stackResponse.name
}
await exportedModule.start(config).then(result => {
return
}).catch(error => {
console.log("Error to fetch the stack details" + error);
})
}
await exportedModule.start(config).then(result => {
return
})
}
addlogs(config, chalk.green('Stack: ' + config.target_stack + ' has been imported succesfully!'), 'success')
if (config.target_stack && config.source_stack) {
addlogs(config, 'The log for this is stored at' + path.join(config.data, 'logs', 'import'), 'success')
} else {
addlogs(config, 'The log for this is stored at' + path.join(config.oldPath, 'logs', 'import'), 'success')
addlogs(config, chalk.green('Stack: ' + config.stackName + ' has been imported succesfully!'), 'success')
if (config.target_stack && config.source_stack) {
addlogs(config, 'The log for this is stored at' + path.join(config.data, 'logs', 'import'), 'success')
} else {
addlogs(config, 'The log for this is stored at' + path.join(config.oldPath, 'logs', 'import'), 'success')
}
return resolve()
} catch (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')
return reject()
}
return resolve()
} catch (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')
return reject()
}
})
})
}

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)
})
return client.stack({ api_key: credentialConfig.target_stack }).fetch()
.then(response => {
return resolve(response)
}).catch(error => {
return reject(error)
})
})
}

function createBackup (backupDirPath, config) {
function createBackup(backupDirPath, config) {
return new Promise((resolve, reject) => {
if (config.hasOwnProperty('useBackedupDir') && fs.existsSync(config.useBackedupDir)) {
return resolve(config.useBackedupDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ importContentTypes.prototype = {
})
}

if( globalFieldPendingPath.length !== 0 ) {
if(globalFieldPendingPath && globalFieldPendingPath.length !== 0) {
return self.updateGlobalfields().then(function () {
addlogs(config, chalk.green('Content types have been imported successfully!'), 'success')
return resolve()
Expand Down

0 comments on commit d8ec3fc

Please sign in to comment.