Skip to content

Commit 3c2ac72

Browse files
committed
fix(create): inject default fmt: {} config and rebrand oxfmt messages (#1163)
Projects created by `vp create` or migrated via `vp migrate` now get `fmt: {}` in vite.config.ts, preventing the "No config found" message when running `vp fmt`. For projects that genuinely lack fmt config, the stderr message now suggests `vp fmt --init` instead of `oxfmt --init`.
1 parent 26b9d3a commit 3c2ac72

File tree

29 files changed

+137
-1
lines changed

29 files changed

+137
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/vite_migration/src/vite_config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,27 @@ export default defineConfig({
10121012
);
10131013
}
10141014

1015+
#[test]
1016+
fn test_merge_json_config_content_empty_object() {
1017+
let vite_config = r#"import { defineConfig } from 'vite-plus';
1018+
1019+
export default defineConfig({
1020+
lint: { options: { typeAware: true, typeCheck: true } },
1021+
});"#;
1022+
1023+
let result = merge_json_config_content(vite_config, "{}", "fmt").unwrap();
1024+
assert!(result.updated);
1025+
assert_eq!(
1026+
result.content,
1027+
r#"import { defineConfig } from 'vite-plus';
1028+
1029+
export default defineConfig({
1030+
fmt: {},
1031+
lint: { options: { typeAware: true, typeCheck: true } },
1032+
});"#
1033+
);
1034+
}
1035+
10151036
#[test]
10161037
fn test_merge_tsdown_config_content_simple() {
10171038
let vite_config = r#"import { defineConfig } from 'vite-plus';

packages/cli/binding/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rolldown = ["dep:rolldown_binding"]
1010
anyhow = { workspace = true }
1111
async-trait = { workspace = true }
1212
clap = { workspace = true, features = ["derive"] }
13+
cow-utils = { workspace = true }
1314
fspy = { workspace = true }
1415
rustc-hash = { workspace = true }
1516
napi = { workspace = true }

packages/cli/binding/src/cli.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use clap::{
1212
Parser, Subcommand,
1313
error::{ContextKind, ContextValue, ErrorKind},
1414
};
15+
use cow_utils::CowUtils;
1516
use owo_colors::OwoColorize;
1617
use rustc_hash::FxHashMap;
1718
use serde::{Deserialize, Serialize};
@@ -743,6 +744,33 @@ async fn resolve_and_execute_with_stdout_filter(
743744
Ok(ExitStatus(output.status.code().unwrap_or(1) as u8))
744745
}
745746

747+
/// Like `resolve_and_execute`, but captures stderr, applies a text filter,
748+
/// and writes the result to real stderr. Stdout remains inherited (streaming).
749+
async fn resolve_and_execute_with_stderr_filter(
750+
resolver: &SubcommandResolver,
751+
subcommand: SynthesizableSubcommand,
752+
resolved_vite_config: Option<&ResolvedUniversalViteConfig>,
753+
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
754+
cwd: &AbsolutePathBuf,
755+
cwd_arc: &Arc<AbsolutePath>,
756+
filter: impl Fn(&str) -> Cow<'_, str>,
757+
) -> Result<ExitStatus, Error> {
758+
let mut cmd =
759+
resolve_and_build_command(resolver, subcommand, resolved_vite_config, envs, cwd, cwd_arc)
760+
.await?;
761+
cmd.stderr(Stdio::piped());
762+
763+
let child = cmd.spawn().map_err(|e| Error::Anyhow(e.into()))?;
764+
let output = child.wait_with_output().await.map_err(|e| Error::Anyhow(e.into()))?;
765+
766+
use std::io::Write;
767+
let stderr = String::from_utf8_lossy(&output.stderr);
768+
let filtered = filter(&stderr);
769+
let _ = std::io::stderr().lock().write_all(filtered.as_bytes());
770+
771+
Ok(ExitStatus(output.status.code().unwrap_or(1) as u8))
772+
}
773+
746774
struct CapturedCommandOutput {
747775
status: ExitStatus,
748776
stdout: String,
@@ -1258,6 +1286,17 @@ async fn execute_direct_subcommand(
12581286
|_| Cow::Borrowed(""),
12591287
)
12601288
.await?
1289+
} else if matches!(&other, SynthesizableSubcommand::Fmt { .. }) {
1290+
resolve_and_execute_with_stderr_filter(
1291+
&resolver,
1292+
other,
1293+
None,
1294+
&envs,
1295+
cwd,
1296+
&cwd_arc,
1297+
|s| s.cow_replace("oxfmt --init", "vp fmt --init"),
1298+
)
1299+
.await?
12611300
} else {
12621301
resolve_and_execute(&resolver, other, None, &envs, cwd, &cwd_arc).await?
12631302
}

packages/cli/snap-tests-global/create-missing-typecheck/snap.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default defineConfig({
66
staged: {
77
"*": "vp check --fix",
88
},
9+
fmt: {},
910
lint: { options: { typeAware: true, typeCheck: true } },
1011
});
1112

@@ -17,6 +18,7 @@ export default defineConfig({
1718
staged: {
1819
"*": "vp check --fix",
1920
},
21+
fmt: {},
2022
lint: { options: { typeAware: true, typeCheck: true } },
2123
});
2224

packages/cli/snap-tests-global/migration-baseurl-tsconfig/snap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
staged: {
1616
"*": "vp check --fix"
1717
},
18+
fmt: {},
1819
lint: {
1920
"rules": {
2021
"no-unused-vars": "error"

packages/cli/snap-tests-global/migration-chained-lint-staged-pre-commit/snap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
3030
import { defineConfig } from 'vite-plus';
3131

3232
export default defineConfig({
33+
fmt: {},
3334
lint: {"options":{"typeAware":true,"typeCheck":true}},
3435
staged: {
3536
"*.js": "vp lint --fix"

packages/cli/snap-tests-global/migration-env-prefix-lint-staged/snap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
3030
import { defineConfig } from 'vite-plus';
3131

3232
export default defineConfig({
33+
fmt: {},
3334
lint: {"options":{"typeAware":true,"typeCheck":true}},
3435
staged: {
3536
"*.js": "vp lint --fix"

packages/cli/snap-tests-global/migration-eslint-lint-staged/snap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
3030
import { defineConfig } from 'vite-plus';
3131

3232
export default defineConfig({
33+
fmt: {},
3334
lint: {
3435
"plugins": [
3536
"oxc",

packages/cli/snap-tests-global/migration-eslint-lintstagedrc/snap.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ VITE+ - The Unified Toolchain for the Web
3131
import { defineConfig } from 'vite-plus';
3232

3333
export default defineConfig({
34+
fmt: {},
3435
lint: {
3536
"plugins": [
3637
"oxc",

0 commit comments

Comments
 (0)