-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelm.rs
46 lines (40 loc) · 1.12 KB
/
elm.rs
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
use std::{
fs,
io::{self, Write},
path::Path,
process::{Command, Stdio},
};
use anyhow::{bail, Result};
use assert_fs::fixture::{FileTouch, PathChild};
use tracing::warn;
pub fn elm_init<T>(parent: &T) -> Result<()>
where
T: PathChild + AsRef<Path>,
{
fs::create_dir_all(parent.as_ref())?;
let mut proc = match Command::new("elm")
.arg("init")
.current_dir(parent.as_ref())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Ok(proc) => proc,
Err(e) if e.kind() == io::ErrorKind::NotFound => {
warn!("failed to exec elm: {}", e);
// not installed on this system.. let's fake it then
parent.child("elm.json").touch()?;
return Ok(());
}
Err(e) => return Err(e.into()),
};
// elm init expects a "y"es (it asks whether to actually init)
let mut stdin = proc.stdin.take().unwrap();
stdin.write_all(b"y")?;
drop(stdin);
let ecode = proc.wait()?;
if !ecode.success() {
bail!("elm init failed with status code {:?}", ecode.code());
}
Ok(())
}