Skip to content

feat: add support for .jsx file format in component generation #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ When you run GRC within your project the first time, it will ask you a series of
```json
{
"usesTypeScript": true,
"usesJsxFormat": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
Expand Down Expand Up @@ -193,6 +194,7 @@ You can do so by extending the **generate-react-cli.json** config file like this
```json
{
"usesTypeScript": false,
"usesJsxFormat": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
Expand Down Expand Up @@ -259,6 +261,7 @@ The keys represent the type of file, and the values are the paths that point to
```json
{
"usesTypeScript": false,
"usesJsxFormat": false,
"usesCssModule": true,
"cssPreprocessor": "scss",
"testLibrary": "Testing Library",
Expand Down Expand Up @@ -437,6 +440,7 @@ You can do so by editing your **generate-react-cli.json** config file like so.
```json
{
"usesTypeScript": false,
"usesJsxFormat": false,
"usesCssModule": false,
"cssPreprocessor": "css",
"testLibrary": "Testing Library",
Expand Down
37 changes: 27 additions & 10 deletions src/utils/generateComponentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function componentDirectoryNameGenerator({ cmd, componentName, cliConfigFile, fi

function componentTemplateGenerator({ cmd, componentName, cliConfigFile, convertors }) {
// @ts-ignore
const { usesStyledComponents, cssPreprocessor, testLibrary, usesCssModule, usesTypeScript } = cliConfigFile;
const { usesStyledComponents, cssPreprocessor, testLibrary, usesCssModule, usesTypeScript, usesJsxFormat } =
cliConfigFile;
const { customTemplates } = cliConfigFile.component[cmd.type];
let template = null;
let filename = null;
Expand All @@ -149,7 +150,7 @@ function componentTemplateGenerator({ cmd, componentName, cliConfigFile, convert
// --- Else use GRC built-in component template

template = usesTypeScript ? componentTsTemplate : componentJsTemplate;
filename = usesTypeScript ? `${componentName}.tsx` : `${componentName}.js`;
filename = usesTypeScript ? `${componentName}.tsx` : usesJsxFormat ? `${componentName}.jsx` : `${componentName}.js`;

// --- If test library is not Testing Library or if withTest is false. Remove data-testid from template

Expand Down Expand Up @@ -215,9 +216,13 @@ function componentStyleTemplateGenerator({ cliConfigFile, cmd, componentName, co
template = customTemplate;
filename = customTemplateFilename;
} else {
const { usesTypeScript, usesStyledComponents, cssPreprocessor, usesCssModule } = cliConfigFile;
const { usesTypeScript, usesStyledComponents, cssPreprocessor, usesCssModule, usesJsxFormat } = cliConfigFile;
if (usesStyledComponents) {
filename = usesTypeScript ? `${componentName}.styled.ts` : `${componentName}.styled.js`;
filename = usesTypeScript
? `${componentName}.styled.ts`
: usesJsxFormat
? `${componentName}.styled.jsx`
: `${componentName}.styled.js`;
template = componentStyledTemplate;
} else {
const module = usesCssModule ? '.module' : '';
Expand All @@ -239,7 +244,7 @@ function componentStyleTemplateGenerator({ cliConfigFile, cmd, componentName, co

function componentTestTemplateGenerator({ cliConfigFile, cmd, componentName, convertors }) {
const { customTemplates } = cliConfigFile.component[cmd.type];
const { testLibrary, usesTypeScript } = cliConfigFile;
const { testLibrary, usesTypeScript, usesJsxFormat } = cliConfigFile;
let template = null;
let filename = null;

Expand All @@ -256,7 +261,11 @@ function componentTestTemplateGenerator({ cliConfigFile, cmd, componentName, con
template = customTemplate;
filename = customTemplateFilename;
} else {
filename = usesTypeScript ? `${componentName}.test.tsx` : `${componentName}.test.js`;
filename = usesTypeScript
? `${componentName}.test.tsx`
: usesJsxFormat
? `${componentName}.test.jsx`
: `${componentName}.test.js`;

if (testLibrary === 'Enzyme') {
// --- Else use GRC built-in test template based on test library type
Expand All @@ -277,7 +286,7 @@ function componentTestTemplateGenerator({ cliConfigFile, cmd, componentName, con
}

function componentStoryTemplateGenerator({ cliConfigFile, cmd, componentName, convertors }) {
const { usesTypeScript } = cliConfigFile;
const { usesTypeScript, usesJsxFormat } = cliConfigFile;
const { customTemplates } = cliConfigFile.component[cmd.type];
let template = null;
let filename = null;
Expand All @@ -298,7 +307,11 @@ function componentStoryTemplateGenerator({ cliConfigFile, cmd, componentName, co
// --- Else use GRC built-in story template

template = componentStoryTemplate;
filename = usesTypeScript ? `${componentName}.stories.tsx` : `${componentName}.stories.js`;
filename = usesTypeScript
? `${componentName}.stories.tsx`
: usesJsxFormat
? `${componentName}.stories.jsx`
: `${componentName}.stories.js`;
}

return {
Expand All @@ -309,7 +322,7 @@ function componentStoryTemplateGenerator({ cliConfigFile, cmd, componentName, co
}

function componentLazyTemplateGenerator({ cmd, componentName, cliConfigFile, convertors }) {
const { usesTypeScript } = cliConfigFile;
const { usesTypeScript, usesJsxFormat } = cliConfigFile;
const { customTemplates } = cliConfigFile.component[cmd.type];
let template = null;
let filename = null;
Expand All @@ -330,7 +343,11 @@ function componentLazyTemplateGenerator({ cmd, componentName, cliConfigFile, con
// --- Else use GRC built-in lazy template

template = usesTypeScript ? componentTsLazyTemplate : componentLazyTemplate;
filename = usesTypeScript ? `${componentName}.lazy.tsx` : `${componentName}.lazy.js`;
filename = usesTypeScript
? `${componentName}.lazy.tsx`
: usesJsxFormat
? `${componentName}.lazy.jsx`
: `${componentName}.lazy.js`;
}

return {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/grcConfigUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const projectLevelQuestions = [
name: 'usesTypeScript',
message: 'Does this project use TypeScript?',
},
{
type: 'confirm',
name: 'usesJsxFormat',
message: 'Do you want to use the .jsx file format for components?',
default: false,
},
{
type: 'confirm',
name: 'usesStyledComponents',
Expand Down