Skip to content

Commit

Permalink
fix: reduce fetch data
Browse files Browse the repository at this point in the history
  • Loading branch information
winchesHe committed Dec 8, 2024
1 parent a66d7b5 commit 44eb372
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
29 changes: 22 additions & 7 deletions src/constants/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<string, (typeof store.nextUIComponents)[number]>;
Expand Down
34 changes: 33 additions & 1 deletion src/constants/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,47 @@ export type Store = {
cliLatestVersion: string;
latestVersion: string;
betaVersion: string;

// NextUI
nextUIComponents: Components;
nextUIComponentsKeys: string[];
nextUIcomponentsPackages: string[];
nextUIComponentsKeysSet: Set<string>;
nextUIComponentsMap: NextUIComponentsMap;
nextUIComponentsPackageMap: NextUIComponentsMap;

// Beta NextUI
betaNextUIComponents: Components;
betaNextUIComponentsKeys: string[];
betaNextUIcomponentsPackages: string[];
betaNextUIComponentsKeysSet: Set<string>;
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;

Expand Down
9 changes: 7 additions & 2 deletions src/helpers/beta.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {store} from 'src/constants/store';
import {getCacheExecData} from 'src/scripts/cache/cache';

import {Logger} from './logger';
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -129,6 +129,10 @@ export async function oraExecCmd(cmd: string, text?: string): Promise<SAFE_ANY>
}

export async function getLatestVersion(packageName: string): Promise<string> {
if (store.nextUIComponentsPackageMap[packageName]) {
return store.nextUIComponentsPackageMap[packageName]!.version;
}

const result = await getPackageVersion(packageName);

return result.version;
Expand Down

0 comments on commit 44eb372

Please sign in to comment.