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