compiles a folder of typescript files recursively, returning a list of files its compiled
(path: &Path)
| 46 | |
| 47 | // compiles a folder of typescript files recursively, returning a list of files its compiled |
| 48 | fn compile_folder(path: &Path) -> Vec<String> { |
| 49 | let relative_path = path.strip_prefix("./src/ts/").unwrap(); |
| 50 | let mut result = Vec::<String>::new(); |
| 51 | let entries = std::fs::read_dir(path) |
| 52 | .unwrap() |
| 53 | .map(|res| res.map(|e| e.path())) |
| 54 | .collect::<Result<Vec<_>, std::io::Error>>() |
| 55 | .unwrap(); |
| 56 | |
| 57 | let mut loaded_files = Vec::new(); |
| 58 | for file in &entries { |
| 59 | let filename = file.file_name().unwrap().to_str().unwrap(); |
| 60 | if filename.ends_with(".d.ts") || !filename.ends_with(".ts") { |
| 61 | let data = std::fs::metadata(file).unwrap(); |
| 62 | if data.is_dir() { |
| 63 | if filename == "node_modules" { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | let mut compiled = compile_folder(file); |
| 68 | result.append(&mut compiled); |
| 69 | } |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | let mut result = File::open(file).unwrap(); |
| 74 | let mut contents = String::new(); |
| 75 | result.read_to_string(&mut contents).unwrap(); |
| 76 | |
| 77 | loaded_files.push((filename.strip_suffix(".ts").unwrap(), contents)); |
| 78 | } |
| 79 | |
| 80 | let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap()); |
| 81 | let target_dir = out_dir.join(Path::new("js").join(relative_path)); |
| 82 | fs::create_dir_all(&target_dir).unwrap(); |
| 83 | |
| 84 | for (name, file) in loaded_files { |
| 85 | let output = compile_typescript(&file, format!("{}.ts", file)).unwrap(); |
| 86 | fs::write(target_dir.join(format!("{name}.js")), output.output).unwrap(); |
| 87 | |
| 88 | // we manually construct the joined path because on windows the std::path separator is \ vs what we want, / |
| 89 | let path = relative_path.join(name); |
| 90 | let components = path |
| 91 | .components() |
| 92 | .map(|v| v.as_os_str().to_string_lossy()) |
| 93 | .collect::<Vec<_>>(); |
| 94 | |
| 95 | let joined = components.join("/"); |
| 96 | |
| 97 | result.push(joined); |
| 98 | } |
| 99 | |
| 100 | result |
| 101 | } |
no test coverage detected