Last active
March 21, 2023 08:06
-
-
Save believer/38e72729803b75c304fa81d57f99bb39 to your computer and use it in GitHub Desktop.
Parse Fluent files to TypeScript definitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anyhow::{anyhow, Result}; | |
use std::fs; | |
fn main() -> Result<()> { | |
let args: Vec<String> = std::env::args().collect(); | |
if args.len() <= 1 { | |
return Err(anyhow!("No file specified")); | |
} | |
let filename = &args[1]; | |
let file = fs::read_to_string(filename).expect("Unable to read file"); | |
let translation_keys = file | |
.lines() | |
.filter(|line| !line.is_empty()) | |
// Remove indented lines, terms, and comments | |
.filter(|line| !line.starts_with([' ', '-', '#'])) | |
.filter_map(|line| line.split_once(" = ")) | |
.map(|(key, _)| key) | |
.collect::<Vec<_>>(); | |
let mut output = String::new(); | |
output.push_str("export type TranslationKey =\n"); | |
for key in translation_keys { | |
output.push_str(&format!("\t| '{key}'\n")); | |
} | |
// write output to file | |
fs::write("output.ts", output)?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment