Skip to content

Commit

Permalink
feat: styled-components support
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleedesign committed Mar 19, 2023
1 parent 38c9723 commit 2968bb2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/templates/component/componentStyledTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default `import styled from 'styled-components';
export const TemplateNameWrapper = styled.div\`
\`;
`;
47 changes: 32 additions & 15 deletions src/utils/generateComponentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { aiComponentGenerator } from '../services/openAiService.js';
import componentJsTemplate from '../templates/component/componentJsTemplate.js';
import componentTsTemplate from '../templates/component/componentTsTemplate.js';
import componentCssTemplate from '../templates/component/componentCssTemplate.js';
import componentStyledTemplate from '../templates/component/componentStyledTemplate.js';
import componentLazyTemplate from '../templates/component/componentLazyTemplate.js';
import componentTsLazyTemplate from '../templates/component/componentTsLazyTemplate.js';
import componentStoryTemplate from '../templates/component/componentStoryTemplate.js';
Expand Down Expand Up @@ -81,7 +82,7 @@ Please make sure you're pointing to the right custom template path in your gener
}

function componentTemplateGenerator({ cmd, componentName, cliConfigFile }) {
const { cssPreprocessor, testLibrary, usesCssModule, usesTypeScript } = cliConfigFile;
const { usesStyledComponents, cssPreprocessor, testLibrary, usesCssModule, usesTypeScript } = cliConfigFile;
const { customTemplates } = cliConfigFile.component[cmd.type];
let template = null;
let filename = null;
Expand Down Expand Up @@ -113,16 +114,27 @@ function componentTemplateGenerator({ cmd, componentName, cliConfigFile }) {
// --- If it has a corresponding stylesheet

if (cmd.withStyle) {
const module = usesCssModule ? '.module' : '';
const cssPath = `${componentName}${module}.${cssPreprocessor}`;
if (cliConfigFile.usesStyledComponents) {
const cssPath = `${componentName}.styled`;
template = template.replace(
`import styles from './TemplateName.module.css'`,
`import { TemplateNameWrapper } from './${cssPath}'`
);
template = template.replace(` className={styles.TemplateName}`, '');
template = template.replace(` <div`, '<TemplateNameWrapper');
template = template.replace(` </div>`, '</TemplateNameWrapper>');
} else {
const module = usesCssModule ? '.module' : '';
const cssPath = `${componentName}${module}.${cssPreprocessor}`;

// --- If the css module is true make sure to update the template accordingly
// --- If the css module is true make sure to update the template accordingly

if (module.length) {
template = template.replace(`'./TemplateName.module.css'`, `'./${cssPath}'`);
} else {
template = template.replace(`{styles.TemplateName}`, `"${componentName}"`);
template = template.replace(`styles from './TemplateName.module.css'`, `'./${cssPath}'`);
if (module.length) {
template = template.replace(`'./TemplateName.module.css'`, `'./${cssPath}'`);
} else {
template = template.replace(`{styles.TemplateName}`, `"${componentName}"`);
template = template.replace(`styles from './TemplateName.module.css'`, `'./${cssPath}'`);
}
}
} else {
// --- If no stylesheet, remove className attribute and style import from jsTemplate
Expand Down Expand Up @@ -157,14 +169,19 @@ function componentStyleTemplateGenerator({ cliConfigFile, cmd, componentName })
template = customTemplate;
filename = customTemplateFilename;
} else {
const { cssPreprocessor, usesCssModule } = cliConfigFile;
const module = usesCssModule ? '.module' : '';
const cssFilename = `${componentName}${module}.${cssPreprocessor}`;
const { usesTypeScript, usesStyledComponents, cssPreprocessor, usesCssModule } = cliConfigFile;
if (usesStyledComponents) {
filename = usesTypeScript ? `${componentName}.styled.ts` : `${componentName}.styled.js`;
template = componentStyledTemplate;
} else {
const module = usesCssModule ? '.module' : '';
const cssFilename = `${componentName}${module}.${cssPreprocessor}`;

// --- Else use GRC built-in style template
// --- Else use GRC built-in style template

template = componentCssTemplate;
filename = cssFilename;
template = componentCssTemplate;
filename = cssFilename;
}
}

return {
Expand Down
11 changes: 10 additions & 1 deletion src/utils/grcConfigUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ const projectLevelQuestions = [
},
{
type: 'confirm',
name: 'usesStyledComponents',
message: 'Does this project use styled-components?',
},
{
type: 'confirm',
when: (answers) => !answers['usesStyledComponents'],
name: 'usesCssModule',
message: 'Does this project use CSS modules?',
},
{
type: 'list',
name: 'cssPreprocessor',
when: (answers) => !answers['usesStyledComponents'],
message: 'Does this project use a CSS Preprocessor?',
choices: ['css', 'scss', 'less', 'styl'],
},
Expand Down Expand Up @@ -184,7 +191,9 @@ export async function getCLIConfigFile() {
*/

const missingConfigQuestions = grcConfigQuestions.filter(
(question) => !deepKeys(currentConfigFile).includes(question.name)
(question) =>
!deepKeys(currentConfigFile).includes(question.name) &&
(question.when ? question.when(currentConfigFile) : true)
);

if (missingConfigQuestions.length) {
Expand Down

0 comments on commit 2968bb2

Please sign in to comment.