diff --git a/src/commands/version.ts b/src/commands/version.ts index 19ff285..d9e0ef2 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -1,5 +1,21 @@ +import {red} from 'kleur'; +import {logHelpVersion} from '../help/version.help'; +import {enableDisableVersionCheck} from '../services/version/version.check.services'; import {printVersion} from '../services/version/version.print.services'; export const logVersion = async () => { await printVersion(); }; + +export const version = async (args?: string[]) => { + const [subCommand] = args ?? []; + + switch (subCommand) { + case 'check': + await enableDisableVersionCheck(); + break; + default: + console.log(red('Unknown subcommand.')); + logHelpVersion(args); + } +}; diff --git a/src/configs/cli.versions.config.ts b/src/configs/cli.versions.config.ts index 655050a..c675aa1 100644 --- a/src/configs/cli.versions.config.ts +++ b/src/configs/cli.versions.config.ts @@ -6,7 +6,21 @@ const getVersionConfig = (): Conf => export const getCachedVersions = (): Conf => getVersionConfig(); -export const updateLastCheckToNow = ({key}: {key: keyof CachedVersions}) => { +export const isWeeklyCheckEnabled = (): boolean => + getCachedVersions().get('weeklyCheckEnabled') !== false; + +export const isWeeklyCheckDisabled = (): boolean => !isWeeklyCheckEnabled(); + +export const toggleWeeklyCheck = (enabled: boolean) => { + const config = getVersionConfig(); + config.set('weeklyCheckEnabled', enabled); +}; + +export const updateLastCheckToNow = ({ + key +}: { + key: keyof Omit; +}) => { const config = getVersionConfig(); const currentVersions = config.get(key); diff --git a/src/constants/help.constants.ts b/src/constants/help.constants.ts index 3fafacc..3d1da38 100644 --- a/src/constants/help.constants.ts +++ b/src/constants/help.constants.ts @@ -16,7 +16,7 @@ export const SNAPSHOT_DESCRIPTION = 'Handle snapshot-related tasks.'; export const START_DESCRIPTION = 'Start a module.'; export const STOP_DESCRIPTION = 'Stop a module.'; export const UPGRADE_DESCRIPTION = 'Upgrade a module to a new version.'; -export const VERSION_DESCRIPTION = 'Check the version of the CLI.'; +export const VERSION_DESCRIPTION = 'Manage version related tasks.'; export const STATUS_DESCRIPTION = 'Check the status of the modules.'; export const WHOAMI_DESCRIPTION = 'Display your current profile, access key, and links to your satellite.'; diff --git a/src/help/version.help.ts b/src/help/version.help.ts index bd7a9f0..7c23f91 100644 --- a/src/help/version.help.ts +++ b/src/help/version.help.ts @@ -1,9 +1,12 @@ -import {cyan, green, yellow} from 'kleur'; +import {cyan, green, magenta} from 'kleur'; import {OPTION_HELP, VERSION_DESCRIPTION} from '../constants/help.constants'; import {helpOutput} from './common.help'; import {TITLE} from './help'; -const usage = `Usage: ${green('juno')} ${cyan('version')} ${yellow('[options]')} +const usage = `Usage: ${green('juno')} ${cyan('version')} ${magenta('')} + +Subcommands: + ${magenta('check')} Configure the weekly version check. Options: ${OPTION_HELP}`; diff --git a/src/index.ts b/src/index.ts index 98f847a..94a8dd4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ import {helpSnapshot, snapshot} from './commands/snapshot'; import {startStop} from './commands/start-stop'; import {status} from './commands/status'; import {upgrade} from './commands/upgrade'; -import {logVersion} from './commands/version'; +import {logVersion, version as versionCommand} from './commands/version'; import {whoami} from './commands/whoami'; import {help} from './help/help'; import {logHelpLogin} from './help/login.help'; @@ -149,7 +149,7 @@ export const run = async () => { await clear(); break; case 'version': - await logVersion(); + await versionCommand(args); break; case 'status': await status(); diff --git a/src/services/version/version.check.services.ts b/src/services/version/version.check.services.ts new file mode 100644 index 0000000..61c1e9c --- /dev/null +++ b/src/services/version/version.check.services.ts @@ -0,0 +1,24 @@ +import {isNullish} from '@dfinity/utils'; +import prompts from 'prompts'; +import {isWeeklyCheckEnabled, toggleWeeklyCheck} from '../../configs/cli.versions.config'; + +export const enableDisableVersionCheck = async () => { + const current = isWeeklyCheckEnabled(); + + const {enabled}: {enabled: boolean | undefined} = await prompts([ + { + type: 'toggle', + name: 'enabled', + message: 'Enable weekly version check?', + initial: current, + active: 'yes', + inactive: 'no' + } + ]); + + if (isNullish(enabled)) { + return; + } + + toggleWeeklyCheck(enabled); +}; diff --git a/src/services/version/version.check.weekly.services.ts b/src/services/version/version.check.weekly.services.ts index 35830f5..331c796 100644 --- a/src/services/version/version.check.weekly.services.ts +++ b/src/services/version/version.check.weekly.services.ts @@ -82,7 +82,7 @@ const check = async ({ releaseFn, checkVersionFn }: { - key: keyof CachedVersions; + key: keyof Omit; currentVersion: string; releaseFn: () => Promise; checkVersionFn: (params: {latestVersion: string}) => void; diff --git a/src/types/cli/cli.versions.ts b/src/types/cli/cli.versions.ts index 365665a..a7e7bb5 100644 --- a/src/types/cli/cli.versions.ts +++ b/src/types/cli/cli.versions.ts @@ -7,6 +7,7 @@ export const CachedVersionSchema = j.strictObject({ }); export const CachedVersionsSchema = j.strictObject({ + weeklyCheckEnabled: j.boolean().optional(), cli: CachedVersionSchema.optional(), emulator: CachedVersionSchema.optional() }); diff --git a/src/version.ts b/src/version.ts index 1aa762c..3b48aea 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,3 +1,4 @@ +import {isWeeklyCheckDisabled} from './configs/cli.versions.config'; import { checkCliVersion, checkEmulatorVersion @@ -10,6 +11,14 @@ export const checkWeeklyVersions = async ({cmd, args}: {cmd: string; args?: stri return; } + if (isWeeklyCheckDisabled()) { + return; + } + + if (cmd === 'version') { + return; + } + const [subCommand] = args ?? []; if (cmd === 'emulator' && ['start', 'wait'].includes(subCommand)) {