@contentstack/cli-migration 0.1.1-beta.1
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.
- package/README.md +69 -0
- package/oclif.manifest.json +1 -0
- package/package.json +68 -0
- package/src/actions/action-list.js +32 -0
- package/src/actions/index.js +218 -0
- package/src/commands/cm/migration.js +182 -0
- package/src/config/api-config.js +19 -0
- package/src/config/default-options.js +7 -0
- package/src/config/index.js +7 -0
- package/src/config/master-locale.js +10 -0
- package/src/modules/base.js +95 -0
- package/src/modules/content-types.js +208 -0
- package/src/modules/fields.js +304 -0
- package/src/modules/index.js +8 -0
- package/src/modules/locale.js +33 -0
- package/src/modules/migration.js +106 -0
- package/src/modules/parser.js +84 -0
- package/src/services/content-types.js +323 -0
- package/src/services/index.js +6 -0
- package/src/services/locales.js +74 -0
- package/src/utils/auto-retry.js +30 -0
- package/src/utils/callsite.js +22 -0
- package/src/utils/constants.js +219 -0
- package/src/utils/contentstack-sdk.js +71 -0
- package/src/utils/error-handler.js +21 -0
- package/src/utils/error-helper.js +58 -0
- package/src/utils/fs-helper.js +14 -0
- package/src/utils/get-batches.js +7 -0
- package/src/utils/get-config.js +13 -0
- package/src/utils/group-by.js +38 -0
- package/src/utils/index.js +21 -0
- package/src/utils/logger.js +84 -0
- package/src/utils/map.js +40 -0
- package/src/utils/object-helper.js +9 -0
- package/src/utils/request.js +95 -0
- package/src/utils/safe-promise.js +3 -0
- package/src/utils/schema-helper.js +35 -0
- package/src/utils/success-handler.js +12 -0
- package/src/validators/api-error.js +18 -0
- package/src/validators/base-validator.js +39 -0
- package/src/validators/create-content-type-validator.js +58 -0
- package/src/validators/edit-content-type-validator.js +56 -0
- package/src/validators/field-validator.js +19 -0
- package/src/validators/index.js +11 -0
- package/src/validators/migration-error.js +18 -0
- package/src/validators/schema-validator.js +21 -0
- package/src/validators/type-error.js +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
@contentstack/cli-migration
|
|
2
|
+
===========================
|
|
3
|
+
|
|
4
|
+
The Contentstack CLI’s “Migration” plugin allows developers to automate the content migration process and easily migrate your content from your system to Contentstack.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
[](https://oclif.io)
|
|
8
|
+
[](https://npmjs.org/package/@contentstack/cli-migration)
|
|
9
|
+
[](https://npmjs.org/package/@contentstack/cli-migration)
|
|
10
|
+
[](https://github.com/ninadhatkar/cli-migration/blob/master/package.json)
|
|
11
|
+
|
|
12
|
+
<!-- toc -->
|
|
13
|
+
* [Usage](#usage)
|
|
14
|
+
* [Commands](#commands)
|
|
15
|
+
<!-- tocstop -->
|
|
16
|
+
# Usage
|
|
17
|
+
<!-- usage -->
|
|
18
|
+
```sh-session
|
|
19
|
+
$ npm install -g @contentstack/cli-migration
|
|
20
|
+
$ csdx COMMAND
|
|
21
|
+
running command...
|
|
22
|
+
$ csdx (-v|--version|version)
|
|
23
|
+
@contentstack/cli-migration/0.1.1-beta.1 linux-x64 node-v12.18.4
|
|
24
|
+
$ csdx --help [COMMAND]
|
|
25
|
+
USAGE
|
|
26
|
+
$ csdx COMMAND
|
|
27
|
+
...
|
|
28
|
+
```
|
|
29
|
+
<!-- usagestop -->
|
|
30
|
+
# Commands
|
|
31
|
+
<!-- commands -->
|
|
32
|
+
* [`csdx cm:migration`](#csdx-cmmigration)
|
|
33
|
+
|
|
34
|
+
## `csdx cm:migration`
|
|
35
|
+
|
|
36
|
+
Contentstack migration script.
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Contentstack migration script.
|
|
40
|
+
|
|
41
|
+
USAGE
|
|
42
|
+
$ csdx cm:migration
|
|
43
|
+
|
|
44
|
+
OPTIONS
|
|
45
|
+
-A, --authtoken Use this flag to use the auth token of the current session. After
|
|
46
|
+
logging in CLI, an auth token is generated for each new session.
|
|
47
|
+
|
|
48
|
+
-B, --branch=branch Use this flag to add the branch name where you want to perform
|
|
49
|
+
the migration.
|
|
50
|
+
|
|
51
|
+
-a, --management-token-alias=management-token-alias Use this flag to add the management token alias.
|
|
52
|
+
|
|
53
|
+
-k, --api-key=api-key With this flag add the API key of your stack.
|
|
54
|
+
|
|
55
|
+
-n, --filePath=filePath Use this flag to provide the path of the file of the migration
|
|
56
|
+
script provided by the user.
|
|
57
|
+
|
|
58
|
+
--multi This flag helps you to migrate multiple content files in a single
|
|
59
|
+
instance.
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
_See code: [src/commands/cm/migration.js](https://github.com/contentstack/cli-migration/blob/v0.1.1-beta.1/src/commands/cm/migration.js)_
|
|
63
|
+
<!-- commandsstop -->
|
|
64
|
+
|
|
65
|
+
### Points to remember
|
|
66
|
+
|
|
67
|
+
* Currently, the Migration plugin does not support Group fields migration. You can pass a custom schema to the createField method to migrate Group fields. [here](packages/contentstack-migration/examples/)
|
|
68
|
+
* Currently, the Migration plugin does not support Global fields migration. You can migrate Global fields by creating an SDK instance and adding it to content types using the createField method.[here](packages/contentstack-migration/examples/)
|
|
69
|
+
* Currently, the Migration plugin does not support migration of Entries. You can migrate entries by creating an SDK instance to create/ update/ delete entries for your content type. [here](packages/contentstack-migration/examples/)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.1.1-beta.1","commands":{"cm:migration":{"id":"cm:migration","description":"Contentstack migration script.","pluginName":"@contentstack/cli-migration","pluginType":"core","aliases":[],"flags":{"api-key":{"name":"api-key","type":"option","char":"k","description":"With this flag add the API key of your stack."},"authtoken":{"name":"authtoken","type":"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.","allowNo":false},"management-token-alias":{"name":"management-token-alias","type":"option","char":"a","description":"Use this flag to add the management token alias."},"filePath":{"name":"filePath","type":"option","char":"n","description":"Use this flag to provide the path of the file of the migration script provided by the user."},"branch":{"name":"branch","type":"option","char":"B","description":"Use this flag to add the branch name where you want to perform the migration."},"multi":{"name":"multi","type":"boolean","description":"This flag helps you to migrate multiple content files in a single instance.","allowNo":false}},"args":[]}}}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@contentstack/cli-migration",
|
|
3
|
+
"version": "0.1.1-beta.1",
|
|
4
|
+
"author": "@contentstack",
|
|
5
|
+
"bugs": "https://github.com/contentstack/cli/issues",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@contentstack/cli-command": "^0.1.1-beta.2",
|
|
8
|
+
"@contentstack/management": "github:contentstack/contentstack-management-javascript#65e8588",
|
|
9
|
+
"@oclif/command": "^1.6.1",
|
|
10
|
+
"@oclif/config": "^1.15.1",
|
|
11
|
+
"async": "^3.2.0",
|
|
12
|
+
"axios": "^0.21.1",
|
|
13
|
+
"callsites": "^3.1.0",
|
|
14
|
+
"cardinal": "^2.1.1",
|
|
15
|
+
"chalk": "^4.1.0",
|
|
16
|
+
"dot-object": "^2.1.4",
|
|
17
|
+
"dotenv": "^8.2.0",
|
|
18
|
+
"listr": "^0.14.3",
|
|
19
|
+
"winston": "^3.3.3"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@oclif/dev-cli": "^1.26.0",
|
|
23
|
+
"@oclif/plugin-help": "^2.2.3",
|
|
24
|
+
"@oclif/test": "^1.2.8",
|
|
25
|
+
"chai": "^4.3.4",
|
|
26
|
+
"eslint": "^5.16.0",
|
|
27
|
+
"eslint-config-oclif": "^3.1.0",
|
|
28
|
+
"eslint-config-oclif-typescript": "^0.2.0",
|
|
29
|
+
"globby": "^10.0.2",
|
|
30
|
+
"husky": "^4.2.5",
|
|
31
|
+
"jsdoc": "^3.6.7",
|
|
32
|
+
"jsdoc-to-markdown": "^7.0.1",
|
|
33
|
+
"mkdirp": "^1.0.4",
|
|
34
|
+
"mocha": "^5.2.0",
|
|
35
|
+
"nock": "^13.1.1",
|
|
36
|
+
"nyc": "^14.1.1"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=8.3.0"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"/npm-shrinkwrap.json",
|
|
43
|
+
"/oclif.manifest.json",
|
|
44
|
+
"/src",
|
|
45
|
+
"/yarn.lock"
|
|
46
|
+
],
|
|
47
|
+
"homepage": "https://github.com/contentstack/cli",
|
|
48
|
+
"keywords": [
|
|
49
|
+
"oclif-plugin"
|
|
50
|
+
],
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"oclif": {
|
|
53
|
+
"commands": "./src/commands",
|
|
54
|
+
"bin": "csdx",
|
|
55
|
+
"devPlugins": [
|
|
56
|
+
"@oclif/plugin-help"
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
"repository": "contentstack/cli-migration",
|
|
60
|
+
"scripts": {
|
|
61
|
+
"postpack": "rm -f oclif.manifest.json",
|
|
62
|
+
"posttest": "eslint ./src",
|
|
63
|
+
"generate-api-ref-md-doc": "nyc jsdoc2md ./src/modules/* > ./docs/api-reference.md",
|
|
64
|
+
"prepack": "oclif-dev manifest && oclif-dev readme && npm run generate-api-ref-md-doc",
|
|
65
|
+
"test": "nyc mocha \"test/**/*.test.js\"",
|
|
66
|
+
"version": "oclif-dev readme && git add README.md"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* eslint-disable no-unused-expressions */
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
class ActionList {
|
|
5
|
+
constructor(actionList, typeErrors) {
|
|
6
|
+
typeErrors && (this.typeErrors = typeErrors)
|
|
7
|
+
actionList && (this.actionList = actionList)
|
|
8
|
+
this.validators = []
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
addValidators(validator) {
|
|
12
|
+
this.validators.push(validator)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
validate() {
|
|
16
|
+
const {validators, actionList} = this
|
|
17
|
+
|
|
18
|
+
let errors = []
|
|
19
|
+
for (const action of actionList) {
|
|
20
|
+
for (const validator of validators) {
|
|
21
|
+
if (validator.isApplicable(action)) {
|
|
22
|
+
errors = validator.validate(action)
|
|
23
|
+
break
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return errors
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = ActionList
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Utils
|
|
4
|
+
const {constants} = require('../utils')
|
|
5
|
+
// Properties
|
|
6
|
+
const {actions, validationAction} = constants
|
|
7
|
+
const {
|
|
8
|
+
create,
|
|
9
|
+
customTask,
|
|
10
|
+
edit,
|
|
11
|
+
transformEntries,
|
|
12
|
+
deriveLinkedEntries,
|
|
13
|
+
transformEntriesToType,
|
|
14
|
+
typeError,
|
|
15
|
+
apiError,
|
|
16
|
+
schema,
|
|
17
|
+
__migrationError,
|
|
18
|
+
field,
|
|
19
|
+
} = validationAction
|
|
20
|
+
|
|
21
|
+
const actionCreators = {
|
|
22
|
+
customTasks: (callsite, opts) => {
|
|
23
|
+
const {CUSTOM_TASK} = actions
|
|
24
|
+
return {
|
|
25
|
+
type: customTask,
|
|
26
|
+
meta: {
|
|
27
|
+
callsite: {
|
|
28
|
+
file: callsite.getFileName(),
|
|
29
|
+
line: callsite.getLineNumber(),
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
payload: {
|
|
33
|
+
options: opts,
|
|
34
|
+
action: CUSTOM_TASK,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
contentType: {
|
|
39
|
+
create: (callsite, id, opts) => {
|
|
40
|
+
const {CREATE_CT} = actions
|
|
41
|
+
return {
|
|
42
|
+
type: create,
|
|
43
|
+
meta: {
|
|
44
|
+
callsite: {
|
|
45
|
+
file: callsite.getFileName(),
|
|
46
|
+
line: callsite.getLineNumber(),
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
payload: {
|
|
50
|
+
contentTypeId: id,
|
|
51
|
+
options: opts,
|
|
52
|
+
action: CREATE_CT,
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
edit: (callsite, id, opts) => {
|
|
57
|
+
const {EDIT_CT} = actions
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
type: edit,
|
|
61
|
+
meta: {
|
|
62
|
+
callsite: {
|
|
63
|
+
file: callsite.getFileName(),
|
|
64
|
+
line: callsite.getLineNumber(),
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
payload: {
|
|
68
|
+
contentTypeId: id,
|
|
69
|
+
options: opts,
|
|
70
|
+
action: EDIT_CT,
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
// delete: () => { },
|
|
75
|
+
transformEntries: (callsite, id, opts) => {
|
|
76
|
+
return {
|
|
77
|
+
type: transformEntries,
|
|
78
|
+
meta: {
|
|
79
|
+
callsite: {
|
|
80
|
+
file: callsite.getFileName(),
|
|
81
|
+
line: callsite.getLineNumber(),
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
payload: {
|
|
85
|
+
options: opts,
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
deriveLinkedEntries: (callsite, id, opts) => {
|
|
90
|
+
return {
|
|
91
|
+
type: deriveLinkedEntries,
|
|
92
|
+
meta: {
|
|
93
|
+
callsite: {
|
|
94
|
+
file: callsite.getFileName(),
|
|
95
|
+
line: callsite.getLineNumber(),
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
payload: {
|
|
99
|
+
options: opts,
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
transformEntriesToType: (callsite, id, opts) => {
|
|
104
|
+
return {
|
|
105
|
+
type: transformEntriesToType,
|
|
106
|
+
meta: {
|
|
107
|
+
callsite: {
|
|
108
|
+
file: callsite.getFileName(),
|
|
109
|
+
line: callsite.getLineNumber(),
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
payload: {
|
|
113
|
+
options: opts,
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
typeError: (callsite, id, {typeErrors}) => {
|
|
118
|
+
return {
|
|
119
|
+
type: typeError,
|
|
120
|
+
meta: {
|
|
121
|
+
callsite: {
|
|
122
|
+
file: callsite.getFileName(),
|
|
123
|
+
line: callsite.getLineNumber(),
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
payload: {typeErrors},
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
apiError: (callsite, id, opts) => {
|
|
130
|
+
return {
|
|
131
|
+
type: apiError,
|
|
132
|
+
meta: {
|
|
133
|
+
callsite: {
|
|
134
|
+
file: callsite.getFileName(),
|
|
135
|
+
line: callsite.getLineNumber(),
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
payload: {apiError: opts},
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
fromFields: (callsite, id, opts) => {
|
|
142
|
+
return {
|
|
143
|
+
type: schema,
|
|
144
|
+
meta: {
|
|
145
|
+
callsite: {
|
|
146
|
+
file: callsite.getFileName(),
|
|
147
|
+
line: callsite.getLineNumber(),
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
payload: {fromField: opts.fromField},
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
toFields: (callsite, id, opts) => {
|
|
154
|
+
return {
|
|
155
|
+
type: schema,
|
|
156
|
+
meta: {
|
|
157
|
+
callsite: {
|
|
158
|
+
file: callsite.getFileName(),
|
|
159
|
+
line: callsite.getLineNumber(),
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
payload: {toField: opts.toField},
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
toReferenceFields: (callsite, id, opts) => {
|
|
166
|
+
return {
|
|
167
|
+
type: schema,
|
|
168
|
+
meta: {
|
|
169
|
+
callsite: {
|
|
170
|
+
file: callsite.getFileName(),
|
|
171
|
+
line: callsite.getLineNumber(),
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
payload: {toField: opts.toReferenceField},
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
deriveFields: (callsite, id, opts) => {
|
|
178
|
+
return {
|
|
179
|
+
type: schema,
|
|
180
|
+
meta: {
|
|
181
|
+
callsite: {
|
|
182
|
+
file: callsite.getFileName(),
|
|
183
|
+
line: callsite.getLineNumber(),
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
payload: {deriveField: opts.deriveField},
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
migrationError: (callsite, id, opts) => {
|
|
190
|
+
return {
|
|
191
|
+
type: __migrationError,
|
|
192
|
+
meta: {
|
|
193
|
+
callsite: {
|
|
194
|
+
file: callsite.getFileName(),
|
|
195
|
+
line: callsite.getLineNumber(),
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
payload: {migrationError: opts},
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
field: (callsite, id, opts) => {
|
|
202
|
+
return {
|
|
203
|
+
type: field,
|
|
204
|
+
meta: {
|
|
205
|
+
callsite: {
|
|
206
|
+
file: callsite.getFileName(),
|
|
207
|
+
line: callsite.getLineNumber(),
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
payload: {field: opts},
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
},
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
exports.actionCreators = actionCreators
|
|
218
|
+
exports.ActionList = require('./action-list')
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
const {CONTENTSTACK_API_KEY, CONTENTSTACK_AUTHTOKEN} = process.env
|
|
5
|
+
const {version} = require('../../package.json')
|
|
6
|
+
module.exports = {
|
|
7
|
+
hostname: 'dev9-api.contentstack.com',
|
|
8
|
+
// hostname: 'stag-app.contentstack.com',
|
|
9
|
+
version: '/v3',
|
|
10
|
+
method: 'GET', // Default Http method
|
|
11
|
+
headers: {
|
|
12
|
+
'Content-Type': 'application/json',
|
|
13
|
+
'Content-Length': null,
|
|
14
|
+
'X-User-Agent': `@contentstack-migration/v${version}`,
|
|
15
|
+
authtoken: CONTENTSTACK_AUTHTOKEN,
|
|
16
|
+
api_key: CONTENTSTACK_API_KEY,
|
|
17
|
+
// management_token: CONTENTSTACK_MANAGEMENT_TOKEN
|
|
18
|
+
},
|
|
19
|
+
}
|