0.2.6 ā€¢ Published 10 months ago

lenzy v0.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

šŸš€ Lenzy

NPM downloads

Lenzy is a TypeScript package that helps you extract your nested JSX components within your React project into a JSON file showcasing the imports and hierarchy your hierarchical components are following. It provides a simple command-line tool which is responsible to generate the indexes used for the search engine to do its thing šŸŒ  . Providing a Provider component that acts as a wrapper to whatever you want to do with the results, optimally allowing you to search for and navigate to your JSX components. This package uses fuse.js to handle the search over large amount of data šŸŽ‰!

šŸ“¦ Installation

To install Lenzy, simply run:

npm install --save-dev lenzy

or

yarn add --dev lenzy

šŸ¤– CLI

Lenzy provides a command-line tool that allows you to extract your JSX components imports and declaration to generate an overview of your rendering tree in the form of a JSON.

Arguments

  • <pages-dir>: The directory of your pages folder that you want to scan and analyze.
  • <pages-catalog-output>: The path to which the cli would save the pages catalog json.
  • <fuse-index-output>: The path to which the cli would save the fuse index json.
  • <generate-absolute-paths>: A boolean that allows you to choose between absolute or relative paths.

To use the CLI, simply run:

yarn lenzy compute-index <pages-dir> <pages-catalog-output> <fuse-index-output> <generate-absolute-paths>

This will compute the index of your JSX components tree of the provided pages-dir and save it in the location you provided in pages-catalog-output and fuse-index-output.

Example

Assuming your project is structured as follows:

ā”œā”€ā”€ .next/
ā”œā”€ā”€ components/
ā”‚   ā”œā”€ā”€ About/
ā”‚   ā”‚   ā”œā”€ā”€ Header.js
ā”‚   ā”‚   ā””ā”€ā”€ AboutPage.js
ā”‚   ā”œā”€ā”€ development-tool/
ā”‚   ā”‚   ā””ā”€ā”€ QuickAccess/
ā”‚   ā”‚       ā””ā”€ā”€ QuickAccess.js
ā”œā”€ā”€ pages/
ā”‚   ā”œā”€ā”€ _app.js
ā”‚   ā”œā”€ā”€ _document.js
ā”‚   ā”œā”€ā”€ index.js
ā”‚   ā”œā”€ā”€ about.js
ā”‚   ā”œā”€ā”€ blog/
ā”‚   ā”‚   ā”œā”€ā”€ index.js
ā”‚   ā”‚   ā”œā”€ā”€ [slug].js
ā”‚   ā”‚   ā””ā”€ā”€ posts.js
ā”‚   ā””ā”€ā”€ contact.js
ā”œā”€ā”€ .eslintrc
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ next.config.js
ā”œā”€ā”€ package.json
ā”œā”€ā”€ README.md
ā””ā”€ā”€ yarn.lock

You have to run this command in the root path

yarn lenzy compute-index ./pages ./components/development-tools/QuickAccess/pages-catalog.json ./components/development-tools/QuickAccess/fuse-index.json false

The following JSONs will be created

./components/development-tools/QuickAccess/pages-catalog.json

This is the whole list of all components from your pages, keep in mind the paths generated are relative since generateAbsolutePaths is false by default.

[
  {
    "route": "/about",
    "path": "./components/About/AboutPage.js",
    "component": "AboutPage",
    "parents": []
  },
  {
    "route": "/about",
    "path": "./components/About/Header.js",
    "component": "Header",
    "parents": [
      {
        "component": "AboutPage",
        "path": "./components/About/AboutPage.js"
      }
    ]
  }
  // further similar records...
]

Or if you provide the last <generate-absolute-paths> true, you will get the following

[
  {
    "route": "/about",
    "path": "/user/dev/project/components/About/AboutPage.js",
    "component": "AboutPage",
    "parents": []
  },
  {
    "route": "/about",
    "path": "/user/dev/project/components/About/Header.js",
    "component": "Header",
    "parents": [
      {
        "component": "AboutPage",
        "path": "/user/dev/project/components/About/AboutPage.js"
      }
    ]
  }
  // further similar records...
]

./components/development-tools/QuickAccess/fuse-index.json

{
  // some indexing stuff that we don't want to get involved in šŸ„²
}

šŸ“– API

Lenzy exports a single component, Provider, which is a search bar that allows you to quickly find and navigate to your JSX components.

Props

  • pagesDictionary: The path to the pages catalog json file.
  • fuseIndex: The path to the fuse index json file.
  • fetchDebounceTimeout (optional): The debounce timeout for the search engine, defaults to 500.
  • fuseOptions other configurations that you can find in the fuse.js documentation.
import { Provider } from "lenzy";
import PAGES_DICT from "./pages-catalog.json";
import FUSE_INDEX from "./fuse-index.json";

const QuickAccess = () => (
  <Provider pagesDictionary={PAGES_DICT} fuseIndex={FUSE_INDEX}>
    {({ results, value, onChange }) => (
      <>
        <h1>Quick Access Component</h1>
        {/* your thang šŸŒƒ */}
        <input value={value} onChange={(ev) => event.target.value} />
        <ul>
          {results.map((result) => (
            <li onClick={() => router.push(result.route)}>
              {result.parents.join("/")} - {result.component}
            </li>
          ))}
        </ul>
      </>
    )}
  </Provider>
);

This will add the Provider component to your app, allowing you to have access to the value of the search and the onChange which is a search triggerer and finally the results which is mainly everything related to the value you provided.

results is a list of matches related to the provided value and you can interpret that one item from the list will follow the object provided earlier in pages-catalog.json

Local Example

Check example folder and feel free to run the project locally through the following command

node ./dist/cli.js compute-index ./example/pages example/pages-dictionary.json example/fuse-index.json true

šŸ“œ License

Lenzy is released under the MIT License.

šŸ‘Øā€šŸ’» Contributing

Contributions are welcome! If you'd like to contribute to Lenzy, please open an issue or pull request.

šŸ“¬ Contact

If you have any questions or comments about Lenzy, please feel free to contact me at yousseftarek@live.com.

Thank you for using Lenzy! šŸŽ‰

0.2.1

12 months ago

0.2.6

10 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.0

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago