Skip to content

Commit a54373a

Browse files
authored
Merge pull request #75 from damiansilbergleithcunniff/cased-replacement-keys
enable alternate replacement keys to control case of replaced strings
2 parents 3fac216 + 5633140 commit a54373a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

docs/CUSTOM-TEMPLATES.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
With this library you can create your own templates and use them to generate your components in a second!
44

5+
`create-component-app` offers 4 replacement keys which can be used in the names of the files in your template, as well as within the templates themselves. Each key corresponds to a formatted transformation of the component name that you enter when running `create-component-app`.
6+
7+
#### Keys and Replacements
8+
Replacement Key | Description
9+
--- | ---
10+
`COMPONENT_NAME` | Each instance of the string is replaced with the component name that you entered without modification. This is the standard behavior. (eg: `MyComponent` => `MyComponent` and replaces all instances of `COMPONENT_NAME` in files and file names.)
11+
`COMPONENT_CAP_NAME` | Each instance of the string is replaced with an uppercased transformation of the component name that you entered. (eg: `MyComponent` => `MYCOMPONENTNAME` and replaces all instances of `COMPONENT_CAP_NAME` in files and file names.)
12+
`component_name` | Each instance of the string is replaced with a lowercased transformation of the component name that you entered. (eg: `MyComponent` => `mycomponentname` and replaces all instances of `component_name` in files and file names.)
13+
`cOMPONENT_NAME` | Each instance of the string is replaced with a lower camel case transformation of the component name that you entered. For clarity, the first letter is simply lowercased. (eg: `MyComponent` => `myComponent` and replaces all instances of `cOMPONENT_NAME` in files and file names.)
14+
15+
516
### 1) Create your custom template folder
617

718
Create a folder to contain all your custom templates.

src/files.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,27 @@ function readFile(path, fileName) {
4343

4444
/**
4545
* generate the file name
46-
* @param {string} newFileName
47-
* @param {string} templateFileName
46+
* @param {string} searchString
47+
* @param {string} replacement
4848
*/
49-
function generateFileName(newFileName, templateFileName) {
50-
if (templateFileName.includes('COMPONENT_NAME')) {
51-
return templateFileName.replace(/COMPONENT_NAME/g, newFileName)
49+
function replaceKeys(searchString, replacement) {
50+
const replacementKeys = {
51+
COMPONENT_NAME: replacement,
52+
component_name: replacement.toLowerCase(),
53+
COMPONENT_CAP_NAME: replacement.toUpperCase(),
54+
cOMPONENT_NAME: replacement[0].toLowerCase() + replacement.substr(1),
5255
}
53-
return templateFileName
56+
57+
return Object.keys(replacementKeys).reduce(
58+
(acc, curr) => {
59+
if (acc.includes(curr)) {
60+
const regEx = new RegExp(curr, 'g')
61+
return acc.replace(regEx, replacementKeys[curr])
62+
}
63+
return acc
64+
},
65+
searchString
66+
)
5467
}
5568

5669
/**
@@ -68,9 +81,10 @@ async function generateFilesFromTemplate({ name, path, templatesPath }) {
6881
files.map(async (templateFileName) => {
6982
// Get the template content
7083
const content = await readFile(templatesPath, templateFileName)
71-
const replaced = content.replace(/COMPONENT_NAME/g, name)
84+
const replaced = replaceKeys(content, name)
85+
7286
// Exist ?
73-
const newFileName = generateFileName(name, templateFileName)
87+
const newFileName = replaceKeys(templateFileName, name)
7488
// Write the new file with the new content
7589
fs.outputFile(`${outputPath}/${newFileName}`, replaced)
7690
})
@@ -94,8 +108,7 @@ function getFileNames(fileNames = [], componentName) {
94108

95109
const formattedFileNames = Object.keys(fileNames).reduce(
96110
(acc, curr) => {
97-
acc[curr] = fileNames[curr].replace(/COMPONENT_NAME/g, componentName)
98-
111+
acc[curr] = replaceKeys(fileNames[curr], componentName)
99112
return acc
100113
},
101114
{ ...defaultFileNames }

0 commit comments

Comments
 (0)