Skip to content

Commit

Permalink
Merge pull request #3 from tareqdayya/dev
Browse files Browse the repository at this point in the history
Dev 1.4.1
  • Loading branch information
tareqdayya committed Jun 9, 2020
2 parents fb4a26e + 1ec4546 commit 452fa30
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 145 deletions.
60 changes: 30 additions & 30 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true,
},
extends: [
'standard', 'eslint',
],
parserOptions: {
ecmaVersion: 11,
},
rules: {
'require-jsdoc': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-jsdoc': 'off',
quotes: ['error', 'single'],
strict: 'off',
'valid-jsdoc': 'off',
'comma-dangle': ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'always-multiline',
}],
semi: ['error', 'always'],
'prefer-template': 'error',
curly: ['error', 'multi-line'],
},
env: {
commonjs: true,
es6: true,
node: true,
},
extends: [
'standard',
],
parserOptions: {
ecmaVersion: 11,
},
rules: {
'require-jsdoc': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-description': 'off',
'jsdoc/require-jsdoc': 'off',
quotes: ['error', 'single'],
strict: 'off',
'valid-jsdoc': 'off',
'comma-dangle': ['error', {
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'always-multiline',
}],
semi: ['error', 'always'],
'prefer-template': 'error',
curly: ['error', 'multi-line'],
},
};
74 changes: 37 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@ const writeFile = require('./lib/writeFile');
const { stylesheetType } = require('./lib/constants');

const styleSheetTypes = [
{ name: 'CSS', value: { extension: 'css', type: stylesheetType.cssBased } },
{ name: 'SCSS', value: { extension: 'scss', type: stylesheetType.cssBased } },
{ name: 'Less', value: { extension: 'less', type: stylesheetType.cssBased } },
{ name: 'Sass', value: { extension: 'sass', type: stylesheetType.cssBased } },
{ name: 'React Native Style', value: { extension: 'ts', type: stylesheetType.reactNative } },
{ name: 'Styled Components', value: { extension: 'ts', type: stylesheetType.styledComponents } },
{ name: 'CSS', value: { extension: 'css', type: stylesheetType.cssBased } },
{ name: 'SCSS', value: { extension: 'scss', type: stylesheetType.cssBased } },
{ name: 'Less', value: { extension: 'less', type: stylesheetType.cssBased } },
{ name: 'Sass', value: { extension: 'sass', type: stylesheetType.cssBased } },
{ name: 'React Native Style', value: { extension: 'ts', type: stylesheetType.reactNative } },
{ name: 'Styled Components', value: { extension: 'ts', type: stylesheetType.styledComponents } },
];

// PROMPT
inquirer
.prompt([
{ type: 'input', name: 'componentName', message: 'whats your component\'s name?' },
{
type: 'confirm',
name: 'isFunction',
message: 'is this a function component?',
},
{
type: 'list',
name: 'styleSheetExtension',
message: 'whats stylesheet preprocessor are u using?',
choices: styleSheetTypes,
},
{
type: 'confirm',
name: 'hasTestFile',
message: 'Do you want to generate a test file?',
},
{
type: 'input',
name: 'finalPath',
message: 'what\'s the path to the folder? (leave empty for ./src/components)',
},
])
.then(answers => writeFile(
answers.componentName,
answers.finalPath || './src/components',
!!answers.isFunction,
answers.styleSheetExtension,
!!answers.hasTestFile,
));
.prompt([
{ type: 'input', name: 'componentName', message: 'whats your component\'s name?' },
{
type: 'confirm',
name: 'isFunction',
message: 'is this a function component?',
},
{
type: 'list',
name: 'styleSheetExtension',
message: 'whats stylesheet preprocessor are u using?',
choices: styleSheetTypes,
},
{
type: 'confirm',
name: 'hasTestFile',
message: 'Do you want to generate a test file?',
},
{
type: 'input',
name: 'finalPath',
message: 'what\'s the path to the folder? (leave empty for ./src/components)',
},
])
.then(answers => writeFile(
answers.componentName,
answers.finalPath || './src/components',
!!answers.isFunction,
answers.styleSheetExtension,
!!answers.hasTestFile,
));
6 changes: 3 additions & 3 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const stylesheetType = {
cssBased: 'cssBased',
styledComponents: 'styledComponents',
reactNative: 'reactNativeStyles',
cssBased: 'cssBased',
styledComponents: 'styledComponents',
reactNative: 'reactNativeStyles',
};

module.exports = { stylesheetType };
30 changes: 15 additions & 15 deletions lib/stringHelpers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
function convertCamelCaseToSpinal(string) {
if (!(typeof string === 'string')) return string;
let convertedString = '';
let character = '';
function convertCamelCaseToSpinal (string) {
if (!(typeof string === 'string')) return string;
let convertedString = '';
let character = '';

// LOOP OVER CHARACTERS
for (let i = 0; i < string.length; i++) {
character = string.charAt(i);
// LOOP OVER CHARACTERS
for (let i = 0; i < string.length; i++) {
character = string.charAt(i);

// NUMERIC CHARACTER: KEEP AS IS
if (!isNaN(parseInt(character, 10))) convertedString += String(character);
// NUMERIC CHARACTER: KEEP AS IS
if (!isNaN(parseInt(character, 10))) convertedString += String(character);

// ALPHA CHARACTER
else { // SEPARATE AT UPPERCASE WITH '-'
if (character === character.toUpperCase() && i > 0) convertedString += '-';
// ALPHA CHARACTER
else { // SEPARATE AT UPPERCASE WITH '-'
if (character === character.toUpperCase() && i > 0) convertedString += '-';

convertedString += character.toLowerCase();
}
convertedString += character.toLowerCase();
}
}

return convertedString;
return convertedString;
}

module.exports = { convertCamelCaseToSpinal };
117 changes: 59 additions & 58 deletions lib/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,77 @@ const mustache = require('mustache');
const { convertCamelCaseToSpinal } = require('./stringHelpers');
const { stylesheetType } = require('./constants');

async function createDirectoryIfNotExistOrReturnPath(componentName, targetPath) {
const targetDir = path.join(process.cwd(), targetPath, componentName);
async function createDirectoryIfNotExistOrReturnPath (componentName, targetPath) {
const targetDir = path.join(process.cwd(), targetPath, componentName);

if (!fs.existsSync(targetDir)) await mkdirp(targetDir);
if (!fs.existsSync(targetDir)) await mkdirp(targetDir);

return targetDir;
return targetDir;
}

async function writeFile(
componentName, targetPath, isFunctionComponent, styleSheetChoice, hasTestFile,
async function writeFile (
componentName, targetPath, isFunctionComponent, styleSheetChoice, hasTestFile,
) {
const targetDir = await createDirectoryIfNotExistOrReturnPath(componentName, targetPath);
const targetDir = await createDirectoryIfNotExistOrReturnPath(componentName, targetPath);

// SELECT TEMPLATES
const componentTemplate = isFunctionComponent ? 'reactFunctionalComponentTemplate.txt'
: 'reactComponentTemplate.txt';
let styleSheetTemplate;
// SELECT TEMPLATES
const componentTemplate = isFunctionComponent ? 'reactFunctionalComponentTemplate.txt'
: 'reactComponentTemplate.txt';
let styleSheetTemplate, explicitStyleSheetImport;
switch (styleSheetChoice.type) {
case stylesheetType.reactNative:
styleSheetTemplate = 'reactNative.styleSheet.txt';
explicitStyleSheetImport = ' styles from';
break;
case stylesheetType.styledComponents:
styleSheetTemplate = 'styledComponents.styleSheet.txt';
explicitStyleSheetImport = ' Styles from';
break;
case stylesheetType.cssBased:
default:
styleSheetTemplate = 'cssBased.styleSheet.txt';
explicitStyleSheetImport = '';
}

switch (styleSheetChoice.type) {
case stylesheetType.reactNative:
styleSheetTemplate = 'reactNative.styleSheet.txt';
break;
case stylesheetType.styledComponents:
styleSheetTemplate = 'styledComponents.styleSheet.txt';
break;
case stylesheetType.cssBased:
default:
styleSheetTemplate = 'cssBased.styleSheet.txt';
}
// MAP FILES TO PATHS
const templatePaths = {
index: path.join(__dirname, 'templates', 'index.txt'),
component: path.join(__dirname, 'templates/components', componentTemplate),
stylesheet: path.join(__dirname, 'templates/stylesheets', styleSheetTemplate),
test: hasTestFile ? path.join(__dirname, 'templates', 'testTemplate.txt') : null,
};

// MAP FILES TO PATHS
const templatePaths = {
index: path.join(__dirname, 'templates', 'index.txt'),
component: path.join(__dirname, 'templates/components', componentTemplate),
stylesheet: path.join(__dirname, 'templates/stylesheets', styleSheetTemplate),
test: hasTestFile ? path.join(__dirname, 'templates', 'testTemplate.txt') : null,
};
const namesOfFilesToWrite = {
index: 'index.tsx',
component: `${componentName}.tsx`,
stylesheet: `${componentName}Styles.${styleSheetChoice.extension}`,
test: hasTestFile ? `${componentName}.test.tsx` : null,
};

const namesOfFilesToWrite = {
index: 'index.tsx',
component: `${componentName}.tsx`,
stylesheet: `${componentName}Styles.${styleSheetChoice.extension}`,
test: hasTestFile ? `${componentName}.test.tsx` : null,
};
// LOOP OVER PATHS AND WRITE
Object.entries(templatePaths).forEach(([file, templatePath]) => {
if (!templatePath) return;

// LOOP OVER PATHS AND WRITE
Object.entries(templatePaths).forEach(([file, templatePath]) => {
if (!templatePath) return;
(async function () {
const templateString = await fsRead(templatePath, 'utf-8');

(async function() {
const templateString = await fsRead(templatePath, 'utf-8');
// PLUG-IN VARS INTO TEMPLATE
const finalFileContent = mustache.render(
templateString,
{
COMPONENT_NAME: componentName,
EXPLICIT_STYLESHEET_IMPORT: explicitStyleSheetImport,
STYLESHEET_FILENAME: `${componentName}Styles`,
STYLESHEET_EXTENSION: styleSheetChoice.extension === 'ts' ? ''
: `.${styleSheetChoice.extension}`,
WEB_CLASS_NAME: convertCamelCaseToSpinal(componentName),
},
);

// PLUG-IN VARS INTO TEMPLATE
const finalFileContent = mustache.render(
templateString,
{
COMPONENT_NAME: componentName,
EXPLICIT_STYLESHEET_IMPORT: styleSheetChoice.type ===
stylesheetType.cssBased ? '' : ' styles from',
STYLESHEET_FILENAME: `${componentName}Styles`,
STYLESHEET_EXTENSION: styleSheetChoice.extension === 'ts' ? ''
: `.${styleSheetChoice.extension}`,
WEB_CLASS_NAME: convertCamelCaseToSpinal(componentName),
},
);

// WRITE
await fsWrite(path.join(targetDir, namesOfFilesToWrite[file]), finalFileContent);
})();
});
// WRITE
await fsWrite(path.join(targetDir, namesOfFilesToWrite[file]), finalFileContent);
})();
});
}

module.exports = writeFile;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tareqdayya/react-ts-files-generator",
"version": "1.4.0",
"version": "1.4.1",
"description": "generates directories with react components, index, and stylesheet files inside",
"main": "index.js",
"scripts": {
Expand All @@ -23,7 +23,6 @@
},
"devDependencies": {
"eslint": "^7.2.0",
"eslint-config-eslint": "^6.0.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.21.1",
"eslint-plugin-node": "^11.1.0",
Expand Down

0 comments on commit 452fa30

Please sign in to comment.