Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions crates/vite_migration/src/vite_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,27 @@ export default defineConfig({
);
}

#[test]
fn test_merge_json_config_content_empty_object() {
let vite_config = r#"import { defineConfig } from 'vite-plus';

export default defineConfig({
lint: { options: { typeAware: true, typeCheck: true } },
});"#;

let result = merge_json_config_content(vite_config, "{}", "fmt").unwrap();
assert!(result.updated);
assert_eq!(
result.content,
r#"import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: { options: { typeAware: true, typeCheck: true } },
});"#
);
}

#[test]
fn test_merge_tsdown_config_content_simple() {
let vite_config = r#"import { defineConfig } from 'vite-plus';
Expand Down
1 change: 1 addition & 0 deletions packages/cli/binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rolldown = ["dep:rolldown_binding"]
anyhow = { workspace = true }
async-trait = { workspace = true }
clap = { workspace = true, features = ["derive"] }
cow-utils = { workspace = true }
fspy = { workspace = true }
rustc-hash = { workspace = true }
napi = { workspace = true }
Expand Down
39 changes: 39 additions & 0 deletions packages/cli/binding/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::{
Parser, Subcommand,
error::{ContextKind, ContextValue, ErrorKind},
};
use cow_utils::CowUtils;
use owo_colors::OwoColorize;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -743,6 +744,33 @@ async fn resolve_and_execute_with_stdout_filter(
Ok(ExitStatus(output.status.code().unwrap_or(1) as u8))
}

/// Like `resolve_and_execute`, but captures stderr, applies a text filter,
/// and writes the result to real stderr. Stdout remains inherited (streaming).
async fn resolve_and_execute_with_stderr_filter(
resolver: &SubcommandResolver,
subcommand: SynthesizableSubcommand,
resolved_vite_config: Option<&ResolvedUniversalViteConfig>,
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
cwd: &AbsolutePathBuf,
cwd_arc: &Arc<AbsolutePath>,
filter: impl Fn(&str) -> Cow<'_, str>,
) -> Result<ExitStatus, Error> {
let mut cmd =
resolve_and_build_command(resolver, subcommand, resolved_vite_config, envs, cwd, cwd_arc)
.await?;
cmd.stderr(Stdio::piped());

let child = cmd.spawn().map_err(|e| Error::Anyhow(e.into()))?;
let output = child.wait_with_output().await.map_err(|e| Error::Anyhow(e.into()))?;

use std::io::Write;
let stderr = String::from_utf8_lossy(&output.stderr);
let filtered = filter(&stderr);
let _ = std::io::stderr().lock().write_all(filtered.as_bytes());

Ok(ExitStatus(output.status.code().unwrap_or(1) as u8))
}

struct CapturedCommandOutput {
status: ExitStatus,
stdout: String,
Expand Down Expand Up @@ -1258,6 +1286,17 @@ async fn execute_direct_subcommand(
|_| Cow::Borrowed(""),
)
.await?
} else if matches!(&other, SynthesizableSubcommand::Fmt { .. }) {
resolve_and_execute_with_stderr_filter(
&resolver,
other,
None,
&envs,
cwd,
&cwd_arc,
|s| s.cow_replace("oxfmt --init", "vp fmt --init"),
)
.await?
} else {
resolve_and_execute(&resolver, other, None, &envs, cwd, &cwd_arc).await?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix",
},
fmt: {},
lint: { options: { typeAware: true, typeCheck: true } },
});

