nsgm-cli 2.1.13 → 2.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +394 -156
- package/client/components/Button.tsx +3 -5
- package/client/components/ClientProviders.tsx +29 -0
- package/client/components/LanguageSwitcher.tsx +59 -0
- package/client/components/SSRSafeAntdProvider.tsx +24 -0
- package/client/components/SuppressHydrationWarnings.tsx +55 -0
- package/client/components/__tests__/Button.test.tsx +10 -10
- package/client/layout/index.tsx +153 -185
- package/client/redux/reducers.ts +1 -1
- package/client/redux/store.ts +2 -1
- package/client/redux/template/manage/actions.ts +77 -88
- package/client/redux/template/manage/reducers.ts +25 -37
- package/client/redux/template/manage/types.ts +1 -1
- package/client/service/template/manage.ts +20 -21
- package/client/styled/common.ts +12 -14
- package/client/styled/layout/index.ts +234 -120
- package/client/styled/template/manage.ts +102 -14
- package/client/utils/common.ts +23 -21
- package/client/utils/cookie.ts +18 -19
- package/client/utils/fetch.ts +64 -100
- package/client/utils/i18n.ts +68 -0
- package/client/utils/menu.tsx +42 -23
- package/client/utils/navigation.ts +58 -0
- package/client/utils/sso.ts +74 -84
- package/client/utils/suppressWarnings.ts +32 -0
- package/eslint.config.js +53 -19
- package/generation/README.md +25 -6
- package/generation/__tests__/example.test.js +41 -0
- package/generation/client/redux/reducers.ts +1 -1
- package/generation/client/utils/menu.tsx +36 -23
- package/generation/env +3 -0
- package/generation/env.example +3 -0
- package/generation/eslint.config.js +112 -0
- package/generation/gitignore +6 -1
- package/generation/jest.config.js +40 -0
- package/generation/next.config.js +7 -3
- package/generation/package.json +28 -4
- package/generation/tsconfig.json +6 -19
- package/jest.config.js +23 -6
- package/lib/args.js +9 -1
- package/lib/cli/app.d.ts +28 -0
- package/lib/cli/app.js +99 -0
- package/lib/cli/commands/build.d.ts +2 -0
- package/lib/cli/commands/build.js +29 -0
- package/lib/cli/commands/create.d.ts +2 -0
- package/lib/cli/commands/create.js +113 -0
- package/lib/cli/commands/delete.d.ts +3 -0
- package/lib/cli/commands/delete.js +151 -0
- package/lib/cli/commands/export.d.ts +2 -0
- package/lib/cli/commands/export.js +42 -0
- package/lib/cli/commands/help.d.ts +2 -0
- package/lib/cli/commands/help.js +42 -0
- package/lib/cli/commands/init.d.ts +2 -0
- package/lib/cli/commands/init.js +115 -0
- package/lib/cli/commands/server.d.ts +3 -0
- package/lib/cli/commands/server.js +26 -0
- package/lib/cli/commands/upgrade.d.ts +2 -0
- package/lib/cli/commands/upgrade.js +38 -0
- package/lib/cli/commands/version.d.ts +2 -0
- package/lib/cli/commands/version.js +24 -0
- package/lib/cli/index.d.ts +16 -0
- package/lib/cli/index.js +33 -0
- package/lib/cli/parser.d.ts +22 -0
- package/lib/cli/parser.js +115 -0
- package/lib/cli/registry.d.ts +33 -0
- package/lib/cli/registry.js +81 -0
- package/lib/cli/types/project.d.ts +10 -0
- package/lib/cli/types/project.js +2 -0
- package/lib/cli/types.d.ts +31 -0
- package/lib/cli/types.js +20 -0
- package/lib/cli/utils/console.d.ts +62 -0
- package/lib/cli/utils/console.js +148 -0
- package/lib/cli/utils/index.d.ts +2 -0
- package/lib/cli/utils/index.js +7 -0
- package/lib/cli/utils/prompt.d.ts +83 -0
- package/lib/cli/utils/prompt.js +327 -0
- package/lib/constants.d.ts +65 -0
- package/lib/constants.js +177 -0
- package/lib/generate.d.ts +25 -3
- package/lib/generate.js +98 -621
- package/lib/generate_create.d.ts +9 -0
- package/lib/generate_create.js +329 -0
- package/lib/generate_delete.d.ts +8 -0
- package/lib/generate_delete.js +233 -0
- package/lib/generate_init.d.ts +56 -0
- package/lib/generate_init.js +612 -0
- package/lib/generators/base-generator.d.ts +47 -0
- package/lib/generators/base-generator.js +92 -0
- package/lib/generators/file-generator.d.ts +48 -0
- package/lib/generators/file-generator.js +455 -0
- package/lib/generators/generator-factory.d.ts +20 -0
- package/lib/generators/generator-factory.js +25 -0
- package/lib/generators/i18n-generator.d.ts +51 -0
- package/lib/generators/i18n-generator.js +320 -0
- package/lib/generators/page-generator.d.ts +45 -0
- package/lib/generators/page-generator.js +578 -0
- package/lib/generators/resolver-generator.d.ts +14 -0
- package/lib/generators/resolver-generator.js +342 -0
- package/lib/generators/schema-generator.d.ts +7 -0
- package/lib/generators/schema-generator.js +57 -0
- package/lib/generators/service-generator.d.ts +11 -0
- package/lib/generators/service-generator.js +233 -0
- package/lib/generators/sql-generator.d.ts +8 -0
- package/lib/generators/sql-generator.js +52 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +14 -173
- package/lib/server/csrf.js +9 -16
- package/lib/server/db.js +6 -7
- package/lib/server/graphql.js +5 -6
- package/lib/server/plugins/date.js +1 -1
- package/lib/server/utils/graphql-cache.js +3 -3
- package/lib/tsconfig.build.tsbuildinfo +1 -1
- package/lib/utils/project-config.d.ts +5 -0
- package/lib/utils/project-config.js +145 -0
- package/lib/utils.js +1 -1
- package/next-i18next.config.js +18 -0
- package/next.config.js +61 -18
- package/package.json +16 -8
- package/pages/_app.tsx +77 -33
- package/pages/_document.tsx +78 -21
- package/pages/_error.tsx +66 -0
- package/pages/index.tsx +109 -47
- package/pages/login.tsx +66 -41
- package/pages/template/manage.tsx +162 -151
- package/public/fonts/font-awesome.min.css +4 -0
- package/public/fonts/fontawesome-webfont.woff +0 -0
- package/public/fonts/fontawesome-webfont.woff2 +0 -0
- package/public/locales/en-US/common.json +48 -0
- package/public/locales/en-US/home.json +57 -0
- package/public/locales/en-US/layout.json +22 -0
- package/public/locales/en-US/login.json +13 -0
- package/public/locales/en-US/template.json +42 -0
- package/public/locales/ja-JP/common.json +48 -0
- package/public/locales/ja-JP/home.json +57 -0
- package/public/locales/ja-JP/layout.json +22 -0
- package/public/locales/ja-JP/login.json +13 -0
- package/public/locales/ja-JP/template.json +42 -0
- package/public/locales/zh-CN/common.json +48 -0
- package/public/locales/zh-CN/home.json +57 -0
- package/public/locales/zh-CN/layout.json +22 -0
- package/public/locales/zh-CN/login.json +13 -0
- package/public/locales/zh-CN/template.json +42 -0
- package/public/slbhealthcheck.html +1 -1
- package/server/apis/template.js +0 -2
- package/server/utils/validation.js +163 -0
- package/types/i18next.d.ts +10 -0
- package/generation/eslintrc.js +0 -16
package/README.md
CHANGED
|
@@ -1,237 +1,475 @@
|
|
|
1
1
|
# NSGM CLI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<div align="center">
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
- [Styled-components](https://github.com/styled-components/styled-components) - CSS-in-JS solution
|
|
9
|
-
- [GraphQL](https://graphql.org/) - API query language
|
|
10
|
-
- [MySQL](https://www.mysql.com/) - Relational database
|
|
10
|
+
**A powerful full-stack development framework with intelligent code generation**
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
[Installation](#installation) • [Quick Start](#quick-start) • [Features](#features) • [Documentation](#documentation) • [Contributing](#contributing)
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
- Automatic code template generation
|
|
16
|
-
- Rapid development workflow
|
|
17
|
-
- Integrated GraphQL API
|
|
18
|
-
- MySQL database support
|
|
19
|
-
- Secure login system with bcrypt encryption
|
|
14
|
+
</div>
|
|
20
15
|
|
|
21
|
-
##
|
|
16
|
+
## 🚀 Overview
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
NSGM CLI is a comprehensive full-stack development framework that combines the power of modern web technologies with intelligent code generation capabilities. It helps developers rapidly build scalable web applications through an interactive CLI wizard and automated code templates.
|
|
24
19
|
|
|
25
|
-
|
|
26
|
-
| --------------- | --------------------------------------- |
|
|
27
|
-
| `nsgm init` | Initialize project |
|
|
28
|
-
| `nsgm upgrade` | Upgrade project base files |
|
|
29
|
-
| `nsgm create` | Create template page |
|
|
30
|
-
| `nsgm delete` | Delete template page |
|
|
31
|
-
| `nsgm deletedb` | Delete template page and database table |
|
|
32
|
-
| `nsgm dev` | Development mode |
|
|
33
|
-
| `nsgm start` | Production mode |
|
|
34
|
-
| `nsgm build` | Build project |
|
|
35
|
-
| `nsgm export` | Export static pages |
|
|
20
|
+
### 🏗️ Architecture
|
|
36
21
|
|
|
37
|
-
|
|
22
|
+
```
|
|
23
|
+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
24
|
+
│ Frontend │ │ Backend │ │ Database │
|
|
25
|
+
│ Next.js │◄──►│ Express.js │◄──►│ MySQL │
|
|
26
|
+
│ React │ │ GraphQL │ │ Native SQL │
|
|
27
|
+
│ Styled-Comp │ │ REST API │ │ Relations │
|
|
28
|
+
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
29
|
+
```
|
|
38
30
|
|
|
39
|
-
|
|
31
|
+
## 🎯 Key Features
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
nsgm init dictionary=webapp
|
|
43
|
-
# or simplified as
|
|
44
|
-
nsgm init webapp
|
|
45
|
-
```
|
|
33
|
+
### 🧙♂️ **Intelligent CLI Wizard**
|
|
46
34
|
|
|
47
|
-
- **
|
|
35
|
+
- **Smart Mode Detection**: Automatically switches between interactive and command-line modes
|
|
36
|
+
- **Beginner-Friendly**: Step-by-step guided setup for newcomers
|
|
37
|
+
- **Expert-Efficient**: Quick command-line shortcuts for experienced developers
|
|
48
38
|
|
|
49
|
-
|
|
50
|
-
nsgm create math
|
|
51
|
-
```
|
|
39
|
+
### ⚡ **Rapid Development**
|
|
52
40
|
|
|
53
|
-
- **
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
```
|
|
41
|
+
- **Code Generation**: Automatic CRUD operations, API endpoints, and database schemas
|
|
42
|
+
- **Hot Reload**: Instant development feedback
|
|
43
|
+
- **Type Safety**: Full TypeScript support throughout the stack
|
|
57
44
|
|
|
58
|
-
|
|
45
|
+
### 🔒 **Production-Ready Security**
|
|
59
46
|
|
|
60
|
-
|
|
47
|
+
- **CSRF Protection**: Built-in cross-site request forgery prevention
|
|
48
|
+
- **Password Encryption**: bcrypt-based secure authentication
|
|
49
|
+
- **Session Management**: Robust user session handling
|
|
50
|
+
- **CSP Headers**: Content Security Policy implementation
|
|
61
51
|
|
|
62
|
-
###
|
|
52
|
+
### 🏗️ **Modern Tech Stack**
|
|
63
53
|
|
|
64
|
-
|
|
54
|
+
- **Frontend**: Next.js 13+, React 18+, Styled-components, Redux Toolkit
|
|
55
|
+
- **Backend**: Express.js, GraphQL, REST APIs
|
|
56
|
+
- **Database**: MySQL with native drivers
|
|
57
|
+
- **DevTools**: TypeScript, ESLint, Prettier, Jest
|
|
65
58
|
|
|
66
|
-
|
|
67
|
-
# Using npm script
|
|
68
|
-
npm run generate-password yourSecurePassword
|
|
69
|
-
```
|
|
59
|
+
## 📦 Installation
|
|
70
60
|
|
|
71
|
-
|
|
61
|
+
### Global Installation (Recommended)
|
|
72
62
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
```
|
|
63
|
+
```bash
|
|
64
|
+
# Using npm
|
|
65
|
+
npm install -g nsgm-cli
|
|
77
66
|
|
|
78
|
-
|
|
67
|
+
# Using yarn
|
|
68
|
+
yarn global add nsgm-cli
|
|
79
69
|
|
|
80
|
-
|
|
70
|
+
# Using pnpm
|
|
71
|
+
pnpm add -g nsgm-cli
|
|
72
|
+
```
|
|
81
73
|
|
|
82
|
-
|
|
74
|
+
### Verify Installation
|
|
83
75
|
|
|
84
|
-
|
|
76
|
+
```bash
|
|
77
|
+
nsgm --version
|
|
78
|
+
# Should output: 2.1.13
|
|
79
|
+
```
|
|
85
80
|
|
|
86
|
-
|
|
87
|
-
const { nextConfig } = require('nsgm-cli')
|
|
88
|
-
const projectConfig = require('./project.config')
|
|
81
|
+
## 🚀 Quick Start
|
|
89
82
|
|
|
90
|
-
|
|
83
|
+
### 1. Initialize Your First Project
|
|
91
84
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
prefix,
|
|
96
|
-
protocol,
|
|
97
|
-
host
|
|
98
|
-
})
|
|
85
|
+
```bash
|
|
86
|
+
# Start the interactive wizard (recommended for beginners)
|
|
87
|
+
nsgm init
|
|
99
88
|
|
|
100
|
-
|
|
101
|
-
|
|
89
|
+
# Or specify project name directly
|
|
90
|
+
nsgm init my-awesome-app
|
|
102
91
|
```
|
|
103
92
|
|
|
104
|
-
|
|
93
|
+
The wizard will guide you through:
|
|
105
94
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
95
|
+
- ✅ Project name and directory
|
|
96
|
+
- ✅ Database configuration
|
|
97
|
+
- ✅ Security settings
|
|
98
|
+
- ✅ Initial controller setup
|
|
110
99
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
}
|
|
100
|
+
### 2. Environment Setup
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
cd your-project-name
|
|
104
|
+
|
|
105
|
+
# Copy environment template
|
|
106
|
+
cp .env.example .env
|
|
120
107
|
```
|
|
121
108
|
|
|
122
|
-
|
|
109
|
+
**Default Login**: `admin/admin123`
|
|
123
110
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
111
|
+
**To change password** (optional):
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# Generate secure password hash
|
|
115
|
+
npm run generate-password yourNewPassword
|
|
116
|
+
|
|
117
|
+
# Edit .env file with generated hash
|
|
118
|
+
nano .env
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 3. Start Development
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
# Install dependencies
|
|
125
|
+
npm install
|
|
126
|
+
|
|
127
|
+
# Start development server
|
|
128
|
+
npm run dev
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Your application will be available at `http://localhost:3000` with:
|
|
132
|
+
|
|
133
|
+
- 🎛️ Admin dashboard with CRUD interface
|
|
134
|
+
- 📊 Data import/export functionality
|
|
135
|
+
- 🗑️ Batch operations support
|
|
136
|
+
- 🔐 Secure login system (Default: admin/admin123)
|
|
137
|
+
|
|
138
|
+
## 🛠️ CLI Commands
|
|
139
|
+
|
|
140
|
+
### Core Commands
|
|
141
|
+
|
|
142
|
+
| Command | Description | Mode | Example |
|
|
143
|
+
| ------------- | ----------------------------- | --------------- | --------------------- |
|
|
144
|
+
| `nsgm init` | Initialize new project | Interactive/CLI | `nsgm init blog-app` |
|
|
145
|
+
| `nsgm create` | Generate controller with CRUD | Interactive/CLI | `nsgm create user` |
|
|
146
|
+
| `nsgm delete` | Remove controller and files | Interactive/CLI | `nsgm delete product` |
|
|
147
|
+
| `nsgm dev` | Start development server | CLI | `nsgm dev` |
|
|
148
|
+
| `nsgm build` | Build for production | CLI | `nsgm build` |
|
|
149
|
+
| `nsgm start` | Start production server | CLI | `nsgm start` |
|
|
150
|
+
|
|
151
|
+
### Advanced Commands
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# Database operations
|
|
155
|
+
nsgm deletedb user # Delete controller + database table
|
|
127
156
|
|
|
128
|
-
|
|
129
|
-
|
|
157
|
+
# Project maintenance
|
|
158
|
+
nsgm upgrade # Upgrade project base files
|
|
159
|
+
nsgm export # Export static pages
|
|
130
160
|
|
|
161
|
+
# Development tools
|
|
162
|
+
npm run lint # Code linting
|
|
163
|
+
npm run test # Run tests
|
|
164
|
+
npm run test:coverage # Test coverage report
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## 🎨 Generated Controller Features
|
|
168
|
+
|
|
169
|
+
Each controller created with `nsgm create` includes:
|
|
170
|
+
|
|
171
|
+
### 🔧 **Backend Components**
|
|
172
|
+
|
|
173
|
+
- **GraphQL Schema**: Typed queries and mutations
|
|
174
|
+
- **GraphQL Resolvers**: Business logic implementation
|
|
175
|
+
- **REST API Endpoints**: RESTful service layer
|
|
176
|
+
- **Database Models**: MySQL schema definitions
|
|
177
|
+
- **Data Validation**: Input sanitization and validation
|
|
178
|
+
|
|
179
|
+
### 🎯 **Frontend Components**
|
|
180
|
+
|
|
181
|
+
- **React Components**: Modern functional components with hooks
|
|
182
|
+
- **Styled Components**: CSS-in-JS styling
|
|
183
|
+
- **Redux Integration**: State management
|
|
184
|
+
- **Form Handling**: Create, edit, and validation forms
|
|
185
|
+
- **Data Tables**: Sortable, filterable data grids
|
|
186
|
+
|
|
187
|
+
### 📊 **CRUD Operations**
|
|
188
|
+
|
|
189
|
+
- **Create**: Add new records with validation
|
|
190
|
+
- **Read**: List, search, and pagination
|
|
191
|
+
- **Update**: Edit existing records
|
|
192
|
+
- **Delete**: Single and batch deletion
|
|
193
|
+
- **Import/Export**: CSV and JSON data handling
|
|
194
|
+
|
|
195
|
+
## 🏗️ Project Structure
|
|
196
|
+
|
|
197
|
+
```
|
|
198
|
+
your-project/
|
|
199
|
+
├── client/ # Frontend code
|
|
200
|
+
│ ├── components/ # React components
|
|
201
|
+
│ ├── layout/ # Layout components
|
|
202
|
+
│ ├── redux/ # State management
|
|
203
|
+
│ ├── service/ # API services
|
|
204
|
+
│ ├── styled/ # Styled components
|
|
205
|
+
│ └── utils/ # Utility functions
|
|
206
|
+
├── server/ # Backend code
|
|
207
|
+
│ ├── apis/ # REST API routes
|
|
208
|
+
│ ├── modules/ # GraphQL modules
|
|
209
|
+
│ ├── sql/ # Database scripts
|
|
210
|
+
│ └── utils/ # Server utilities
|
|
211
|
+
├── pages/ # Next.js pages
|
|
212
|
+
├── public/ # Static assets
|
|
213
|
+
├── scripts/ # Build and deployment scripts
|
|
214
|
+
├── __tests__/ # Test files
|
|
215
|
+
├── .env.example # Environment template
|
|
216
|
+
├── next.config.js # Next.js configuration
|
|
217
|
+
├── mysql.config.js # Database configuration
|
|
218
|
+
└── package.json # Dependencies
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## ⚙️ Configuration
|
|
222
|
+
|
|
223
|
+
### Environment Variables
|
|
224
|
+
|
|
225
|
+
```bash
|
|
226
|
+
# .env file
|
|
227
|
+
NODE_ENV=development
|
|
228
|
+
LOGIN_USERNAME=admin
|
|
229
|
+
LOGIN_PASSWORD_HASH=your_generated_hash # Default: admin123
|
|
230
|
+
DATABASE_URL=mysql://user:password@localhost:3306/dbname
|
|
231
|
+
|
|
232
|
+
# Optional
|
|
233
|
+
PORT=3000
|
|
234
|
+
NEXT_PUBLIC_API_URL=http://localhost:3000
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Database Configuration
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
// mysql.config.js
|
|
131
241
|
module.exports = {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
242
|
+
mysqlOptions: {
|
|
243
|
+
host: process.env.DB_HOST || 'localhost',
|
|
244
|
+
port: process.env.DB_PORT || 3306,
|
|
245
|
+
user: process.env.DB_USER || 'root',
|
|
246
|
+
password: process.env.DB_PASSWORD || '',
|
|
247
|
+
database: process.env.DB_NAME || 'nsgm_db',
|
|
248
|
+
},
|
|
137
249
|
}
|
|
138
250
|
```
|
|
139
251
|
|
|
140
|
-
##
|
|
252
|
+
## 🔒 Security Setup
|
|
141
253
|
|
|
142
|
-
|
|
254
|
+
### Default Authentication
|
|
143
255
|
|
|
144
|
-
|
|
256
|
+
**Default Login Credentials**: `admin/admin123`
|
|
145
257
|
|
|
146
|
-
|
|
147
|
-
- `modules/` - Stores GraphQL resolvers and schemas
|
|
148
|
-
- `plugins/` - Stores GraphQL plugins
|
|
149
|
-
- `*.js` - Route files
|
|
258
|
+
### Custom Password Setup (Optional)
|
|
150
259
|
|
|
151
|
-
|
|
260
|
+
```bash
|
|
261
|
+
# Generate secure hash for custom password
|
|
262
|
+
npm run generate-password yourNewPassword
|
|
263
|
+
|
|
264
|
+
# Add to .env file
|
|
265
|
+
LOGIN_PASSWORD_HASH=your_generated_hash_here
|
|
266
|
+
```
|
|
152
267
|
|
|
153
|
-
|
|
268
|
+
### CSRF Protection
|
|
269
|
+
|
|
270
|
+
NSGM CLI includes built-in CSRF protection:
|
|
154
271
|
|
|
155
272
|
```javascript
|
|
156
|
-
|
|
157
|
-
|
|
273
|
+
// Automatic CSRF token generation
|
|
274
|
+
app.use(csrfProtection)
|
|
275
|
+
|
|
276
|
+
// Custom CSP headers
|
|
277
|
+
app.use(
|
|
278
|
+
createCSPMiddleware({
|
|
279
|
+
directives: {
|
|
280
|
+
defaultSrc: ["'self'"],
|
|
281
|
+
styleSrc: ["'self'", "'unsafe-inline'"],
|
|
282
|
+
scriptSrc: ["'self'"],
|
|
283
|
+
},
|
|
284
|
+
})
|
|
285
|
+
)
|
|
286
|
+
```
|
|
158
287
|
|
|
159
|
-
|
|
288
|
+
## 🧪 Testing
|
|
160
289
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
next()
|
|
165
|
-
})
|
|
290
|
+
```bash
|
|
291
|
+
# Run all tests
|
|
292
|
+
npm test
|
|
166
293
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
res.json({ name: 'TEST' })
|
|
170
|
-
})
|
|
294
|
+
# Watch mode
|
|
295
|
+
npm run test:watch
|
|
171
296
|
|
|
172
|
-
|
|
297
|
+
# Coverage report
|
|
298
|
+
npm run test:coverage
|
|
299
|
+
|
|
300
|
+
# Lint code
|
|
301
|
+
npm run lint
|
|
302
|
+
|
|
303
|
+
# Format code
|
|
304
|
+
npm run format
|
|
173
305
|
```
|
|
174
306
|
|
|
175
|
-
|
|
307
|
+
## 📚 Examples
|
|
308
|
+
|
|
309
|
+
### Creating a Blog System
|
|
310
|
+
|
|
311
|
+
```bash
|
|
312
|
+
# 1. Initialize project
|
|
313
|
+
nsgm init my-blog
|
|
314
|
+
|
|
315
|
+
# 2. Create controllers
|
|
316
|
+
cd my-blog
|
|
317
|
+
nsgm create post
|
|
318
|
+
nsgm create category
|
|
319
|
+
nsgm create user
|
|
320
|
+
|
|
321
|
+
# 3. Start development
|
|
322
|
+
npm run dev
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Adding Custom API Endpoint
|
|
176
326
|
|
|
177
327
|
```javascript
|
|
328
|
+
// server/apis/custom.js
|
|
178
329
|
const express = require('express')
|
|
179
330
|
const router = express.Router()
|
|
180
331
|
|
|
181
|
-
router.get('
|
|
182
|
-
res.
|
|
183
|
-
|
|
332
|
+
router.get('/stats', (req, res) => {
|
|
333
|
+
res.json({
|
|
334
|
+
totalPosts: 42,
|
|
335
|
+
totalUsers: 15,
|
|
336
|
+
lastUpdate: new Date(),
|
|
337
|
+
})
|
|
184
338
|
})
|
|
185
339
|
|
|
186
340
|
module.exports = router
|
|
187
341
|
```
|
|
188
342
|
|
|
189
|
-
|
|
343
|
+
### Custom GraphQL Schema
|
|
190
344
|
|
|
191
345
|
```javascript
|
|
346
|
+
// server/modules/blog/schema.js
|
|
192
347
|
module.exports = {
|
|
193
348
|
query: `
|
|
194
|
-
|
|
195
|
-
|
|
349
|
+
posts(limit: Int, offset: Int): [Post]
|
|
350
|
+
post(id: ID!): Post
|
|
351
|
+
`,
|
|
196
352
|
mutation: `
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
353
|
+
createPost(title: String!, content: String!): Post
|
|
354
|
+
updatePost(id: ID!, title: String, content: String): Post
|
|
355
|
+
deletePost(id: ID!): Boolean
|
|
356
|
+
`,
|
|
357
|
+
type: `
|
|
358
|
+
type Post {
|
|
359
|
+
id: ID!
|
|
360
|
+
title: String!
|
|
361
|
+
content: String!
|
|
362
|
+
createdAt: Date!
|
|
363
|
+
updatedAt: Date!
|
|
364
|
+
}
|
|
365
|
+
`,
|
|
201
366
|
}
|
|
202
367
|
```
|
|
203
368
|
|
|
204
|
-
|
|
369
|
+
## 🚀 Performance & Production
|
|
205
370
|
|
|
206
|
-
|
|
207
|
-
let localLink = ''
|
|
371
|
+
### Build Optimization
|
|
208
372
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
373
|
+
```bash
|
|
374
|
+
# Production build
|
|
375
|
+
npm run build
|
|
376
|
+
|
|
377
|
+
# Analyze bundle size
|
|
378
|
+
npm run analyze
|
|
379
|
+
|
|
380
|
+
# Export static site
|
|
381
|
+
npm run export
|
|
219
382
|
```
|
|
220
383
|
|
|
221
|
-
|
|
384
|
+
### Production Deployment
|
|
222
385
|
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
386
|
+
```bash
|
|
387
|
+
# Start production server
|
|
388
|
+
npm start
|
|
389
|
+
|
|
390
|
+
# Or use PM2
|
|
391
|
+
pm2 start npm --name "nsgm-app" -- start
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## 🤝 Contributing
|
|
395
|
+
|
|
396
|
+
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
397
|
+
|
|
398
|
+
### Development Setup
|
|
399
|
+
|
|
400
|
+
```bash
|
|
401
|
+
# Clone repository
|
|
402
|
+
git clone https://github.com/erishen/nsgm.git
|
|
403
|
+
cd nsgm
|
|
235
404
|
|
|
236
|
-
|
|
405
|
+
# Install dependencies
|
|
406
|
+
npm install
|
|
407
|
+
|
|
408
|
+
# Run tests
|
|
409
|
+
npm test
|
|
410
|
+
|
|
411
|
+
# Build CLI
|
|
412
|
+
npm run tsbuild
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
## 📖 Documentation
|
|
416
|
+
|
|
417
|
+
- [Security Guide](SECURITY.md) - Security best practices
|
|
418
|
+
- [API Reference](docs/api.md) - Complete API documentation
|
|
419
|
+
- [Migration Guide](docs/migration.md) - Upgrade instructions
|
|
420
|
+
- [Troubleshooting](docs/troubleshooting.md) - Common issues and solutions
|
|
421
|
+
|
|
422
|
+
## 🐛 Troubleshooting
|
|
423
|
+
|
|
424
|
+
### Common Issues
|
|
425
|
+
|
|
426
|
+
**Port already in use**
|
|
427
|
+
|
|
428
|
+
```bash
|
|
429
|
+
# Kill process on port 3000
|
|
430
|
+
lsof -ti:3000 | xargs kill -9
|
|
431
|
+
|
|
432
|
+
# Or use different port
|
|
433
|
+
PORT=3001 npm run dev
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
**Database connection failed**
|
|
437
|
+
|
|
438
|
+
```bash
|
|
439
|
+
# Check MySQL service
|
|
440
|
+
sudo systemctl status mysql
|
|
441
|
+
|
|
442
|
+
# Verify credentials in .env
|
|
443
|
+
cat .env | grep DB_
|
|
237
444
|
```
|
|
445
|
+
|
|
446
|
+
**Permission denied**
|
|
447
|
+
|
|
448
|
+
```bash
|
|
449
|
+
# Fix npm permissions
|
|
450
|
+
sudo chown -R $(whoami) ~/.npm
|
|
451
|
+
|
|
452
|
+
# Or use different install location
|
|
453
|
+
npm config set prefix '~/.local'
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
## 📄 License
|
|
457
|
+
|
|
458
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
459
|
+
|
|
460
|
+
## 🙏 Acknowledgments
|
|
461
|
+
|
|
462
|
+
- [Next.js](https://nextjs.org/) - React framework
|
|
463
|
+
- [GraphQL](https://graphql.org/) - Query language
|
|
464
|
+
- [Styled Components](https://styled-components.com/) - CSS-in-JS
|
|
465
|
+
- [MySQL](https://www.mysql.com/) - Relational database
|
|
466
|
+
|
|
467
|
+
---
|
|
468
|
+
|
|
469
|
+
<div align="center">
|
|
470
|
+
|
|
471
|
+
**Made with ❤️ by the NSGM Team**
|
|
472
|
+
|
|
473
|
+
[GitHub](https://github.com/erishen/nsgm) • [npm](https://www.npmjs.com/package/nsgm-cli) • [Issues](https://github.com/erishen/nsgm/issues)
|
|
474
|
+
|
|
475
|
+
</div>
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
// src/components/Button.js
|
|
2
|
-
import React from 'react'
|
|
2
|
+
import React from 'react'
|
|
3
3
|
|
|
4
|
-
const Button = ({ onClick, children }) =>
|
|
5
|
-
<button onClick={onClick}>{children}</button>
|
|
6
|
-
);
|
|
4
|
+
const Button = ({ onClick, children }) => <button onClick={onClick}>{children}</button>
|
|
7
5
|
|
|
8
|
-
export default Button
|
|
6
|
+
export default Button
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react'
|
|
2
|
+
import { ThemeProvider } from 'styled-components'
|
|
3
|
+
import { GlobalStyle } from '@/styled/common'
|
|
4
|
+
|
|
5
|
+
interface ClientProvidersProps {
|
|
6
|
+
children: React.ReactNode
|
|
7
|
+
theme: any
|
|
8
|
+
whiteColor?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const ClientProviders: React.FC<ClientProvidersProps> = ({ children, theme, whiteColor = true }) => {
|
|
12
|
+
const [isClient, setIsClient] = useState(false)
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
// 使用更安全的客户端检测
|
|
16
|
+
setIsClient(true)
|
|
17
|
+
}, [])
|
|
18
|
+
|
|
19
|
+
// 在服务端渲染时,使用一个占位符来保持结构一致性
|
|
20
|
+
// 但不渲染可能引起 useLayoutEffect 警告的组件
|
|
21
|
+
return (
|
|
22
|
+
<ThemeProvider theme={theme}>
|
|
23
|
+
{isClient && <GlobalStyle whiteColor={whiteColor} />}
|
|
24
|
+
{children}
|
|
25
|
+
</ThemeProvider>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default ClientProviders
|