Giter Site home page Giter Site logo

nbatony / script-init-component Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 26 KB

Customizable script. Scaffolds TypeScript/React component template generating custom folders and file contents based on the specific architecture and stack of the project.

JavaScript 71.91% TypeScript 28.09%
frontend jss productivity react scripts typescript scaffold ui

script-init-component's Introduction

About

A script to generate a folder named as your component, containing a minimalist react-typescript (.tsx) template component with its jest test file, types file, stories file (storybook.js), and styles file (react-jss). Develop faster by creating all you need for your new component at once.

└── my-component
    │
    └── MyComponent.tsx
    └── MyComponent.types.tsx
    └── MyComponent.styles.tsx
    └── MyComponent.test.tsx
    └── MyComponent.stories.tsx
    └── index.ts

Installation

npm i -D @tonisanchez.dev/init-component

Alternatively

yarn add -D @tonisanchez.dev/init-component

How it works

Execute the script on your package.json like this:

// package.json
...
  "component": "node node_modules/@tonisanchez.dev/init-component/src 'YOUR_COMPONENTS_FOLDER_DIR'",
...

Everytime you call this script a new folder will be created containing 6 files:

  • Types file with a minimal type declaration for props.
  • Declaration of styles with a basic react-jss configuration.
  • Stories file for storybook.js code-level documentation.
  • Test file with a pre-built test that is passing.
  • Component file that renders a minimalistic component importing above files.
  • An index to use it optionally for repositories with an folder structure of "index" files.

Notice that the folder created containing the above files will be named after the component name you specify as first parameter. See next section.

Parameters

Component name

Using script as shown above, you can specify the name of your component with a following parameter as shown below:

npm run component MyComponent

Alternatively

yarn component MyComponent

subfolder

You can specify a subfolder with the second parameter. This is specially useful for projects that follows atomic design principles. With the second optional parameter you can specify the level of the component represented in the scale [atoms, molecules, organisms, templates, pages].

Examples

Assuming the following in your package.json

...
  "component": "node '@tonisanchez.dev/init-component' './src/components'"
...

And then running the script below

yarn run component MyComponent organisms

Will generate the next content

src
│
└───components
│   │
│   └── ...
│   │
│   └── organisms
│       │
│       └── my-component
│           │
│           └── MyComponent.tsx
│           └── MyComponent.types.tsx
│           └── MyComponent.styles.tsx
│           └── MyComponent.test.tsx
│           └── MyComponent.stories.tsx
│           └── index.ts
└─...

Default content of files generated

[COMPONENT.tsx]

import React from 'react'

import { useStyles } from './styles'
import { MyComponentProps } from './types'

const MyComponent = ({ children }: MyComponentProps) => {
  const classes = useStyles()
  return (
    <button className={classes.myButton}>
      <span className={classes.myLabel}>{children}</span>
    </button>
  )
}

export default MyComponent

[COMPONENT.types.tsx]

export type MyComponentProps = {
  children: string
}

[COMPONENT.styles.tsx]

import { createUseStyles } from 'react-jss'

export const useStyles = createUseStyles({
  myButton: {
    color: 'green',
    margin: {
      top: 5,
      right: 0,
      bottom: 0,
      left: '1rem'
    },
    '& span': {
      fontWeight: 'bold'
    }
  },
  myLabel: {
    fontStyle: 'italic'
  }
})

[COMPONENT.test.tsx]

import React from 'react'
import { render, screen } from '@testing-library/react'
import MyComponent from './component'

describe('Block of tests', () => {
  test('renders children text', () => {
    render(<MyComponent>Hola</MyComponent>)
    const demoElement = screen.getByText(/Hola/i)
    expect(demoElement).toBeInTheDocument()
  })
})

[COMPONENT.stories.tsx]

import React from 'react';
import { ComponentStory, ComponentMeta } from '@storybook/react';

import MyComponent from '.';

export default {
  title: 'Components/MyComponent',
  component: MyComponent,
} as ComponentMeta<typeof MyComponent>;

const Template: ComponentStory<typeof MyComponent> = (args: any) => <MyComponent {...args} />;

export const Primary = Template.bind({});
/* Primary.parameters = {
  backgrounds: { default: 'dark' }
};

Primary.args = {
  label: 'MyComponent',
  variant: MyComponentVariants.primary
};

export const Secondary = Template.bind({});
Secondary.args = {
  label: 'MyComponent',
  variant: MyComponentVariants.secondary
}; */

Have fun with it

I will be publishing newer versions and extend this simple and small tool. Meanwhile, I have more interesting projects on my Github @TonySapa or site tonisanchez.dev

script-init-component's People

Contributors

nbatony avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.