Skip to content

Commit cd91470

Browse files
authored
Merge pull request #60 from CVarisco/57-filenames-formats
Add capability to use in filenames options COMPONENT_NAME replacement
2 parents 221f4f6 + 8c1033f commit cd91470

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

.ccarc.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"connected": false,
1010
"componentMethods": [],
1111
"fileNames": {
12-
"testFileMatch": "spec",
1312
"testFileName": "myTest",
1413
"componentFileName": "template",
1514
"styleFileName": "style"

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ Currently supported options are:
9898
`indexFile` | Default flag to create an index file in the folder `[false, true]`
9999
`connected` | Default flag to integrate connect redux in the index file `[false, true]`
100100
`componentMethods` | Only for "class" and "pure", insert method inside the component (i.e. `["componentDidMount", "shouldComponentUpdate", "onClick"]`)
101-
`fileNames` | Choose the specific filename for your component's file.
102-
`fileNames.testFileMatch` | specify the match part of test file
101+
`fileNames` | Choose the specific filename for your component's file. (COMPONENT_NAME will be replaced)
103102
`fileNames.testFileName` | specify the file name of your test file
104103
`fileNames.componentFileName` | specify the component file name
105104
`fileNames.styleFileName` | specify the style file name !!IMPORTANT: Include cssExtension.

src/files.js

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

4444
/**
4545
* generate the file name
46-
* @param {string} newFilePath
4746
* @param {string} newFileName
4847
* @param {string} templateFileName
4948
*/
50-
function generateFileName(newFilePath, newFileName, templateFileName) {
49+
function generateFileName(newFileName, templateFileName) {
5150
if (templateFileName.includes('COMPONENT_NAME')) {
5251
return templateFileName.replace(/COMPONENT_NAME/g, newFileName)
5352
}
@@ -71,7 +70,7 @@ async function generateFilesFromTemplate({ name, path, templatesPath }) {
7170
const content = await readFile(templatesPath, templateFileName)
7271
const replaced = content.replace(/COMPONENT_NAME/g, name)
7372
// Exist ?
74-
const newFileName = generateFileName(`${outputPath}/`, name, templateFileName)
73+
const newFileName = generateFileName(name, templateFileName)
7574
// Write the new file with the new content
7675
fs.outputFile(`${outputPath}/${newFileName}`, replaced)
7776
})
@@ -88,20 +87,29 @@ async function generateFilesFromTemplate({ name, path, templatesPath }) {
8887
*/
8988
function getFileNames(fileNames, componentName) {
9089
const defaultFileNames = {
91-
testFileName: defaultOptions.testFileName,
92-
testFileMatch: componentName,
90+
testFileName: `${defaultOptions.testFileName}.${componentName}`,
9391
componentFileName: componentName,
9492
styleFileName: componentName,
9593
}
9694

97-
return { ...defaultFileNames, ...fileNames }
95+
const formattedFileNames = Object.keys(fileNames).reduce(
96+
(acc, curr) => {
97+
acc[curr] = fileNames[curr].replace(/COMPONENT_NAME/g, componentName)
98+
99+
return acc
100+
},
101+
{ ...defaultFileNames }
102+
)
103+
104+
return formattedFileNames
98105
}
99106

100107
/**
101108
* Generate component files
102109
*
103110
* @param {object} params object with:
104111
* @param {string} type: the type of component template
112+
* @param {object} fileNames: object that contains the filenames to replace
105113
* @param {string} name: the name of the component used to create folder and file
106114
* @param {string} path: where the component folder is created
107115
* @param {string} cssExtension: the extension of the css file
@@ -128,10 +136,7 @@ function generateFiles(params) {
128136
} = params
129137
const destination = `${path}/${name}`
130138

131-
const { testFileName, testFileMatch, componentFileName, styleFileName } = getFileNames(
132-
fileNames,
133-
name
134-
)
139+
const { testFileName, componentFileName, styleFileName } = getFileNames(fileNames, name)
135140

136141
if (indexFile || connected) {
137142
fs.outputFile(`${destination}/index.js`, generateIndexFile(componentFileName, connected))
@@ -142,10 +147,7 @@ function generateFiles(params) {
142147
}
143148

144149
if (includeTests) {
145-
fs.outputFile(
146-
`${destination}/${testFileName}.${testFileMatch}.${jsExtension}`,
147-
generateTestTemplate(name)
148-
)
150+
fs.outputFile(`${destination}/${testFileName}.${jsExtension}`, generateTestTemplate(name))
149151
}
150152

151153
// Create js file
@@ -168,4 +170,5 @@ function generateFiles(params) {
168170
}
169171

170172
const generateFilesFromCustom = generateFilesFromTemplate
173+
171174
export { generateFiles, generateFilesFromTemplate, generateFilesFromCustom, getDirectories }

0 commit comments

Comments
 (0)