Skip to content

Commit 0ea1599

Browse files
authored
feat(vite-plus): add oxfmt and oxlint config types (#328)
1 parent 4417251 commit 0ea1599

14 files changed

Lines changed: 1310 additions & 165 deletions

File tree

.oxfmtrc.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

.typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ extend-exclude = [
77
"**/snap-tests/**/snap.txt",
88
"crates/fspy_detours_sys/detours",
99
"crates/fspy_detours_sys/src/generated_bindings.rs",
10+
"packages/cli/src/oxfmt-config.ts",
11+
"packages/cli/src/oxlint-config.ts",
1012
]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
],
3737
"*.@(js|ts|tsx)": [
3838
"oxlint -- --fix",
39-
"oxfmt"
39+
"vite fmt"
4040
],
4141
"*.rs": [
4242
"cargo fmt --"

packages/cli/binding/src/cli.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,16 @@ use vite_task::{
1818
};
1919

2020
use crate::commands::{
21-
add::AddCommand,
22-
dedupe::DedupeCommand,
23-
doc::doc as doc_cmd,
24-
fmt::{FmtConfig, fmt},
25-
install::InstallCommand,
26-
lib_cmd::lib,
27-
link::LinkCommand,
28-
lint::{LintConfig, lint},
29-
outdated::OutdatedCommand,
30-
pm::PmCommand,
31-
remove::RemoveCommand,
32-
test::test,
33-
unlink::UnlinkCommand,
34-
update::UpdateCommand,
35-
vite::vite as vite_cmd,
36-
why::WhyCommand,
21+
add::AddCommand, dedupe::DedupeCommand, doc::doc as doc_cmd, fmt::fmt, install::InstallCommand,
22+
lib_cmd::lib, link::LinkCommand, lint::lint, outdated::OutdatedCommand, pm::PmCommand,
23+
remove::RemoveCommand, test::test, unlink::UnlinkCommand, update::UpdateCommand,
24+
vite::vite as vite_cmd, why::WhyCommand,
3725
};
3826

3927
#[derive(Debug, Clone, Serialize, Deserialize)]
4028
pub struct ResolvedUniversalViteConfig {
41-
pub lint: Option<LintConfig>,
42-
pub fmt: Option<FmtConfig>,
29+
pub lint: Option<serde_json::Value>,
30+
pub fmt: Option<serde_json::Value>,
4331
}
4432

4533
#[derive(Parser, Debug)]
@@ -993,9 +981,29 @@ pub async fn main<
993981
}
994982
Commands::Fmt { args } => {
995983
let workspace = Workspace::partial_load(cwd)?;
984+
let vite_config = read_vite_config_from_workspace_root(
985+
workspace.root_dir(),
986+
options.as_ref().map(|o| &o.resolve_universal_vite_config),
987+
)
988+
.await?;
996989
let fmt_fn =
997990
options.map(|o| o.fmt).expect("fmt command requires CliOptions to be provided");
998-
991+
let resolved_vite_config: Option<ResolvedUniversalViteConfig> = vite_config
992+
.map(|vite_config| {
993+
serde_json::from_str(&vite_config).inspect_err(|_| {
994+
tracing::error!("Failed to parse vite config: {vite_config}");
995+
})
996+
})
997+
.transpose()?;
998+
let fmt_config = resolved_vite_config.and_then(|c| c.fmt);
999+
if let Some(fmt_config) = fmt_config {
1000+
let oxfmt_config_path = workspace.cache_path().join(".oxfmtrc.json");
1001+
write(&oxfmt_config_path, serde_json::to_string(&fmt_config)?).await?;
1002+
args.extend_from_slice(&[
1003+
"--config".to_string(),
1004+
oxfmt_config_path.as_path().to_string_lossy().into_owned(),
1005+
]);
1006+
}
9991007
let summary = fmt(fmt_fn, &workspace, args).await?;
10001008
workspace.unload().await?;
10011009
summary

packages/cli/binding/src/commands/fmt.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
use std::{collections::HashMap, future::Future, iter::once};
1+
use std::{future::Future, iter::once};
22

33
use petgraph::stable_graph::StableGraph;
4-
use serde::{Deserialize, Serialize};
5-
use serde_json::Value;
64
use vite_error::Error as ViteError;
75
use vite_task::{
86
Error, ExecutionPlan, ExecutionSummary, ResolveCommandResult, ResolvedTask, Workspace,
97
};
108

11-
#[derive(Debug, Clone, Serialize, Deserialize)]
12-
pub struct FmtConfig {
13-
pub rules: HashMap<String, Value>,
14-
}
15-
169
#[tracing::instrument(skip(resolve_fmt_command, workspace))]
1710
pub async fn fmt<
1811
Fmt: Future<Output = Result<ResolveCommandResult, ViteError>>,

packages/cli/binding/src/commands/lint.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
use std::{collections::HashMap, future::Future, iter::once};
1+
use std::{future::Future, iter::once};
22

33
use petgraph::stable_graph::StableGraph;
4-
use serde::{Deserialize, Serialize};
54
use vite_error::Error as ViteError;
65
use vite_task::{
76
Error, ExecutionPlan, ExecutionSummary, ResolveCommandResult, ResolvedTask, Workspace,
87
};
98

10-
#[derive(Debug, Clone, Serialize, Deserialize)]
11-
pub struct LintConfig {
12-
pub rules: HashMap<String, String>,
13-
}
14-
159
#[tracing::instrument(skip(resolve_lint_command, workspace))]
1610
pub async fn lint<
1711
Lint: Future<Output = Result<ResolveCommandResult, ViteError>>,

packages/cli/build.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
12
import { dirname, join } from 'node:path';
23
import { fileURLToPath } from 'node:url';
34

@@ -32,8 +33,18 @@ async function buildNapiBinding() {
3233
});
3334

