next-dev v1.2.8
Tentu, berikut adalah markdown yang telah diperbaiki:
CLI Tool for Next.js Project Automation

Overview
This CLI tool provides automation for common tasks in a Next.js project. It includes commands for generating and clearing DevBox components, generating API endpoints, generating pages, and more. It also ensures the required project structure and configuration files are present.
Prerequisites
- Ensure you have Node.js installed.
- This tool should be run in the root directory of a Next.js project.
Installation
Using Yarn
Add to project:
yarn add next-devGlobal installation:
yarn global add next-devUsing npm
Add to project:
npm i next-devGlobal installation:
npm i -g next-devUsing npx
You can also run the tool directly with npx:
npx next-devUsage
This tool provides several commands that can be run using next-dev.
Commands
- gen-box: Generates a
DevBoxcomponent around JSX elements in.tsxfiles. - clear-box: Clears
DevBoxcomponents from.tsxfiles. - gen-api: Generates API endpoints.
- gen-page: Generates new pages.
- gen-tree: Generates a tree structure of the project.
- gen-model: Generates TypeScript models from JSON files, URLs, or directories.
Example Usage
Generate DevBox:
next-dev gen-boxClear DevBox:
next-dev clear-boxGenerate API:
next-dev gen-apiGenerate Page:
next-dev gen-pageGenerate Tree:
next-dev gen-tree --name myTreeGenerate Model:
next-dev gen-model --dir ./path/to/json/files --out ./src/models
Adding DevBox Components Automatically
Add use dev to the top of the file or below use client to automatically generate DevBox around the first return element.
Example:
return (
<DevBox path="dnNjb2RlOi8vZmlsZS8vVXNlcnMvYmlwL0RvY3VtZW50cy9wcm9qZWN0cy9iaXAvd2lidS1zZXJ2ZXIvc3JjL3VpL2NvbXBvbmVudC9CdXR0b25Mb2dvdXQudHN4OjE3OjE=">
<Button
loading={loading}
size="compact-sm"
variant="subtle"
onClick={logout}
>
<Badge>Logout</Badge>
</Button>
</DevBox>
);Toggling Dev Mode with a Button
Call ButtonToogle to activate or deactivate dev mode:
'use client'
import { Button } from "@mantine/core";
import { ButtonToogle } from "next-dev";
export default function ButtonDev() {
return <ButtonToogle>
{(isDev) => <Button size='compact-xs' color={isDev ? "red" : "blue"}>{isDev ? "DEV" : "PROD"}</Button>}
</ButtonToogle>
}Options
All commands accept the following option:
- --log, -l: Enable logging (default:
false)
Example with Options
next-dev gen-box --logScript Details
File Structure and Configuration
Project Root Check: The script checks if it is running in the root directory of a Next.js project by calling
isNextJsRootDir(). If the check fails, it exits with an error message.Directory and Configuration File Creation: If the
./src/utildirectory does not exist, it creates it. Then, it creates a default configuration fileapp_config.tsif it does not exist.
Command Definitions
gen-box:
- Generates
DevBoxcomponents around JSX elements in.tsxfiles. - Adds an import statement for
DevBoxif it does not exist.
- Generates
clear-box:
- Removes
DevBoxcomponents from.tsxfiles.
- Removes
gen-api:
- Generates API endpoint boilerplate.
gen-page:
- Generates new page boilerplate.
gen-tree:
- Generates a tree structure of the project.
gen-model:
- Generates TypeScript models from JSON files, URLs, or directories.
- Saves each JSON file from the input directory to the output directory as separate TypeScript files with corresponding names.
Error Handling
Each command has a try-catch block to handle errors gracefully and log appropriate error messages.
Development
Setup
- Clone the repository.
- Run
npm installto install dependencies.
Running the CLI
Run the CLI using next-dev:
next-dev <command>Example Development Flow
Create a New Command: Add a new command to the
yargsconfiguration and implement the corresponding function.Test the Command: Run the command using
next-devto ensure it works as expected.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request for review.
This README provides an overview of the CLI tool, including installation, usage, and development instructions. Make sure to replace <command> with the actual command you want to run when using the tool.