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

Develop #60

Merged
merged 20 commits into from
May 12, 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.

4 changes: 2 additions & 2 deletions packages/contentstack-export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ npm install -g @contentstack/cli-cm-export
$ csdx COMMAND
running command...
$ csdx (-v|--version|version)
@contentstack/cli-cm-export/0.1.1-beta.3 darwin-x64 node-v13.14.0
@contentstack/cli-cm-export/0.1.1-beta.5 darwin-x64 node-v13.14.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -57,5 +57,5 @@ EXAMPLES
csdx cm:export -A -m <single module name>
```

_See code: [src/commands/cm/export.js](https://github.com/contentstack/cli/blob/v0.1.1-beta.3/packages/contentstack-export/src/commands/cm/export.js)_
_See code: [src/commands/cm/export.js](https://github.com/contentstack/cli/blob/v0.1.1-beta.5/packages/contentstack-export/src/commands/cm/export.js)_
<!-- commandsstop -->
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.1-beta.5",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-export/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var allExport = async (config, types) => {
var exportedModule = require('./lib/export/' + type)
await exportedModule.start(config).then(result => {
if (type === 'stack') {
let master_locale = { master_locale: { code: result.master_locale } }
let master_locale = { master_locale: { code: result.code } }
config = _.merge(config, master_locale)
}
return
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-export/src/lib/export/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ExportLocales.prototype.start = function (credentialConfig) {
addlogs(credentialConfig, locale.name + ' locale was exported successfully', 'success')
for (const key in locale) {
if (requiredKeys.indexOf(key) === -1) {
delete locale.key
delete locale[key]
}
}
self.locales[locale.uid] = locale
Expand All @@ -59,10 +59,10 @@ ExportLocales.prototype.start = function (credentialConfig) {
addlogs(credentialConfig, 'No languages found except the master language', 'success')
}
helper.writeFile(path.join(localesFolderPath, localeConfig.fileName), self.locales)
resolve()
return resolve()
}).catch(error => {
addlogs(credentialConfig, error, 'error')
reject()
return reject()
})
})
}
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[0])
}).catch(error => {
return reject(error)
})
Expand Down
1 change: 0 additions & 1 deletion packages/contentstack-export/src/lib/util/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function init (_logPath, logfileName) {
}

exports.addlogs = async (config, message, type) => {
// console.log("datattaat", data)
if (type !== 'error') {
init(config.data, type).log(message)
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ npm install -g @contentstack/cli-cm-import
$ csdx COMMAND
running command...
$ csdx (-v|--version|version)
@contentstack/cli-cm-import/0.1.1-beta.4 darwin-x64 node-v13.14.0
@contentstack/cli-cm-import/0.1.1-beta.6 darwin-x64 node-v13.14.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -56,5 +56,5 @@ EXAMPLES
csdx cm:import -A -m <single module name>
```

_See code: [src/commands/cm/import.js](https://github.com/contentstack/cli/blob/v0.1.1-beta.4/packages/contentstack-import/src/commands/cm/import.js)_
_See code: [src/commands/cm/import.js](https://github.com/contentstack/cli/blob/v0.1.1-beta.6/packages/contentstack-import/src/commands/cm/import.js)_
<!-- commandsstop -->
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.1-beta.6",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down
78 changes: 5 additions & 73 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,71 +98,16 @@ let singleImport = async (moduleName, types, 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)
})
}
}
}
})
})
}

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) {
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,7 +117,7 @@ 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.target_stack + ' has been imported succesfully!'), 'success')
addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success')
}
return resolve()
Expand All @@ -186,18 +130,6 @@ 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)
})
})
}

function createBackup(backupDirPath, config) {
return new Promise((resolve, reject) => {
if (config.hasOwnProperty('useBackedupDir') && fs.existsSync(config.useBackedupDir)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-import/src/commands/cm/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ImportCommand extends Command {
})
}
} else {
this.log('management Token is not present please add managment token first')
console.log('management Token is not present please add managment token first')
}
} else if (authToken && authToken !== undefined && _authToken && _authToken !== undefined) {
if (extConfig && extConfig !== undefined) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class ImportCommand extends Command {
})
}
} else {
this.log('Provide alias for managementToken or authtoken')
console.log('Provide alias for managementToken or authtoken')
}
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/contentstack-import/src/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
'code',
'uid',
'name',
'fallback_locale'
],
},
environments: {
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-import/src/lib/import/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let uniqueUidMapperPath
let modifiedSchemaPath
let createdEntriesWOUidPath
let failedWOPath
let masterLanguage

let masterLanguage = config.master_locale
let skipFiles = ['__master.json', '__priority.json', 'schema.json']
let entryBatchLimit = config.rateLimit || 10

Expand Down Expand Up @@ -99,6 +99,7 @@ importEntries.prototype = {
let self = this
config = credentialConfig
client = stack.Client(config)
masterLanguage = config.master_locale
addlogs(config, 'Migrating entries', 'success')
let languages = helper.readFile(lPath)
return new Promise(function (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
Loading