diff --git a/.ccarc.example b/.ccarc.example index e8b0f0d..450d373 100644 --- a/.ccarc.example +++ b/.ccarc.example @@ -9,7 +9,6 @@ "connected": false, "componentMethods": [], "fileNames": { - "testFileMatch": "spec", "testFileName": "myTest", "componentFileName": "template", "styleFileName": "style" diff --git a/README.md b/README.md index 9afa538..92a1ffa 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/files.js b/src/files.js index f41bd3e..8094450 100644 --- a/src/files.js +++ b/src/files.js @@ -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) } @@ -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) }) @@ -88,13 +87,21 @@ 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 } /** @@ -102,6 +109,7 @@ function getFileNames(fileNames, componentName) { * * @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 @@ -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)) @@ -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 @@ -168,4 +170,5 @@ function generateFiles(params) { } const generateFilesFromCustom = generateFilesFromTemplate + export { generateFiles, generateFilesFromTemplate, generateFilesFromCustom, getDirectories }