From 439f3c0e92955c9bebbced3a78768a9e64e67325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= Date: Wed, 3 Jul 2024 21:25:47 +0800 Subject: [PATCH] fix(cli): fix common args - Fix alias of common args wont be resolved by citty Related Issue: #1123 --- packages/cli/src/index.ts | 10 ++++++++-- packages/cli/src/types.ts | 6 +++--- packages/cli/src/utils.ts | 27 +++++++++++++++++++-------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 0f500b661..a89f4a549 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -112,7 +112,10 @@ const buildCommand = defineCommand({ } }, async run({ args }: { args: FarmCLICommonOptions & FarmCLIBuildOptions }) { - const { root, configPath } = resolveCliConfig(args.root, args.config); + const { root, configPath } = resolveCliConfig( + args.root, + args.config ?? args.c + ); const defaultOptions = { root, @@ -169,7 +172,10 @@ const watchCommand = defineCommand({ } }, async run({ args }: { args: FarmCLIBuildOptions & GlobalFarmCLIOptions }) { - const { root, configPath } = resolveCliConfig(args.root, args.config); + const { root, configPath } = resolveCliConfig( + args.root, + args.config ?? args.c + ); const defaultOptions = { root, diff --git a/packages/cli/src/types.ts b/packages/cli/src/types.ts index b963095a1..d457f4976 100644 --- a/packages/cli/src/types.ts +++ b/packages/cli/src/types.ts @@ -1,6 +1,6 @@ export interface FarmCLICommonOptions { _?: string[]; - c?: boolean | string; + c?: string; config?: string; m?: 'development' | 'production' | string; mode?: 'development' | 'production' | string; @@ -59,10 +59,10 @@ export interface NormalizedFarmCLIBuildOptions extends FarmCLIBuildOptions { export interface GlobalFarmCLIOptions { _?: string[]; - c?: boolean | string; + c?: string; config?: string; configPath?: string; - m?: string; + m?: 'development' | 'production'; base?: string; mode?: 'development' | 'production'; w?: boolean; diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts index 09e3d0b84..73652f5e6 100644 --- a/packages/cli/src/utils.ts +++ b/packages/cli/src/utils.ts @@ -7,12 +7,7 @@ import { Logger } from '@farmfe/core'; import spawn from 'cross-spawn'; import walkdir from 'walkdir'; -import type { - FarmCLICommonOptions, - FarmCLIServerOptions, - GlobalFarmCLIOptions, - ICleanOptions -} from './types.js'; +import type { FarmCLICommonOptions, GlobalFarmCLIOptions } from './types.js'; const logger = new Logger(); interface installProps { @@ -99,7 +94,7 @@ export async function install(options: installProps): Promise { } /** * 用于规范化目标路径 - * @param {string |undefined} targetDir + * @param {string | undefined} targetDir * @returns */ export function formatTargetDir(targetDir: string | undefined) { @@ -148,7 +143,7 @@ export function cleanOptions(options: GlobalFarmCLIOptions) { export function resolveCommandOptions( options: GlobalFarmCLIOptions ): GlobalFarmCLIOptions { - const resolveOptions = { ...options }; + const resolveOptions = resolveCommonOptions({ ...options }); filterDuplicateOptions(resolveOptions); return cleanOptions(resolveOptions); } @@ -194,3 +189,19 @@ export function resolveCliConfig(root: string, config: string) { configPath }; } + +/** + * resolve common options to make sure they are consistent with the their alias + * @param {FarmCLICommonOptions & GlobalFarmCLIOptions} options + * @returns {GlobalFarmCLIOptions} + */ +export function resolveCommonOptions( + options: FarmCLICommonOptions & GlobalFarmCLIOptions +): GlobalFarmCLIOptions { + const resolvedOptions = { ...options }; + resolvedOptions.c && (resolvedOptions.config = resolvedOptions.c); + resolvedOptions.config && (resolvedOptions.c = resolvedOptions.config); + resolvedOptions.m && (resolvedOptions.mode = resolvedOptions.m); + resolvedOptions.mode && (resolvedOptions.m = resolvedOptions.mode); + return resolvedOptions; +}