Skip to content

Commit

Permalink
fix(cli): fix common args
Browse files Browse the repository at this point in the history
- Fix alias of common args wont be resolved by citty

Related Issue: farm-fe#1123
  • Loading branch information
fu050409 committed Jul 3, 2024
1 parent 8aad3aa commit 439f3c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
10 changes: 8 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface FarmCLICommonOptions {
_?: string[];
c?: boolean | string;
c?: string;
config?: string;
m?: 'development' | 'production' | string;
mode?: 'development' | 'production' | string;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 19 additions & 8 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -99,7 +94,7 @@ export async function install(options: installProps): Promise<void> {
}
/**
* 用于规范化目标路径
* @param {string |undefined} targetDir
* @param {string | undefined} targetDir
* @returns
*/
export function formatTargetDir(targetDir: string | undefined) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 439f3c0

Please sign in to comment.