@contentstack/cli-cm-export 1.2.3 → 1.4.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/LICENSE +1 -1
- package/README.md +74 -22
- package/oclif.manifest.json +128 -1
- package/package.json +15 -14
- package/src/app.js +30 -32
- package/src/commands/cm/stacks/export.js +3 -5
- package/src/config/default.js +5 -0
- package/src/lib/export/assets.js +201 -194
- package/src/lib/export/custom-roles.js +7 -16
- package/src/lib/export/entries.js +6 -1
- package/src/lib/export/environments.js +6 -7
- package/src/lib/export/extensions.js +6 -7
- package/src/lib/export/global-fields.js +12 -12
- package/src/lib/export/labels.js +8 -11
- package/src/lib/export/locales.js +8 -6
- package/src/lib/export/marketplace-apps.js +102 -102
- package/src/lib/export/stack.js +78 -84
- package/src/lib/export/webhooks.js +4 -9
- package/src/lib/export/workflows.js +11 -11
- package/src/lib/util/helper.js +16 -25
- package/src/lib/util/login.js +59 -55
- package/src/lib/util/marketplace-app-helper.js +3 -40
- package/src/lib/util/setup-branches.js +14 -12
- package/src/lib/util/contentstack-management-sdk.js +0 -39
package/src/lib/export/assets.js
CHANGED
|
@@ -17,54 +17,52 @@ const helper = require('../util/helper');
|
|
|
17
17
|
const { addlogs } = require('../util/log');
|
|
18
18
|
|
|
19
19
|
let config = require('../../config/default');
|
|
20
|
-
const stack = require('../util/contentstack-management-sdk');
|
|
21
20
|
|
|
22
21
|
module.exports = class ExportAssets {
|
|
23
|
-
config
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.config = config
|
|
37
|
-
this.folderData = []
|
|
38
|
-
this.assetContents = {}
|
|
39
|
-
this.assetDownloadRetry = {}
|
|
40
|
-
this.assetDownloadRetryLimit = 3
|
|
41
|
-
this.invalidKeys = this.assetConfig.invalidKeys
|
|
42
|
-
this.bLimit = this.assetConfig.batchLimit || 15
|
|
43
|
-
this.vLimit = this.assetConfig.downloadLimit || config.fetchConcurrency || 3
|
|
22
|
+
config;
|
|
23
|
+
bLimit;
|
|
24
|
+
vLimit;
|
|
25
|
+
invalidKeys;
|
|
26
|
+
folderJSONPath;
|
|
27
|
+
folderData = [];
|
|
28
|
+
assetsFolderPath;
|
|
29
|
+
assetContents = {};
|
|
30
|
+
httpClient = HttpClient.create();
|
|
31
|
+
assetConfig = config.modules.assets;
|
|
32
|
+
|
|
33
|
+
constructor(exportConfig, stackAPIClient) {
|
|
34
|
+
this.stackAPIClient = stackAPIClient;
|
|
35
|
+
this.config = _.merge(config, exportConfig);
|
|
36
|
+
this.folderData = [];
|
|
37
|
+
this.assetContents = {};
|
|
38
|
+
this.assetDownloadRetry = {};
|
|
39
|
+
this.assetDownloadRetryLimit = 3;
|
|
40
|
+
this.invalidKeys = this.assetConfig.invalidKeys;
|
|
41
|
+
this.bLimit = this.assetConfig.batchLimit || 15;
|
|
42
|
+
this.vLimit = this.assetConfig.downloadLimit || this.config.fetchConcurrency || 3;
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
start() {
|
|
47
|
-
const self = this
|
|
48
|
-
this.assetsFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.assetConfig.dirName)
|
|
49
|
-
this.assetContentsFile = path.resolve(this.assetsFolderPath, 'assets.json')
|
|
50
|
-
this.folderJSONPath = path.resolve(this.assetsFolderPath, 'folders.json')
|
|
51
|
-
self.client = stack.Client(this.config)
|
|
46
|
+
const self = this;
|
|
47
|
+
this.assetsFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.assetConfig.dirName);
|
|
48
|
+
this.assetContentsFile = path.resolve(this.assetsFolderPath, 'assets.json');
|
|
49
|
+
this.folderJSONPath = path.resolve(this.assetsFolderPath, 'folders.json');
|
|
52
50
|
|
|
53
|
-
addlogs(this.config, 'Starting assets export', 'success')
|
|
51
|
+
addlogs(this.config, 'Starting assets export', 'success');
|
|
54
52
|
|
|
55
53
|
// Create asset folder
|
|
56
|
-
mkdirp.sync(this.assetsFolderPath)
|
|
54
|
+
mkdirp.sync(this.assetsFolderPath);
|
|
57
55
|
|
|
58
56
|
return new Promise(function (resolve, reject) {
|
|
59
57
|
// TBD: getting all the assets should have optimized
|
|
60
58
|
return self
|
|
61
59
|
.getAssetCount()
|
|
62
60
|
.then(function (count) {
|
|
63
|
-
const assetBatches = []
|
|
61
|
+
const assetBatches = [];
|
|
64
62
|
|
|
65
63
|
if (typeof count !== 'number' || count === 0) {
|
|
66
|
-
addlogs(self.config, 'No assets found', 'success')
|
|
67
|
-
return resolve()
|
|
64
|
+
addlogs(self.config, 'No assets found', 'success');
|
|
65
|
+
return resolve();
|
|
68
66
|
}
|
|
69
67
|
for (let i = 0; i <= count; i += self.bLimit) {
|
|
70
68
|
assetBatches.push(i);
|
|
@@ -84,76 +82,89 @@ module.exports = class ExportAssets {
|
|
|
84
82
|
.getVersionedAssetJSON(assetJSON.uid, assetJSON._version)
|
|
85
83
|
.then(function () {
|
|
86
84
|
self.assetContents[assetJSON.uid] = assetJSON;
|
|
87
|
-
})
|
|
85
|
+
})
|
|
86
|
+
.catch(function (error) {
|
|
88
87
|
addlogs(
|
|
89
88
|
self.config,
|
|
90
89
|
chalk.red('The following asset failed to download\n' + JSON.stringify(assetJSON)),
|
|
91
|
-
)
|
|
92
|
-
addlogs(self.config, error, 'error')
|
|
93
|
-
})
|
|
90
|
+
);
|
|
91
|
+
addlogs(self.config, error, 'error');
|
|
92
|
+
});
|
|
94
93
|
} else {
|
|
95
|
-
return self
|
|
94
|
+
return self
|
|
95
|
+
.downloadAsset(assetJSON)
|
|
96
96
|
.then(function () {
|
|
97
|
-
self.assetContents[assetJSON.uid] = assetJSON
|
|
98
|
-
}).catch((err) => {
|
|
99
|
-
addlogs({ errorCode: (err && err.code), uid: assetJSON.uid }, `Asset download failed - ${assetJSON.uid}`, 'error')
|
|
100
|
-
return err
|
|
97
|
+
self.assetContents[assetJSON.uid] = assetJSON;
|
|
101
98
|
})
|
|
99
|
+
.catch((err) => {
|
|
100
|
+
addlogs(
|
|
101
|
+
{ errorCode: err && err.code, uid: assetJSON.uid },
|
|
102
|
+
`Asset download failed - ${assetJSON.uid}`,
|
|
103
|
+
'error',
|
|
104
|
+
);
|
|
105
|
+
return err;
|
|
106
|
+
});
|
|
102
107
|
}
|
|
103
108
|
},
|
|
104
|
-
{ concurrency: self.vLimit }
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
reject(error)
|
|
109
|
+
{ concurrency: self.vLimit },
|
|
110
|
+
)
|
|
111
|
+
.then(function () {
|
|
112
|
+
addlogs(self.config, 'Batch no ' + (batch + 1) + ' of assets is complete', 'success');
|
|
113
|
+
// helper.writeFileSync(this.assetContentsFile, self.assetContents)
|
|
114
|
+
})
|
|
115
|
+
.catch(function (error) {
|
|
116
|
+
console.log('Error fetch/download the asset', error && error.message);
|
|
117
|
+
addlogs(self.config, 'Asset batch ' + (batch + 1) + ' failed to download', 'error');
|
|
118
|
+
addlogs(self.config, error, 'error');
|
|
119
|
+
});
|
|
116
120
|
})
|
|
121
|
+
.catch(function (error) {
|
|
122
|
+
addlogs(self.config, error, 'error');
|
|
123
|
+
reject(error);
|
|
124
|
+
});
|
|
117
125
|
},
|
|
118
|
-
{ concurrency: self.assetConfig.concurrencyLimit || 1 }
|
|
119
|
-
)
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
self.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
reject(error)
|
|
126
|
+
{ concurrency: self.assetConfig.concurrencyLimit || 1 },
|
|
127
|
+
)
|
|
128
|
+
.then(function () {
|
|
129
|
+
helper.writeFileSync(self.assetContentsFile, self.assetContents);
|
|
130
|
+
|
|
131
|
+
return self
|
|
132
|
+
.exportFolders()
|
|
133
|
+
.then(function () {
|
|
134
|
+
addlogs(self.config, chalk.green('Asset export completed successfully'), 'success');
|
|
135
|
+
return resolve();
|
|
136
|
+
})
|
|
137
|
+
.catch(function (error) {
|
|
138
|
+
addlogs(self.config, error, 'success');
|
|
139
|
+
reject(error);
|
|
140
|
+
});
|
|
141
|
+
})
|
|
142
|
+
.catch(function (error) {
|
|
143
|
+
helper.writeFileSync(self.assetContentsFile, self.assetContents);
|
|
144
|
+
addlogs(
|
|
145
|
+
self.config,
|
|
146
|
+
chalk.red('Asset export failed due to the following errors ' + JSON.stringify(error), 'error'),
|
|
147
|
+
);
|
|
148
|
+
addlogs(self.config, error, 'success');
|
|
149
|
+
reject(error);
|
|
150
|
+
});
|
|
144
151
|
})
|
|
145
|
-
|
|
152
|
+
.catch(function (error) {
|
|
153
|
+
addlogs(self.config, error, 'success');
|
|
154
|
+
reject(error);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
exportFolders() {
|
|
149
|
-
|
|
160
|
+
const self = this;
|
|
150
161
|
return new Promise(function (resolve, reject) {
|
|
151
162
|
return self
|
|
152
163
|
.getAssetCount(true)
|
|
153
164
|
.then(function (fCount) {
|
|
154
165
|
if (fCount === 0) {
|
|
155
166
|
addlogs(self.config, 'No folders were found in the stack!', 'success');
|
|
156
|
-
return resolve()
|
|
167
|
+
return resolve();
|
|
157
168
|
}
|
|
158
169
|
|
|
159
170
|
return self
|
|
@@ -162,62 +173,61 @@ module.exports = class ExportAssets {
|
|
|
162
173
|
// asset folders have been successfully exported
|
|
163
174
|
addlogs(self.config, 'Asset-folders have been successfully exported!', 'success');
|
|
164
175
|
return resolve();
|
|
165
|
-
})
|
|
176
|
+
})
|
|
177
|
+
.catch(function (error) {
|
|
166
178
|
addlogs(self.config, chalk.red('Error while exporting asset-folders!'), 'error');
|
|
167
179
|
return reject(error);
|
|
168
|
-
})
|
|
180
|
+
});
|
|
169
181
|
})
|
|
170
182
|
.catch(function (error) {
|
|
171
|
-
addlogs(self.config, error, 'error')
|
|
183
|
+
addlogs(self.config, error, 'error');
|
|
172
184
|
// error while fetching asset folder count
|
|
173
|
-
return reject(error)
|
|
185
|
+
return reject(error);
|
|
174
186
|
});
|
|
175
187
|
});
|
|
176
188
|
}
|
|
177
189
|
|
|
178
190
|
getFolderJSON(skip, fCount) {
|
|
179
|
-
|
|
191
|
+
const self = this;
|
|
180
192
|
return new Promise(function (resolve, reject) {
|
|
181
193
|
if (typeof skip !== 'number') {
|
|
182
|
-
skip = 0
|
|
194
|
+
skip = 0;
|
|
183
195
|
}
|
|
184
196
|
|
|
185
197
|
if (skip >= fCount) {
|
|
186
|
-
helper.writeFileSync(self.folderJSONPath, self.folderData)
|
|
187
|
-
return resolve()
|
|
198
|
+
helper.writeFileSync(self.folderJSONPath, self.folderData);
|
|
199
|
+
return resolve();
|
|
188
200
|
}
|
|
189
201
|
|
|
190
202
|
const queryRequestObj = {
|
|
191
203
|
skip,
|
|
192
204
|
include_folders: true,
|
|
193
|
-
query: { is_dir: true }
|
|
194
|
-
}
|
|
205
|
+
query: { is_dir: true },
|
|
206
|
+
};
|
|
195
207
|
|
|
196
|
-
self.
|
|
197
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
208
|
+
self.stackAPIClient
|
|
198
209
|
.asset()
|
|
199
210
|
.query(queryRequestObj)
|
|
200
211
|
.find()
|
|
201
212
|
.then((response) => {
|
|
202
|
-
skip += 100
|
|
203
|
-
self.folderData.push(...response.items)
|
|
204
|
-
return self.getFolderJSON(skip, fCount).then(resolve).catch(reject)
|
|
205
|
-
})
|
|
206
|
-
})
|
|
213
|
+
skip += 100;
|
|
214
|
+
self.folderData.push(...response.items);
|
|
215
|
+
return self.getFolderJSON(skip, fCount).then(resolve).catch(reject);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
207
218
|
}
|
|
208
219
|
|
|
209
220
|
getAssetCount(folder) {
|
|
210
|
-
const self = this
|
|
221
|
+
const self = this;
|
|
211
222
|
return new Promise(function (resolve, reject) {
|
|
212
223
|
if (folder && typeof folder === 'boolean') {
|
|
213
|
-
|
|
224
|
+
const queryOptions = {
|
|
214
225
|
skip: 99999990,
|
|
215
226
|
include_count: true,
|
|
216
227
|
include_folders: true,
|
|
217
|
-
query: { is_dir: true }
|
|
218
|
-
}
|
|
219
|
-
self.
|
|
220
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
228
|
+
query: { is_dir: true },
|
|
229
|
+
};
|
|
230
|
+
self.stackAPIClient
|
|
221
231
|
.asset()
|
|
222
232
|
.query(queryOptions)
|
|
223
233
|
.find()
|
|
@@ -228,74 +238,71 @@ module.exports = class ExportAssets {
|
|
|
228
238
|
addlogs(self.config, error, 'error');
|
|
229
239
|
});
|
|
230
240
|
} else {
|
|
231
|
-
|
|
232
|
-
self.
|
|
233
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
241
|
+
const queryOptions = { skip: 99999990, include_count: true };
|
|
242
|
+
self.stackAPIClient
|
|
234
243
|
.asset()
|
|
235
244
|
.query(queryOptions)
|
|
236
245
|
.find()
|
|
237
246
|
.then(({ count }) => resolve(count))
|
|
238
247
|
.catch((error) => {
|
|
239
|
-
addlogs(self.config, error, 'error')
|
|
240
|
-
reject(error)
|
|
241
|
-
})
|
|
248
|
+
addlogs(self.config, error, 'error');
|
|
249
|
+
reject(error);
|
|
250
|
+
});
|
|
242
251
|
}
|
|
243
|
-
})
|
|
252
|
+
});
|
|
244
253
|
}
|
|
245
254
|
|
|
246
255
|
getAssetJSON(skip) {
|
|
247
|
-
const self = this
|
|
256
|
+
const self = this;
|
|
248
257
|
return new Promise(function (resolve, reject) {
|
|
249
258
|
if (typeof skip !== 'number') {
|
|
250
|
-
skip = 0
|
|
259
|
+
skip = 0;
|
|
251
260
|
}
|
|
252
261
|
const queryRequestObj = {
|
|
253
262
|
skip: skip,
|
|
254
263
|
limit: self.bLimit,
|
|
255
264
|
include_publish_details: true,
|
|
256
265
|
except: {
|
|
257
|
-
BASE: self.invalidKeys
|
|
258
|
-
}
|
|
259
|
-
}
|
|
266
|
+
BASE: self.invalidKeys,
|
|
267
|
+
},
|
|
268
|
+
};
|
|
260
269
|
|
|
261
|
-
self.
|
|
262
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
270
|
+
self.stackAPIClient
|
|
263
271
|
.asset()
|
|
264
272
|
.query(queryRequestObj)
|
|
265
273
|
.find()
|
|
266
274
|
.then(({ items }) => resolve(items))
|
|
267
275
|
.catch((error) => {
|
|
268
|
-
addlogs(self.config, error, 'error')
|
|
269
|
-
return reject()
|
|
270
|
-
})
|
|
271
|
-
})
|
|
276
|
+
addlogs(self.config, error, 'error');
|
|
277
|
+
return reject();
|
|
278
|
+
});
|
|
279
|
+
});
|
|
272
280
|
}
|
|
273
281
|
|
|
274
282
|
getVersionedAssetJSON(uid, version, bucket) {
|
|
275
|
-
|
|
276
|
-
|
|
283
|
+
const self = this;
|
|
284
|
+
const assetVersionInfo = bucket || [];
|
|
277
285
|
|
|
278
286
|
return new Promise(function (resolve, reject) {
|
|
279
287
|
if (self.assetDownloadRetry[uid + version] > self.assetDownloadRetryLimit) {
|
|
280
|
-
console.log('Reached max', self.assetDownloadRetry[uid + version])
|
|
281
|
-
return reject(new Error('Asset Max download retry limit exceeded! ' + uid))
|
|
288
|
+
console.log('Reached max', self.assetDownloadRetry[uid + version]);
|
|
289
|
+
return reject(new Error('Asset Max download retry limit exceeded! ' + uid));
|
|
282
290
|
}
|
|
283
291
|
|
|
284
292
|
if (version <= 0) {
|
|
285
293
|
const assetVersionInfoFile = path.resolve(self.assetsFolderPath, uid, '_contentstack_' + uid + '.json');
|
|
286
|
-
helper.writeFileSync(assetVersionInfoFile, assetVersionInfo)
|
|
287
|
-
return resolve()
|
|
294
|
+
helper.writeFileSync(assetVersionInfoFile, assetVersionInfo);
|
|
295
|
+
return resolve();
|
|
288
296
|
}
|
|
289
|
-
|
|
297
|
+
const queryrequestOption = {
|
|
290
298
|
version: version,
|
|
291
299
|
include_publish_details: true,
|
|
292
300
|
except: {
|
|
293
|
-
BASE: self.invalidKeys
|
|
294
|
-
}
|
|
295
|
-
}
|
|
301
|
+
BASE: self.invalidKeys,
|
|
302
|
+
},
|
|
303
|
+
};
|
|
296
304
|
|
|
297
|
-
self.
|
|
298
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
305
|
+
self.stackAPIClient
|
|
299
306
|
.asset(uid)
|
|
300
307
|
.fetch(queryrequestOption)
|
|
301
308
|
.then((versionedAssetJSONResponse) => {
|
|
@@ -305,15 +312,12 @@ module.exports = class ExportAssets {
|
|
|
305
312
|
assetVersionInfo.splice(0, 0, versionedAssetJSONResponse);
|
|
306
313
|
// Remove duplicates
|
|
307
314
|
assetVersionInfo = _.uniqWith(assetVersionInfo, _.isEqual);
|
|
308
|
-
self
|
|
309
|
-
.getVersionedAssetJSON(uid, --version, assetVersionInfo)
|
|
310
|
-
.then(resolve)
|
|
311
|
-
.catch(reject)
|
|
315
|
+
self.getVersionedAssetJSON(uid, --version, assetVersionInfo).then(resolve).catch(reject);
|
|
312
316
|
})
|
|
313
|
-
.catch(reject)
|
|
317
|
+
.catch(reject);
|
|
314
318
|
})
|
|
315
319
|
.catch((error) => {
|
|
316
|
-
addlogs(self.config, error, 'error')
|
|
320
|
+
addlogs(self.config, error, 'error');
|
|
317
321
|
console.log('Error on fetch', error && error.message);
|
|
318
322
|
|
|
319
323
|
if (error.status === 408) {
|
|
@@ -322,37 +326,37 @@ module.exports = class ExportAssets {
|
|
|
322
326
|
self.assetDownloadRetry[uid + version]
|
|
323
327
|
? ++self.assetDownloadRetry[uid + version]
|
|
324
328
|
: (self.assetDownloadRetry[uid + version] = 1);
|
|
325
|
-
return self.getVersionedAssetJSON(uid, version, assetVersionInfo)
|
|
326
|
-
.then(resolve)
|
|
327
|
-
.catch(reject)
|
|
329
|
+
return self.getVersionedAssetJSON(uid, version, assetVersionInfo).then(resolve).catch(reject);
|
|
328
330
|
}
|
|
329
331
|
|
|
330
|
-
reject(error)
|
|
331
|
-
})
|
|
332
|
-
})
|
|
332
|
+
reject(error);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
333
335
|
}
|
|
334
336
|
|
|
335
337
|
downloadAsset(asset) {
|
|
336
|
-
|
|
338
|
+
const self = this;
|
|
337
339
|
return new Promise(async function (resolve, reject) {
|
|
338
|
-
const assetFolderPath = path.resolve(self.assetsFolderPath, asset.uid)
|
|
339
|
-
const assetFilePath = path.resolve(assetFolderPath, asset.filename)
|
|
340
|
+
const assetFolderPath = path.resolve(self.assetsFolderPath, asset.uid);
|
|
341
|
+
const assetFilePath = path.resolve(assetFolderPath, asset.filename);
|
|
340
342
|
|
|
341
343
|
if (fs.existsSync(assetFilePath)) {
|
|
342
344
|
addlogs(
|
|
343
345
|
self.config,
|
|
344
346
|
'Skipping download of { title: ' + asset.filename + ', uid: ' + asset.uid + ' }, as they already exist',
|
|
345
|
-
'success'
|
|
346
|
-
)
|
|
347
|
-
return resolve()
|
|
347
|
+
'success',
|
|
348
|
+
);
|
|
349
|
+
return resolve();
|
|
348
350
|
}
|
|
349
351
|
self.assetStream = {
|
|
350
|
-
url: self.config.securedAssets
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
352
|
+
url: self.config.securedAssets
|
|
353
|
+
? `${asset.url}?authtoken=${self.config.authtoken || self.config.auth_token}`
|
|
354
|
+
: asset.url,
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
await helper.makeDirectory(assetFolderPath);
|
|
358
|
+
const assetFileStream = fs.createWriteStream(assetFilePath);
|
|
359
|
+
self.assetStream.url = encodeURI(self.assetStream.url);
|
|
356
360
|
self.httpClient
|
|
357
361
|
.options({ responseType: 'stream' })
|
|
358
362
|
.get(self.assetStream.url)
|
|
@@ -361,84 +365,87 @@ module.exports = class ExportAssets {
|
|
|
361
365
|
const str = progress({
|
|
362
366
|
time: 5000,
|
|
363
367
|
length: assetStreamRequest.headers['content-length'],
|
|
364
|
-
})
|
|
368
|
+
});
|
|
365
369
|
str.on('progress', function (progressData) {
|
|
366
370
|
console.log(`${asset.filename}: ${Math.round(progressData.percentage)}%`);
|
|
367
371
|
});
|
|
368
372
|
assetStreamRequest.pipe(str).pipe(assetFileStream);
|
|
369
373
|
}
|
|
370
|
-
assetStreamRequest.pipe(assetFileStream)
|
|
371
|
-
}).catch((error) => {
|
|
372
|
-
addlogs(self.config, error, 'error')
|
|
373
|
-
reject(error)
|
|
374
|
+
assetStreamRequest.pipe(assetFileStream);
|
|
374
375
|
})
|
|
376
|
+
.catch((error) => {
|
|
377
|
+
addlogs(self.config, error, 'error');
|
|
378
|
+
reject(error);
|
|
379
|
+
});
|
|
375
380
|
assetFileStream
|
|
376
381
|
.on('close', function () {
|
|
377
382
|
addlogs(self.config, 'Downloaded ' + asset.filename + ': ' + asset.uid + ' successfully!', 'success');
|
|
378
383
|
return resolve();
|
|
379
384
|
})
|
|
380
385
|
.on('error', (error) => {
|
|
381
|
-
addlogs(self.config, error, 'error')
|
|
382
|
-
reject(error)
|
|
383
|
-
})
|
|
386
|
+
addlogs(self.config, error, 'error');
|
|
387
|
+
reject(error);
|
|
388
|
+
});
|
|
384
389
|
});
|
|
385
390
|
}
|
|
386
391
|
|
|
387
392
|
getFolders() {
|
|
388
|
-
|
|
393
|
+
const self = this;
|
|
389
394
|
return new Promise(function (resolve, reject) {
|
|
390
395
|
return self
|
|
391
396
|
.getAssetCount(true)
|
|
392
397
|
.then(function (count) {
|
|
393
398
|
if (count === 0) {
|
|
394
|
-
addlogs(self.config, 'No folders were found in the stack', 'success')
|
|
395
|
-
return resolve()
|
|
399
|
+
addlogs(self.config, 'No folders were found in the stack', 'success');
|
|
400
|
+
return resolve();
|
|
396
401
|
}
|
|
397
402
|
return self
|
|
398
403
|
.getFolderDetails(0, count)
|
|
399
404
|
.then(function () {
|
|
400
|
-
addlogs(self.config, chalk.green('Exported asset-folders successfully!'), 'success')
|
|
401
|
-
return resolve()
|
|
402
|
-
}).catch(function (error) {
|
|
403
|
-
addlogs(self.config, error, 'error')
|
|
404
|
-
reject(error)
|
|
405
|
+
addlogs(self.config, chalk.green('Exported asset-folders successfully!'), 'success');
|
|
406
|
+
return resolve();
|
|
405
407
|
})
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
408
|
+
.catch(function (error) {
|
|
409
|
+
addlogs(self.config, error, 'error');
|
|
410
|
+
reject(error);
|
|
411
|
+
});
|
|
412
|
+
})
|
|
413
|
+
.catch(function (error) {
|
|
414
|
+
addlogs(self.config, error, 'error');
|
|
415
|
+
reject(error);
|
|
409
416
|
});
|
|
410
417
|
});
|
|
411
418
|
}
|
|
412
419
|
|
|
413
420
|
getFolderDetails(skip, tCount) {
|
|
414
|
-
|
|
421
|
+
const self = this;
|
|
415
422
|
return new Promise(function (resolve, reject) {
|
|
416
423
|
if (typeof skip !== 'number') {
|
|
417
|
-
skip = 0
|
|
424
|
+
skip = 0;
|
|
418
425
|
}
|
|
419
426
|
if (skip > tCount) {
|
|
420
|
-
helper.writeFileSync(self.folderJSONPath, self.folderContents)
|
|
421
|
-
return resolve()
|
|
427
|
+
helper.writeFileSync(self.folderJSONPath, self.folderContents);
|
|
428
|
+
return resolve();
|
|
422
429
|
}
|
|
423
|
-
|
|
430
|
+
const queryRequestObj = {
|
|
424
431
|
skip: skip,
|
|
425
432
|
include_folders: true,
|
|
426
|
-
query: { is_dir: true }
|
|
427
|
-
}
|
|
428
|
-
self.
|
|
429
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
433
|
+
query: { is_dir: true },
|
|
434
|
+
};
|
|
435
|
+
self.stackAPIClient
|
|
430
436
|
.asset()
|
|
431
437
|
.query(queryRequestObj)
|
|
432
438
|
.find()
|
|
433
439
|
.then((folderDetailsResponse) => {
|
|
434
440
|
for (let i in folderDetailsResponse.items) {
|
|
435
|
-
self.folderContents.push(folderDetailsResponse.items[i])
|
|
441
|
+
self.folderContents.push(folderDetailsResponse.items[i]);
|
|
436
442
|
}
|
|
437
|
-
skip += 100
|
|
438
|
-
return self.getFolderDetails(skip, tCount).then(resolve).catch(reject)
|
|
439
|
-
}).catch((error) => {
|
|
440
|
-
addlogs(self.config, error, 'error')
|
|
443
|
+
skip += 100;
|
|
444
|
+
return self.getFolderDetails(skip, tCount).then(resolve).catch(reject);
|
|
441
445
|
})
|
|
442
|
-
|
|
446
|
+
.catch((error) => {
|
|
447
|
+
addlogs(self.config, error, 'error');
|
|
448
|
+
});
|
|
449
|
+
});
|
|
443
450
|
}
|
|
444
|
-
}
|
|
451
|
+
};
|
|
@@ -9,11 +9,9 @@ const helper = require('../util/helper');
|
|
|
9
9
|
const { addlogs } = require('../util/log');
|
|
10
10
|
const { formatError } = require('../util');
|
|
11
11
|
const config = require('../../config/default');
|
|
12
|
-
const stack = require('../util/contentstack-management-sdk');
|
|
13
12
|
|
|
14
13
|
module.exports = class ExportCustomRoles {
|
|
15
14
|
roles = {};
|
|
16
|
-
client = null;
|
|
17
15
|
customRoles = {};
|
|
18
16
|
EXISTING_ROLES = {
|
|
19
17
|
Admin: 1,
|
|
@@ -22,9 +20,9 @@ module.exports = class ExportCustomRoles {
|
|
|
22
20
|
};
|
|
23
21
|
rolesConfig = config.modules.customRoles;
|
|
24
22
|
|
|
25
|
-
constructor(
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
23
|
+
constructor(exportConfig, stackAPIClient) {
|
|
24
|
+
this.stackAPIClient = stackAPIClient;
|
|
25
|
+
this.config = merge(config, exportConfig);
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
async start() {
|
|
@@ -35,10 +33,7 @@ module.exports = class ExportCustomRoles {
|
|
|
35
33
|
const rolesFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.rolesConfig.dirName);
|
|
36
34
|
mkdirp.sync(rolesFolderPath);
|
|
37
35
|
|
|
38
|
-
const roles = await this.
|
|
39
|
-
.stack({ api_key: self.config.source_stack, management_token: self.config.management_token })
|
|
40
|
-
.role()
|
|
41
|
-
.fetchAll({ include_rules: true, include_permissions: true });
|
|
36
|
+
const roles = await this.stackAPIClient.role().fetchAll({ include_rules: true, include_permissions: true });
|
|
42
37
|
|
|
43
38
|
const customRoles = roles.items.filter((role) => !self.EXISTING_ROLES[role.name]);
|
|
44
39
|
|
|
@@ -50,7 +45,7 @@ module.exports = class ExportCustomRoles {
|
|
|
50
45
|
await self.getCustomRolesLocales(
|
|
51
46
|
customRoles,
|
|
52
47
|
path.join(rolesFolderPath, self.rolesConfig.customRolesLocalesFileName),
|
|
53
|
-
this.
|
|
48
|
+
this.stackAPIClient,
|
|
54
49
|
self.config,
|
|
55
50
|
);
|
|
56
51
|
self.customRoles = {};
|
|
@@ -75,7 +70,7 @@ module.exports = class ExportCustomRoles {
|
|
|
75
70
|
}
|
|
76
71
|
}
|
|
77
72
|
|
|
78
|
-
async getCustomRolesLocales(customRoles, customRolesLocalesFilepath,
|
|
73
|
+
async getCustomRolesLocales(customRoles, customRolesLocalesFilepath, stackAPIClient, config) {
|
|
79
74
|
const localesMap = {};
|
|
80
75
|
for (const role of customRoles) {
|
|
81
76
|
const rulesLocales = role.rules.find((rule) => rule.module === 'locale');
|
|
@@ -87,11 +82,7 @@ module.exports = class ExportCustomRoles {
|
|
|
87
82
|
}
|
|
88
83
|
|
|
89
84
|
if (Object.keys(localesMap).length) {
|
|
90
|
-
const locales = await
|
|
91
|
-
.stack({ api_key: config.source_stack, management_token: config.management_token })
|
|
92
|
-
.locale()
|
|
93
|
-
.query({})
|
|
94
|
-
.find();
|
|
85
|
+
const locales = await stackAPIClient.locale().query({}).find();
|
|
95
86
|
const sourceLocalesMap = {};
|
|
96
87
|
for (const locale of locales.items) {
|
|
97
88
|
sourceLocalesMap[locale.uid] = locale;
|