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

mvcube

v1.0.5

Published

MVCube is a powerful CLI tool designed to streamline the development process in Next.js projects. It allows developers to quickly generate MVC (Model-View-Controller) structures, including Models, Controllers, Views, and reusable UI Components. MVCube sim

Downloads

323

Readme

MVCube

MVCube is a CLI tool that simplifies the creation of MVC structures and reusable components in Next.js projects. It helps to quickly set up Models, Controllers, Views, and Components (both global and local).


Installation

Install globally with:

npm install -g mvcube

Usage

Create an MVC structure for a resource

mvcube create <resource-name>

Example:

mvcube create product

This will generate the necessary files for creating a Model, Controller, and Views for a resource.

Componentize a section of your code into a reusable component

mvcube componentize <component-name>

Example:

mvcube componentize button

This will extract a section of your code into a reusable component and place it in the appropriate components folder. You will then choose whether the component is global or local.


Generated Structure

The CLI generates the following structure for the create command:

src/
  ├── models/
  │     └── product.ts
  ├── controllers/
  │     └── productController.ts
  └── app/
        └── product/
              ├── page.tsx
              └── [id]/
                    └── page.tsx

For the componentize command, the CLI creates a component in the components/ folder:

src/
  └── components/
        └── <component-name>.tsx

Explanation:

  1. Model (product.ts): Defines the resource's type in TypeScript.
  2. Controller (productController.ts): Implements CRUD operations.
  3. Next.js Pages:
    • List Page (page.tsx): Displays a list of resources.
    • Details Page ([id]/page.tsx): Displays details of a specific resource.
  4. Component (<component-name>.tsx): A reusable UI component. You can choose whether the component will be global (used throughout the entire application with the alias @/components/{Componente}) or local (used only within the current page with a relative import ./components/{Componente}).

Example: product

When you run:

mvcube create product

The following files will be created:

1. Model (src/models/product.ts)

export type Product = {
  id: string;
  name: string;
  description?: string;
  price: number;
  stock: number;
  category: string;
};

2. Controller (src/controllers/productController.ts)

import { Product } from "../models/product";

// List all products
export const getAllProducts = async () => {
  return [{ id: "1", name: "Product A", price: 100 }];
};

// Get a product by ID
export const getProductById = async (id) => {
  return { id, name: "Product A", price: 100 };
};

// Create a product
export const createProduct = async (data: Product) => {
  const newProduct = { id: Date.now().toString(), ...data };
  return newProduct;
};

// Update a product
export const updateProduct = async (id, data) => {
  const updatedProduct = { id, ...data };
  return updatedProduct;
};

// Delete a product
export const deleteProduct = async (id) => {
  return { success: true, id };
};

3. List Page (src/app/product/page.tsx)

import { getAllProducts } from "../../controllers/productController";

export default async function ProductPage() {
  const products = await getAllProducts();

  return (
    <div>
      <h1>Product List</h1>
      <ul>
        {products.map((product) => (
          <li key={product.id}>
            {product.name} - ${product.price}
          </li>
        ))}
      </ul>
    </div>
  );
}

4. Details Page (src/app/product/[id]/page.tsx)

import { getProductById } from "../../../controllers/productController";

type Params = {
  params: {
    id: string;
  };
};

export default async function ProductDetailPage({ params }: Params) {
  const product = await getProductById(params.id);

  return (
    <div>
      <h1>Product Details</h1>
      <p>Name: {product.name}</p>
      <p>Price: ${product.price}</p>
    </div>
  );
}

Example: componentize

When you run:

mvcube componentize button

The CLI will prompt you to choose whether the component should be global or local. Based on your choice, the component will be placed in the components/ folder, and it will either be imported globally or locally.

Global Component

  • Global components are stored in the src/components/ directory and can be used throughout the entire application.

  • The import path for global components will be:

    import Button from "@/components/button";

    Example structure:

    src/
      └── components/
            └── button.tsx

Local Component

  • Local components are also stored in the src/components/ directory but are used only within the page where they are located.

  • The import path for local components will be:

    import Button from "./components/button";

    Example structure:

    src/
      └── components/
            └── button.tsx

1. Global Component Example (src/components/button.tsx)

export default function Button({ label }: { label: string }) {
  return <button>{label}</button>;
}

2. Local Component Example (src/components/button.tsx)

export default function Button({ label }: { label: string }) {
  return <button>{label}</button>;
}

Contributing

Feel free to contribute! Open an issue or submit a pull request on the GitHub repository.


License

This project is licensed under the MIT License.