Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add canary option support #115

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"dependencies": {
"@clack/prompts": "0.7.0",
"@winches/prompts": "0.0.6",
"@winches/prompts": "0.0.7",
"async-retry": "1.3.3",
"chalk": "5.3.0",
"commander": "11.0.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/actions/add-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {existsSync, writeFileSync} from 'node:fs';
import chalk from 'chalk';

import {getBetaComponents} from '@helpers/beta';
import {getCanaryComponents} from '@helpers/canary';
import {
checkApp,
checkIllegalComponents,
Expand Down Expand Up @@ -38,6 +39,7 @@ interface AddActionOptions {
appPath?: string;
addApp?: boolean;
beta?: boolean;
canary?: boolean;
}

export async function addAction(components: string[], options: AddActionOptions) {
Expand All @@ -46,6 +48,7 @@ export async function addAction(components: string[], options: AddActionOptions)
all = false,
appPath = findFiles('**/App.(j|t)sx')[0],
beta = false,
canary = false,
packagePath = resolver('package.json'),
tailwindPath = findFiles('**/tailwind.config.(j|t)s')[0]
} = options;
Expand Down Expand Up @@ -122,7 +125,7 @@ export async function addAction(components: string[], options: AddActionOptions)
const [, ...missingDependencies] = await checkRequiredContentInstalled(
'all',
allDependenciesKeys,
{beta}
{beta, canary}
);

if (missingDependencies.length) {
Expand All @@ -141,11 +144,13 @@ export async function addAction(components: string[], options: AddActionOptions)
const [, ..._missingDependencies] = await checkRequiredContentInstalled(
'partial',
allDependenciesKeys,
{beta}
{beta, canary}
);
const mergedComponents = beta
? await getBetaComponents(components)
: components.map((c) => store.nextUIComponentsMap[c]!.package);
: canary
? await getCanaryComponents(components)
: components.map((c) => store.nextUIComponentsMap[c]!.package);
const missingDependencies = [..._missingDependencies, ...mergedComponents];

Logger.info(
Expand Down
42 changes: 32 additions & 10 deletions src/actions/upgrade-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type {AppendKeyValue} from '@helpers/type';
import fs from 'node:fs';

import {getBetaVersion} from '@helpers/beta';
import {checkIllegalComponents} from '@helpers/check';
import {getCanaryVersion} from '@helpers/canary';
import {checkIllegalComponents, getConditionLatestVersion} from '@helpers/check';
import {detect} from '@helpers/detect';
import {exec} from '@helpers/exec';
import {Logger} from '@helpers/logger';
Expand All @@ -27,13 +28,19 @@ interface UpgradeActionOptions {
patch?: boolean;
write?: boolean;
beta?: boolean;
canary?: boolean;
}

type TransformComponent = Required<
AppendKeyValue<NextUIComponents[0], 'latestVersion', string> & {isLatest: boolean}
>;

function betaCompareVersions(version: string, latestVersion: string, beta: boolean) {
function extraCompareVersions(
version: string,
latestVersion: string,
beta: boolean,
canary: boolean
) {
// compareResult(beta, 2.1.0) = 0
// So we need to check if it is autoChangeTag like `beta` or `canary` and latestVersion is not match `beta` or `canary` then return false
// Example: `beta` Compare `2.1.0` (not latest), `beta` Compare `2.1.0-beta.0` (latest)
Expand All @@ -48,13 +55,18 @@ function betaCompareVersions(version: string, latestVersion: string, beta: boole
// Beta version is greater than latest version if beta is true
// compareResult(2.1.0, 2.1.0-beta.0) = 1
// Example: 2.1.0 < 2.1.0-beta.0
return beta && compareResult === 1 && !version.includes('beta') ? false : compareResult >= 0;
return beta && compareResult === 1 && !version.includes('beta')
? false
: canary && compareResult === 1 && !version.includes('canary')
? false
: compareResult >= 0;
}

export async function upgradeAction(components: string[], options: UpgradeActionOptions) {
const {
all = false,
beta = false,
canary = false,
packagePath = resolver('package.json'),
write = false
} = options;
Expand All @@ -70,8 +82,12 @@ export async function upgradeAction(components: string[], options: UpgradeAction
const latestVersion =
store.nextUIComponentsMap[component.name]?.version ||
(await getLatestVersion(component.package));
const mergedVersion = beta ? await getBetaVersion(component.package) : latestVersion;
const compareResult = betaCompareVersions(component.version, mergedVersion, beta);
const mergedVersion = beta
? await getBetaVersion(component.package)
: canary
? await getCanaryVersion(component.package)
: latestVersion;
const compareResult = extraCompareVersions(component.version, mergedVersion, beta, canary);

transformComponents.push({
...component,
Expand All @@ -93,12 +109,13 @@ export async function upgradeAction(components: string[], options: UpgradeAction
} else if (!components.length) {
// If have the main nextui then add
if (isNextUIAll) {
const version = transformPeerVersion(allDependencies[NEXT_UI]);
const latestVersion = getConditionLatestVersion(store.beta, store.canary);
const nextuiData = {
isLatest:
compareVersions(store.latestVersion, transformPeerVersion(allDependencies[NEXT_UI])) <= 0,
latestVersion: store.latestVersion,
isLatest: extraCompareVersions(version, latestVersion, store.beta, store.canary),
latestVersion,
package: NEXT_UI,
version: transformPeerVersion(allDependencies[NEXT_UI])
version
} as TransformComponent;

transformComponents.push(nextuiData);
Expand All @@ -113,7 +130,12 @@ export async function upgradeAction(components: string[], options: UpgradeAction
components = await getAutocompleteMultiselect(
'Select the components to upgrade',
transformComponents.map((component) => {
const isUpToDate = betaCompareVersions(component.version, component.latestVersion, beta);
const isUpToDate = extraCompareVersions(
component.version,
component.latestVersion,
beta,
canary
);

return {
disabled: isUpToDate,
Expand Down
13 changes: 11 additions & 2 deletions src/constants/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export function getNextuiComponentsData(nextUIComponents: Components) {
}

export function initStoreComponentsData({
beta,
beta = false,
canary = false,
nextUIComponents
}: {
beta: boolean;
beta?: boolean;
canary?: boolean;
nextUIComponents: Components;
}) {
const {
Expand All @@ -50,6 +52,13 @@ export function initStoreComponentsData({
store.betaNextUIComponentsMap = nextUIComponentsMap;
store.betaNextUIComponentsPackageMap = nextUIComponentsPackageMap;
store.betaNextUIcomponentsPackages = nextUIcomponentsPackages;
} else if (canary) {
store.canaryNextUIComponents = nextUIComponents;
store.canaryNextUIComponentsKeys = nextUIComponentsKeys;
store.canaryNextUIComponentsKeysSet = nextUIComponentsKeysSet;
store.canaryNextUIComponentsMap = nextUIComponentsMap;
store.canaryNextUIComponentsPackageMap = nextUIComponentsPackageMap;
store.canaryNextUIcomponentsPackages = nextUIcomponentsPackages;
} else {
store.nextUIComponents = nextUIComponents;
store.nextUIComponentsKeys = nextUIComponentsKeys;
Expand Down
Loading
Loading