<script type="module">
import augustindltCraftsman from 'https://cdn.skypack.dev/@augustindlt/craftsman';
</script>
README
Craftsman
What is craftsman ?
Craftsman is CLI tool that helps you to simplify the process of creating code files by using templates.
It's a simple way to make conventions on how a file must look like.
Installation
# Install craftsman
npm i -g @augustindlt/craftsman
Config
Create a config file inside your project : .craftsman/config.json
Config parameters :
parameter
type
meaning
templates
Template[]
List of all templates
helpers
Helpers
Set of functions that you can use inside the config and in your templates
Template :
parameter
type
meaning
name
string
Name of the template
files
File[]
List of all the files to generate
path
string
Optional parameter to declare where the files will be created
variables
Variables
Set of variables you need to create the files
File :
parameter
type
meaning
path
string
Destination of the generated file
name
string
Name of the generated file
template
string
File name without extension which contains the template
replaceExistingFile
"yes" | "no" | "ask"
Determines if the cli should replace the file or not if it already exists, by default this parameter by default this parameter is set to "ask"
Variables :
The variables have all this base config :
{
"templates": [
{
...
"variables": {
"salutation": {...},
"nameOfMyVariable": { // The name of the variable that you want to set
"type": "text", // The type of question you want the cli ask you, it can be "text", "choices", "autocomplete", "file", "directory", "array"
"message": "What value are you going to set to nameOfMyVariable ?", // The message that will be print, it's an optional parameter
"condition": "salutation.toLowerCase() === 'hello' && 1 === 1" // A JS condition which will determine whether the cli will ask or not for this variable, if not the default value of variable will be "", it's an optional parameter
// Here the other parameters depending on the type of the variable
}
}
}
]
}
choices and autocomplete :
{
...
"variables": {
"nameOfMyVariable": {
"type": "choices",
"choices": ["yes", "no", "may be"] // Array of string that represent all the choices you can select
}
}
}
file and directory :
{
...
"variables": {
"component": {
"type": "file",
"matchString": ".component.tsx", // Filter files to show only those that contains this string in their path, it's an optional parameter
"matchRegex": "\\.component\\.tsx
quot;, // Same as the matchString but with a regex, it's an optional parameter
"path": "./src" // Base path where is the file you want to select, the file can be inside of a sub folder, by default this parameter is set to "."
}
}
}
With the config below by default the cli will ask for text questions and will set nameOfMyVariable to an simple array of string like: ["hello", "hey" ...]
The config below will set nameOfMyVariable to an array of object with sub variables like: [{ name: "Jhon", wantACofee: "yes", typeOfCofee: "Cappuccino"}, { name: "Mary", wantACofee: "no", typeOfCofee: ""}]
Helpers :
Helpers are custom functions written in javascript, which can be used in your templates and configuration, here is an example of a helper declaration :
{
"templates": [...],
"helpers": {
"skewer": "helpers/skewer" // The declaration of a helper takes his name as a key and his path without the extension as value
}
}