Skip to content

Commit

Permalink
feat(packages/sui-studio): Add new param to use explicit import
Browse files Browse the repository at this point in the history
  • Loading branch information
stivaliserna committed Sep 1, 2022
1 parent 9c066a0 commit 816fc6b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
8 changes: 8 additions & 0 deletions packages/sui-studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Use `-C path` or `--context path` parameter to generate the component with a giv
$ npx sui-studio generate house window -C ./myCustomContext.js
```

#### Explicit import

Use `-E` or `--explicit` parameter to generate a component in a file with its same name and an index re-export.

```sh
$ npx sui-studio generate house window -E
```

### Use new compiler

We're migrating to `swc` so you could generate the new component with the expected `prepare` field by using the flag `--swc` or `-W`.
Expand Down
41 changes: 26 additions & 15 deletions packages/sui-studio/bin/sui-studio-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.option('-P, --prefix <prefix>', 'add prefix for this component')
.option('-S, --scope <scope>', 'add scope for this component')
.option('-W, --swc', 'Use the new SWC compiler', false)
.option('-E, --explicit', 'Use explicit import', false)
.on('--help', () => {
console.log(' Examples:')
console.log('')
Expand Down Expand Up @@ -52,6 +53,7 @@ const componentInPascal = toPascalCase(
const COMPONENT_DIR = `/components/${category}/${component}/`
const COMPONENT_PATH = `${BASE_DIR}${COMPONENT_DIR}`
const COMPONENT_ENTRY_JS_POINT_FILE = `${COMPONENT_PATH}src/index.js`
const COMPONENT_JS_POINT_FILE = `${COMPONENT_PATH}src/${componentInPascal}.js`
const COMPONENT_PACKAGE_JSON_FILE = `${COMPONENT_PATH}package.json`
const COMPONENT_PACKAGE_GITIGNORE_FILE = `${COMPONENT_PATH}.gitignore`
const COMPONENT_PACKAGE_NPMIGNORE_FILE = `${COMPONENT_PATH}.npmignore`
Expand All @@ -66,7 +68,7 @@ const COMPONENT_CONTEXT_FILE = `${DEMO_DIR}context.js`
const TEST_DIR = `${COMPONENT_PATH}/test/`
const COMPONENT_TEST_FILE = `${TEST_DIR}index.test.js`

const {context, scope, prefix = 'sui', swc} = program.opts()
const {context, scope, prefix = 'sui', swc, explicit} = program.opts()
const packageScope = scope ? `@${scope}/` : ''
const packageCategory = category ? `${toKebabCase(category)}-` : ''
const packageName = `${packageScope}${prefix}-${packageCategory}${toKebabCase(
Expand Down Expand Up @@ -139,6 +141,25 @@ describe${context ? '.context.default' : ''}('${componentInPascal}', ${
})
`

const componentTemplate = `// import PropTypes from 'prop-types'
export default function ${componentInPascal}() {
return (
<div className="${prefix}-${componentInPascal}">
<h1>${componentInPascal}</h1>
</div>
)
}
${componentInPascal}.displayName = '${componentInPascal}'
${componentInPascal}.propTypes = {}
`

const explicitImportTemplate = `import ${componentInPascal} from './${componentInPascal}.js'
export default ${componentInPascal}
`

const defaultContext = `module.exports = {
default: {
i18n: {
Expand Down Expand Up @@ -225,21 +246,11 @@ test

writeFile(
COMPONENT_ENTRY_JS_POINT_FILE,
`// import PropTypes from 'prop-types'
export default function ${componentInPascal}() {
return (
<div className="${prefix}-${componentInPascal}">
<h1>${componentInPascal}</h1>
</div>
)
}
${componentInPascal}.displayName = '${componentInPascal}'
${componentInPascal}.propTypes = {}
`
explicit ? explicitImportTemplate : componentTemplate
),

explicit && writeFile(COMPONENT_JS_POINT_FILE, componentTemplate),

writeFile(
COMPONENT_ENTRY_SCSS_POINT_FILE,
`@import '~@s-ui/theme/lib/index';
Expand Down Expand Up @@ -296,7 +307,7 @@ export default () => <${componentInPascal} />
),

context &&
(function () {
(function() {
const isBooleanContext = typeof context === 'boolean'

writeFile(
Expand Down

0 comments on commit 816fc6b

Please sign in to comment.