From 7ce1228d99a38802557c0b49629a71c2a0de7ff9 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Tue, 26 Aug 2025 08:34:08 +0200 Subject: [PATCH] feat: check if cli config exists before trying to get token --- src/api/actor.api.ts | 39 ++++++++++++++++++++++++++++----------- src/utils/config.utils.ts | 2 ++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/api/actor.api.ts b/src/api/actor.api.ts index f0c42615..c395c371 100644 --- a/src/api/actor.api.ts +++ b/src/api/actor.api.ts @@ -1,26 +1,29 @@ import {Ed25519KeyIdentity} from '@dfinity/identity/lib/cjs/identity/ed25519'; -import {isNullish, nonNullish} from '@dfinity/utils'; +import {isNullish, nonNullish, notEmptyString} from '@dfinity/utils'; import type {ActorParameters} from '@junobuild/ic-client/actor'; -import {green, red} from 'kleur'; +import {green, red, yellow} from 'kleur'; import {getToken} from '../configs/cli.config'; +import {readEmulatorConfig} from '../configs/emulator.config'; import {ENV} from '../env'; -import {getProcessToken} from '../utils/process.utils'; -import {NEW_CMD_LINE} from '../utils/prompt.utils'; +import {noConfigFile} from '../utils/config.utils'; +import {getProcessToken, isHeadless} from '../utils/process.utils'; import {initAgent} from './agent.api'; export const actorParameters = async (): Promise< Omit & Required> > => { + const configNotFound = !isHeadless() && noConfigFile(); + + if (configNotFound) { + await missingConfigInfo({errorType: 'not-configured'}); + + process.exit(1); + } + const token = getProcessToken() ?? (await getToken()); if (isNullish(token)) { - console.log(`${red(`No access key found for ${ENV.mode}.`)} Are you logged in?`); - - if (ENV.mode !== 'production') { - console.log( - `${NEW_CMD_LINE}šŸ’” To enforce separation of concerns and security, each mode requires a separate login — for example: ${green(`juno login --mode ${ENV.mode}`)}` - ); - } + await missingConfigInfo({errorType: 'not-logged-in'}); process.exit(1); } @@ -39,3 +42,17 @@ export const actorParameters = async (): Promise< agent }; }; + +const missingConfigInfo = async ({errorType}: {errorType: 'not-configured' | 'not-logged-in'}) => { + console.log( + `${red(`Looks like your CLI is not ${errorType === 'not-logged-in' ? 'logged in' : 'configured'}.`)} Try to run:` + ); + + const parsedResult = await readEmulatorConfig(); + const satelliteEmulator = + parsedResult.success && parsedResult.config.derivedConfig.emulatorType === 'satellite'; + + console.log( + `\nšŸ‘‰ ${green('juno')} ${green('login')}${ENV.mode !== 'production' ? ` ${yellow(`--mode ${ENV.mode}`)}` : ''}${notEmptyString(ENV.profile) ? ` ${yellow(`--profile ${ENV.profile}`)}` : ''}${satelliteEmulator ? ` ${yellow(`--emulator`)}` : ''}` + ); +}; diff --git a/src/utils/config.utils.ts b/src/utils/config.utils.ts index c5dc3c36..d12e773c 100644 --- a/src/utils/config.utils.ts +++ b/src/utils/config.utils.ts @@ -29,3 +29,5 @@ const configPath = (): string => { }; export const configFileExists = (): boolean => existsSync(configPath()); + +export const noConfigFile = (): boolean => !configFileExists();