Skip to content

Commit 63cb7ff

Browse files
feat(cli): enhance init command (#7753)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1b037e3 commit 63cb7ff

File tree

13 files changed

+350
-95
lines changed

13 files changed

+350
-95
lines changed

.changeset/busy-pens-send.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
"@biomejs/biome": minor
3+
---
4+
5+
Enhanced the `init` command. The `init` command now checks if the existing project contains known ignore files and known generated folders.
6+
7+
If Biome finds `.gitignore` or `.ignore` files, it will add the following configuration to `biome.json`:
8+
```diff
9+
{
10+
+ "vcs": {
11+
+ "enabled": true,
12+
+ "clientKind": "git",
13+
+ "useIgnoreFile": true
14+
+ }
15+
}
16+
```
17+
18+
If Biome finds a `dist/` folder, it will exclude it automatically using the double-exclude syntax:
19+
20+
```diff
21+
{
22+
+ "files": {
23+
+ "includes": ["**", "!!**/dist"]
24+
+ }
25+
}
26+
```

crates/biome_cli/src/commands/init.rs

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,102 @@
11
use crate::{CliDiagnostic, CliSession};
2-
use biome_configuration::Configuration;
2+
use biome_configuration::vcs::{GIT_IGNORE_FILE_NAME, IGNORE_FILE_NAME, VcsConfiguration};
3+
use biome_configuration::{Configuration, FilesConfiguration};
4+
use biome_console::fmt::{Display, Formatter};
35
use biome_console::{ConsoleExt, markup};
6+
use biome_diagnostics::{Category, Diagnostic, PrintDiagnostic, Severity, category};
47
use biome_fs::ConfigName;
58
use biome_service::configuration::create_config;
69

710
pub(crate) fn init(session: CliSession, emit_jsonc: bool) -> Result<(), CliDiagnostic> {
811
let fs = session.app.workspace.fs();
9-
create_config(fs, Configuration::init(), emit_jsonc)?;
12+
let working_directory = fs.working_directory().unwrap_or_default();
13+
let mut config = Configuration::init();
14+
let mut vcs_enabled = false;
15+
let mut dist_enabled = false;
16+
if fs.path_exists(&working_directory.join(IGNORE_FILE_NAME))
17+
|| fs.path_exists(&working_directory.join(GIT_IGNORE_FILE_NAME))
18+
{
19+
vcs_enabled = true;
20+
config.vcs = Some(VcsConfiguration::new_git_ignore());
21+
}
22+
23+
if fs.path_exists(&working_directory.join("dist")) {
24+
dist_enabled = true;
25+
config.files = Some(FilesConfiguration {
26+
includes: Some(vec![
27+
"**".parse::<biome_glob::NormalizedGlob>().unwrap(),
28+
"!!**/dist".parse::<biome_glob::NormalizedGlob>().unwrap(),
29+
]),
30+
ignore_unknown: None,
31+
max_size: None,
32+
experimental_scanner_ignores: None,
33+
})
34+
}
35+
36+
create_config(fs, config, emit_jsonc)?;
1037
let file_created = if emit_jsonc {
1138
ConfigName::biome_jsonc()
1239
} else {
1340
ConfigName::biome_json()
1441
};
15-
session.app.console.log(markup! {
16-
"
17-
Welcome to Biome! Let's get you started...
42+
let diagnostic = InitDiagnostic {
43+
dist_enabled,
44+
vcs_enabled,
45+
file_created,
46+
};
47+
session
48+
.app
49+
.console
50+
.log(markup! {{PrintDiagnostic::simple(&diagnostic)}});
51+
52+
Ok(())
53+
}
54+
55+
#[derive(Debug)]
56+
struct InitDiagnostic {
57+
dist_enabled: bool,
58+
vcs_enabled: bool,
59+
file_created: &'static str,
60+
}
61+
62+
impl Diagnostic for InitDiagnostic {
63+
fn category(&self) -> Option<&'static Category> {
64+
Some(category!("init"))
65+
}
66+
67+
fn severity(&self) -> Severity {
68+
Severity::Information
69+
}
70+
71+
fn message(&self, fmt: &mut Formatter<'_>) -> std::io::Result<()> {
72+
fmt.write_markup(markup! {
73+
{self}
74+
})
75+
}
76+
}
77+
78+
impl Display for InitDiagnostic {
79+
fn fmt(&self, f: &mut Formatter<'_>) -> std::io::Result<()> {
80+
f.write_markup(markup! {"Welcome to Biome! Let's get you started...
1881
1982
"<Info><Emphasis>"Files created "</Emphasis></Info>"
2083
21-
"<Dim>"- "</Dim><Emphasis>{file_created}</Emphasis>"
22-
Your project configuration. See "<Hyperlink href="https://biomejs.dev/reference/configuration">"https://biomejs.dev/reference/configuration"</Hyperlink>"
84+
"<Dim>"- "</Dim><Emphasis>{self.file_created}</Emphasis>"
85+
Your project configuration. See "<Hyperlink href="https://biomejs.dev/reference/configuration">"https://biomejs.dev/reference/configuration"</Hyperlink>})?;
2386

24-
"<Info><Emphasis>"Next Steps "</Emphasis></Info>"
87+
if self.vcs_enabled {
88+
f.write_markup(markup!{
89+
"\n\nFound an ignore file. Biome enabled "<Hyperlink href="https://biomejs.dev/guides/integrate-in-vcs">"VCS integration."</Hyperlink>
90+
})?;
91+
}
92+
93+
if self.dist_enabled {
94+
f.write_markup(markup!{
95+
"\n\nFound a "<Emphasis>"dist/"</Emphasis>" folder. Biome ignored it "<Hyperlink href="https://biomejs.dev/reference/configuration/#interaction-with-the-scanner">"using the double-exclude syntax."</Hyperlink>
96+
})?;
97+
}
98+
99+
f.write_markup(markup! {<Info><Emphasis>"\n\nNext Steps "</Emphasis></Info>"
25100
26101
"<Dim>"1."</Dim>" "<Emphasis>"Setup an editor extension"</Emphasis>"
27102
Get live errors as you type and format when you save.
@@ -42,6 +117,8 @@ Welcome to Biome! Let's get you started...
42117
Ask questions and contribute on GitHub: "<Hyperlink href="https://github.com/biomejs/biome">"https://github.com/biomejs/biome"</Hyperlink>"
43118
Seek for help on Discord: "<Hyperlink href="https://biomejs.dev/chat">"https://biomejs.dev/chat"</Hyperlink>"
44119
"
45-
});
46-
Ok(())
120+
})?;
121+
122+
Ok(())
123+
}
47124
}

crates/biome_cli/tests/commands/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3429,7 +3429,7 @@ fn html_enabled_by_default() {
34293429
Args::from(["format", "--write", file_path.as_str()].as_slice()),
34303430
);
34313431

3432-
assert!(result.is_ok(), "run_cli returned {result:?}");
3432+
assert!(result.is_err(), "run_cli returned {result:?}");
34333433

34343434
assert_cli_snapshot(SnapshotPayload::new(
34353435
module_path!(),

crates/biome_cli/tests/commands/init.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ fn creates_config_file() {
4040
));
4141
}
4242

43+
#[test]
44+
fn enables_vcs_and_ignore_dist() {
45+
let fs = MemoryFileSystem::default();
46+
let mut console = BufferConsole::default();
47+
48+
fs.insert(".gitignore".into(), "".as_bytes());
49+
fs.insert("dist".into(), "".as_bytes());
50+
51+
let (fs, result) = run_cli(fs, &mut console, Args::from(["init"].as_slice()));
52+
assert!(result.is_ok(), "run_cli returned {result:?}");
53+
54+
assert_cli_snapshot(SnapshotPayload::new(
55+
module_path!(),
56+
"enables_vcs_and_ignore_dist",
57+
fs,
58+
console,
59+
result,
60+
));
61+
}
62+
4363
#[test]
4464
fn creates_config_jsonc_file() {
4565
let fs = MemoryFileSystem::default();

crates/biome_cli/tests/snapshots/main_commands_format/html_enabled_by_default.snap

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@ expression: redactor(content)
55
## `file.html`
66

77
```html
8-
<!DOCTYPE html>
8+
<!DOCTYPE HTML>
9+
```
10+
11+
# Termination Message
12+
13+
```block
14+
format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
15+
16+
× No files were processed in the specified paths.
17+
18+
i Check your biome.json or biome.jsonc to ensure the paths are not ignored by the configuration.
19+
20+
i These paths were provided but ignored:
21+
22+
- file.html
23+
24+
925
1026
```
1127

1228
# Emitted Messages
1329

1430
```block
15-
Formatted 1 file in <TIME>. Fixed 1 file.
31+
Formatted 0 files in <TIME>. No fixes applied.
1632
```

crates/biome_cli/tests/snapshots/main_commands_init/creates_config_file.snap

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,36 @@ expression: redactor(content)
4444
# Emitted Messages
4545

4646
```block
47+
init ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4748
48-
Welcome to Biome! Let's get you started...
49-
50-
Files created
51-
52-
- biome.json
53-
Your project configuration. See https://biomejs.dev/reference/configuration
54-
55-
Next Steps
56-
57-
1. Setup an editor extension
58-
Get live errors as you type and format when you save.
59-
Learn more at https://biomejs.dev/guides/editors/first-party-extensions/
60-
61-
2. Try a command
62-
biome check checks formatting, import sorting, and lint rules.
63-
biome --help displays the available commands.
64-
65-
3. Migrate from ESLint and Prettier
66-
biome migrate eslint migrates your ESLint configuration to Biome.
67-
biome migrate prettier migrates your Prettier configuration to Biome.
68-
69-
4. Read the documentation
70-
Find guides and documentation at https://biomejs.dev/guides/getting-started/
71-
72-
5. Get involved with the community
73-
Ask questions and contribute on GitHub: https://github.com/biomejs/biome
74-
Seek for help on Discord: https://biomejs.dev/chat
49+
i Welcome to Biome! Let's get you started...
50+
51+
Files created
52+
53+
- biome.json
54+
Your project configuration. See https://biomejs.dev/reference/configuration
55+
56+
Next Steps
57+
58+
1. Setup an editor extension
59+
Get live errors as you type and format when you save.
60+
Learn more at https://biomejs.dev/guides/editors/first-party-extensions/
61+
62+
2. Try a command
63+
biome check checks formatting, import sorting, and lint rules.
64+
biome --help displays the available commands.
65+
66+
3. Migrate from ESLint and Prettier
67+
biome migrate eslint migrates your ESLint configuration to Biome.
68+
biome migrate prettier migrates your Prettier configuration to Biome.
69+
70+
4. Read the documentation
71+
Find guides and documentation at https://biomejs.dev/guides/getting-started/
72+
73+
5. Get involved with the community
74+
Ask questions and contribute on GitHub: https://github.com/biomejs/biome
75+
Seek for help on Discord: https://biomejs.dev/chat
76+
77+
7578
7679
```

crates/biome_cli/tests/snapshots/main_commands_init/creates_config_file_when_biome_installed_via_package_manager.snap

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,36 @@ expression: redactor(content)
5050
# Emitted Messages
5151

5252
```block
53+
init ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
5354
54-
Welcome to Biome! Let's get you started...
55-
56-
Files created
57-
58-
- biome.json
59-
Your project configuration. See https://biomejs.dev/reference/configuration
60-
61-
Next Steps
62-
63-
1. Setup an editor extension
64-
Get live errors as you type and format when you save.
65-
Learn more at https://biomejs.dev/guides/editors/first-party-extensions/
66-
67-
2. Try a command
68-
biome check checks formatting, import sorting, and lint rules.
69-
biome --help displays the available commands.
70-
71-
3. Migrate from ESLint and Prettier
72-
biome migrate eslint migrates your ESLint configuration to Biome.
73-
biome migrate prettier migrates your Prettier configuration to Biome.
74-
75-
4. Read the documentation
76-
Find guides and documentation at https://biomejs.dev/guides/getting-started/
77-
78-
5. Get involved with the community
79-
Ask questions and contribute on GitHub: https://github.com/biomejs/biome
80-
Seek for help on Discord: https://biomejs.dev/chat
55+
i Welcome to Biome! Let's get you started...
56+
57+
Files created
58+
59+
- biome.json
60+
Your project configuration. See https://biomejs.dev/reference/configuration
61+
62+
Next Steps
63+
64+
1. Setup an editor extension
65+
Get live errors as you type and format when you save.
66+
Learn more at https://biomejs.dev/guides/editors/first-party-extensions/
67+
68+
2. Try a command
69+
biome check checks formatting, import sorting, and lint rules.
70+
biome --help displays the available commands.
71+
72+
3. Migrate from ESLint and Prettier
73+
biome migrate eslint migrates your ESLint configuration to Biome.
74+
biome migrate prettier migrates your Prettier configuration to Biome.
75+
76+
4. Read the documentation
77+
Find guides and documentation at https://biomejs.dev/guides/getting-started/
78+
79+
5. Get involved with the community
80+
Ask questions and contribute on GitHub: https://github.com/biomejs/biome
81+
Seek for help on Discord: https://biomejs.dev/chat
82+
83+
8184
8285
```

0 commit comments

Comments
 (0)