From 74ab4281b3ea3572c4f681f4dc7debfda06ecfb7 Mon Sep 17 00:00:00 2001 From: harshitha-cstk Date: Tue, 30 Jun 2026 13:20:02 +0530 Subject: [PATCH 1/2] fix: show login error on console and improve API key validation add API key validation in import/export configurations --- .../contentstack-export/src/utils/export-config-handler.ts | 6 +++--- packages/contentstack-export/src/utils/interactive.ts | 6 ++++++ .../test/unit/utils/export-config-handler.test.ts | 2 +- .../src/commands/cm/stacks/import-setup.ts | 4 ++++ .../src/utils/import-config-handler.ts | 4 ++-- packages/contentstack-import-setup/src/utils/interactive.ts | 6 ++++++ .../contentstack-import/src/utils/import-config-handler.ts | 5 +++-- packages/contentstack-import/src/utils/interactive.ts | 6 ++++++ 8 files changed, 31 insertions(+), 8 deletions(-) diff --git a/packages/contentstack-export/src/utils/export-config-handler.ts b/packages/contentstack-export/src/utils/export-config-handler.ts index 3b1d559ff..36c803e31 100644 --- a/packages/contentstack-export/src/utils/export-config-handler.ts +++ b/packages/contentstack-export/src/utils/export-config-handler.ts @@ -100,9 +100,9 @@ const setupConfig = async (exportCmdFlags: any): Promise => { config.apiKey = exportCmdFlags['stack-uid'] || exportCmdFlags['stack-api-key'] || config.apiKey || (await askAPIKey()); - if (typeof config.apiKey !== 'string') { - log.debug('Invalid API key received!', { apiKey: config.apiKey }); - throw new Error('Invalid API key received'); + if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) { + log.debug('Invalid or empty API key received!', { apiKey: config.apiKey }); + throw new Error('Invalid or empty API key received. Please provide a valid stack API key.'); } } } diff --git a/packages/contentstack-export/src/utils/interactive.ts b/packages/contentstack-export/src/utils/interactive.ts index 141ec1f6e..45087f572 100644 --- a/packages/contentstack-export/src/utils/interactive.ts +++ b/packages/contentstack-export/src/utils/interactive.ts @@ -64,5 +64,11 @@ export const askAPIKey = async (): Promise => { type: 'input', message: 'Enter the stack api key', name: 'apiKey', + validate: (input: string) => { + if (!input || !input.trim()) { + return 'Stack API key cannot be empty. Please enter a valid stack API key.'; + } + return true; + }, }); }; diff --git a/packages/contentstack-export/test/unit/utils/export-config-handler.test.ts b/packages/contentstack-export/test/unit/utils/export-config-handler.test.ts index eeb612d98..2ba1ba98f 100644 --- a/packages/contentstack-export/test/unit/utils/export-config-handler.test.ts +++ b/packages/contentstack-export/test/unit/utils/export-config-handler.test.ts @@ -371,7 +371,7 @@ describe('Export Config Handler', () => { await setupConfig(flags); expect.fail('Should have thrown an error'); } catch (error: any) { - expect(error.message).to.include('Invalid API key received'); + expect(error.message).to.include('Invalid or empty API key received. Please provide a valid stack API key.'); } }); }); diff --git a/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts b/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts index 5a601813e..8d70de3ad 100644 --- a/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts +++ b/packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts @@ -13,6 +13,8 @@ import { handleAndLogError, configHandler, createLogContext, + cliux, + loadChalk } from '@contentstack/cli-utilities'; import { ImportConfig, Context } from '../../../types'; @@ -64,6 +66,7 @@ export default class ImportSetupCommand extends Command { static usage = 'cm:stacks:import-setup [-k ] [-d ] [-a ] [--modules ]'; async run(): Promise { + await loadChalk(); try { const { flags } = await this.parse(ImportSetupCommand); let importSetupConfig = await setupImportConfig(flags); @@ -106,6 +109,7 @@ export default class ImportSetupCommand extends Command { } catch (error) { CLIProgressManager.printGlobalSummary(); handleAndLogError(error); + cliux.error(`ERROR: ${error instanceof Error ? error.message : String(error)}`); } } diff --git a/packages/contentstack-import-setup/src/utils/import-config-handler.ts b/packages/contentstack-import-setup/src/utils/import-config-handler.ts index 6e3036d96..6b2481961 100644 --- a/packages/contentstack-import-setup/src/utils/import-config-handler.ts +++ b/packages/contentstack-import-setup/src/utils/import-config-handler.ts @@ -59,8 +59,8 @@ const setupConfig = async (importCmdFlags: any): Promise => { } else { config.apiKey = importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.target_stack || (await askAPIKey()); - if (typeof config.apiKey !== 'string') { - throw new Error('Invalid API key received'); + if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) { + throw new Error('Invalid or empty API key received. Please provide a valid stack API key.'); } } } diff --git a/packages/contentstack-import-setup/src/utils/interactive.ts b/packages/contentstack-import-setup/src/utils/interactive.ts index be3161401..98575dca6 100644 --- a/packages/contentstack-import-setup/src/utils/interactive.ts +++ b/packages/contentstack-import-setup/src/utils/interactive.ts @@ -30,5 +30,11 @@ export const askAPIKey = async (): Promise => { type: 'input', message: 'Enter the stack api key', name: 'apiKey', + validate: (input: string) => { + if (!input || !input.trim()) { + return 'Stack API key cannot be empty. Please enter a valid stack API key.'; + } + return true; + }, }); }; diff --git a/packages/contentstack-import/src/utils/import-config-handler.ts b/packages/contentstack-import/src/utils/import-config-handler.ts index abcacecb9..8126ee580 100644 --- a/packages/contentstack-import/src/utils/import-config-handler.ts +++ b/packages/contentstack-import/src/utils/import-config-handler.ts @@ -86,8 +86,9 @@ const setupConfig = async (importCmdFlags: any): Promise => { } config.apiKey = importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.apiKey || (await askAPIKey()); - if (typeof config.apiKey !== 'string') { - throw new Error('Invalid API key received'); + if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) { + log.debug('Invalid or empty API key received!', { apiKey: config.apiKey }); + throw new Error('Invalid or empty API key received. Please provide a valid stack API key.'); } } } diff --git a/packages/contentstack-import/src/utils/interactive.ts b/packages/contentstack-import/src/utils/interactive.ts index 02eec4001..81519e2ca 100644 --- a/packages/contentstack-import/src/utils/interactive.ts +++ b/packages/contentstack-import/src/utils/interactive.ts @@ -19,6 +19,12 @@ export const askAPIKey = async (): Promise => { type: 'input', message: 'Enter the stack api key', name: 'apiKey', + validate: (input: string) => { + if (!input || !input.trim()) { + return 'Stack API key cannot be empty. Please enter a valid stack API key.'; + } + return true; + }, }); }; From ce017407b239efeb45e34d999401541a671d6ca3 Mon Sep 17 00:00:00 2001 From: harshitha-cstk Date: Tue, 30 Jun 2026 15:54:21 +0530 Subject: [PATCH 2/2] pnpm lock update --- pnpm-lock.yaml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ed533c130..2f212d824 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4535,8 +4535,8 @@ packages: resolution: {integrity: sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==} engines: {node: '>=10'} - caniuse-lite@1.0.30001799: - resolution: {integrity: sha512-hG1bReV+OUU+MOqK4t/ZWI0tZOyz3rqS9XuhOUz1cIcbwBKjOyJEJuw9ER5JuNyqxNk8u/JUVbGibBOL1yrjFw==} + caniuse-lite@1.0.30001800: + resolution: {integrity: sha512-MMHtuAz9Ys840zAY5F4k6fV5GaivZ9sPk+nz0mY+GYVzRBnYkN0mpqkSR92oWRQ19yQWo4HvBV/FnC16AJX8MA==} capital-case@1.0.4: resolution: {integrity: sha512-ds37W8CytHgwnhGGTi88pcPyR15qoNkOpYwmMMfnWqqWgESapLqvDx6huFjQ5vqWSn2Z06173XNA7LtMOeUh1A==} @@ -5172,8 +5172,8 @@ packages: engines: {node: '>=0.10.0'} hasBin: true - electron-to-chromium@1.5.381: - resolution: {integrity: sha512-n9Wa6yB+vDsGuA8AKbl/0z7HbvWqt5jxIdvr1IUicd0ryPrk7/xzwqLv8D9AbbvZ6avVNtXYLTfmgFHkwkyelg==} + electron-to-chromium@1.5.382: + resolution: {integrity: sha512-8ETaWbV6SZOrno+G93Ffd9ENsMtetqdnqj4nlfxFW90Sm5GgnuV28Kf62hqQVD6VUgzm7qFQKsTsAPmeUiU3Ug==} elegant-spinner@1.0.1: resolution: {integrity: sha512-B+ZM+RXvRqQaAmkMlO/oSe5nMUOaUnyfGYCEHoR8wrXsZR2mA0XVibsxV1bvTwxdRWah1PkQqso2EzhILGHtEQ==} @@ -13982,8 +13982,8 @@ snapshots: browserslist@4.28.4: dependencies: baseline-browser-mapping: 2.10.40 - caniuse-lite: 1.0.30001799 - electron-to-chromium: 1.5.381 + caniuse-lite: 1.0.30001800 + electron-to-chromium: 1.5.382 node-releases: 2.0.50 update-browserslist-db: 1.2.3(browserslist@4.28.4) @@ -14068,7 +14068,7 @@ snapshots: camelcase@6.3.0: {} - caniuse-lite@1.0.30001799: {} + caniuse-lite@1.0.30001800: {} capital-case@1.0.4: dependencies: @@ -14743,7 +14743,7 @@ snapshots: dependencies: jake: 10.9.4 - electron-to-chromium@1.5.381: {} + electron-to-chromium@1.5.382: {} elegant-spinner@1.0.1: {}