@contentstack/cli-cm-export-query 1.0.0-beta.2 → 1.0.0-beta.3

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.
@@ -225,19 +225,97 @@ class QueryExporter {
225
225
  async exportReferencedAssets() {
226
226
  (0, logger_1.log)(this.exportQueryConfig, 'Starting export of referenced assets...', 'info');
227
227
  try {
228
+ const assetsDir = path.join((0, cli_utilities_1.sanitizePath)(this.exportQueryConfig.exportDir), (0, cli_utilities_1.sanitizePath)(this.exportQueryConfig.branchName || ''), 'assets');
229
+ const metadataFilePath = path.join(assetsDir, 'metadata.json');
230
+ const assetFilePath = path.join(assetsDir, 'assets.json');
231
+ // Define temp file paths
232
+ const tempMetadataFilePath = path.join(assetsDir, 'metadata_temp.json');
233
+ const tempAssetFilePath = path.join(assetsDir, 'assets_temp.json');
228
234
  const assetHandler = new utils_4.AssetReferenceHandler(this.exportQueryConfig);
229
235
  // Extract referenced asset UIDs from all entries
230
236
  const assetUIDs = assetHandler.extractReferencedAssets();
231
237
  if (assetUIDs.length > 0) {
232
- (0, logger_1.log)(this.exportQueryConfig, `Exporting ${assetUIDs.length} referenced assets...`, 'info');
233
- const query = {
234
- modules: {
235
- assets: {
236
- uid: { $in: assetUIDs },
238
+ (0, logger_1.log)(this.exportQueryConfig, `Found ${assetUIDs.length} referenced assets to export`, 'info');
239
+ // Define batch size - can be configurable through exportQueryConfig
240
+ const batchSize = this.exportQueryConfig.assetBatchSize || 100;
241
+ if (assetUIDs.length <= batchSize) {
242
+ const query = {
243
+ modules: {
244
+ assets: {
245
+ uid: { $in: assetUIDs },
246
+ },
237
247
  },
238
- },
239
- };
240
- await this.moduleExporter.exportModule('assets', { query });
248
+ };
249
+ await this.moduleExporter.exportModule('assets', { query });
250
+ }
251
+ // if asset size is bigger than batch size, then we need to export in batches
252
+ // Calculate number of batches
253
+ const totalBatches = Math.ceil(assetUIDs.length / batchSize);
254
+ (0, logger_1.log)(this.exportQueryConfig, `Processing assets in ${totalBatches} batches of ${batchSize}`, 'info');
255
+ // Process assets in batches
256
+ for (let i = 0; i < totalBatches; i++) {
257
+ const start = i * batchSize;
258
+ const end = Math.min(start + batchSize, assetUIDs.length);
259
+ const batchAssetUIDs = assetUIDs.slice(start, end);
260
+ (0, logger_1.log)(this.exportQueryConfig, `Exporting batch ${i + 1}/${totalBatches} (${batchAssetUIDs.length} assets)...`, 'info');
261
+ const query = {
262
+ modules: {
263
+ assets: {
264
+ uid: { $in: batchAssetUIDs },
265
+ },
266
+ },
267
+ };
268
+ await this.moduleExporter.exportModule('assets', { query });
269
+ // Read the current batch's metadata.json and assets.json files
270
+ const currentMetadata = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(metadataFilePath));
271
+ const currentAssets = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(assetFilePath));
272
+ // Check if this is the first batch
273
+ if (i === 0) {
274
+ // For first batch, initialize temp files with current content
275
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(tempMetadataFilePath), currentMetadata);
276
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(tempAssetFilePath), currentAssets);
277
+ (0, logger_1.log)(this.exportQueryConfig, `Initialized temporary files with first batch data`, 'info');
278
+ }
279
+ else {
280
+ // For subsequent batches, append to temp files with incremented keys
281
+ // Handle metadata (which contains arrays of asset info)
282
+ const tempMetadata = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(tempMetadataFilePath)) || {};
283
+ // Merge metadata by combining arrays
284
+ if (currentMetadata) {
285
+ Object.keys(currentMetadata).forEach((key) => {
286
+ if (!tempMetadata[key]) {
287
+ tempMetadata[key] = currentMetadata[key];
288
+ }
289
+ });
290
+ }
291
+ // Write updated metadata back to temp file
292
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(tempMetadataFilePath), tempMetadata);
293
+ // Handle assets (which is an object with numeric keys)
294
+ const tempAssets = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(tempAssetFilePath)) || {};
295
+ let nextIndex = Object.keys(tempAssets).length + 1;
296
+ // Add current assets with incremented keys
297
+ Object.values(currentAssets).forEach((value) => {
298
+ tempAssets[nextIndex.toString()] = value;
299
+ nextIndex++;
300
+ });
301
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(tempAssetFilePath), tempAssets);
302
+ (0, logger_1.log)(this.exportQueryConfig, `Updated temporary files with batch ${i + 1} data`, 'info');
303
+ }
304
+ // Optional: Add delay between batches to avoid rate limiting
305
+ if (i < totalBatches - 1 && this.exportQueryConfig.batchDelayMs) {
306
+ await new Promise((resolve) => setTimeout(resolve, this.exportQueryConfig.batchDelayMs));
307
+ }
308
+ }
309
+ // After all batches are processed, copy temp files back to original files
310
+ const finalMetadata = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(tempMetadataFilePath));
311
+ const finalAssets = utils_2.fsUtil.readFile((0, cli_utilities_1.sanitizePath)(tempAssetFilePath));
312
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(metadataFilePath), finalMetadata);
313
+ utils_2.fsUtil.writeFile((0, cli_utilities_1.sanitizePath)(assetFilePath), finalAssets);
314
+ (0, logger_1.log)(this.exportQueryConfig, `Final data written back to original files`, 'info');
315
+ // Clean up temp files
316
+ utils_2.fsUtil.removeFile((0, cli_utilities_1.sanitizePath)(tempMetadataFilePath));
317
+ utils_2.fsUtil.removeFile((0, cli_utilities_1.sanitizePath)(tempAssetFilePath));
318
+ (0, logger_1.log)(this.exportQueryConfig, `Temporary files cleaned up`, 'info');
241
319
  (0, logger_1.log)(this.exportQueryConfig, 'Referenced assets exported successfully', 'success');