3435
const outputs = await task;
36+
const fmtConfigPath = join(projectDir, '../../node_modules/.vite/task-cache/.oxfmtrc.json');
37+
if (!existsSync(fmtConfigPath)) {
38+
const viteConfig = await import('../../vite.config');
39+
mkdirSync(dirname(fmtConfigPath), { recursive: true });
40+
writeFileSync(fmtConfigPath, JSON.stringify(viteConfig.default.fmt, null, 2));
41+
}
3542
await format(
36-
outputs.filter((o) => o.kind !== 'node').map((o) => o.path),
43+
[
44+
'-c',
45+
'../../node_modules/.vite/task-cache/.oxfmtrc.json',
46+
...outputs.filter((o) => o.kind !== 'node').map((o) => o.path),
47+
],
3748
formatEmbeddedCode,
3849
);
3950
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Options:
5252

5353

5454
> vite fmt -h # fmt help message
55-
Usage: [--check | --list-different] [-c=PATH] [--ignore-path=PATH]... [PATH]...
55+
Usage: [-c=PATH] [PATH]...
5656

5757
Output Options
5858
--check Check mode - check if files are formatted, also show statistics
@@ -67,15 +67,15 @@ Ignore Options
6767
used.
6868
--with-node-modules Format code in node_modules directory (skipped by default)
6969

70-
Miscellaneous
70+
Misc Options
7171
--lsp Start language server protocol (LSP) server
7272
--no-error-on-unmatched-pattern Do not exit with error when pattern is unmatched
7373
--threads=INT Number of threads to use. Set to 1 for using only 1 CPU core.
7474

7575
Available positional items:
7676
PATH Single file, single path or list of paths. If not provided, current
7777
working directory is used. Glob is supported only for exclude patterns
78-
like `'!**/fixtures/*.js'.
78+
like `'!**/fixtures/*.js'`.
7979

8080
Available options:
8181
-h, --help Prints help information

packages/cli/src/index.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
import { defineConfig } from '@voidzero-dev/vite-plus-test/config';
22

3-
// replace it with the real oxlint config in the future
4-
export type LintConfig = {
5-
rules: {
6-
[key: string]: string;
7-
};
8-
};
9-
10-
export type FmtConfig = {
11-
rules: {
12-
[key: string]: string;
13-
};
14-
};
3+
import type { OxfmtConfig } from './oxfmt-config';
4+
import type { OxlintConfig } from './oxlint-config';
155

166
declare module '@voidzero-dev/vite-plus-core' {
177
interface UserConfig {
188
/**
199
* Options for oxlint
2010
*/
21-
lint?: LintConfig;
11+
lint?: OxlintConfig;
2212

23-
fmt?: FmtConfig;
13+
fmt?: OxfmtConfig;
2414
}
2515
}
2616

0 commit comments

Comments
 (0)