@contentstack/cli-migration 0.1.1-beta.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +33 -25
  3. package/oclif.manifest.json +1 -1
  4. package/package.json +17 -12
  5. package/src/actions/action-list.js +11 -11
  6. package/src/actions/index.js +33 -34
  7. package/src/commands/cm/stacks/migration.js +299 -0
  8. package/src/config/api-config.js +5 -6
  9. package/src/config/default-options.js +2 -2
  10. package/src/config/index.js +2 -2
  11. package/src/config/master-locale.js +2 -2
  12. package/src/modules/base.js +33 -33
  13. package/src/modules/content-types.js +76 -76
  14. package/src/modules/fields.js +73 -73
  15. package/src/modules/index.js +2 -2
  16. package/src/modules/locale.js +13 -13
  17. package/src/modules/migration.js +45 -46
  18. package/src/modules/parser.js +68 -47
  19. package/src/services/content-types.js +160 -163
  20. package/src/services/index.js +2 -2
  21. package/src/services/locales.js +33 -35
  22. package/src/utils/auto-retry.js +14 -12
  23. package/src/utils/callsite.js +14 -14
  24. package/src/utils/constants.js +35 -33
  25. package/src/utils/contentstack-sdk.js +42 -43
  26. package/src/utils/error-handler.js +8 -8
  27. package/src/utils/error-helper.js +41 -40
  28. package/src/utils/fs-helper.js +24 -10
  29. package/src/utils/get-batches.js +4 -4
  30. package/src/utils/get-config.js +6 -6
  31. package/src/utils/group-by.js +17 -17
  32. package/src/utils/index.js +2 -2
  33. package/src/utils/logger.js +42 -52
  34. package/src/utils/object-helper.js +7 -7
  35. package/src/utils/safe-promise.js +2 -2
  36. package/src/utils/schema-helper.js +12 -12
  37. package/src/utils/success-handler.js +5 -5
  38. package/src/validators/api-error.js +10 -8
  39. package/src/validators/base-validator.js +14 -14
  40. package/src/validators/create-content-type-validator.js +21 -25
  41. package/src/validators/edit-content-type-validator.js +21 -24
  42. package/src/validators/field-validator.js +10 -8
  43. package/src/validators/index.js +2 -2
  44. package/src/validators/migration-error.js +9 -7
  45. package/src/validators/schema-validator.js +11 -9
  46. package/src/validators/type-error.js +11 -10
  47. package/src/commands/cm/migration.js +0 -182
