npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

k-react-cm

v4.1.0

Published

manages your project components from cli

Downloads

11

Readme

React component manager (k-react-cm)

Manages your react components from cli. Easy to configure and extend.

npm npm NPM

Getting started

First of all install the package from npm.

# npm global installation
npm i -g k-react-cm

# npm local installation
npm i -D k-react-cm

# yarn local installation
yarn add -D k-react-cm

Then setup the config. You can do this in two ways: by creating separated react-cm.json file in the project root or by creating reactCm field in "package.json". Info about how to fill it below.

Usage

Check out full config example if you want to start quickly, otherwise read usage sections.

Templates

K-react-cm supports complex & single file component templates. To use template you need assign a name, the path to file or directory containing template and the out dir where components will be created.

Example of templates structure:

/templates
    /simple_component.tsx

    /complex_component
        /index.tsx
        /style.module.scss
        /test.tsx

Example of config:

{
    "templates": [
        {
            "name": "simple",
            "path": "./templates/simple_component.tsx",
            "outDir": "./components/"
        },

        {
            "name": "complex",
            "path": "./templates/complex_component",
            "outDir": "./components/"
        }
    ]
}

What if we want to create single file component in its own directory, or complex component without subdir? We can do it with subDir prop in the template config.

Example of subDir prop:

{
    "components": [
        {
            "name": "fn",
            "path": "@t/fn.tsx",
            "subDir": true
        }
    ]
}

We can set default outDir to all components and omit it declaration in templates:

{
    "defaults": {
        "outDir": "./example/out/"
    },

    "templates": [
        {
            "name": "simple",
            "path": "./templates/simple_component.tsx",
        },
    ]
}

Use component names

K-react-cm do replaces in file name and file content!

There are few types of available component name cases:

  • cname original case (without any transform);
  • CName PascalCase;
  • c-name kebab-case;
  • c_name snake_case;

Example of template file names:

/complex_component
    /index.tsx
    /cname.test.ts
    /cname.module.scss

Example of tsx template file:

import React from "react";

export const CName = () => {
    return (
        <div>
            <p>Text</p>
        </div>
    );
}

Example of css template file:

.c-name {
    /* your styles here */
    /* or just empty space */
}

Paths (Aliases)

Enter all paths is not convenient, because of that aliases exist! It's very easy to use them. Just declare paths array in the config, where each alias will be object, that contains name and value. Name should begin with @ symbol, and value should refer to directory full or relative path. After aliases declarations, we can use them in config & cli.

Example of aliases declaration:

"paths": [
    {
        "name": "@c",
        "value": "./components"
    },

    {
        "name": "@t",
        "value": "./templates"
    }
]

Example of aliases usage in cfg file:

{
    "components": [
        {
            "name": "fn",
            "path": "@t/fn.tsx",
            "outDir": "@c"
        }
    ]
}

Rewrites

There are situations when some tools scan files for specific extensions and it can't ignore certain directories. So we have to name files with extension prefixes like .!stories.ts or .!test.ts. But it quickly becomes tedious to rename file names in created components. We can use rewrites to fix this problem. We define aliases inside templates, because they are related to each other.

{
    "templates": [
        {
            "name": "complex",
            "path": "@t/complex",
            "subDir": false,
            
            "rewrites": [
                {
                    "from": "/cname.test.!tsx",
                    "to": "/cname.test.tsx"
                }
            ]
        }
    ]
}

Auto arches

Auto architecture helps keep the file structure clean. No need in type full path like @p/page/components to set outDir. With auto arch just type @p/page and it'll be transformed to @p/page/components automatic.

{
    "autoArches": [
        {
            "name": "c_arch",
            "pathPrefix": "example/out/pages",
            "subdirName": "components"
        }
    ],

    "templates": [
        {
            "name": "fn",
            "path": "@t/fn.tsx",
            "usingArches": ["c_arch"],
        }
    ]
}

Auto arch in action:

# Every thing is thought out, it won't mess up your path
-o @p/page => @p/page/components # works only with 1 nesting level
-o @p/page/components => @p/page/components # already have components subDir, skipping
-o @p/page/components/component => @p/page/components/component # too deep, skip

Full config example

Here is the example of working config with various component types.

{
    "defaults": {
        "outDir": "@c"
    }

    "paths": [
        {
            "name": "@c",
            "value": "./example/out",
        },

        {
            "name": "@t",
            "value": "./example/templates"
        }
    ],

    "autoArches": [
        {
            "name": "c_arch",
            "pathPrefix": "example/out/pages",
            "subdirName": "components"
        }
    ],

    "templates": [
        {
            "name": "fn",
            "path": "@t/fn.tsx",
            "outDir": "@c",

            "rewrites": [
                {
                    "from": "/cname.test.!tsx",
                    "to": "/cname.test.tsx"
                }
            ],

            "usingArches": ["c_arch"]
        },

        {
            "name": "cl",
            "path": "@t/cl"
        },

        {
            "name": "page",
            "path": "@t/page",
            "subDir": false
        }
    ]
}

Commands

! For local installation use npx k-react-cm !

k-react-cm help // to display help
k-react-cm (create | c) template_name component_name // to create component
k-react-cm (create | c) template_name component_name (-o | --out) out_dir_path // to override out dir
k-react-cm (create | c) template_name component_name (-s | --subdir) // to create all in subdir

Sources