Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ccarc.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"connected": false,
"componentMethods": [],
"fileNames": {
"testFileMatch": "spec",
"testFileName": "myTest",
"componentFileName": "template",
"styleFileName": "style"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ Currently supported options are:
`indexFile` | Default flag to create an index file in the folder `[false, true]`
`connected` | Default flag to integrate connect redux in the index file `[false, true]`
`componentMethods` | Only for "class" and "pure", insert method inside the component (i.e. `["componentDidMount", "shouldComponentUpdate", "onClick"]`)
`fileNames` | Choose the specific filename for your component's file.
`fileNames.testFileMatch` | specify the match part of test file
`fileNames` | Choose the specific filename for your component's file. (COMPONENT_NAME will be replaced)
`fileNames.testFileName` | specify the file name of your test file
`fileNames.componentFileName` | specify the component file name
`fileNames.styleFileName` | specify the style file name !!IMPORTANT: Include cssExtension.
Expand Down
31 changes: 17 additions & 14 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ function readFile(path, fileName) {

/**
* generate the file name
* @param {string} newFilePath
* @param {string} newFileName
* @param {string} templateFileName
*/
function generateFileName(newFilePath, newFileName, templateFileName) {
function generateFileName(newFileName, templateFileName) {
if (templateFileName.includes('COMPONENT_NAME')) {
return templateFileName.replace(/COMPONENT_NAME/g, newFileName)
}
Expand All @@ -71,7 +70,7 @@ async function generateFilesFromTemplate({ name, path, templatesPath }) {
const content = await readFile(templatesPath, templateFileName)
const replaced = content.replace(/COMPONENT_NAME/g, name)
// Exist ?
const newFileName = generateFileName(`${outputPath}/`, name, templateFileName)
const newFileName = generateFileName(name, templateFileName)
// Write the new file with the new content
fs.outputFile(`${outputPath}/${newFileName}`, replaced)
})
Expand All @@ -88,20 +87,29 @@ async function generateFilesFromTemplate({ name, path, templatesPath }) {
*/
function getFileNames(fileNames, componentName) {
const defaultFileNames = {
testFileName: defaultOptions.testFileName,
testFileMatch: componentName,
testFileName: `${defaultOptions.testFileName}.${componentName}`,
componentFileName: componentName,
styleFileName: componentName,
}

return { ...defaultFileNames, ...fileNames }
const formattedFileNames = Object.keys(fileNames).reduce(
(acc, curr) => {
acc[curr] = fileNames[curr].replace(/COMPONENT_NAME/g, componentName)

return acc
},
{ ...defaultFileNames }
)

return formattedFileNames
}

/**
* Generate component files
*
* @param {object} params object with:
* @param {string} type: the type of component template
* @param {object} fileNames: object that contains the filenames to replace
* @param {string} name: the name of the component used to create folder and file
* @param {string} path: where the component folder is created
* @param {string} cssExtension: the extension of the css file
Expand All @@ -128,10 +136,7 @@ function generateFiles(params) {
} = params
const destination = `${path}/${name}`

const { testFileName, testFileMatch, componentFileName, styleFileName } = getFileNames(
fileNames,
name
)
const { testFileName, componentFileName, styleFileName } = getFileNames(fileNames, name)

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

if (includeTests) {
fs.outputFile(
`${destination}/${testFileName}.${testFileMatch}.${jsExtension}`,
generateTestTemplate(name)
)
fs.outputFile(`${destination}/${testFileName}.${jsExtension}`, generateTestTemplate(name))
}

// Create js file
Expand All @@ -168,4 +170,5 @@ function generateFiles(params) {
}

const generateFilesFromCustom = generateFilesFromTemplate

export { generateFiles, generateFilesFromTemplate, generateFilesFromCustom, getDirectories }