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

fix(cli): when the user enters the same command #10474

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ interface GlobalCLIOptions {
force?: boolean
}

const normalizeOptionsConfig: Record<string, (v: any[]) => any> = {
port: (v) => {
return v.find((p) => typeof p === 'number' || typeof p === 'string') || 5173
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe findLast should be used here? Because if the other options are arrays, the last value you select.

Copy link
Contributor Author

@yucccc yucccc Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
}

const normalizeOptions = <T extends object>(
options: T,
config = normalizeOptionsConfig
): T => {
const c = { ...options }
for (const [key, value] of Object.entries(options)) {
if (Array.isArray(value)) {
// may need to define the final return of option by themselves,
// so they retain their implementation portal.
// If it is not customized, take the last item of InlineConfig as the final result.
c[key as keyof T] = config[key]?.(value) ?? value[value.length - 1]
}
}
return c
}
/**
* removing global flags before passing as command specific sub-configs
*/
Expand All @@ -48,7 +69,7 @@ function cleanOptions<Options extends GlobalCLIOptions>(
delete ret.filter
delete ret.m
delete ret.mode
return ret
return normalizeOptions(ret)
}

cli
Expand Down Expand Up @@ -197,6 +218,7 @@ cli
.action(
async (root: string, options: { force?: boolean } & GlobalCLIOptions) => {
const { optimizeDeps } = await import('./optimizer')
options = normalizeOptions(options)
try {
const config = await resolveConfig(
{
Expand Down Expand Up @@ -238,6 +260,7 @@ cli
} & GlobalCLIOptions
) => {
const { preview } = await import('./preview')
options = normalizeOptions(options)
try {
const server = await preview({
root,
Expand Down