From 44eb372a6cafce78d013e670807df2e835ebd875 Mon Sep 17 00:00:00 2001 From: winches <329487092@qq.com> Date: Sun, 8 Dec 2024 13:43:08 +0800 Subject: [PATCH] fix: reduce fetch data --- src/constants/component.ts | 29 ++++++++++++++++++++++------- src/constants/store.ts | 34 +++++++++++++++++++++++++++++++++- src/helpers/beta.ts | 9 +++++++-- src/index.ts | 4 +++- src/scripts/helpers.ts | 6 +++++- 5 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/constants/component.ts b/src/constants/component.ts index 4aacd1f..89c06b7 100644 --- a/src/constants/component.ts +++ b/src/constants/component.ts @@ -28,7 +28,13 @@ export function getNextuiComponentsData(nextUIComponents: Components) { }; } -export function initStoreComponentsData(nextUIComponents: Components) { +export function initStoreComponentsData({ + beta, + nextUIComponents +}: { + beta: boolean; + nextUIComponents: Components; +}) { const { nextUIComponentsKeys, nextUIComponentsKeysSet, @@ -37,12 +43,21 @@ export function initStoreComponentsData(nextUIComponents: Components) { nextUIcomponentsPackages } = getNextuiComponentsData(nextUIComponents); - store.nextUIComponents = nextUIComponents; - store.nextUIComponentsKeys = nextUIComponentsKeys; - store.nextUIComponentsKeysSet = nextUIComponentsKeysSet; - store.nextUIComponentsMap = nextUIComponentsMap; - store.nextUIComponentsPackageMap = nextUIComponentsPackageMap; - store.nextUIcomponentsPackages = nextUIcomponentsPackages; + if (beta) { + store.betaNextUIComponents = nextUIComponents; + store.betaNextUIComponentsKeys = nextUIComponentsKeys; + store.betaNextUIComponentsKeysSet = nextUIComponentsKeysSet; + store.betaNextUIComponentsMap = nextUIComponentsMap; + store.betaNextUIComponentsPackageMap = nextUIComponentsPackageMap; + store.betaNextUIcomponentsPackages = nextUIcomponentsPackages; + } else { + store.nextUIComponents = nextUIComponents; + store.nextUIComponentsKeys = nextUIComponentsKeys; + store.nextUIComponentsKeysSet = nextUIComponentsKeysSet; + store.nextUIComponentsMap = nextUIComponentsMap; + store.nextUIComponentsPackageMap = nextUIComponentsPackageMap; + store.nextUIcomponentsPackages = nextUIcomponentsPackages; + } } export type NextUIComponentsMap = Record; diff --git a/src/constants/store.ts b/src/constants/store.ts index 3b5240d..6dfd692 100644 --- a/src/constants/store.ts +++ b/src/constants/store.ts @@ -13,15 +13,47 @@ export type Store = { cliLatestVersion: string; latestVersion: string; betaVersion: string; + + // NextUI nextUIComponents: Components; nextUIComponentsKeys: string[]; nextUIcomponentsPackages: string[]; nextUIComponentsKeysSet: Set; nextUIComponentsMap: NextUIComponentsMap; nextUIComponentsPackageMap: NextUIComponentsMap; + + // Beta NextUI + betaNextUIComponents: Components; + betaNextUIComponentsKeys: string[]; + betaNextUIcomponentsPackages: string[]; + betaNextUIComponentsKeysSet: Set; + betaNextUIComponentsMap: NextUIComponentsMap; + betaNextUIComponentsPackageMap: NextUIComponentsMap; }; -export const store = {} as Store; +/* eslint-disable sort-keys-fix/sort-keys-fix, sort-keys */ +export const store = { + debug: false, + beta: false, + cliLatestVersion: '', + latestVersion: '', + betaVersion: '', + + betaNextUIComponents: [], + betaNextUIComponentsKeys: [], + betaNextUIComponentsKeysSet: new Set(), + betaNextUIComponentsMap: {}, + betaNextUIComponentsPackageMap: {}, + betaNextUIcomponentsPackages: [], + + nextUIComponents: [], + nextUIComponentsKeys: [], + nextUIComponentsKeysSet: new Set(), + nextUIComponentsMap: {}, + nextUIComponentsPackageMap: {}, + nextUIcomponentsPackages: [] +} as Store; +/* eslint-enable sort-keys-fix/sort-keys-fix, sort-keys */ export type StoreKeys = keyof Store; diff --git a/src/helpers/beta.ts b/src/helpers/beta.ts index 6c49bf7..c5c5adb 100644 --- a/src/helpers/beta.ts +++ b/src/helpers/beta.ts @@ -1,3 +1,4 @@ +import {store} from 'src/constants/store'; import {getCacheExecData} from 'src/scripts/cache/cache'; import {Logger} from './logger'; @@ -15,8 +16,12 @@ export function getPrefixComponent(component: string) { return `@nextui-org/${component.replace('@nextui-org/', '')}`; } -export async function getBetaVersion(component: string) { - const data = await getBetaVersionData(component); +export async function getBetaVersion(componentName: string) { + if (store.betaNextUIComponentsPackageMap[componentName]) { + return store.betaNextUIComponentsPackageMap[componentName]!.version; + } + + const data = await getBetaVersionData(componentName); try { return JSON.parse(data).beta; diff --git a/src/index.ts b/src/index.ts index ad2be38..e588f07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -161,8 +161,10 @@ nextui.hook('preAction', async (command) => { if (args && commandList.includes(args as CommandName)) { // Before run the command init the components.json const nextUIComponents = (await getComponents()).components; + const nextUIComponentsBeta = (await getComponents()).betaComponents; - initStoreComponentsData(nextUIComponents); + initStoreComponentsData({beta: false, nextUIComponents}); + store.beta && initStoreComponentsData({beta: true, nextUIComponents: nextUIComponentsBeta}); } const [cliLatestVersion, latestVersion] = await Promise.all([ diff --git a/src/scripts/helpers.ts b/src/scripts/helpers.ts index f4f5ece..90cfb57 100644 --- a/src/scripts/helpers.ts +++ b/src/scripts/helpers.ts @@ -10,7 +10,7 @@ import ora, {oraPromise} from 'ora'; import {Logger} from '@helpers/logger'; import {COMPONENTS_PATH} from 'src/constants/path'; -import {getStore, getStoreSync} from 'src/constants/store'; +import {getStore, getStoreSync, store} from 'src/constants/store'; import {getPackageVersion} from './cache/cache'; @@ -129,6 +129,10 @@ export async function oraExecCmd(cmd: string, text?: string): Promise } export async function getLatestVersion(packageName: string): Promise { + if (store.nextUIComponentsPackageMap[packageName]) { + return store.nextUIComponentsPackageMap[packageName]!.version; + } + const result = await getPackageVersion(packageName); return result.version;