CFFT - Create Files From Template
Create Files From Template is a simple but powerful Visual Studio Code extension to generate a list of files from templates.
Stop copying/pasting and renaming files and folders or creating these manually, repeating the same file structures every time. CFFT generates custom file structure easily and quickly.
You can use this extension for any language or framework (JavaScript, Node, React, Angular, .NET, Java, Python, ...)
Features
- Create a custom file structure from a context menu command
- Search and replace - Replace file content with a custom text or file name (text, regex, inject files, ...)
- Create multiple templates
- Set options by configuring extension using a .config JSON file for each template - cfft.config.json, or:
- Provide missing options by answering IDE questions
Releases
See Releases to see what is changed in the latest version.
Create Files From Template CLI - CLI - Use a terminal to create a list of files from templates.
Create Files From Template Base - Includes core utilities required for CFFT apps to work.
Table of contents
Getting started (Tutorial)
Create a configuration file - cfft.config.json. It can be done automatically (recommended), or you can do it manually.
Click on any directory in your project and select New From Template... (CFFT):
You should see the message that cfft.config.json is created with a default values prefilled:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [
{
"search": "FileName",
"replace": "{fileName}"
}
]
}
}
]
}
You can edit these values however you want. See the Options section where it is explained which options exist.
Additionally, this command also creates the .cfft.templates
directory and the component
files for demo purposes.
Feel free to edit or remove these files.
- At this moment, the configuration for demo purposes is ready, and you can start using the extension. To see how it works, right click on any directory in your project and select New From Template... (CFFT). Enter any file name you want (for example "Test") and confirm. The extension should generate a new directory and two files inside with updated content:
├── [YOUR DIRECTORY]
│ ├── Test
│ │ ├── Test.tsx
│ │ ├── Test.module.scss
- The next step is to create your templates (you can create one or more templates). Let's use a default
templatePath
. In your root folder, create the following structure:
├── .cfft.templates
│ ├── component
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
Note: You can also create inner folders. For example:
├── .cfft.templates
│ ├── component
│ │ ├── component-inner
│ │ │ ├── component-inner.styles.tsx
│ │ │ ├── component-inner.tsx
│ │ │ ├── index.ts
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
Note: If you use GIT, you can ignore cfft.config.json and your templates if you wish to.
- Insert some content into your newly created files
component.tsx
, component.styles.ts
and index.ts
:
component.styles.ts
export const style = {
backgroundColor: "black",
color: "white",
};
component.tsx
import React, { FC } from "react";
import { style } from "./FileName.styles";
const FileName: FC = () => {
return (
<div style={style}>
<p>TODO: FileName</p>
</div>
);
};
export default FileName;
index.ts
export { default } from "./FileName";
Right click on any directory in which you want to create files and choose New From Template... (CFFT).
Enter a file name. For example MyFile
.
CFFT Extensions created new files in the selected folder.
├── [SELECTED FOLDER]
│ ├── MyFile
│ │ ├── MyFile.tsx
│ │ ├── MyFile.styles.ts
│ │ ├── index.ts
Additionally, the extension replaced the FileName text with the entered file name:
MyFile.styles.ts (unchanged)
export const style = {
backgroundColor: "black",
color: "white",
};
MyFile.tsx
import React, { FC } from "react";
import { style } from "./MyFile.styles";
const MyFile: FC = () => {
return (
<div style={style}>
<p>TODO: MyFile</p>
</div>
);
};
export default MyFile;
index.ts
export { default } from "./MyFile";
Filling the missing configuration options
CFFT Extension is interactive. You can remove all default options from your template, and CFFT extension will ask you for missing information every time you run it.
Example
If you were following the previous example, remove the MyFile folder.
- Remove all options from your cfft.config.json file:
{}
Right click in the root
folder in your project and select New From Template... (CFFT).
Answer questions:
? Enter template name: component
? Enter file name: MyFile
? Enter dir path: ./MyFile
? Enter template path: /.cfft.templates/component
? Should replace file name text? Yes
? Enter file name text to be replaced: component
? Should replace text? Yes
? Enter text to be replaced: FileName
? Replace text with: MyFile
- The extension will create files using the provided options.
Search and replace - replace multiple placeholders
CFFT extension allows you to search and replace multiple placeholders. This is possible by adding the additional items in the searchAndReplace
array.
Example
If you were following one of the previous examples, remove the MyFile folder.
- Update the
searchAndReplace
in the cfft.config.json:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [
{
"search": "FileName",
"replace": "{fileName}"
},
{
"search": "FC",
"replace": "FunctionComponent"
}
]
}
}
]
}
- An alternative is to enter the list of placeholders and replacement values separated by the
searchAndReplaceSeparator
character to both textToBeReplaced
and replaceTextWith
:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplaceSeparator": ";",
"textToBeReplaced": "FileName;FC",
"replaceTextWith": "{fileName};FunctionComponent"
}
}
]
}
Note: It is important that textToBeReplaced and replaceTextWith have the same number of segments.
Execute the New From Template... (CFFT) command:
The extension will create files and replace both, FileName with MyFile and FC with FunctionComponent.
import React, { FunctionComponent } from "react";
import { style } from "./MyFile.styles";
const MyFile: FunctionComponent = () => {
return (
<div style={style}>
<p>TODO: MyFile</p>
</div>
);
};
export default MyFile;
Add additional templates
While you can use only one template, it is also possible to create and use multiple templates. To achieve this, you need to create the template folder and files, to update the cfft.config.json and to select the --template
when VS Code extension asks you.
Note: If the defaultTemplateName
is not specified in the configuration and multiple templates are defined, the extension will ask for the template name. The extension will use the first template if only one template has been defined.
Example
If you were following one of the previous examples, remove the MyFile folder.
- Add a new template folder next to the existing one - css, and add a file inside it main.scss (in the .cfft.templates folder):
├── .cfft.templates
│ ├── component
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
│ ├── css
│ │ ├── main.scss
- Fill the file content:
main.scss
#root {
box-sizing: border-box;
}
- Update the cfft.config.json file:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [
{
"search": "FileName",
"replace": "{fileName}"
},
{
"search": "FC",
"replace": "FunctionComponent"
}
]
}
},
{
"name": "css",
"options": {
"templatePath": "/.cfft.templates/css",
"dirPath": "./{fileName}",
"shouldReplaceFileContent": false,
"shouldReplaceFileName": false
}
}
]
}
Execute the New From Template... (CFFT) command:
Select the template: css
.
Enter the file name: my-styles
.
The CFFT extension will generate the following folder and file in selected directory:
├── [SELECTED DIRECTORY]
│ ├── my-styles
│ │ ├── main.scss
The main.scss, will be exactly the same, since we were not replacing the file content.
Injecting a file content
Besides replacing the placeholders with text, it is possible to do the replacement with the file content. To inject the file content, set the injectFile
to true.
Example
If you were following one of the previous examples, remove the MyFile folder.
- Add the table.html file to .cfft.templates directory:
├── .cfft.templates
│ ├── component
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
│ ├──table.html
table.html
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
<td>Cell 1.4</td>
<td>Cell 1.5</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
<td>Cell 2.4</td>
<td>Cell 2.5</td>
</tr>
</table>
- Update the template file:
component.tsx
import { FC } from "react";
import { style } from "./FileName.styles";
const FileName: FC = () => {
return (
<div style={style}>
<p>TODO: FileName</p>
{table}
</div>
);
};
export default FileName;
- Update the cfft.config.json:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [
{
"search": "{table}",
"replace": "/.cfft.templates/table.html",
"injectFile": true
}
]
}
}
]
}
Execute the New From Template... (CFFT) command.
The extension will create files and inject the table in the MyFile.tsx file:
import { FC } from "react";
import { style } from "./FileName.styles";
const FileName: FC = () => {
return (
<div style={style}>
<p>TODO: FileName</p>
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
<td>Cell 1.4</td>
<td>Cell 1.5</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
<td>Cell 2.4</td>
<td>Cell 2.5</td>
</tr>
</table>
</div>
);
};
export default FileName;
The order of the search and replace execution
In some cases, the replacement order may matter. For example, you may want to inject file content and after that to replace parts of it.
Default orders
Method |
Order |
textToBeReplaced & replaceTextWith |
0 |
searchAndReplace |
1 |
Special replacement placeholders |
last |
Example
If you were following one of the previous examples, remove the MyFile folder.
- Update the cfft.config.json file:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [
{
"search": "{table}",
"replace": "/.cfft.templates/table.html",
"injectFile": true,
"order": -2
},
{ "search": "Cell 1.5", "replace": "HELLO!", "order": -1 }
]
}
}
]
}
Execute the New From Template... (CFFT) command and enter the file name: MyFile
.
The extension will create files and inject the table in the MyFile.tsx file:
import { FC } from "react";
import { style } from "./FileName.styles";
const FileName: FC = () => {
return (
<div style={style}>
<p>TODO: FileName</p>
<table>
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
<td>Cell 1.4</td>
<td>HELLO!</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
<td>Cell 2.4</td>
<td>Cell 2.5</td>
</tr>
</table>
</div>
);
};
export default FileName;
Ignoring the case of the letters on text searching
By default, searching by text is case-sensitive. You can change this behavior by using the ignoreCase
option. For example:
"searchAndReplace": [
{ "search": "FileName", "replace": "{fileName}", "ignoreCase": true }
]
Using the special replacement placeholders
The special replacement placeholders (for example: {env:ENV_VARIABLE_NAME} or {dateTimeNow:DNS_FORMAT}) are replaced as the last replacement task. See the Special replacement placeholders table for more info. This allows you to add specific or dynamic values during the file creation.
Example
If you were following one of the previous examples, remove the MyFile folder.
Add environment variables or use the existing ones. In this example, we are going to use TEST_ENV=myEnv and ANOTHER_ENV=anotherEnv.
Update your template by adding the following tags:
component.tsx
import { FC } from "react";
import { style } from "./FileName.styles";
const FileName: FC = () => {
return (
<div style={style}>
<p>My env variable: {env:TEST_ENV} and {env:ANOTHER_ENV}</p>
<p>{dateTimeNow:yyyy-MM-dd}</p>
</div>
);
};
export default FileName;
Execute the New From Template... (CFFT) command and enter the file name: MyFile
.
The extension will create files and replace the special tags with the values in the MyFile.tsx file:
import { FC } from "react";
import { style } from "./MyFile.styles";
const MyFile: FC = () => {
return (
<div style={style}>
<p>My env variable: myEnv and anotherEnv</p>
<p>2023-05-06</p>
</div>
);
};
export default MyFile;
Custom hooks
onFileCreated
CFFT allows you to specify the custom logic to be executed after each file creation. To achieve so, you should create a custom .js
file and implement and export the onFileCreated()
function. This can be used for various tasks, but one logical and useful task is to use the prettier.
Example
If you were following one of the previous examples, remove the MyFile folder.
Note: If you want to implement the same behavior make sure to install and configure the prettier for your project.
- Update your package.json by adding the
node script
:
package.json
"scripts": {
"prettier:only": "prettier",
}
- Update the cfft.config.json file:
{
"defaultTemplateName": "component",
"templates": [
{
"name": "component",
"options": {
"templatePath": "/.cfft.templates/component",
"dirPath": "./{fileName}",
"fileNameTextToBeReplaced": "component",
"hooksPath": "/.cfft.templates/hooks/component.js",
"searchAndReplace": [
{
"search": "{table}",
"replace": "/.cfft.templates/table.html",
"injectFile": true,
"order": -2
},
{ "search": "Cell 1.5", "replace": "HELLO!", "order": -1 }
]
}
}
]
}
- Add the
component.js
file:
├── .cfft.templates
│ ├── component
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
│ ├── hooks
│ │ ├── component.js
│ ├──table.html
component.js
const { execSync } = require("child_process");
const onFileCreated = ({ filePath, templatePath }) => {
// Run prettier on a created file
execSync(`npm run prettier:only -- ${filePath} --write`);
};
module.exports = {
onFileCreated,
};
Note: In this example, we are executing the prettier, but you can specify whatever logic you want.
Execute the New From Template... (CFFT) command and enter the file name: MyFile
.
The extension will create files and execute the onFileCreated()
hook for each created file!
Creating a single file
It is also useful to create a single file. To achieve that, the templatePath
must point to a file. To create a file in the current folder, update the dirPath
to .
.
Example
- Add a
component.styles.tsx
file to you templates folder:
├── .cfft.templates
│ ├── component
│ │ ├── component.tsx
│ │ ├── component.styles.ts
│ │ ├── index.ts
│ ├── hooks
│ │ ├── component.js
│ ├──table.html
│ ├──component.styles.tsx
component.styles.tsx
import { styled } from "@mui/material";
const COMPONENT_NAME = "FileName";
export const FileNameRoot = styled("div", {
name: COMPONENT_NAME,
slot: "Root",
})(({ theme }) => ({}));
- Add a new template to the cfft.config.json file:
{
"defaultTemplateName": "component-styles-file",
"templates": [
{
"name": "component-styles-file",
"options": {
"templatePath": "/.cfft.templates/component.styles.tsx",
"dirPath": ".",
"fileNameTextToBeReplaced": "component",
"searchAndReplace": [{ "search": "FileName", "replace": "{fileName}" }]
}
}
]
}
Execute the New From Template... (CFFT) command and enter the file name: _MyComponent
.
The extension will create the MyComponent.styles.tsx
file and replace the FileName with the provided name (MyComponent) file:
MyComponent.styles.tsx
import { styled } from "@mui/material";
const COMPONENT_NAME = "MyComponent";
export const MyComponentRoot = styled("div", {
name: COMPONENT_NAME,
slot: "Root",
})(({ theme }) => ({}));
Convert placeholder cases
CFFT allows you to convert strings from your template to different cases, making it especially useful for transforming placeholders like "file name" into various formats.
The available convertors
- Camel case -
param-case
-> paramCase
- Snake case -
camelCase
-> camel_case
- Pascal case -
param-case
-> ParamCase
- Dot case -
Title Case
-> title.case
- Path case -
camelCase
-> camel/case
- Text case -
camelCase
-> camel case
- Sentence case -
camelCase
-> Camel case
- Header case -
param-case
-> Param Case
- Lower case -
Title Case
-> title case
- Upper case -
param-case
-> PARAM-CASE
- Kebab case -
Title Case
-> title-case
- Lower snake case
ParamCase
-> param-case
- Upper snake case
ParamCase
-> PARAM-CASE
Usage
To apply a case converter to any string in your template, wrap it with #(<TextToBeConverted>, <Option>)
.
You can specify the option in multiple formats; options are case-insensitive, and characters such as
, _
, -
, .
, /
and \
are ignored. For instance, to apply Pascal case, you can write:
#(TextToBeConverted, PascalCase)
,
#(TextToBeConverted, Pascal case)
,
#(TextToBeConverted, pascalcase)
,
#(TextToBeConverted, PASCAL_CASE)
,
#(TextToBeConverted, PASCAL-CASE)
,
...
Example
- Add a
constants.ts
file to your template folder (.cfft.templates):
├── .cfft.templates
│ ├── constants.ts
- Populate the file with the following content:
constants.ts
import { routes } from 'constants/routes';
// Routes 1 to 15 demonstrate available converters
const route1 = routes.#(param-case, CAMEL_CASE).route;
const route2 = routes.#(param-case, SNAKE_CASE).route;
const route3 = routes.#(param-case, PASCAL_CASE).route;
const route4 = routes.#(param-case, DOT_CASE).route;
const route5 = routes.#(param-case, PATH_CASE).route;
const route6 = routes.#(param-case, TEXT_CASE).route;
const route7 = routes.#(param-case, SENTENCE_CASE).route;
const route8 = routes.#(param-case, HEADER_CASE).route;
const route9 = routes.#(paRam-case, LOWER_CASE).route;
const route10 = routes.#(param-case, UPPER_CASE).route;
const route11 = routes.#(param-case, KEBAB_CASE).route;
const route12 = routes.#(ParamCase, UPPER_SNAKE_CASE).route;
const route13 = routes.#(ParamCase, LOWER_SNAKE_CASE).route;
const route14 = routes.#(FileName, LOWER_SNAKE_CASE).route;
const route15 = routes.#(FileName,UPPER_SNAKE_CASE).route;
// Routes 16 to 26 demonstrate different ways to apply the same option
const route16 = routes.#(param-case, PASCAL_CASE).route;
const route17 = routes.#(param-case, PASCALCASE).route;
const route18 = routes.#(param-case, pascalcase).route;
const route19 = routes.#(param-case, PascalCase).route;
const route20 = routes.#(param-case, pascalCase).route;
const route21 = routes.#(param-case, pascal.Case).route;
const route22 = routes.#(param-case, pascal/case).route;
const route23 = routes.#(param-case, pascal\case).route;
const route24 = routes.#(param-case, PASCAL-case).route;
const route25 = routes.#(param-case, PASCAL case).route;
const route26 = routes.#(param-case, pascal case).route;
- Update your cfft.config.json by adding the following configuration to the templates array:
{
"name": "constants",
"description": "Creates a constants file",
"options": {
"templatePath": "/.cfft.templates/constants.ts",
"dirPath": ".",
"fileNameTextToBeReplaced": "constants",
"shouldReplaceFileContent": true,
"searchAndReplace": [{ "search": "FileName", "replace": "{fileName}" }]
}
}
Run the New From Template... (CFFT) command, select the constants template, and enter the file name: MyConstants
.
The extension will generate a MyConstants.ts
file with the following content:
import { routes } from 'constants/routes';
// Routes 1 to 15 demonstrate available converters
const route1 = routes.paramCase.route;
const route2 = routes.param_case.route;
const route3 = routes.ParamCase.route;
const route4 = routes.param.case.route;
const route5 = routes.param/case.route;
const route6 = routes.param case.route;
const route7 = routes.Param case.route;
const route8 = routes.Param Case.route;
const route9 = routes.param-case.route;
const route10 = routes.PARAM-CASE.route;
const route11 = routes.param-case.route;
const route12 = routes.PARAM_CASE.route;
const route13 = routes.param_case.route;
const route14 = routes.my_constants.route;
const route15 = routes.MY_CONSTANTS.route;
// Routes 16 to 26 demonstrate different ways to apply the same option
const route16 = routes.ParamCase.route;
const route17 = routes.ParamCase.route;
const route18 = routes.ParamCase.route;
const route19 = routes.ParamCase.route;
const route20 = routes.ParamCase.route;
const route21 = routes.ParamCase.route;
const route22 = routes.ParamCase.route;
const route23 = routes.ParamCase.route;
const route24 = routes.ParamCase.route;
const route25 = routes.ParamCase.route;
const route26 = routes.ParamCase.route;
Configuration
Root fields
Command |
Description |
Type |
defaultTemplateName |
Default template name to be used |
string |
templates |
List of template configurations |
array |
Template fields
Command |
Description |
Type |
name |
Template name |
string |
description |
Text to be displayed next to the template name |
string |
options |
List of template options |
object |
Template options
Command |
Description |
Type |
dirPath |
Path to the location where to generate files |
string |
templatePath |
Path to the specific template folder |
string |
shouldReplaceFileName |
Should or not extension replace a file name |
boolean |
fileNameTextToBeReplaced |
Which part of the file name should be replaced |
string |
shouldReplaceFileContent |
Should or not extension replace a file content |
boolean |
textToBeReplaced |
Text (or regex) to be replaced separated by a search and replace separator |
string |
replaceTextWith |
Text to be used for replacement separated by a separator |
string |
searchAndReplaceSeparator |
Custom separator for search and replace |
string |
searchAndReplace |
Add additional search and replace items through config (with extended options) |
array |
Search and replace options
Field |
Description |
required |
default |
search |
Text (or regex) to be replaced |
✓ |
|
replace |
Text to be used for replacement - or path to the file when injectFile=true |
✓ |
|
ignoreCase |
Should ignore the letters case |
|
false |
injectFile |
Should inject a file content at the found placeholder |
|
false |
order |
In which order to do the search and replace (lower order has precedence) |
|
1 |
Special replacement placeholders
Tag |
Description |
{env:ENV_VARIABLE_NAME} |
Replaces the placeholder with the specified environment variable |
{dateTimeNow:DATE_FNS_FORMAT} |
Replaces the placeholder with the current date and time using the date-fns format. See: date-fns format |
Placeholders
- {fileName} is a special value to indicate that specific text should be replaced with a specified file name (--fileName)
License
MIT