Expand All @@ -17,6 +18,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix",
},
fmt: {},
lint: { options: { typeAware: true, typeCheck: true } },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix"
},
fmt: {},
lint: {
"rules": {
"no-unused-vars": "error"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.js": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.js": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {
"plugins": [
"oxc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {
"plugins": [
"oxc",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/snap-tests-global/migration-eslint/snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix"
},
fmt: {},
lint: {
"plugins": [
"oxc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.js": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.ts": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.js": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
"cwd": "./src"
}
},
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
server: {
port: 3000,
Expand Down Expand Up @@ -72,6 +73,7 @@ export default defineConfig({
"cwd": "./src"
}
},
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
server: {
port: 3000,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/snap-tests-global/migration-from-tsdown/snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineConfig({
"*": "vp check --fix"
},
pack: tsdownConfig,
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
});

Expand Down Expand Up @@ -78,6 +79,7 @@ export default defineConfig({
"*": "vp check --fix"
},
pack: tsdownConfig,
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.js": "vp lint --fix"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
"*.@(js|ts|tsx|yml|yaml|md|json|html|toml)": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ lintstagedrc.json still exists
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
staged: {
'*.js': 'vp check --fix',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
staged: {
"*": "vp check --fix"
},
fmt: {},
lint: {
"rules": {
"no-unused-vars": "error"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix"
},
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},
plugins: [react()],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
staged: {
"*": "vp check --fix"
},
fmt: {},
lint: {
"rules": {
"no-unused-vars": "error"
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/snap-tests-global/migration-subpath/snap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ VITE+ - The Unified Toolchain for the Web
import { defineConfig } from 'vite-plus';

export default defineConfig({
fmt: {},
lint: {"options":{"typeAware":true,"typeCheck":true}},

});

> git config --local core.hooksPath || echo 'core.hooksPath is not set' # should NOT be set
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/snap-tests/fmt-no-config-message/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@test/fmt-no-config-message",
"version": "1.0.0",
"private": true,
"type": "module"
}
16 changes: 16 additions & 0 deletions packages/cli/snap-tests/fmt-no-config-message/snap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
> vp fmt 2>&1 # should show 'vp fmt --init' instead of 'oxfmt --init'
Finished in <variable>ms on 4 files using <variable> threads.
No config found, using defaults. Please add a config file or try `vp fmt --init` if needed.

> vp fmt --init
Added 'fmt' to 'vite.config.ts'.

> cat vite.config.ts # should have fmt config
export default {
fmt: {
ignorePatterns: [],
},
};

> vp fmt 2>&1 # should no longer show 'No config found' message
Finished in <variable>ms on 4 files using <variable> threads.
5 changes: 5 additions & 0 deletions packages/cli/snap-tests/fmt-no-config-message/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function hello() {
return 'world';
}

export { hello };
8 changes: 8 additions & 0 deletions packages/cli/snap-tests/fmt-no-config-message/steps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"commands": [
"vp fmt 2>&1 # should show 'vp fmt --init' instead of 'oxfmt --init'",
"vp fmt --init",
"cat vite.config.ts # should have fmt config",
"vp fmt 2>&1 # should no longer show 'No config found' message"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
17 changes: 17 additions & 0 deletions packages/cli/src/migration/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ export function rewriteStandaloneProject(
}
mergeViteConfigFiles(projectPath, silent, report);
injectLintTypeCheckDefaults(projectPath, silent, report);
injectFmtDefaults(projectPath, silent, report);
mergeTsdownConfigFile(projectPath, silent, report);
// rewrite imports in all TypeScript/JavaScript files
rewriteAllImports(projectPath, silent, report);
Expand Down Expand Up @@ -771,6 +772,7 @@ export function rewriteMonorepo(
}
mergeViteConfigFiles(workspaceInfo.rootDir, silent, report);
injectLintTypeCheckDefaults(workspaceInfo.rootDir, silent, report);
injectFmtDefaults(workspaceInfo.rootDir, silent, report);
mergeTsdownConfigFile(workspaceInfo.rootDir, silent, report);
// rewrite imports in all TypeScript/JavaScript files
rewriteAllImports(workspaceInfo.rootDir, silent, report);
Expand Down Expand Up @@ -1340,6 +1342,21 @@ export function injectLintTypeCheckDefaults(
);
}

export function injectFmtDefaults(
projectPath: string,
silent = false,
report?: MigrationReport,
): void {
injectConfigDefaults(
projectPath,
'fmt',
'.vite-plus-fmt-init.oxfmtrc.json',
JSON.stringify({}),
silent,
report,
);
}

function injectConfigDefaults(
projectPath: string,
configKey: string,
Expand Down
Loading