Skip to content

Commit ca2e417

Browse files
semimikohfengmk2
andauthored
feat(cli): add format alias for vp fmt (#1727)
## Summary Closes #1721. Running `vp format` (a common slip for `vp fmt`) previously failed to parse and clap suggested an unrelated command. This adds `format` as a visible alias of `fmt`. ## Changes - Add `visible_alias = "format"` to the `Fmt` subcommand in: - `crates/vite_global_cli/src/cli.rs` (global `vp` binary parser) - `packages/cli/binding/src/cli/types.rs` (local NAPI binding parser) - Normalize `format` → `fmt` in `init-config.ts` so that `vp format --init` and `vp format --migrate` apply the same `vite.config.ts` integration as `vp fmt` (without it, only the underlying oxfmt run happened and the config wiring was silently skipped). - Add a unit test covering `format` as an init alias. ## Validation - `vp format --help` → resolves to `vp fmt` (no more "did you mean" suggestion), verified on both the global binary and the local CLI after `pnpm bootstrap-cli`. - `vp test run init-config` → 9 passed (incl. the new `format` alias case). - Ran `snap-test-global`/`snap-test-local` for `cli-helper-message` and `command-vp-alias`: no snapshot diffs (help output is custom-rendered, so the alias doesn't alter `--help` text). --------- Co-authored-by: MK (fengmk2) <fengmk2@gmail.com>
1 parent 6a5246a commit ca2e417

10 files changed

Lines changed: 34 additions & 15 deletions

File tree

crates/vite_global_cli/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub enum Commands {
136136
},
137137

138138
/// Format code
139-
#[command(disable_help_flag = true)]
139+
#[command(disable_help_flag = true, visible_alias = "format")]
140140
Fmt {
141141
/// Additional arguments
142142
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]

crates/vite_global_cli/src/help.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ pub fn top_level_help_doc() -> HelpDoc {
442442
row("dev", "Run the development server"),
443443
row("check", "Run format, lint, and type checks"),
444444
row("lint", "Lint code"),
445-
row("fmt", "Format code"),
445+
row("fmt, format", "Format code"),
446446
row("test", "Run tests"),
447447
],
448448
),

packages/cli/binding/src/cli/help.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ pub(super) fn print_help() {
187187
{bold}build{reset} Build for production
188188
{bold}test{reset} Run tests
189189
{bold}lint{reset} Lint code
190-
{bold}fmt{reset} Format code
190+
{bold}fmt, format{reset} Format code
191191
{bold}check{reset} Run format, lint, and type checks
192192
{bold}pack{reset} Build library
193193
{bold}run{reset} Run tasks

packages/cli/binding/src/cli/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum SynthesizableSubcommand {
3535
args: Vec<String>,
3636
},
3737
/// Format code
38-
#[command(disable_help_flag = true)]
38+
#[command(disable_help_flag = true, visible_alias = "format")]
3939
Fmt {
4040
#[clap(allow_hyphen_values = true, trailing_var_arg = true)]
4141
args: Vec<String>,

packages/cli/snap-tests-global/cli-helper-message/snap.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Start:
1111
env Manage Node.js versions
1212

1313
Develop:
14-
dev Run the development server
15-
check Run format, lint, and type checks
16-
lint Lint code
17-
fmt Format code
18-
test Run tests
14+
dev Run the development server
15+
check Run format, lint, and type checks
16+
lint Lint code
17+
fmt, format Format code
18+
test Run tests
1919

2020
Execute:
2121
run Run tasks (also available as standalone `vpr`)

packages/cli/snap-tests/cli-helper-message/snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Core Commands:
88
build Build for production
99
test Run tests
1010
lint Lint code
11-
fmt Format code
11+
fmt, format Format code
1212
check Run format, lint, and type checks
1313
pack Build library
1414
run Run tasks

packages/cli/snap-tests/command-helper/snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Core Commands:
88
build Build for production
99
test Run tests
1010
lint Lint code
11-
fmt Format code
11+
fmt, format Format code
1212
check Run format, lint, and type checks
1313
pack Build library
1414
run Run tasks

packages/cli/snap-tests/command-vp-alias/snap.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Core Commands:
88
build Build for production
99
test Run tests
1010
lint Lint code
11-
fmt Format code
11+
fmt, format Format code
1212
check Run format, lint, and type checks
1313
pack Build library
1414
run Run tasks

packages/cli/src/__tests__/init-config.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ export default defineConfig({
204204
expect(fs.existsSync(path.join(projectPath, '.oxfmtrc.json'))).toBe(false);
205205
});
206206

207+
it('treats format as a fmt init alias when merging generated config', async () => {
208+
const projectPath = createTempDir();
209+
fs.writeFileSync(path.join(projectPath, '.oxfmtrc.json'), '{\n "semi": true\n}\n');
210+
211+
const result = await applyToolInitConfigToViteConfig('format', ['--init'], projectPath);
212+
expect(result.handled).toBe(true);
213+
expect(result.action).toBe('added');
214+
215+
const content = fs.readFileSync(path.join(projectPath, 'vite.config.ts'), 'utf8');
216+
expect(content).toContain('fmt:');
217+
expect(content).toContain('semi');
218+
expect(fs.existsSync(path.join(projectPath, '.oxfmtrc.json'))).toBe(false);
219+
});
220+
207221
it('uses explicit --config path when provided', async () => {
208222
const projectPath = createTempDir();
209223
const customConfigPath = path.join(projectPath, 'custom-oxfmt.json');

packages/cli/src/init-config.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const INIT_COMMAND_SPECS: Record<string, InitCommandSpec> = {
2828
},
2929
};
3030

31+
function normalizeInitCommand(command: string | undefined): string | undefined {
32+
return command === 'format' ? 'fmt' : command;
33+
}
34+
3135
const VITE_CONFIG_FILES = [
3236
'vite.config.ts',
3337
'vite.config.mts',
@@ -153,10 +157,11 @@ async function vpFmt(cwd: string, filePath: string): Promise<void> {
153157
}
154158

155159
function resolveInitSpec(command: string | undefined, args: string[]): InitCommandSpec | null {
156-
if (!command) {
160+
const normalizedCommand = normalizeInitCommand(command);
161+
if (!normalizedCommand) {
157162
return null;
158163
}
159-
const spec = INIT_COMMAND_SPECS[command];
164+
const spec = INIT_COMMAND_SPECS[normalizedCommand];
160165
if (!spec || !hasTriggerFlag(args, spec.triggerFlags)) {
161166
return null;
162167
}
@@ -205,7 +210,7 @@ export async function applyToolInitConfigToViteConfig(
205210
if (!inspection.handled || !inspection.configKey) {
206211
return { handled: false };
207212
}
208-
const spec = INIT_COMMAND_SPECS[command as keyof typeof INIT_COMMAND_SPECS];
213+
const spec = INIT_COMMAND_SPECS[normalizeInitCommand(command) as keyof typeof INIT_COMMAND_SPECS];
209214
const viteConfigPath = ensureViteConfigPath(projectPath);
210215
const generatedConfigPath = resolveGeneratedConfigPath(
211216
projectPath,

0 commit comments

Comments
 (0)