@@ -1,182 +0,0 @@
1
- /* eslint-disable no-unused-expressions */
2
- /* eslint-disable no-warning-comments */
3
- /* eslint-disable camelcase */
4
- 'use strict'
5
-
6
- // Dependencies
7
- const Listr = require('listr')
8
- const {resolve, extname} = require('path')
9
- const {Command, flags} = require('@contentstack/cli-command')
10
- const {waterfall} = require('async')
11
- const {Parser} = require('../../modules')
12
- const {ActionList} = require('../../actions')
13
- const fs = require('fs')
14
- const chalk = require('chalk')
15
-
16
- const {ApiError, SchemaValidator, MigrationError, FieldValidator} = require('../../validators')
17
-
18
- // Utils
19
- const {map: _map, constants, safePromise, errorHelper} = require('../../utils')
20
- const {success} = require('../../utils/logger')
21
-
22
- // Properties
23
- const {get, set, getMapInstance, resetMapInstance} = _map
24
- const {requests: _requests, actionMapper, MANAGEMENT_SDK, MANAGEMENT_TOKEN, AUTH_TOKEN, API_KEY, BRANCH} = constants
25
-
26
- class MigrationCommand extends Command {
27
- async run() {
28
- // TODO: filePath validation required.
29
- const migrationCommandFlags = this.parse(MigrationCommand).flags
30
- const {filePath, multi, branch} = migrationCommandFlags
31
- const authtoken = migrationCommandFlags.authtoken
32
- const apiKey = migrationCommandFlags['api-key']
33
- const alias = migrationCommandFlags['management-token-alias']
34
-
35
- let stackSDKInstance
36
-
37
- // Reset map instance
38
- const mapInstance = getMapInstance()
39
- resetMapInstance(mapInstance)
40
- if (branch) {
41
- set(BRANCH, mapInstance, branch)
42
- }
43
-
44
- if (alias) {
45
- let managementToken = this.getToken(alias)
46
- if (managementToken) {
47
- set(MANAGEMENT_TOKEN, mapInstance, managementToken)
48
- set(API_KEY, mapInstance, managementToken.apiKey)
49
- if (branch) {
50
- stackSDKInstance = this.managementAPIClient.stack({management_token: managementToken.token, api_key: managementToken.apiKey, branch_uid: branch})
51
- } else {
52
- stackSDKInstance = this.managementAPIClient.stack({management_token: managementToken.token, api_key: managementToken.apiKey})
53
- }
54
- }
55
- }
56
-
57
- if (authtoken) {
58
- set(AUTH_TOKEN, mapInstance, authtoken)
59
- set(API_KEY, mapInstance, apiKey)
60
- this.managementAPIClient = {authtoken: this.authToken}
61
- if (branch) {
62
- stackSDKInstance = this.managementAPIClient.stack({api_key: apiKey, branch_uid: branch})
63
- } else {
64
- stackSDKInstance = this.managementAPIClient.stack({api_key: apiKey})
65
- }
66
- }
67
-
68
- set(MANAGEMENT_SDK, mapInstance, stackSDKInstance)
69
-
70
- if (multi) {
71
- await this.execMultiFiles(filePath, mapInstance)
72
- } else {
73
- await this.execSingleFile(filePath, mapInstance)
74
- }
75
- }
76
-
77
- async execSingleFile(filePath, mapInstance) {
78
- // Resolved absolute path
79
- const resolvedMigrationPath = resolve(filePath)
80
- // User provided migration function
81
- const migrationFunc = require(resolvedMigrationPath)
82
-
83
- const parser = new Parser()
84
-
85
- try {
86
- const migrationParser = await parser.getMigrationParser(migrationFunc)
87
- if (migrationParser.hasErrors) {
88
- errorHelper(migrationParser.hasErrors)
89
- // When the process is child, send error message to parent
90
- if (process.send) process.send({errorOccurred: true})
91
- this.exit(1)
92
- }
93
-
94
- // Make calls from here
95
- const requests = get(_requests, mapInstance)
96
- // Fetches tasks array
97
- const tasks = this.getTasks(requests)
98
-
99
- const listr = new Listr(tasks)
100
-
101
- await listr.run().catch(error => {
102
- this.handleErrors(error)
103
- // When the process is child, send error message to parent
104
- if (process.send) process.send({errorOccurred: true})
105
- })
106
- requests.splice(0, requests.length)
107
- } catch (error) {
108
- // errorHandler(null, null, null, error)
109
- }
110
- }
111
-
112
- async execMultiFiles(filePath, mapInstance) {
113
- // Resolved absolute path
114
- const resolvedMigrationPath = resolve(filePath)
115
- try {
116
- const files = fs.readdirSync(resolvedMigrationPath)
117
- for (let index = 0; index < files.length; index++) {
118
- const file = files[index]
119
- if (extname(file) === '.js') {
120
- success(chalk`{white Executing file:} {grey {bold ${file}}}`)
121
- // eslint-disable-next-line no-await-in-loop
122
- await this.execSingleFile(resolve(filePath, file), mapInstance)
123
- }
124
- }
125
- } catch (error) {
126
- error(error)
127
- }
128
- }
129
-
130
- getTasks(requests) {
131
- const _tasks = []
132
- const results = []
133
-
134
- for (let i = 0; i < requests.length; i++) {
135
- let reqObj = requests[i]
136
- const {title, failedTitle, successTitle, tasks} = reqObj
137
- const task = {
138
- title: title,
139
- task: async (ctx, task) => {
140
- const [err, result] = await safePromise(waterfall(tasks))
141
- if (err) {
142
- ctx.error = true
143
- task.title = failedTitle
144
- throw err
145
- }
146
- result && results.push(result)
147
- task.title = successTitle
148
- return result
149
- },
150
- }
151
- _tasks.push(task)
152
- }
153
- return _tasks
154
- }
155
-
156
- handleErrors() {
157
- const mapInstance = getMapInstance()
158
- const actions = get(actionMapper, mapInstance)
159
- const actionList = new ActionList(actions)
160
-
161
- actionList.addValidators(new ApiError())
162
- actionList.addValidators(new SchemaValidator())
163
- actionList.addValidators(new MigrationError())
164
- actionList.addValidators(new FieldValidator())
165
-
166
- const errors = actionList.validate()
167
- errorHelper(errors)
168
- }
169
- }
170
-
171
- MigrationCommand.description = 'Contentstack migration script.'
172
-
173
- MigrationCommand.flags = {
174
- 'api-key': flags.string({char: 'k', description: 'With this flag add the API key of your stack.', dependsOn: ['authtoken'], exclusive: ['management-token-alias']}),
175
- authtoken: flags.boolean({char: 'A', description: 'Use this flag to use the auth token of the current session. After logging in CLI, an auth token is generated for each new session.', dependsOn: ['api-key'], exclusive: ['management-token-alias']}),
176
- 'management-token-alias': flags.string({char: 'a', description: 'Use this flag to add the management token alias.', exclusive: ['authtoken']}), // Add a better description
177
- filePath: flags.string({char: 'n', description: 'Use this flag to provide the path of the file of the migration script provided by the user.'}),
178
- branch: flags.string({char: 'B', description: 'Use this flag to add the branch name where you want to perform the migration.'}),
179
- multi: flags.boolean({description: 'This flag helps you to migrate multiple content files in a single instance.'}), // Add a better description
180
- }
181
-
182
- module.exports = MigrationCommand