forked from dylan-sutton-chavez/edge-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.rs
More file actions
47 lines (39 loc) · 1.26 KB
/
init.rs
File metadata and controls
47 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
`edge init`: scaffold a ready-to-run project (entry script, host page, manifest).
*/
use anyhow::{bail, Context, Result};
use std::fs;
use std::path::Path;
const MAIN_PY: &str = "print(\"hello from edge python\")\n";
const PACKAGES_JSON: &str = "{}\n";
const INDEX_HTML: &str = include_str!("templates/scaffold.html");
fn index_html(title: &str) -> String {
INDEX_HTML.replace("__EDGE_TITLE__", title)
}
pub fn run(name: Option<&str>, bare: bool) -> Result<()> {
let dir = name.unwrap_or(".");
let root = Path::new(dir);
if dir != "." {
if root.exists() {
bail!("'{dir}' already exists");
}
fs::create_dir_all(root).with_context(|| format!("creating {dir}"))?;
}
fs::write(root.join("main.py"), MAIN_PY)?;
fs::write(root.join("packages.json"), PACKAGES_JSON)?;
let mut items = vec![];
if !bare {
let title = if dir == "." { "edge app" } else { dir };
fs::write(root.join("index.html"), index_html(title))?;
items.push("index.html");
}
items.push("main.py");
items.push("packages.json");
let next = if dir == "." {
"edge serve".to_string()
} else {
format!("cd {dir} && edge serve")
};
crate::ui::scaffolded(dir, &items, &next);
Ok(())
}