cpt
Copy with Templates
Copies a folder structure and if templating data is supplied then all .tpl
files will be converted using Handlebars and the .tpl
file extension will then be stripped.
It does not write over existing files unless the -f
or --force
flag is present.
It can be run dry
which will skip any file writes, but still logs what would it do. Use the -d
or --dry
flags.
Folder and file names also support Handlebars syntax. (Although you can't use \
and many others in folder names so you are limited). After applying the template into the file/folder names, \n
characters (since they invalid anyway) will be handled specially. At every line break the created folder structure branches off. The content of each of them will be identical.
For example, with this data
The second line will be serialized as "file1.txt.tpl\nfile2.txt.tpl"
from this folder
}} }}
}}
these output files and folders will be produced
You can try this out with this command after downloading this repository (Given that you have Rust and Cargo installed):
Install
As a library
[]
= "0.4.1"
As a command line tool
Usage
As a library
Using shorthands
use cpt;
Using the builder
use Cpt;
As a command line tool
Using help:
<from> The
<to> The
Valid input
The serializer only supports strings and arrays. A valid TypeScript type of the input would look like this:
interface Input {
[key: string]: string | string[];
}
Motivation
I made this for my Advent of Code project scaffolder which you can find in my AoC repo.
What's next?
For this to be a little more than just a tiny toy project the next step would be to implement context-aware templating. If we think of a template as a tree where the leaves are the contents of a file, and their parents are the names of their files, then it would be nice to pass some context to these nodes about their parents and their positions.
This would allow automatic indexing for example.
Used libraries
- Handlebars
Templating engine
- Walkdir
Recursive directory walker
- Clap
Command-line arguments parser
- Serde
Serializer, deserializer. Here used for JSON parsing