Skip to content

Commit

Permalink
fix: ci and compare vesion optimize cache (#26)
Browse files Browse the repository at this point in the history
* ci: fix release add install

* fix: change the compare version and optimize cache

* fix: ci error

* refactor: use store instead basic data

* fix: compare cli version mistake
  • Loading branch information
winchesHe committed Apr 17, 2024
1 parent 38e4c7c commit ea1ef59
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 55 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: 18.x
check-latest: true
node-version-file: '.nvmrc'

- run: npx changelogithub
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Publish to npm
run: npm publish
env:
Expand Down
6 changes: 3 additions & 3 deletions src/actions/add-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {fixPnpm, fixProvider, fixTailwind} from '@helpers/fix';
import {Logger} from '@helpers/logger';
import {getPackageInfo} from '@helpers/package';
import {findFiles} from '@helpers/utils';
import {nextUIComponents, nextUIComponentsMap} from 'src/constants/component';
import {resolver} from 'src/constants/path';
import {
DOCS_PROVIDER_SETUP,
NEXT_UI,
individualTailwindRequired,
pnpmRequired
} from 'src/constants/required';
import {store} from 'src/constants/store';
import {tailwindTemplate} from 'src/constants/templates';
import {getAutocompleteMultiselect} from 'src/prompts';

Expand Down Expand Up @@ -55,7 +55,7 @@ export async function addAction(components: string[], options: AddActionOptions)
if (!components.length && !all) {
components = await getAutocompleteMultiselect(
'Which components would you like to add?',
nextUIComponents
store.nextUIComponents
.filter(
(component) =>
!currentComponents.some((currentComponent) => currentComponent.name === component.name)
Expand Down Expand Up @@ -129,7 +129,7 @@ export async function addAction(components: string[], options: AddActionOptions)
);
const missingDependencies = [
..._missingDependencies,
...components.map((c) => nextUIComponentsMap[c]!.package)
...components.map((c) => store.nextUIComponentsMap[c]!.package)
];

Logger.info(
Expand Down
5 changes: 3 additions & 2 deletions src/actions/list-action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Logger} from '@helpers/logger';
import {outputComponents} from '@helpers/output-info';
import {getPackageInfo} from '@helpers/package';
import {store} from 'src/constants/store';

import {type NextUIComponents, nextUIComponents} from '../../src/constants/component';
import {type NextUIComponents} from '../../src/constants/component';
import {resolver} from '../../src/constants/path';

interface ListActionOptions {
Expand All @@ -13,7 +14,7 @@ interface ListActionOptions {
export async function listAction(options: ListActionOptions) {
const {packagePath = resolver('package.json'), remote = false} = options;

let components = nextUIComponents as NextUIComponents;
let components = store.nextUIComponents as NextUIComponents;

try {
/** ======================== Get the installed components ======================== */
Expand Down
11 changes: 7 additions & 4 deletions src/actions/upgrade-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {Logger} from '@helpers/logger';
import {getPackageInfo} from '@helpers/package';
import {upgrade} from '@helpers/upgrade';
import {getColorVersion, getPackageManagerInfo} from '@helpers/utils';
import {type NextUIComponents, nextUIComponentsMap} from 'src/constants/component';
import {type NextUIComponents} from 'src/constants/component';
import {resolver} from 'src/constants/path';
import {NEXT_UI} from 'src/constants/required';
import {store} from 'src/constants/store';
import {getAutocompleteMultiselect, getSelect} from 'src/prompts';
import {getLatestVersion} from 'src/scripts/helpers';

Expand All @@ -34,7 +35,9 @@ export async function upgradeAction(components: string[], options: UpgradeAction
>[] = [];

for (const component of currentComponents) {
const latestVersion = await getLatestVersion(component.package);
const latestVersion =
store.nextUIComponentsMap[component.name]?.version ||
(await getLatestVersion(component.package));

transformComponents.push({
...component,
Expand Down Expand Up @@ -82,8 +85,8 @@ export async function upgradeAction(components: string[], options: UpgradeAction
}

components = components.map((c) => {
if (nextUIComponentsMap[c]?.package) {
return nextUIComponentsMap[c]!.package;
if (store.nextUIComponentsMap[c]?.package) {
return store.nextUIComponentsMap[c]!.package;
}

return c;
Expand Down
54 changes: 42 additions & 12 deletions src/constants/component.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
import {getComponents} from 'src/scripts/helpers';
import type {Components} from 'src/scripts/helpers';

export const nextUIComponents = (await getComponents()).components;
import {store} from './store';

export const nextUIComponentsKeys = nextUIComponents.map((component) => component.name);
export const nextUIcomponentsPackages = nextUIComponents.map((component) => component.package);
export function getNextuiComponentsData(nextUIComponents: Components) {
const nextUIComponentsKeys = nextUIComponents.map((component) => component.name);
const nextUIcomponentsPackages = nextUIComponents.map((component) => component.package);

export const nextUIComponentsKeysSet = new Set(nextUIComponentsKeys);
const nextUIComponentsKeysSet = new Set(nextUIComponentsKeys);

export const nextUIComponentsMap = nextUIComponents.reduce(
(acc, component) => {
const nextUIComponentsMap = nextUIComponents.reduce((acc, component) => {
acc[component.name] = component;

return acc;
},
{} as Record<string, (typeof nextUIComponents)[number]>
);
export type NextUIComponentsMap = Record<string, (typeof nextUIComponents)[number]>;
}, {} as NextUIComponentsMap);
const nextUIComponentsPackageMap = nextUIComponents.reduce((acc, component) => {
acc[component.package] = component;

return acc;
}, {} as NextUIComponentsMap);

return {
nextUIComponentsKeys,
nextUIComponentsKeysSet,
nextUIComponentsMap,
nextUIComponentsPackageMap,
nextUIcomponentsPackages
};
}

export function initStoreComponentsData(nextUIComponents: Components) {
const {
nextUIComponentsKeys,
nextUIComponentsKeysSet,
nextUIComponentsMap,
nextUIComponentsPackageMap,
nextUIcomponentsPackages
} = getNextuiComponentsData(nextUIComponents);

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]>;

export const orderNextUIComponentKeys = ['package', 'version', 'status', 'docs'] as const;

Expand All @@ -24,7 +54,7 @@ export const colorNextUIComponentKeys = ['package', 'version', 'status'];
// eslint-disable-next-line @typescript-eslint/ban-types
export type NextUIComponentStatus = 'stable' | 'updated' | 'new' | (string & {});

export type NextUIComponent = (typeof nextUIComponents)[0];
export type NextUIComponent = (typeof store.nextUIComponents)[0];

export type NextUIComponents = (Omit<NextUIComponent, 'status'> & {
status: NextUIComponentStatus;
Expand Down
2 changes: 2 additions & 0 deletions src/constants/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {getPackageInfo} from '@helpers/package';
import {type NextUIComponent, type NextUIComponents} from './component';
import {resolver} from './path';

export const NEXTUI_CLI = 'nextui-cli';

export const FRAMER_MOTION = 'framer-motion';
export const TAILWINDCSS = 'tailwindcss';
export const NEXT_UI = '@nextui-org/react';
Expand Down
42 changes: 42 additions & 0 deletions src/constants/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type {ExtractStoreData, SAFE_ANY} from '@helpers/type';

import {type Components, getLatestVersion} from 'src/scripts/helpers';

import {NEXTUI_CLI, NEXT_UI} from './required';

export type NextUIComponentsMap = Record<string, Components[0]>;

export type Store = {
cliLatestVersion: string;
latestVersion: string;
nextUIComponents: Components;
nextUIComponentsKeys: string[];
nextUIcomponentsPackages: string[];
nextUIComponentsKeysSet: Set<string>;
nextUIComponentsMap: NextUIComponentsMap;
nextUIComponentsPackageMap: NextUIComponentsMap;
};

export const store = {} as Store;

export type StoreKeys = keyof Store;

export async function getStore<T extends StoreKeys = StoreKeys>(
key: T
): Promise<ExtractStoreData<T>> {
let data = store[key];

if (!data) {
if (key === 'latestVersion') {
data = (await getLatestVersion(NEXT_UI)) as SAFE_ANY;

store[key] = data;
} else if (key === 'cliLatestVersion') {
data = (await getLatestVersion(NEXTUI_CLI)) as SAFE_ANY;

store[key] = data;
}
}

return data as unknown as Promise<ExtractStoreData<T>>;
}
11 changes: 4 additions & 7 deletions src/helpers/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import {readFileSync} from 'fs';

import chalk from 'chalk';

import {
type NextUIComponents,
nextUIComponentsKeys,
nextUIComponentsKeysSet
} from 'src/constants/component';
import {type NextUIComponents} from 'src/constants/component';
import {
DOCS_INSTALLED,
DOCS_TAILWINDCSS_SETUP,
Expand All @@ -23,6 +19,7 @@ import {
pnpmRequired,
tailwindRequired
} from 'src/constants/required';
import {store} from 'src/constants/store';

import {Logger} from './logger';
import {getMatchArray, getMatchImport} from './match';
Expand Down Expand Up @@ -263,8 +260,8 @@ export function checkIllegalComponents(components: string[], loggerError = true)
const illegalList: [string, null | string][] = [];

for (const component of components) {
if (!nextUIComponentsKeysSet.has(component)) {
const matchComponent = findMostMatchText(nextUIComponentsKeys, component);
if (!store.nextUIComponentsKeysSet.has(component)) {
const matchComponent = findMostMatchText(store.nextUIComponentsKeys, component);

illegalList.push([component, matchComponent]);
}
Expand Down
14 changes: 6 additions & 8 deletions src/helpers/package.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {readFileSync} from 'fs';

import {
type NextUIComponents,
nextUIComponents,
nextUIComponentsMap
} from 'src/constants/component';
import {type NextUIComponents} from 'src/constants/component';
import {NEXT_UI} from 'src/constants/required';
import {store} from 'src/constants/store';
import {getLatestVersion} from 'src/scripts/helpers';

import {exec} from './exec';
Expand All @@ -31,7 +28,7 @@ export function getPackageInfo(packagePath: string, transformVersion = true) {
const allDependencies = {...devDependencies, ...dependencies};
const allDependenciesKeys = new Set(Object.keys(allDependencies));

const currentComponents = (nextUIComponents as unknown as NextUIComponents).filter(
const currentComponents = (store.nextUIComponents as unknown as NextUIComponents).filter(
(component) => {
if (allDependenciesKeys.has(component.package)) {
const {currentVersion, versionMode} = getVersionAndMode(allDependencies, component.package);
Expand Down Expand Up @@ -62,7 +59,7 @@ export function getPackageInfo(packagePath: string, transformVersion = true) {

export function transformComponentsToPackage(components: string[]) {
return components.map((component) => {
const nextuiComponent = nextUIComponentsMap[component];
const nextuiComponent = store.nextUIComponentsMap[component];
const packageName = nextuiComponent?.package;

return packageName ? packageName : component;
Expand Down Expand Up @@ -97,7 +94,8 @@ export async function transformPackageDetail(
stdio: 'pipe'
})) || '') as string
).replace(/\n/, '');
const latestVersion = await getLatestVersion(component);
const latestVersion =
store.nextUIComponentsPackageMap[component]?.version || (await getLatestVersion(component));

currentVersion = transformVersion ? `${currentVersion} new: ${latestVersion}` : currentVersion;

Expand Down
18 changes: 18 additions & 0 deletions src/helpers/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type {NextUIComponentsMap, StoreKeys} from 'src/constants/store';
import type {Components} from 'src/scripts/helpers';

/**
* @example 'test-test' => 'TestTest'
*/
Expand Down Expand Up @@ -68,3 +72,17 @@ export type ChalkColor =
| 'bgMagentaBright'
| 'bgCyanBright'
| 'bgWhiteBright';

export type ExtractStoreData<T extends StoreKeys> = T extends 'latestVersion' | 'cliLatestVersion'
? string
: T extends 'nextUIComponents'
? Components
: T extends 'nextUIComponentsKeys' | 'nextUIcomponentsPackages'
? string[]
: T extends 'nextUIComponentsKeysSet'
? Set<string>
: T extends 'nextUIComponentsMap'
? NextUIComponentsMap
: T extends 'nextUIComponentsPackageMap'
? NextUIComponentsMap
: never;
4 changes: 2 additions & 2 deletions src/helpers/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {RequiredKey, SAFE_ANY} from './type';
import chalk from 'chalk';

import {NEXT_UI} from 'src/constants/required';
import {getLatestVersion} from 'src/scripts/helpers';
import {store} from 'src/constants/store';

import {outputBox} from './output-info';
import {getColorVersion, getVersionAndMode} from './utils';
Expand Down Expand Up @@ -78,9 +78,9 @@ export async function upgrade<T extends Upgrade = Upgrade>(options: ExtractUpgra
const {allDependencies, isNextUIAll} = options as Required<Upgrade>;
const {upgradeOptionList} = options as Required<Upgrade>;
let result: UpgradeOption[] = [];
const latestVersion = store.latestVersion;

if (isNextUIAll) {
const latestVersion = await getLatestVersion(NEXT_UI);
const {currentVersion, versionMode} = getVersionAndMode(allDependencies, NEXT_UI);
const colorVersion = getColorVersion(currentVersion, latestVersion);
const isLatest = latestVersion === currentVersion;
Expand Down
Loading

0 comments on commit ea1ef59

Please sign in to comment.