From 142e2518b5c0186bbdc2f298c57ccb85e8e8ac52 Mon Sep 17 00:00:00 2001 From: isuruAb Date: Thu, 31 Aug 2017 09:33:08 +0530 Subject: [PATCH] Fixes: #38 - avoid Overwriting existing files --- bin/initialize.js | 12 ++++++-- lib/generate.js | 4 +-- lib/utils/checkduplicateelement.js | 47 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 lib/utils/checkduplicateelement.js diff --git a/bin/initialize.js b/bin/initialize.js index 336d60b..0604f65 100644 --- a/bin/initialize.js +++ b/bin/initialize.js @@ -8,6 +8,8 @@ const configRc = require("../lib/config"); const viewDirectoryStructure = require("../lib/view"); const packageVersion = require("../package.json").version; const checkDuplicates = require("../lib/utils/checkduplicates"); +const checkDuplicateElement = require("../lib/utils/checkduplicateelement"); + const ora = require("ora"); init = new initApp(); @@ -42,7 +44,7 @@ program `\t$ cd ${projectname}\n \t$ npm install \n \tHappy hacking ♥` ); }, 1000); - + } else { setTimeout(() => { spinner.text = "something went wrong!"; @@ -133,7 +135,11 @@ program .description("generate a react component") .action(function(type, modulename, name, options) { if (type !== undefined && modulename !== undefined) { + if(!checkDuplicateElement (type, modulename, name)){ + return; + } if (type === "component") { + let choices = []; let numberOfPropTypes = 0; @@ -172,8 +178,8 @@ program name: "componentType", message: "Select component type", paginated: true, - choices: choices - }, + choices: choices + }, { type: "list", name: "propTypes", diff --git a/lib/generate.js b/lib/generate.js index 979c1ee..9662258 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -30,8 +30,8 @@ generate.prototype.generateComponent = function(type, module, componentName, ans try { if(componentName !== undefined) { const exists = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), 'components', module, componentName), fs.F_OK); - cb(`${componentName}.react.js already exists`); - return; + cb(`${componentName}.react.js already exists`); + return; } else { const exists = fs.accessSync(path.join(process.cwd(), getSourceDirectory(), 'components', module + '.react.js'), fs.F_OK); diff --git a/lib/utils/checkduplicateelement.js b/lib/utils/checkduplicateelement.js new file mode 100644 index 0000000..f08a5c1 --- /dev/null +++ b/lib/utils/checkduplicateelement.js @@ -0,0 +1,47 @@ +const fs = require('fs.extra'); +const path = require('path'); +const getSourceDirectory = require('../source'); + +/** + * check for duplicates in provided set of propNames + * @param {string} moduleName - propnames to compare + * @param {string} elementName - propnames to compare + * @param {string} type - propnames to compare + */ + +const checkDuplicateElement = function (type, moduleName, elementName) { + + if(type === 'component'){ + if (elementName !== undefined) { + if (fs.existsSync(path.join(process.cwd(), getSourceDirectory(), 'components', moduleName, elementName + '.react.js'))) { + console.log('\x1b[1m\x1b[31m%s\x1b[0m', '\n ' + elementName + ' component is aready exsist \n Try with different name'); + return false; + } + } + else { + if (fs.existsSync(path.join(process.cwd(), getSourceDirectory(), 'components', moduleName + '.react.js'))) { + console.log('\x1b[1m\x1b[31m%s\x1b[0m', '\n ' + elementName + ' component is aready exsist \n Try with different name'); + return false; + } + } + } + else if(type === 'test'){ + if (elementName !== undefined) { + if (fs.existsSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', moduleName, elementName + '.js'))) { + console.log('\x1b[1m\x1b[31m%s\x1b[0m', '\n ' + elementName + ' test file is aready exsist \n Try with different name'); + return false; + } + } + else { + if (fs.existsSync(path.join(process.cwd(), getSourceDirectory(), '__tests__', moduleName + '.js'))) { + console.log('\x1b[1m\x1b[31m%s\x1b[0m', '\n ' + elementName + ' test file is aready exsist \n Try with different name'); + return false; + } + } + } + + return true; + +}; + +module.exports = checkDuplicateElement;