From 8e94b3e5762a4aaa15a29f5808088516fa0b27a0 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Mon, 28 Jul 2025 17:47:47 +0530 Subject: [PATCH 1/5] removed invalid alias --- src/commands/cm/stacks/export-query.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/commands/cm/stacks/export-query.ts b/src/commands/cm/stacks/export-query.ts index 5d4452a..ad94ba3 100644 --- a/src/commands/cm/stacks/export-query.ts +++ b/src/commands/cm/stacks/export-query.ts @@ -63,8 +63,6 @@ export default class ExportQueryCommand extends Command { }), }; - static aliases = ['cm:export-query']; - async run(): Promise { try { const { flags } = await this.parse(ExportQueryCommand); From 4646e665998a62589c867a27a67cd14f0bba29f5 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Tue, 29 Jul 2025 17:43:23 +0530 Subject: [PATCH 2/5] fixed branches, stack api --- package.json | 2 +- src/commands/cm/stacks/export-query.ts | 16 ++++++- src/utils/branch-helper.ts | 65 ++++++++++++++++++++++++++ src/utils/config-handler.ts | 12 ++--- src/utils/index.ts | 1 + 5 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/utils/branch-helper.ts diff --git a/package.json b/package.json index 1ee6bb6..31b4030 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@contentstack/cli-cm-export-query", "description": "Contentstack CLI plugin to export content from stack", - "version": "1.0.0-beta.1", + "version": "1.0.0-beta.2", "author": "Contentstack", "bugs": "https://github.com/contentstack/cli/issues", "dependencies": { diff --git a/src/commands/cm/stacks/export-query.ts b/src/commands/cm/stacks/export-query.ts index ad94ba3..462a82a 100644 --- a/src/commands/cm/stacks/export-query.ts +++ b/src/commands/cm/stacks/export-query.ts @@ -9,7 +9,7 @@ import { } from '@contentstack/cli-utilities'; import { QueryExporter } from '../../../core/query-executor'; import { QueryExportConfig } from '../../../types'; -import { log, setupQueryExportConfig } from '../../../utils'; +import { log, setupQueryExportConfig, setupBranches } from '../../../utils'; export default class ExportQueryCommand extends Command { static description = 'Export content from a stack using query-based filtering'; @@ -77,8 +77,20 @@ export default class ExportQueryCommand extends Command { } this.exportDir = sanitizePath(exportQueryConfig.exportDir); - // Initialize and run query export + + // Initialize management API client const managementAPIClient: ContentstackClient = await managementSDKClient(exportQueryConfig); + + // Setup and validate branch configuration + const stackAPIClient = managementAPIClient.stack({ + api_key: exportQueryConfig.stackApiKey, + management_token: exportQueryConfig.managementToken, + }); + + // Setup branches (validate branch or set default to 'main') + await setupBranches(exportQueryConfig, stackAPIClient); + + // Initialize and run query export const queryExporter = new QueryExporter(managementAPIClient, exportQueryConfig); await queryExporter.execute(); diff --git a/src/utils/branch-helper.ts b/src/utils/branch-helper.ts new file mode 100644 index 0000000..6bcfe37 --- /dev/null +++ b/src/utils/branch-helper.ts @@ -0,0 +1,65 @@ +import * as path from 'path'; +import { sanitizePath } from '@contentstack/cli-utilities'; +import { QueryExportConfig } from '../types'; +import { fsUtil } from './file-helper'; +import { log } from './logger'; + +/** + * Validates and sets up branch configuration for the stack + * + * @param config The export configuration + * @param stackAPIClient The stack API client + * @returns Promise that resolves when branch setup is complete + */ +export const setupBranches = async (config: QueryExportConfig, stackAPIClient: any): Promise => { + if (typeof config !== 'object') { + throw new Error('Invalid config to setup the branch'); + } + + try { + if (config.branchName) { + // Check if the specified branch exists + log(config, `Validating branch: ${config.branchName}`, 'info'); + + const result = await stackAPIClient + .branch(config.branchName) + .fetch() + .catch((err: Error): any => { + log(config, `Error fetching branch: ${err.message}`, 'error'); + return null; + }); + + if (result && typeof result === 'object') { + log(config, `Branch '${config.branchName}' found`, 'success'); + } else { + throw new Error(`No branch found with the name '${config.branchName}'`); + } + } else { + // If no branch name provided, check if the stack has branches + log(config, 'No branch specified, checking if stack has branches', 'info'); + + const result = await stackAPIClient + .branch() + .query() + .find() + .catch((err: Error): any => { + return null; + }); + + if (result && result.items && Array.isArray(result.items) && result.items.length > 0) { + // Set default branch to 'main' if it exists + config.branchName = 'main'; + } else { + // Stack doesn't have branches + log(config, 'Stack does not have branches', 'info'); + return; + } + } + config.branchEnabled = true; + } catch (error) { + log(config, `Error setting up branches: ${error.message}`, 'error'); + throw error; + } +}; + +export default setupBranches; diff --git a/src/utils/config-handler.ts b/src/utils/config-handler.ts index 47c5e44..49013c4 100644 --- a/src/utils/config-handler.ts +++ b/src/utils/config-handler.ts @@ -27,19 +27,19 @@ export async function setupQueryExportConfig(flags: any): Promise Date: Tue, 29 Jul 2025 17:43:57 +0530 Subject: [PATCH 3/5] updated talisman r --- .talismanrc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.talismanrc b/.talismanrc index 40be89c..c770ae7 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,4 +1,6 @@ fileignoreconfig: - - filename: package-lock.json - checksum: 4e00357992422982d516ee3231f87a1f98f27c12788c63a6a089cafa059b6b9e + - filename: src/utils/config-handler.ts + checksum: a23fb2f3034046f68d0c24de9a50855579bb0bc86cbc6485aec0f315c9a42d77 + - filename: src/commands/cm/stacks/export-query.ts + checksum: 0a94bd30d852b2d8acb102eaabf602e397a649a7dc7c671efcb66b56d67dc7b7 version: '1.0' From ed0b77f5575bef83c8d59fdfd154db9711e02511 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Tue, 29 Jul 2025 18:28:03 +0530 Subject: [PATCH 4/5] removed the command log --- src/core/module-exporter.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/module-exporter.ts b/src/core/module-exporter.ts index 1d89c05..13e6bab 100644 --- a/src/core/module-exporter.ts +++ b/src/core/module-exporter.ts @@ -20,8 +20,6 @@ export class ModuleExporter { // Build command arguments const cmd = this.buildExportCommand(moduleName, options); - log(this.exportQueryConfig, `Running export command: ${cmd.join(' ')}`, 'debug'); - // Configurable delay const delay = this.exportQueryConfig.exportDelayMs || 2000; await new Promise((resolve) => setTimeout(resolve, delay)); From 4722c33ab09338d144fa8985307b1c015311f096 Mon Sep 17 00:00:00 2001 From: shafeeqd959 Date: Wed, 30 Jul 2025 10:38:21 +0530 Subject: [PATCH 5/5] updated regex to support non prod --- .env-example | 1 + .talismanrc | 6 ++---- snyk_output.log | 0 src/utils/branch-helper.ts | 3 ++- src/utils/referenced-asset-handler.ts | 18 +++++++++++++----- 5 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 .env-example create mode 100644 snyk_output.log diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..a55f16b --- /dev/null +++ b/.env-example @@ -0,0 +1 @@ +ENVIRONMENT=NON_PROD \ No newline at end of file diff --git a/.talismanrc b/.talismanrc index c770ae7..f268b29 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,6 +1,4 @@ fileignoreconfig: - - filename: src/utils/config-handler.ts - checksum: a23fb2f3034046f68d0c24de9a50855579bb0bc86cbc6485aec0f315c9a42d77 - - filename: src/commands/cm/stacks/export-query.ts - checksum: 0a94bd30d852b2d8acb102eaabf602e397a649a7dc7c671efcb66b56d67dc7b7 + - filename: .env-example + checksum: 591f1e672d4df287107092b8fd37c27913e09225c6ced55293e1d459b1119d05 version: '1.0' diff --git a/snyk_output.log b/snyk_output.log new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/branch-helper.ts b/src/utils/branch-helper.ts index 6bcfe37..dc94a04 100644 --- a/src/utils/branch-helper.ts +++ b/src/utils/branch-helper.ts @@ -42,7 +42,8 @@ export const setupBranches = async (config: QueryExportConfig, stackAPIClient: a .branch() .query() .find() - .catch((err: Error): any => { + .catch((): any => { + log(config, 'Stack does not have branches', 'info'); return null; }); diff --git a/src/utils/referenced-asset-handler.ts b/src/utils/referenced-asset-handler.ts index dc2a629..d1f66ce 100644 --- a/src/utils/referenced-asset-handler.ts +++ b/src/utils/referenced-asset-handler.ts @@ -109,13 +109,21 @@ export class AssetReferenceHandler { } } + let assetUrlRegex = ''; + let assetUIDMatchIndex; + if (process.env.ENVIRONMENT === 'NON_PROD') { + assetUrlRegex = '(https://.*?/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))'; + assetUIDMatchIndex = 3; + } else { + assetUrlRegex = + '(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))'; + assetUIDMatchIndex = 6; + } + // Pattern 2: Contentstack asset URLs - const urlRegex = new RegExp( - '(https://(assets|(eu-|azure-na-|azure-eu-|gcp-na-|gcp-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))', - 'g', - ); + const urlRegex = new RegExp(assetUrlRegex, 'g'); while ((match = urlRegex.exec(content)) !== null) { - const assetUID = match[6]; // The asset UID is in the 6th capture group + const assetUID = match[assetUIDMatchIndex]; // The asset UID is in the 6th capture group if (assetUID) { assetUIDs.add(assetUID); }