1use std::{
10 error::Error,
11 fs::{self},
12 process::Command,
13};
14
15use clap::Args;
16
17use crate::config;
18
19#[derive(Args, Debug)]
21pub struct InitOptions {
22 #[arg(long, required = false)]
24 presentation_only: bool,
25}
26
27const PACKAGE_JSON: &str = include_str!("../static/package.json");
29const VITE_CONFIG_JS: &str = include_str!("../static/vite.config.js");
30const INDEX_HTML: &str = include_str!("../static/index.html");
31const MAIN_JS: &str = include_str!("../static/main.js");
32const SLIDES_MD: &str = include_str!("../static/slides.md");
33const CUSTOM_CSS: &str = include_str!("../static/custom.css");
34
35pub fn init(_opts: InitOptions) -> Result<(), Box<dyn Error>> {
49 let conf = config::create_conf()?;
50
51 std::fs::create_dir_all(&conf.title)?;
52
53 let output = Command::new("npm")
54 .args(["init", "-y", "--prefix", &conf.title])
55 .current_dir(&conf.title)
56 .output()?;
57
58 if !output.status.success() {
60 eprintln!(
61 "npm init failed: {}",
62 String::from_utf8_lossy(&output.stderr)
63 );
64 return Err("npm init failed".into());
65 }
66
67 let npm_commands = vec![
68 format!("install --save-dev vite http-server"),
69 format!("install reveal.js serve vite-plugin-singlefile"),
70 ];
71
72 for c in npm_commands {
73 let args: Vec<&str> = c.split(' ').collect();
74 let output = Command::new("npm")
75 .args(&args)
76 .current_dir(&conf.title)
77 .output()?;
78
79 if !output.status.success() {
80 eprintln!(
81 "npm {} failed: {}",
82 c,
83 String::from_utf8_lossy(&output.stderr)
84 );
85 return Err(format!("npm {} failed", c).into());
86 }
87 println!("Bootstrapped npm {}", c);
88
89 println!("Saving config file...");
90
91 config::write_config(&conf.title, &conf)?;
92 }
93
94 fs::write(format!("{}/package.json", &conf.title), PACKAGE_JSON)?;
95 fs::write(format!("{}/vite.config.js", &conf.title), VITE_CONFIG_JS)?;
96 fs::write(format!("{}/index.html", &conf.title), INDEX_HTML)?;
97
98 std::fs::create_dir_all(format!("{}/src", &conf.title))?;
99 fs::write(format!("{}/src/main.js", &conf.title), MAIN_JS)?;
100
101 std::fs::create_dir_all(format!("{}/slides", &conf.title))?;
102 fs::write(format!("{}/slides/slides.md", &conf.title), SLIDES_MD)?;
103
104 std::fs::create_dir_all(format!("{}/css", &conf.title))?;
105 fs::write(format!("{}/css/custom.css", &conf.title), CUSTOM_CSS)?;
106
107 Ok(())
108}