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 40be89c..f268b29 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,4 +1,4 @@ fileignoreconfig: - - filename: package-lock.json - checksum: 4e00357992422982d516ee3231f87a1f98f27c12788c63a6a089cafa059b6b9e + - filename: .env-example + checksum: 591f1e672d4df287107092b8fd37c27913e09225c6ced55293e1d459b1119d05 version: '1.0' 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/snyk_output.log b/snyk_output.log new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/cm/stacks/export-query.ts b/src/commands/cm/stacks/export-query.ts index 5d4452a..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'; @@ -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); @@ -79,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/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)); diff --git a/src/utils/branch-helper.ts b/src/utils/branch-helper.ts new file mode 100644 index 0000000..dc94a04 --- /dev/null +++ b/src/utils/branch-helper.ts @@ -0,0 +1,66 @@ +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((): any => { + log(config, 'Stack does not have branches', 'info'); + 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