Skip to content

Commit 0e033bf

Browse files
committed
ref(init): prompt user for site configuration
This behaviour can be reverted by using `--no-prompt` which will automatically set sane default values as before
1 parent 5f274cf commit 0e033bf

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/cli.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ struct Cli {
3636
enum Commands {
3737
/// Initialize a new Norgolith site
3838
Init {
39+
#[arg(long, default_value_t = true, overrides_with = "_no_prompt", help = "Whether to prompt for site info")]
40+
prompt: bool,
41+
42+
#[arg(long = "no-prompt")]
43+
_no_prompt: bool,
44+
3945
/// Site name
4046
name: Option<String>,
4147
},
@@ -114,7 +120,7 @@ pub async fn start() -> Result<()> {
114120
}
115121

116122
match &cli.command {
117-
Commands::Init { name } => init_site(name.as_ref()).await?,
123+
Commands::Init { name, prompt: _, _no_prompt } => init_site(name.as_ref(), !_no_prompt).await?,
118124
Commands::Theme { subcommand } => theme_handle(subcommand).await?,
119125
Commands::Serve { port, drafts: _, _no_drafts, open } => check_and_serve(*port, !_no_drafts, *open).await?,
120126
Commands::Build { minify } => build_site(*minify).await?,
@@ -134,9 +140,9 @@ pub async fn start() -> Result<()> {
134140
///
135141
/// # Returns:
136142
/// A `Result<()>` indicating success or error. On error, the context message will provide information on why the site could not be initialized.
137-
async fn init_site(name: Option<&String>) -> Result<()> {
143+
async fn init_site(name: Option<&String>, prompt: bool) -> Result<()> {
138144
if let Some(name) = name {
139-
cmd::init(name).await?;
145+
cmd::init(name, prompt).await?;
140146
} else {
141147
bail!("Missing name for the site: could not initialize the new Norgolith site");
142148
}
@@ -229,7 +235,7 @@ mod tests {
229235
#[tokio::test]
230236
async fn test_init_site_with_name() {
231237
let test_name = String::from("my-site");
232-
let result = init_site(Some(&test_name)).await;
238+
let result = init_site(Some(&test_name), false).await;
233239
assert!(result.is_ok());
234240

235241
// Cleanup
@@ -238,7 +244,7 @@ mod tests {
238244

239245
#[tokio::test]
240246
async fn test_init_site_without_name() {
241-
let result = init_site(None).await;
247+
let result = init_site(None, false).await;
242248
assert!(result.is_err());
243249
assert!(result
244250
.unwrap_err()
@@ -274,7 +280,7 @@ mod tests {
274280

275281
// Create temporal site
276282
let test_site_dir = String::from("my-unavailable-site");
277-
init_site(Some(&test_site_dir)).await?;
283+
init_site(Some(&test_site_dir), false).await?;
278284

279285
// Save current directory as the previous directory to restore it later
280286
let previous_dir = std::env::current_dir()?;

src/cmd/init.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use comfy_table::presets::UTF8_FULL;
66
use comfy_table::{Cell, ContentArrangement, Table};
77
use eyre::{bail, Result};
88
use indoc::formatdoc;
9+
use inquire::Text;
910
use tokio::fs;
1011

1112
/// Create basic site configuration TOML
12-
async fn create_config(root: &str) -> Result<()> {
13+
async fn create_config(root: &str, root_url: &str, language: &str, title: &str) -> Result<()> {
1314
let site_config = formatdoc!(
1415
r#"
1516
rootUrl = '{}'
@@ -21,9 +22,9 @@ async fn create_config(root: &str) -> Result<()> {
2122
[highlighter]
2223
enable = false
2324
# engine = 'prism' # Can be 'prism' or 'hljs'. Defaults to 'prism'"#,
24-
"http://localhost:3030", // this is the default port
25-
"en-us",
26-
root.to_owned(),
25+
root_url, // this is the default port
26+
language,
27+
title,
2728
whoami::username()
2829
);
2930
// TBD: add Windows separator support
@@ -92,7 +93,7 @@ async fn create_directories(path: &str) -> Result<()> {
9293
Ok(())
9394
}
9495

95-
pub async fn init(name: &str) -> Result<()> {
96+
pub async fn init(name: &str, prompt: bool) -> Result<()> {
9697
let path_exists = fs::try_exists(name).await?;
9798

9899
if path_exists {
@@ -103,12 +104,38 @@ pub async fn init(name: &str) -> Result<()> {
103104
path.display()
104105
);
105106
} else {
107+
// Prompt site configuration if wanted, otherwise fallback to sane default values
108+
let root_url = if prompt {
109+
Text::new("Site URL:")
110+
.with_default("http://localhost:3030")
111+
.with_help_message("URL to your production site")
112+
.prompt()?
113+
} else {
114+
String::from("http://localhost:3030")
115+
};
116+
let language = if prompt {
117+
Text::new("Site language:")
118+
.with_default("en-US")
119+
.with_help_message("Your site language")
120+
.prompt()?
121+
} else {
122+
String::from("en-US")
123+
};
124+
let title = if prompt {
125+
Text::new("Site title:")
126+
.with_default(name)
127+
.with_help_message("Site title")
128+
.prompt()?
129+
} else {
130+
String::from(name)
131+
};
132+
106133
// Create site directories
107134
create_directories(name).await?;
108135

109136
// Create initial files
110137
// TBD: Basic HTML templates
111-
create_config(name).await?;
138+
create_config(name, &root_url, &language, &title).await?;
112139
create_index_norg(name).await?;
113140
create_html_templates(name).await?;
114141
create_assets(name).await?;
@@ -131,7 +158,7 @@ pub async fn init(name: &str) -> Result<()> {
131158
.add_row(vec![Cell::new("templates"), Cell::new("HTML templates")])
132159
.add_row(vec![
133160
Cell::new("assets"),
134-
Cell::new("Site assets (JS, CSS, favicon, etc)"),
161+
Cell::new("Site assets (JS, CSS, images, etc)"),
135162
])
136163
.add_row(vec![Cell::new("theme"), Cell::new("Site theme files")])
137164
.add_row(vec![Cell::new(".build"), Cell::new("Dev server artifacts")]);

0 commit comments

Comments
 (0)