@contentstack/cli-cm-import 1.1.0 → 1.2.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 +4 -3
- package/oclif.manifest.json +1 -1
- package/package.json +8 -7
- package/src/app.js +90 -106
- package/src/commands/cm/stacks/import.js +8 -1
- package/src/config/default.js +9 -4
- package/src/lib/import/assets.js +291 -296
- package/src/lib/import/content-types.js +171 -246
- package/src/lib/import/custom-roles.js +110 -93
- package/src/lib/import/entries.js +216 -174
- package/src/lib/import/environments.js +40 -50
- package/src/lib/import/extensions.js +35 -41
- package/src/lib/import/global-fields.js +56 -68
- package/src/lib/import/labels.js +62 -61
- package/src/lib/import/locales.js +61 -64
- package/src/lib/import/marketplace-apps.js +293 -290
- package/src/lib/import/webhooks.js +45 -51
- package/src/lib/import/workflows.js +72 -62
- package/src/lib/util/extensionsUidReplace.js +9 -9
- package/src/lib/util/fs.js +91 -12
- package/src/lib/util/index.js +39 -3
- package/src/lib/util/log.js +7 -5
- package/src/lib/util/login.js +2 -1
- package/src/lib/util/lookupReplaceAssets.js +22 -10
- package/src/lib/util/lookupReplaceEntries.js +60 -60
- package/src/lib/util/marketplace-app-helper.js +25 -6
package/src/lib/import/assets.js
CHANGED
|
@@ -1,73 +1,78 @@
|
|
|
1
|
-
/* eslint-disable no-prototype-builtins */
|
|
2
1
|
/*!
|
|
3
2
|
* Contentstack Import
|
|
4
3
|
* Copyright (c) 2019 Contentstack LLC
|
|
5
4
|
* MIT Licensed
|
|
6
5
|
*/
|
|
6
|
+
const fs = require('fs')
|
|
7
|
+
const _ = require('lodash')
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const chalk = require('chalk')
|
|
10
|
+
const mkdirp = require('mkdirp')
|
|
11
|
+
const Promise = require('bluebird')
|
|
7
12
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
13
|
+
const helper = require('../util/fs')
|
|
14
|
+
let upload = require('../util/upload')
|
|
15
|
+
const { addlogs } = require('../util/log')
|
|
16
|
+
let config = require('../../config/default')
|
|
17
|
+
const stack = require('../util/contentstack-management-sdk')
|
|
18
|
+
|
|
19
|
+
module.exports = class ImportAssets {
|
|
20
|
+
config
|
|
21
|
+
client
|
|
22
|
+
assets
|
|
23
|
+
fails = []
|
|
24
|
+
assetConfig
|
|
25
|
+
mapperDirPath
|
|
26
|
+
assetBatchLimit
|
|
27
|
+
uidMapping = {}
|
|
28
|
+
urlMapping = {}
|
|
29
|
+
environmentPath
|
|
30
|
+
assetBucket = []
|
|
31
|
+
assetsFolderPath
|
|
32
|
+
folderBucket = []
|
|
33
|
+
folderDetails = []
|
|
34
|
+
mappedFolderUids = {}
|
|
35
|
+
|
|
36
|
+
constructor(_config) {
|
|
37
|
+
this.config = _config
|
|
38
|
+
this.client = stack.Client(this.config)
|
|
39
|
+
this.assetConfig = config.modules.assets
|
|
40
|
+
this.assetBatchLimit = (this.assetConfig.hasOwnProperty('assetBatchLimit') && typeof this.assetConfig.assetBatchLimit === 'number')
|
|
41
|
+
? this.assetConfig.assetBatchLimit
|
|
42
|
+
: 2
|
|
43
|
+
}
|
|
38
44
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
addlogs(config, 'Migrating assets', 'success')
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
self.environment = helper.readFile(environmentPath);
|
|
53
|
-
if (fs.existsSync(self.uidMapperPath)) {
|
|
54
|
-
self.uidMapping = helper.readFile(self.uidMapperPath);
|
|
45
|
+
start() {
|
|
46
|
+
let self = this
|
|
47
|
+
addlogs(self.config, 'Migrating assets', 'success')
|
|
48
|
+
this.assetsFolderPath = path.join(this.config.data, this.config.modules.assets.dirName)
|
|
49
|
+
this.mapperDirPath = path.resolve(this.config.data, 'mapper', 'assets')
|
|
50
|
+
this.environmentPath = path.resolve(this.config.data, 'environments', 'environments.json')
|
|
51
|
+
this.uidMapperPath = path.join(this.mapperDirPath, 'uid-mapping.json')
|
|
52
|
+
this.urlMapperPath = path.join(this.mapperDirPath, 'url-mapping.json')
|
|
53
|
+
this.failsPath = path.join(this.mapperDirPath, 'fail.json')
|
|
54
|
+
this.assets = helper.readFileSync(path.join(this.assetsFolderPath, this.assetConfig.fileName))
|
|
55
|
+
this.environment = helper.readFileSync(this.environmentPath)
|
|
56
|
+
if (fs.existsSync(this.uidMapperPath)) {
|
|
57
|
+
this.uidMapping = helper.readFileSync(this.uidMapperPath)
|
|
55
58
|
}
|
|
56
|
-
if (fs.existsSync(
|
|
57
|
-
|
|
59
|
+
if (fs.existsSync(this.urlMapperPath)) {
|
|
60
|
+
this.urlMapping = helper.readFileSync(this.urlMapperPath)
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
mkdirp.sync(mapperDirPath)
|
|
63
|
+
mkdirp.sync(this.mapperDirPath)
|
|
61
64
|
|
|
62
65
|
return new Promise(function (resolve, reject) {
|
|
63
66
|
if (self.assets === undefined || _.isEmpty(self.assets)) {
|
|
64
|
-
addlogs(config, 'No Assets Found', 'success')
|
|
65
|
-
return resolve({ empty: true })
|
|
67
|
+
addlogs(self.config, 'No Assets Found', 'success')
|
|
68
|
+
return resolve({ empty: true })
|
|
66
69
|
}
|
|
67
|
-
|
|
68
|
-
let batches = []
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
|
|
71
|
+
let batches = []
|
|
72
|
+
let assetUids = Object.keys(self.assets)
|
|
73
|
+
|
|
74
|
+
for (let i = 0; i < assetUids.length; i += self.assetBatchLimit) {
|
|
75
|
+
batches.push(assetUids.slice(i, i + self.assetBatchLimit))
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
return self
|
|
@@ -81,405 +86,395 @@ importAssets.prototype = {
|
|
|
81
86
|
function (assetUid) {
|
|
82
87
|
if (self.uidMapping.hasOwnProperty(assetUid)) {
|
|
83
88
|
addlogs(
|
|
84
|
-
config,
|
|
89
|
+
self.config,
|
|
85
90
|
'Skipping upload of asset: ' + assetUid + '. Its mapped to: ' + self.uidMapping[assetUid],
|
|
86
|
-
'success'
|
|
87
|
-
)
|
|
91
|
+
'success'
|
|
92
|
+
)
|
|
88
93
|
// the asset has been already imported
|
|
89
|
-
return
|
|
94
|
+
return void 0
|
|
90
95
|
}
|
|
91
|
-
let currentAssetFolderPath = path.join(assetsFolderPath, assetUid)
|
|
96
|
+
let currentAssetFolderPath = path.join(self.assetsFolderPath, assetUid)
|
|
92
97
|
if (fs.existsSync(currentAssetFolderPath)) {
|
|
93
98
|
// if this is true, means, the exported asset data is versioned
|
|
94
99
|
// hence, upload each asset with its version
|
|
95
|
-
if (config.versioning) {
|
|
100
|
+
if (self.config.versioning) {
|
|
96
101
|
return self
|
|
97
102
|
.uploadVersionedAssets(assetUid, currentAssetFolderPath)
|
|
98
|
-
.then(function () {
|
|
99
|
-
// empty function
|
|
100
|
-
})
|
|
101
103
|
.catch(function (error) {
|
|
102
|
-
addlogs(config, (chalk.red('Asset upload failed \n' + error), 'error'))
|
|
103
|
-
})
|
|
104
|
+
addlogs(self.config, (chalk.red('Asset upload failed \n' + error), 'error'))
|
|
105
|
+
})
|
|
104
106
|
}
|
|
105
|
-
|
|
106
|
-
let uidContainer = {}
|
|
107
|
-
let urlContainer = {}
|
|
107
|
+
|
|
108
|
+
let uidContainer = {}
|
|
109
|
+
let urlContainer = {}
|
|
110
|
+
let assetPath = path.resolve(currentAssetFolderPath, self.assets[assetUid].filename)
|
|
111
|
+
|
|
108
112
|
if (self.assets[assetUid].parent_uid && typeof self.assets[assetUid].parent_uid === 'string') {
|
|
109
113
|
if (self.mappedFolderUids.hasOwnProperty(self.assets[assetUid].parent_uid)) {
|
|
110
|
-
self.assets[assetUid].parent_uid = self.mappedFolderUids[self.assets[assetUid].parent_uid]
|
|
114
|
+
self.assets[assetUid].parent_uid = self.mappedFolderUids[self.assets[assetUid].parent_uid]
|
|
111
115
|
} else {
|
|
112
116
|
addlogs(
|
|
113
|
-
config,
|
|
114
|
-
|
|
115
|
-
'error'
|
|
116
|
-
)
|
|
117
|
+
self.config,
|
|
118
|
+
self.assets[assetUid].parent_uid + " parent_uid was not found! Thus, setting it as 'null'",
|
|
119
|
+
'error'
|
|
120
|
+
)
|
|
117
121
|
}
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
return self
|
|
121
125
|
.uploadAsset(assetPath, self.assets[assetUid], uidContainer, urlContainer)
|
|
122
126
|
.then(async function () {
|
|
123
|
-
self.uidMapping[assetUid] = uidContainer[assetUid]
|
|
124
|
-
self.urlMapping[self.assets[assetUid].url] = urlContainer[self.assets[assetUid].url]
|
|
127
|
+
self.uidMapping[assetUid] = uidContainer[assetUid]
|
|
128
|
+
self.urlMapping[self.assets[assetUid].url] = urlContainer[self.assets[assetUid].url]
|
|
125
129
|
|
|
126
|
-
if (config.entriesPublish && self.assets[assetUid].publish_details.length > 0) {
|
|
127
|
-
let assetsUid = uidContainer[assetUid]
|
|
130
|
+
if (self.config.entriesPublish && self.assets[assetUid].publish_details.length > 0) {
|
|
131
|
+
let assetsUid = uidContainer[assetUid]
|
|
128
132
|
try {
|
|
129
|
-
return await self.publish(assetsUid, self.assets[assetUid])
|
|
133
|
+
return await self.publish(assetsUid, self.assets[assetUid])
|
|
130
134
|
} catch (error) {
|
|
131
|
-
return error
|
|
135
|
+
return error
|
|
132
136
|
}
|
|
133
137
|
}
|
|
134
138
|
// assetUid has been successfully uploaded
|
|
135
139
|
// log them onto /mapper/assets/success.json
|
|
136
140
|
})
|
|
137
141
|
.catch(function (error) {
|
|
138
|
-
addlogs(config, chalk.red('Asset upload failed \n' + error, 'error'))
|
|
139
|
-
return error
|
|
142
|
+
addlogs(self.config, chalk.red('Asset upload failed \n' + error, 'error'))
|
|
143
|
+
return error
|
|
140
144
|
// asset failed to upload
|
|
141
145
|
// log them onto /mapper/assets/fail.json
|
|
142
|
-
})
|
|
146
|
+
})
|
|
143
147
|
}
|
|
144
|
-
addlogs(config, currentAssetFolderPath + ' does not exist!', 'error')
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
concurrency: 1,
|
|
148
|
+
addlogs(self.config, currentAssetFolderPath + ' does not exist!', 'error')
|
|
148
149
|
},
|
|
150
|
+
{ concurrency: self.assetConfig.assetBatchLimit }
|
|
149
151
|
).then(function () {
|
|
150
|
-
helper.
|
|
151
|
-
helper.
|
|
152
|
+
helper.writeFileSync(self.uidMapperPath, self.uidMapping)
|
|
153
|
+
helper.writeFileSync(self.urlMapperPath, self.urlMapping)
|
|
152
154
|
// completed uploading assets
|
|
153
|
-
addlogs(config, 'Completed asset import of batch no: ' + (index + 1), 'success')
|
|
154
|
-
return index + 1
|
|
155
|
+
addlogs(self.config, 'Completed asset import of batch no: ' + (index + 1), 'success')
|
|
156
|
+
return index + 1
|
|
155
157
|
// TODO: if there are failures, retry
|
|
156
|
-
})
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
concurrency: 1,
|
|
158
|
+
})
|
|
160
159
|
},
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
.
|
|
175
|
-
|
|
176
|
-
|
|
160
|
+
{ concurrency: 1 }
|
|
161
|
+
).then(function () {
|
|
162
|
+
let numberOfSuccessfulAssetUploads = Object.keys(self.uidMapping).length;
|
|
163
|
+
if (numberOfSuccessfulAssetUploads > 0) {
|
|
164
|
+
addlogs(
|
|
165
|
+
self.config,
|
|
166
|
+
chalk.green(numberOfSuccessfulAssetUploads + ' assets uploaded successfully!'),
|
|
167
|
+
'success',
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
// TODO: if there are failures, retry
|
|
171
|
+
return resolve();
|
|
172
|
+
}).catch(function (error) {
|
|
173
|
+
addlogs(self.config, error, 'error')
|
|
174
|
+
return reject(error)
|
|
175
|
+
})
|
|
176
|
+
}).catch(function (error) {
|
|
177
|
+
addlogs(self.config, error, 'error')
|
|
178
|
+
return reject(error)
|
|
177
179
|
})
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
uploadVersionedAssets: function (uid, assetFolderPath) {
|
|
184
|
-
let self = this;
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
uploadVersionedAssets(uid, assetFolderPath) {
|
|
184
|
+
let self = this
|
|
185
185
|
return new Promise(function (resolve, reject) {
|
|
186
|
-
let versionedAssetMetadata = helper.
|
|
186
|
+
let versionedAssetMetadata = helper.readFileSync(path.join(assetFolderPath, '_contentstack_' + uid + '.json'))
|
|
187
187
|
// using last version, find asset's parent
|
|
188
|
+
let lastVersion = versionedAssetMetadata[versionedAssetMetadata.length - 1]
|
|
188
189
|
|
|
189
|
-
let lastVersion = versionedAssetMetadata[versionedAssetMetadata.length - 1];
|
|
190
190
|
if (typeof lastVersion.parent_uid === 'string') {
|
|
191
191
|
if (self.mappedFolderUids.hasOwnProperty(lastVersion.parent_uid)) {
|
|
192
192
|
// update each version of that asset with the last version's parent_uid
|
|
193
193
|
versionedAssetMetadata.forEach(function (assetMetadata) {
|
|
194
|
-
assetMetadata.parent_uid = self.mappedFolderUids[lastVersion.parent_uid]
|
|
195
|
-
})
|
|
194
|
+
assetMetadata.parent_uid = self.mappedFolderUids[lastVersion.parent_uid]
|
|
195
|
+
})
|
|
196
196
|
} else {
|
|
197
|
-
addlogs(config, (lastVersion.parent_uid + " parent_uid was not found! Thus, setting it as 'null'", 'error'));
|
|
197
|
+
addlogs(self.config, (lastVersion.parent_uid + " parent_uid was not found! Thus, setting it as 'null'", 'error'));
|
|
198
198
|
versionedAssetMetadata.forEach(function (assetMetadata) {
|
|
199
|
-
assetMetadata.parent_uid = null
|
|
200
|
-
})
|
|
199
|
+
assetMetadata.parent_uid = null
|
|
200
|
+
})
|
|
201
201
|
}
|
|
202
202
|
}
|
|
203
|
-
let counter = 0
|
|
204
|
-
let
|
|
205
|
-
let
|
|
206
|
-
let
|
|
203
|
+
let counter = 0
|
|
204
|
+
let uidContainer = {}
|
|
205
|
+
let urlContainer = {}
|
|
206
|
+
let filesStreamed = []
|
|
207
|
+
|
|
207
208
|
return Promise.map(
|
|
208
209
|
versionedAssetMetadata,
|
|
209
210
|
function () {
|
|
210
|
-
let assetMetadata = versionedAssetMetadata[counter]
|
|
211
|
-
let assetPath = path.join(assetFolderPath, assetMetadata.filename)
|
|
211
|
+
let assetMetadata = versionedAssetMetadata[counter]
|
|
212
|
+
let assetPath = path.join(assetFolderPath, assetMetadata.filename)
|
|
213
|
+
|
|
212
214
|
if (++counter === 1) {
|
|
213
215
|
return self
|
|
214
216
|
.uploadAsset(assetPath, assetMetadata, uidContainer, urlContainer)
|
|
215
217
|
.then(function () {
|
|
216
|
-
filesStreamed.push(assetMetadata.filename)
|
|
218
|
+
filesStreamed.push(assetMetadata.filename)
|
|
219
|
+
}).catch((error) => {
|
|
220
|
+
addlogs(self.config, error, 'error')
|
|
221
|
+
reject(error)
|
|
217
222
|
})
|
|
218
|
-
.catch(reject);
|
|
219
223
|
}
|
|
224
|
+
|
|
220
225
|
return self
|
|
221
226
|
.updateAsset(assetPath, assetMetadata, filesStreamed, uidContainer, urlContainer)
|
|
222
227
|
.then(function () {
|
|
223
228
|
filesStreamed.push(assetMetadata.filename);
|
|
229
|
+
}).catch((error) => {
|
|
230
|
+
addlogs(self.config, error, 'error')
|
|
224
231
|
})
|
|
225
|
-
.catch((_error) => {
|
|
226
|
-
// empty function
|
|
227
|
-
});
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
concurrency: 1,
|
|
231
232
|
},
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
updateAsset
|
|
233
|
+
{ concurrency: self.assetConfig.uploadAssetsConcurrency }
|
|
234
|
+
).then(function () {
|
|
235
|
+
self.uidMapping[uid] = uidContainer[uid]
|
|
236
|
+
for (let url in urlContainer) {
|
|
237
|
+
self.urlMapping[url] = urlContainer[url]
|
|
238
|
+
}
|
|
239
|
+
// completed uploading all the versions of the asset
|
|
240
|
+
return resolve()
|
|
241
|
+
}).catch(function (error) {
|
|
242
|
+
// failed to upload asset
|
|
243
|
+
// write it on fail logs, but do not stop the process
|
|
244
|
+
addlogs(self.config, chalk.red('Failed to upload asset\n' + error), 'error')
|
|
245
|
+
return resolve()
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
updateAsset(assetPath, metadata, filesStreamed, _uidContainer, urlContainer) {
|
|
251
|
+
const self = this
|
|
250
252
|
return new Promise(function (resolve, reject) {
|
|
251
|
-
let requestOption = {}
|
|
253
|
+
let requestOption = {}
|
|
252
254
|
if (filesStreamed && filesStreamed.indexOf(metadata.filename) !== -1) {
|
|
253
|
-
addlogs(config, 'Skipping re-upload/streaming of ' + metadata.uid + '/' + metadata.filename, 'success')
|
|
254
|
-
requestOption.formData = {}
|
|
255
|
-
return resolve()
|
|
255
|
+
addlogs(self.config, 'Skipping re-upload/streaming of ' + metadata.uid + '/' + metadata.filename, 'success')
|
|
256
|
+
requestOption.formData = {}
|
|
257
|
+
return resolve()
|
|
256
258
|
}
|
|
257
259
|
|
|
258
|
-
addlogs(config, 'Streaming: ' + metadata.uid + '/' + metadata.filename, 'success');
|
|
259
|
-
requestOption.formData = {}
|
|
260
|
+
addlogs(self.config, 'Streaming: ' + metadata.uid + '/' + metadata.filename, 'success');
|
|
261
|
+
requestOption.formData = {}
|
|
260
262
|
|
|
261
263
|
if (metadata.hasOwnProperty('parent_uid') && typeof metadata.parent_uid === 'string') {
|
|
262
|
-
requestOption.formData['asset[parent_uid]'] = metadata.parent_uid
|
|
264
|
+
requestOption.formData['asset[parent_uid]'] = metadata.parent_uid
|
|
263
265
|
}
|
|
264
266
|
|
|
265
267
|
if (metadata.hasOwnProperty('description') && typeof metadata.description === 'string') {
|
|
266
|
-
requestOption.formData['asset[description]'] = metadata.description
|
|
268
|
+
requestOption.formData['asset[description]'] = metadata.description
|
|
267
269
|
}
|
|
268
270
|
|
|
269
271
|
if (metadata.hasOwnProperty('tags') && Array.isArray(metadata.tags)) {
|
|
270
|
-
requestOption.formData['asset[tags]'] = metadata.tags
|
|
272
|
+
requestOption.formData['asset[tags]'] = metadata.tags
|
|
271
273
|
}
|
|
272
274
|
|
|
273
275
|
if (metadata.hasOwnProperty('title') && typeof metadata.title === 'string') {
|
|
274
|
-
requestOption.formData['asset[title]'] = metadata.title
|
|
276
|
+
requestOption.formData['asset[title]'] = metadata.title
|
|
275
277
|
}
|
|
276
278
|
|
|
277
279
|
return upload(requestOption, assetPath)
|
|
278
280
|
.then(function (response) {
|
|
279
|
-
urlContainer[metadata.url] = response.url
|
|
280
|
-
return resolve()
|
|
281
|
+
urlContainer[metadata.url] = response.url
|
|
282
|
+
return resolve()
|
|
283
|
+
}).catch(function (error) {
|
|
284
|
+
addlogs(self.config, error, 'error')
|
|
285
|
+
return reject(error)
|
|
281
286
|
})
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
uploadAsset: function (assetPath, metadata, uidContainer, urlContainer) {
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
uploadAsset(assetPath, metadata, uidContainer, urlContainer) {
|
|
291
|
+
const self = this
|
|
288
292
|
return new Promise(function (resolve, reject) {
|
|
289
|
-
let requestOption = {}
|
|
293
|
+
let requestOption = {}
|
|
290
294
|
|
|
291
295
|
if (metadata.hasOwnProperty('parent_uid') && typeof metadata.parent_uid === 'string') {
|
|
292
|
-
requestOption.parent_uid = metadata.parent_uid
|
|
296
|
+
requestOption.parent_uid = metadata.parent_uid
|
|
293
297
|
}
|
|
294
298
|
|
|
295
299
|
if (metadata.hasOwnProperty('description') && typeof metadata.description === 'string') {
|
|
296
|
-
requestOption.description = metadata.description
|
|
300
|
+
requestOption.description = metadata.description
|
|
297
301
|
}
|
|
298
302
|
|
|
299
303
|
// eslint-disable-next-line no-prototype-builtins
|
|
300
304
|
if (metadata.hasOwnProperty('tags') && Array.isArray(metadata.tags)) {
|
|
301
|
-
requestOption.tags = metadata.tags
|
|
305
|
+
requestOption.tags = metadata.tags
|
|
302
306
|
}
|
|
303
307
|
|
|
304
308
|
if (metadata.hasOwnProperty('title') && typeof metadata.title === 'string') {
|
|
305
|
-
requestOption.title = metadata.title
|
|
309
|
+
requestOption.title = metadata.title
|
|
306
310
|
}
|
|
307
311
|
return upload(requestOption, assetPath)
|
|
308
312
|
.then(function (response) {
|
|
309
|
-
uidContainer[metadata.uid] = response.uid
|
|
310
|
-
urlContainer[metadata.url] = response.url
|
|
311
|
-
return resolve()
|
|
313
|
+
uidContainer[metadata.uid] = response.uid
|
|
314
|
+
urlContainer[metadata.url] = response.url
|
|
315
|
+
return resolve()
|
|
316
|
+
}).catch(function (error) {
|
|
317
|
+
addlogs(self.config, error, 'error')
|
|
318
|
+
return reject(error)
|
|
312
319
|
})
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
},
|
|
319
|
-
|
|
320
|
-
importFolders: function () {
|
|
321
|
-
let self = this;
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
importFolders() {
|
|
324
|
+
let self = this
|
|
322
325
|
return new Promise(function (resolve, reject) {
|
|
323
|
-
let mappedFolderPath = path.resolve(config.data, 'mapper', 'assets', 'folder-mapping.json')
|
|
324
|
-
self.folderDetails = helper.
|
|
326
|
+
let mappedFolderPath = path.resolve(self.config.data, 'mapper', 'assets', 'folder-mapping.json')
|
|
327
|
+
self.folderDetails = helper.readFileSync(path.resolve(self.assetsFolderPath, 'folders.json'))
|
|
325
328
|
|
|
326
329
|
if (_.isEmpty(self.folderDetails)) {
|
|
327
|
-
addlogs(config, 'No folders were found at: ' + path.join(assetsFolderPath, 'folders.json'), 'success')
|
|
328
|
-
return resolve()
|
|
330
|
+
addlogs(self.config, 'No folders were found at: ' + path.join(self.assetsFolderPath, 'folders.json'), 'success')
|
|
331
|
+
return resolve()
|
|
329
332
|
}
|
|
330
|
-
let tree = self.buildTree(_.cloneDeep(self.folderDetails))
|
|
331
|
-
let createdFolders = {}
|
|
332
|
-
let createdFolderUids = []
|
|
333
|
+
let tree = self.buildTree(_.cloneDeep(self.folderDetails))
|
|
334
|
+
let createdFolders = {}
|
|
335
|
+
let createdFolderUids = []
|
|
333
336
|
// if a few folders have already been created, skip re-creating them
|
|
334
337
|
if (fs.existsSync(mappedFolderPath)) {
|
|
335
|
-
createdFolders = helper.
|
|
338
|
+
createdFolders = helper.readFileSync(mappedFolderPath)
|
|
336
339
|
// check if the read file has mapped objects
|
|
337
340
|
if (_.isPlainObject(createdFolders)) {
|
|
338
|
-
createdFolderUids = Object.keys(createdFolders)
|
|
341
|
+
createdFolderUids = Object.keys(createdFolders)
|
|
339
342
|
}
|
|
340
343
|
}
|
|
341
|
-
self.buildFolderReqObjs(createdFolderUids, tree, null)
|
|
342
|
-
let idx = 0
|
|
344
|
+
self.buildFolderReqObjs(createdFolderUids, tree, null)
|
|
345
|
+
let idx = 0
|
|
343
346
|
return Promise.map(
|
|
344
347
|
self.folderBucket,
|
|
345
348
|
function () {
|
|
346
349
|
let folder = self.folderBucket[idx];
|
|
347
350
|
if (createdFolders.hasOwnProperty(folder.json.asset.parent_uid)) {
|
|
348
351
|
// replace old uid with new
|
|
349
|
-
folder.json.asset.parent_uid = createdFolders[folder.json.asset.parent_uid]
|
|
352
|
+
folder.json.asset.parent_uid = createdFolders[folder.json.asset.parent_uid]
|
|
350
353
|
}
|
|
351
|
-
return client
|
|
352
|
-
.stack({ api_key: config.target_stack, management_token: config.management_token })
|
|
354
|
+
return self.client
|
|
355
|
+
.stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
|
|
353
356
|
.asset()
|
|
354
357
|
.folder()
|
|
355
358
|
.create(folder.json)
|
|
356
359
|
.then((response) => {
|
|
357
|
-
addlogs(config, "Created folder: '" + folder.json.asset.name + "'", 'success')
|
|
360
|
+
addlogs(self.config, "Created folder: '" + folder.json.asset.name + "'", 'success')
|
|
358
361
|
// assigning newUid to oldUid
|
|
359
|
-
createdFolders[folder.oldUid] = response.uid
|
|
360
|
-
helper.
|
|
361
|
-
idx
|
|
362
|
-
})
|
|
363
|
-
|
|
364
|
-
let error = JSON.parse(err.message);
|
|
362
|
+
createdFolders[folder.oldUid] = response.uid
|
|
363
|
+
helper.writeFileSync(mappedFolderPath, createdFolders)
|
|
364
|
+
idx++
|
|
365
|
+
}).catch(function (err) {
|
|
366
|
+
let error = JSON.parse(err.message)
|
|
365
367
|
if (error.errors.authorization || error.errors.api_key) {
|
|
366
|
-
addlogs(config, chalk.red('Api_key or management_token is not valid'), 'error')
|
|
367
|
-
return reject(error)
|
|
368
|
+
addlogs(self.config, chalk.red('Api_key or management_token is not valid'), 'error')
|
|
369
|
+
return reject(error)
|
|
368
370
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
concurrency: 1,
|
|
371
|
+
|
|
372
|
+
addlogs(self.config, err, 'error')
|
|
373
|
+
return error
|
|
374
|
+
})
|
|
374
375
|
},
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
})
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
376
|
+
{ concurrency: self.assetConfig.importFoldersConcurrency }
|
|
377
|
+
).then(function () {
|
|
378
|
+
self.mappedFolderUids = helper.readFileSync(mappedFolderPath)
|
|
379
|
+
// completed creating folders
|
|
380
|
+
return resolve()
|
|
381
|
+
}).catch(function (error) {
|
|
382
|
+
addlogs(self.config, error, 'error')
|
|
383
|
+
return reject(error)
|
|
384
|
+
})
|
|
385
|
+
})
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
buildFolderReqObjs(createdFolderUids, tree, parent_uid) {
|
|
389
|
+
let self = this
|
|
388
390
|
for (let leaf in tree) {
|
|
389
391
|
// if the folder is already created, skip
|
|
390
392
|
if (createdFolderUids.indexOf(leaf) !== -1) {
|
|
391
|
-
continue
|
|
393
|
+
continue
|
|
392
394
|
}
|
|
393
|
-
let folderObj = _.find(self.folderDetails,
|
|
394
|
-
return folder.uid === leaf;
|
|
395
|
-
});
|
|
395
|
+
let folderObj = _.find(self.folderDetails, { uid: leaf })
|
|
396
396
|
let requestOption = {
|
|
397
397
|
json: {
|
|
398
398
|
asset: {
|
|
399
399
|
name: folderObj.name,
|
|
400
|
-
parent_uid: parent_uid || null
|
|
401
|
-
}
|
|
400
|
+
parent_uid: parent_uid || null
|
|
401
|
+
}
|
|
402
402
|
},
|
|
403
|
-
oldUid: leaf
|
|
404
|
-
}
|
|
405
|
-
self.folderBucket.push(requestOption)
|
|
403
|
+
oldUid: leaf
|
|
404
|
+
}
|
|
405
|
+
self.folderBucket.push(requestOption)
|
|
406
406
|
if (Object.keys(tree[leaf]).length > 0) {
|
|
407
|
-
self.buildFolderReqObjs(createdFolderUids, tree[leaf], leaf)
|
|
407
|
+
self.buildFolderReqObjs(createdFolderUids, tree[leaf], leaf)
|
|
408
408
|
}
|
|
409
409
|
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
buildTree(coll) {
|
|
413
|
+
let tree = {}
|
|
413
414
|
for (let i = 0; i < coll.length; i++) {
|
|
414
|
-
// ! hasOwnProperty('parent_uid') added, as some folders do not have `parent_uid`
|
|
415
415
|
if (coll[i].parent_uid === null || !coll[i].hasOwnProperty('parent_uid')) {
|
|
416
|
-
tree[coll[i].uid] = {}
|
|
417
|
-
coll.splice(i, 1)
|
|
418
|
-
i
|
|
416
|
+
tree[coll[i].uid] = {}
|
|
417
|
+
coll.splice(i, 1)
|
|
418
|
+
i--
|
|
419
419
|
}
|
|
420
420
|
}
|
|
421
|
-
this.findBranches(tree, _.keys(tree), coll)
|
|
422
|
-
return tree
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
|
|
421
|
+
this.findBranches(tree, _.keys(tree), coll)
|
|
422
|
+
return tree
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
findBranches(tree, branches, coll) {
|
|
426
|
+
let self = this
|
|
426
427
|
_.forEach(branches, (branch) => {
|
|
427
|
-
for (
|
|
428
|
-
if (branch ===
|
|
429
|
-
let childUid =
|
|
430
|
-
tree[branch][childUid] = {}
|
|
431
|
-
self.findBranches(tree[branch], [childUid], coll)
|
|
428
|
+
for (const element of coll) {
|
|
429
|
+
if (branch === element.parent_uid) {
|
|
430
|
+
let childUid = element.uid
|
|
431
|
+
tree[branch][childUid] = {}
|
|
432
|
+
self.findBranches(tree[branch], [childUid], coll)
|
|
432
433
|
}
|
|
433
434
|
}
|
|
434
|
-
})
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
let envId = []
|
|
439
|
-
let
|
|
440
|
-
|
|
441
|
-
let requestObject = {
|
|
442
|
-
json: {
|
|
443
|
-
asset: {},
|
|
444
|
-
},
|
|
445
|
-
};
|
|
435
|
+
})
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
publish(assetUid, assetObject) {
|
|
439
|
+
let envId = []
|
|
440
|
+
let self = this
|
|
441
|
+
let locales = []
|
|
442
|
+
let requestObject = { json: { asset: {} } }
|
|
446
443
|
|
|
447
444
|
return new Promise(function (resolve, reject) {
|
|
448
445
|
_.forEach(assetObject.publish_details, function (pubObject) {
|
|
449
446
|
if (self.environment.hasOwnProperty(pubObject.environment)) {
|
|
450
|
-
envId.push(self.environment[pubObject.environment].name)
|
|
451
|
-
let idx = _.indexOf(locales, pubObject.locale)
|
|
447
|
+
envId.push(self.environment[pubObject.environment].name)
|
|
448
|
+
let idx = _.indexOf(locales, pubObject.locale)
|
|
452
449
|
if (idx === -1) {
|
|
453
|
-
locales.push(pubObject.locale)
|
|
450
|
+
locales.push(pubObject.locale)
|
|
454
451
|
}
|
|
455
452
|
}
|
|
456
|
-
})
|
|
457
|
-
requestObject.json.asset.environments = envId
|
|
458
|
-
requestObject.json.asset.locales = locales
|
|
459
|
-
return client
|
|
460
|
-
.stack({ api_key: config.target_stack, management_token: config.management_token })
|
|
453
|
+
})
|
|
454
|
+
requestObject.json.asset.environments = envId
|
|
455
|
+
requestObject.json.asset.locales = locales
|
|
456
|
+
return self.client
|
|
457
|
+
.stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
|
|
461
458
|
.asset(assetUid)
|
|
462
459
|
.publish({ publishDetails: requestObject.json.asset })
|
|
463
460
|
.then(function () {
|
|
464
|
-
addlogs(config, 'Asset ' + assetUid + ' published successfully', 'success')
|
|
465
|
-
return resolve()
|
|
466
|
-
})
|
|
467
|
-
.catch(function (err) {
|
|
461
|
+
addlogs(self.config, 'Asset ' + assetUid + ' published successfully', 'success')
|
|
462
|
+
return resolve()
|
|
463
|
+
}).catch(function (err) {
|
|
468
464
|
if (err && err.message) {
|
|
469
|
-
let error
|
|
465
|
+
let error
|
|
470
466
|
try {
|
|
471
|
-
error = JSON.parse(err.message)
|
|
467
|
+
error = JSON.parse(err.message)
|
|
472
468
|
} catch (cError) {
|
|
473
|
-
error = { errorMessage: err.message }
|
|
469
|
+
error = { errorMessage: err.message }
|
|
474
470
|
}
|
|
475
471
|
|
|
476
|
-
addlogs(config, chalk.red('Asset ' + assetUid + ' not published, ' + error.errorMessage), 'error')
|
|
477
|
-
return reject(err)
|
|
472
|
+
addlogs(self.config, chalk.red('Asset ' + assetUid + ' not published, ' + error.errorMessage), 'error')
|
|
473
|
+
return reject(err)
|
|
478
474
|
}
|
|
479
|
-
return reject(err);
|
|
480
|
-
});
|
|
481
|
-
});
|
|
482
|
-
},
|
|
483
|
-
};
|
|
484
475
|
|
|
485
|
-
|
|
476
|
+
return reject(err)
|
|
477
|
+
})
|
|
478
|
+
})
|
|
479
|
+
}
|
|
480
|
+
}
|