242
320
  }
243
321
  else {
@@ -140,6 +140,9 @@ export interface QueryExportConfig extends DefaultConfig {
140
140
  logsPath: string;
141
141
  dataPath: string;
142
142
  exportDelayMs?: number;
143
+ batchDelayMs?: number;
144
+ assetBatchSize?: number;
145
+ assetBatchDelayMs?: number;
143
146
  }
144
147
  export interface QueryMetadata {
145
148
  query: any;
@@ -12,6 +12,10 @@ async function setupQueryExportConfig(flags) {
12
12
  const exportQueryConfig = Object.assign(Object.assign({}, config_1.default), { exportDir, stackApiKey: flags['stack-api-key'] || '', managementToken: flags.alias ? (_a = cli_utilities_1.configHandler.get(`tokens.${flags.alias}`)) === null || _a === void 0 ? void 0 : _a.token : undefined, query: flags.query, skipReferences: flags['skip-references'] || false, skipDependencies: flags['skip-dependencies'] || false, branchName: flags.branch, securedAssets: flags['secured-assets'] || false, isQueryBasedExport: true, logsPath: exportDir, dataPath: exportDir,
13
13
  // Todo: accept the path of the config file from the user
14
14
  externalConfigPath: path.join(__dirname, '../config/export-config.json') });
15
+ // override the external config path if the user provides a config file
16
+ if (flags.config) {
17
+ exportQueryConfig.externalConfigPath = (0, cli_utilities_1.sanitizePath)(flags['config']);
18
+ }
15
19
  // Handle authentication
16
20
  if (flags.alias) {
17
21
  const { token, apiKey } = cli_utilities_1.configHandler.get(`tokens.${flags.alias}`) || {};
@@ -102,5 +102,5 @@
102
102
  ]
103
103
  }
104
104
  },
105
- "version": "1.0.0-beta.2"
105
+ "version": "1.0.0-beta.3"
106
106
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-export-query",
3
3
  "description": "Contentstack CLI plugin to export content from stack",
4
- "version": "1.0.0-beta.2",
4
+ "version": "1.0.0-beta.3",
5
5
  "author": "Contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "dependencies": {