Skip to content

Instantly share code, notes, and snippets.

@tvaisanen
Last active November 10, 2018 10:19
Show Gist options
  • Save tvaisanen/92a8b3097017b463beb87ec897ac5539 to your computer and use it in GitHub Desktop.
Save tvaisanen/92a8b3097017b463beb87ec897ac5539 to your computer and use it in GitHub Desktop.
extern crate handlebars;
#[macro_use]
extern crate serde_json;
use handlebars::Handlebars;
use std::path::Path;
use std::error::Error;
use std::fs::OpenOptions;
use std::io::prelude::*;
fn main() {
let reg = Handlebars::new();
let _template = r###"
import React from 'react';
import { connect } from 'react-redux';
import {{name}}Styled from './<name>.styled';
import * as actions from './<name>.actions';
import <name>Ctrl from './<name>.controller';
type Props = {};
const {{name}} = (props: Props) => {
return <{{name}}Styled />
};
export default connect(
<name>Ctrl.mapStateToProps,
<name>Ctrl.mapDispatchToProps
)({{ name }});"###;
for arg in std::env::args().skip(1) {
let filename: String = format!("{}.js", arg);
let filepath = Path::new(&filename);
let mut _file = OpenOptions::new()
.write(true)
.create_new(true)
.open(filepath).unwrap();
let rendered = reg.render_template(_template, &json!({"name": arg })).unwrap();
match _file.write_all(rendered.as_bytes()){
Err(why) => {
panic!("Couldn't write into the file {}: {}",
filename, why.description()
)
},
Ok(_) => println!("Template created successfully.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment