create-mohit-next 0.0.0-stage → 1.0.0
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 +108 -2
- package/index.js +399 -0
- package/package.json +36 -4
- package/template/AGENTS.md +9 -0
- package/template/Dockerfile +48 -0
- package/template/README.md +262 -0
- package/template/_dockerignore +13 -0
- package/template/_env.example +11 -0
- package/template/_gitignore +42 -0
- package/template/_prettierignore +11 -0
- package/template/_prettierrc +9 -0
- package/template/components.json +25 -0
- package/template/docker-compose.yml +33 -0
- package/template/eslint.config.mjs +21 -0
- package/template/next.config.ts +45 -0
- package/template/package.json +53 -0
- package/template/public/robots.txt +6 -0
- package/template/src/app/(app)/admin/page.tsx +53 -0
- package/template/src/app/(app)/dashboard/page.tsx +127 -0
- package/template/src/app/(app)/error.tsx +28 -0
- package/template/src/app/(app)/layout.tsx +35 -0
- package/template/src/app/(app)/loading.tsx +5 -0
- package/template/src/app/(app)/reports/page.tsx +109 -0
- package/template/src/app/(auth)/layout.tsx +22 -0
- package/template/src/app/(auth)/login/page.tsx +18 -0
- package/template/src/app/(auth)/register/page.tsx +20 -0
- package/template/src/app/api/admin/users/[id]/route.ts +40 -0
- package/template/src/app/api/admin/users/route.ts +18 -0
- package/template/src/app/api/auth/login/route.ts +26 -0
- package/template/src/app/api/auth/logout/route.ts +21 -0
- package/template/src/app/api/auth/me/route.ts +19 -0
- package/template/src/app/api/auth/register/route.ts +40 -0
- package/template/src/app/api/health/route.ts +27 -0
- package/template/src/app/favicon.ico +0 -0
- package/template/src/app/forbidden/page.tsx +49 -0
- package/template/src/app/globals.css +142 -0
- package/template/src/app/layout.tsx +52 -0
- package/template/src/app/not-found.tsx +18 -0
- package/template/src/app/page.tsx +187 -0
- package/template/src/components/admin/users-table.tsx +162 -0
- package/template/src/components/auth/auth-card.tsx +41 -0
- package/template/src/components/auth/can.tsx +34 -0
- package/template/src/components/auth/form-field.tsx +32 -0
- package/template/src/components/auth/login-form.tsx +82 -0
- package/template/src/components/auth/register-form.tsx +114 -0
- package/template/src/components/auth/session-refresher.tsx +44 -0
- package/template/src/components/layout/logout-button.tsx +34 -0
- package/template/src/components/layout/navbar.tsx +67 -0
- package/template/src/components/layout/page-header.tsx +21 -0
- package/template/src/components/layout/skeletons.tsx +35 -0
- package/template/src/components/logo.tsx +19 -0
- package/template/src/components/motion/fade-in.tsx +19 -0
- package/template/src/components/providers/user-provider.tsx +60 -0
- package/template/src/components/reports/animated-bar.tsx +28 -0
- package/template/src/components/role-badge.tsx +17 -0
- package/template/src/components/theme-toggle.tsx +25 -0
- package/template/src/components/ui/badge.tsx +44 -0
- package/template/src/components/ui/button.tsx +66 -0
- package/template/src/components/ui/card.tsx +87 -0
- package/template/src/components/ui/input.tsx +18 -0
- package/template/src/components/ui/label.tsx +20 -0
- package/template/src/components/ui/native-select.tsx +54 -0
- package/template/src/components/ui/skeleton.tsx +13 -0
- package/template/src/config/rbac.ts +140 -0
- package/template/src/config/site.ts +4 -0
- package/template/src/lib/api.ts +143 -0
- package/template/src/lib/auth.ts +42 -0
- package/template/src/lib/dal.ts +60 -0
- package/template/src/lib/db.ts +35 -0
- package/template/src/lib/env.ts +36 -0
- package/template/src/lib/http.ts +57 -0
- package/template/src/lib/redirect.ts +21 -0
- package/template/src/lib/session.ts +67 -0
- package/template/src/lib/theme.ts +8 -0
- package/template/src/lib/users.ts +159 -0
- package/template/src/lib/utils.ts +1 -0
- package/template/src/lib/validations/auth.ts +44 -0
- package/template/src/lib/validations/user.ts +13 -0
- package/template/src/models/user.ts +30 -0
- package/template/src/proxy.ts +57 -0
- package/template/src/types/user.ts +10 -0
- package/template/tsconfig.json +34 -0
package/README.md
CHANGED
|
@@ -1,3 +1,109 @@
|
|
|
1
|
-
#
|
|
1
|
+
# create-mohit-next
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Scaffold a production-ready full-stack **Next.js 16 + TypeScript** app in one command:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm create mohit-next@latest my-app
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
You get:
|
|
10
|
+
|
|
11
|
+
- **Next.js 16** (App Router, Turbopack, Cache Components) + React 19, based on the current
|
|
12
|
+
`create-next-app` output
|
|
13
|
+
- **Tailwind CSS v4 + shadcn/ui** (button, input, label, card, badge, native-select, skeleton),
|
|
14
|
+
light/dark theme, motion animations, sonner toasts, lucide icons
|
|
15
|
+
- **MongoDB + Mongoose** with a cached connection
|
|
16
|
+
- **Auth:** bcrypt (cost 12) + JWT (`jose`, HS256) in an httpOnly cookie, register/login/logout/me
|
|
17
|
+
routes, timing-safe login
|
|
18
|
+
- **RBAC** (`user` < `manager` < `admin`) enforced in three layers: `proxy.ts`, a Data Access Layer
|
|
19
|
+
that re-reads the DB, and `withAuth()` for API routes, plus `useUser()` and `<Can>` on the client.
|
|
20
|
+
The first registered user becomes admin.
|
|
21
|
+
- **zod v4** for env validation and shared request/form schemas, react-hook-form
|
|
22
|
+
- ESLint, Prettier (+ Tailwind plugin), a `typecheck` script, security headers
|
|
23
|
+
- Optional **Docker**: multi-stage `Dockerfile` (standalone output) + `docker-compose.yml` with `mongo:8`
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npm create mohit-next@latest [project-name] [-- options]
|
|
29
|
+
# or
|
|
30
|
+
pnpm create mohit-next [project-name]
|
|
31
|
+
yarn create mohit-next [project-name]
|
|
32
|
+
bun create mohit-next [project-name]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Option | Description |
|
|
36
|
+
| --------------- | ------------------------------------------------------------ |
|
|
37
|
+
| `[name]` | Project directory / package name (`.` = current directory) |
|
|
38
|
+
| `-y`, `--yes` | Skip prompts and use defaults (Docker, install, git: all on) |
|
|
39
|
+
| `--no-docker` | Don't include Dockerfile / docker-compose.yml |
|
|
40
|
+
| `--no-install` | Don't install dependencies |
|
|
41
|
+
| `--no-git` | Don't initialize a git repository |
|
|
42
|
+
| `-h`, `--help` | Show help |
|
|
43
|
+
|
|
44
|
+
> With `npm create`, put a `--` before the flags so npm forwards them:
|
|
45
|
+
> `npm create mohit-next@latest my-app -- --yes --no-docker`
|
|
46
|
+
|
|
47
|
+
The CLI:
|
|
48
|
+
|
|
49
|
+
1. asks for the project name (it must be a valid npm package name; a non-empty existing directory is refused),
|
|
50
|
+
and whether to include Docker, install dependencies, and initialize git
|
|
51
|
+
2. copies `template/` and restores dotfiles (`_gitignore` → `.gitignore`, etc.)
|
|
52
|
+
3. sets `name` in `package.json`, and removes Docker files and `docker:*` scripts if you declined Docker
|
|
53
|
+
4. creates `.env.local` from `.env.example` with a freshly generated random `JWT_SECRET`
|
|
54
|
+
5. installs dependencies with the package manager you ran it with (npm, pnpm, yarn or bun, detected
|
|
55
|
+
from `npm_config_user_agent`)
|
|
56
|
+
6. runs `git init` and makes an initial commit (skipped with a warning if git is missing)
|
|
57
|
+
|
|
58
|
+
Without a TTY (CI), prompts are skipped and defaults are used.
|
|
59
|
+
|
|
60
|
+
## Developing the CLI
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
create-mohit-next/
|
|
64
|
+
├── index.js # the CLI (ESM, @clack/prompts + picocolors)
|
|
65
|
+
├── package.json # "bin" → index.js, "files" → index.js + template
|
|
66
|
+
└── template/ # the generated app; dotfiles are stored as _gitignore, _env.example, ...
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
npm strips or renames dotfiles such as `.gitignore` when publishing, so the template stores them
|
|
70
|
+
with a leading underscore and the CLI renames them after copying. Add new dotfiles to `RENAME_FILES`
|
|
71
|
+
in `index.js`.
|
|
72
|
+
|
|
73
|
+
### Test locally with `npm link`
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cd create-mohit-next
|
|
77
|
+
npm install
|
|
78
|
+
npm link # puts `create-mohit-next` on your PATH
|
|
79
|
+
|
|
80
|
+
cd /some/temp/dir
|
|
81
|
+
create-mohit-next my-app # interactive
|
|
82
|
+
create-mohit-next my-app2 --yes --no-docker
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`npm unlink -g create-mohit-next` removes the link. You can also run it directly with
|
|
86
|
+
`node /path/to/create-mohit-next/index.js my-app`.
|
|
87
|
+
|
|
88
|
+
Before publishing, check the generated app:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
cd my-app
|
|
92
|
+
npm run typecheck && npm run lint && npm run build
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Publish to npm
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm login
|
|
99
|
+
npm pack --dry-run # check the tarball: index.js, package.json, README.md, template/**
|
|
100
|
+
npm version patch # or minor / major
|
|
101
|
+
npm publish # add --access public for a scoped name
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
After publishing, `npm create mohit-next@latest my-app` runs your new version (npm maps
|
|
105
|
+
`npm create <x>` to the `create-<x>` package).
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
import * as p from "@clack/prompts";
|
|
9
|
+
import pc from "picocolors";
|
|
10
|
+
|
|
11
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const TEMPLATE_DIR = path.join(__dirname, "template");
|
|
13
|
+
const DEFAULT_NAME = "my-app";
|
|
14
|
+
|
|
15
|
+
// npm strips dotfiles (and renames .gitignore) when publishing, so the template
|
|
16
|
+
// stores them with a leading underscore and we restore the real names here.
|
|
17
|
+
const RENAME_FILES = {
|
|
18
|
+
_gitignore: ".gitignore",
|
|
19
|
+
_dockerignore: ".dockerignore",
|
|
20
|
+
"_env.example": ".env.example",
|
|
21
|
+
_prettierrc: ".prettierrc",
|
|
22
|
+
_prettierignore: ".prettierignore",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const DOCKER_FILES = ["Dockerfile", "docker-compose.yml", ".dockerignore"];
|
|
26
|
+
|
|
27
|
+
// Never copy local build/dependency artifacts, even if they exist in template/.
|
|
28
|
+
const COPY_IGNORE = new Set(["node_modules", ".next", "next-env.d.ts", "package-lock.json", ".env.local"]);
|
|
29
|
+
|
|
30
|
+
const HELP = `
|
|
31
|
+
${pc.bold("create-mohit-next")} - scaffold a full-stack Next.js + TypeScript app
|
|
32
|
+
|
|
33
|
+
${pc.bold("Usage")}
|
|
34
|
+
npm create mohit-next@latest ${pc.cyan("[project-name]")} ${pc.dim("[options]")}
|
|
35
|
+
|
|
36
|
+
${pc.bold("Options")}
|
|
37
|
+
-y, --yes Skip prompts and use defaults (Docker, install, git: all on)
|
|
38
|
+
--no-docker Don't include Dockerfile / docker-compose.yml
|
|
39
|
+
--no-install Don't install dependencies
|
|
40
|
+
--no-git Don't initialize a git repository
|
|
41
|
+
-h, --help Show this help
|
|
42
|
+
|
|
43
|
+
${pc.bold("Examples")}
|
|
44
|
+
npm create mohit-next@latest my-app
|
|
45
|
+
npm create mohit-next@latest my-app -- --yes --no-docker
|
|
46
|
+
pnpm create mohit-next my-app
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
// Argument parsing
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
function parseArgs(argv) {
|
|
54
|
+
const opts = { name: undefined, yes: false, docker: undefined, install: undefined, git: undefined, help: false };
|
|
55
|
+
for (const arg of argv) {
|
|
56
|
+
switch (arg) {
|
|
57
|
+
case "-y":
|
|
58
|
+
case "--yes":
|
|
59
|
+
opts.yes = true;
|
|
60
|
+
break;
|
|
61
|
+
case "-h":
|
|
62
|
+
case "--help":
|
|
63
|
+
opts.help = true;
|
|
64
|
+
break;
|
|
65
|
+
case "--docker":
|
|
66
|
+
opts.docker = true;
|
|
67
|
+
break;
|
|
68
|
+
case "--no-docker":
|
|
69
|
+
opts.docker = false;
|
|
70
|
+
break;
|
|
71
|
+
case "--install":
|
|
72
|
+
opts.install = true;
|
|
73
|
+
break;
|
|
74
|
+
case "--no-install":
|
|
75
|
+
opts.install = false;
|
|
76
|
+
break;
|
|
77
|
+
case "--git":
|
|
78
|
+
opts.git = true;
|
|
79
|
+
break;
|
|
80
|
+
case "--no-git":
|
|
81
|
+
opts.git = false;
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
if (arg.startsWith("-")) {
|
|
85
|
+
console.error(pc.red(`Unknown option: ${arg}`));
|
|
86
|
+
console.log(HELP);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
if (opts.name) {
|
|
90
|
+
console.error(pc.red(`Unexpected argument: ${arg}`));
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
opts.name = arg;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return opts;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// Helpers
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
// Same rules npm uses for new package names (validate-npm-package-name).
|
|
104
|
+
function validateProjectName(input) {
|
|
105
|
+
const value = (input ?? "").trim();
|
|
106
|
+
if (!value) return "Project name is required.";
|
|
107
|
+
// "." scaffolds into the current directory, whose name becomes the package name.
|
|
108
|
+
const name = value === "." ? path.basename(process.cwd()) : path.basename(value);
|
|
109
|
+
if (value === "." && name !== name.toLowerCase()) {
|
|
110
|
+
return `The current directory name "${name}" is not a valid npm package name (must be lowercase).`;
|
|
111
|
+
}
|
|
112
|
+
if (name.length > 214) return "Name must be 214 characters or fewer.";
|
|
113
|
+
if (name !== name.toLowerCase()) return "Name must be lowercase.";
|
|
114
|
+
if (/^[._]/.test(name)) return "Name cannot start with a dot or underscore.";
|
|
115
|
+
if (!/^[a-z0-9][a-z0-9._~-]*$/.test(name)) {
|
|
116
|
+
return "Use only lowercase letters, digits, '-', '.', '_' or '~' (npm package name rules).";
|
|
117
|
+
}
|
|
118
|
+
if (["node_modules", "favicon.ico"].includes(name)) return `"${name}" is a reserved name.`;
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isDirEmpty(dir) {
|
|
123
|
+
if (!fs.existsSync(dir)) return true;
|
|
124
|
+
if (!fs.statSync(dir).isDirectory()) return false;
|
|
125
|
+
const ignorable = new Set([".git", ".DS_Store", "Thumbs.db", ".idea", ".vscode"]);
|
|
126
|
+
return fs.readdirSync(dir).every((entry) => ignorable.has(entry));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function validateTarget(input) {
|
|
130
|
+
const nameError = validateProjectName(input);
|
|
131
|
+
if (nameError) return nameError;
|
|
132
|
+
const dir = path.resolve(process.cwd(), input.trim());
|
|
133
|
+
if (!isDirEmpty(dir)) return `Directory "${input.trim()}" already exists and is not empty.`;
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function detectPackageManager() {
|
|
138
|
+
const ua = process.env.npm_config_user_agent ?? "";
|
|
139
|
+
const name = ua.split(" ")[0]?.split("/")[0];
|
|
140
|
+
if (name === "pnpm" || name === "yarn" || name === "bun") return name;
|
|
141
|
+
return "npm";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function runScriptCmd(pm, script) {
|
|
145
|
+
return pm === "npm" || pm === "bun" ? `${pm} run ${script}` : `${pm} ${script}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function toTitle(name) {
|
|
149
|
+
return name
|
|
150
|
+
.replace(/^@[^/]+\//, "")
|
|
151
|
+
.split(/[-_.~]+/)
|
|
152
|
+
.filter(Boolean)
|
|
153
|
+
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
154
|
+
.join(" ");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function run(cmd, args, cwd, { shell = false } = {}) {
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
let output = "";
|
|
160
|
+
const child = spawn(cmd, args, { cwd, shell, env: process.env, stdio: ["ignore", "pipe", "pipe"] });
|
|
161
|
+
child.stdout?.on("data", (d) => (output += d));
|
|
162
|
+
child.stderr?.on("data", (d) => (output += d));
|
|
163
|
+
child.on("error", (err) => resolve({ ok: false, output: String(err) }));
|
|
164
|
+
child.on("close", (code) => resolve({ ok: code === 0, output }));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Animated spinner in a terminal; plain log lines in CI / piped output
|
|
169
|
+
// (an animated spinner would flood non-TTY logs with frames).
|
|
170
|
+
function spinner() {
|
|
171
|
+
if (process.stdout.isTTY) return p.spinner();
|
|
172
|
+
return {
|
|
173
|
+
start: (msg) => p.log.step(msg),
|
|
174
|
+
stop: (msg) => p.log.success(msg),
|
|
175
|
+
error: (msg) => p.log.error(msg),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function bail(message) {
|
|
180
|
+
p.cancel(message);
|
|
181
|
+
process.exit(1);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function guard(value) {
|
|
185
|
+
if (p.isCancel(value)) bail("Operation cancelled.");
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
// Scaffolding steps
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
|
|
193
|
+
function copyTemplate(targetDir) {
|
|
194
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
195
|
+
fs.cpSync(TEMPLATE_DIR, targetDir, {
|
|
196
|
+
recursive: true,
|
|
197
|
+
force: false,
|
|
198
|
+
errorOnExist: false,
|
|
199
|
+
filter: (src) => !COPY_IGNORE.has(path.basename(src)),
|
|
200
|
+
});
|
|
201
|
+
for (const [from, to] of Object.entries(RENAME_FILES)) {
|
|
202
|
+
const src = path.join(targetDir, from);
|
|
203
|
+
if (fs.existsSync(src)) fs.renameSync(src, path.join(targetDir, to));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function updatePackageJson(targetDir, packageName, includeDocker) {
|
|
208
|
+
const file = path.join(targetDir, "package.json");
|
|
209
|
+
const pkg = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
210
|
+
pkg.name = packageName;
|
|
211
|
+
if (!includeDocker) {
|
|
212
|
+
for (const script of Object.keys(pkg.scripts ?? {})) {
|
|
213
|
+
if (script.startsWith("docker:")) delete pkg.scripts[script];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + "\n");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function removeDocker(targetDir) {
|
|
220
|
+
for (const file of DOCKER_FILES) {
|
|
221
|
+
fs.rmSync(path.join(targetDir, file), { force: true });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// README blocks wrapped in <!-- docker:start --> ... <!-- docker:end --> (each
|
|
226
|
+
// marker on its own line, followed by a blank line) are dropped when Docker is
|
|
227
|
+
// declined; the markers themselves are always removed.
|
|
228
|
+
function processReadme(targetDir, { projectTitle, includeDocker, pm }) {
|
|
229
|
+
const file = path.join(targetDir, "README.md");
|
|
230
|
+
if (!fs.existsSync(file)) return;
|
|
231
|
+
let readme = fs.readFileSync(file, "utf8");
|
|
232
|
+
readme = includeDocker
|
|
233
|
+
? readme.replace(/^<!-- docker:(start|end) -->\r?\n(\r?\n)?/gm, "")
|
|
234
|
+
: readme.replace(/^<!-- docker:start -->[\s\S]*?<!-- docker:end -->\r?\n(\r?\n)?/gm, "");
|
|
235
|
+
readme = readme
|
|
236
|
+
.replaceAll("{{PROJECT_TITLE}}", projectTitle)
|
|
237
|
+
.replaceAll("npm run ", pm === "npm" ? "npm run " : runScriptCmd(pm, ""))
|
|
238
|
+
.replaceAll("npm install", `${pm} install`);
|
|
239
|
+
fs.writeFileSync(file, readme);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function setSiteName(targetDir, projectTitle) {
|
|
243
|
+
const file = path.join(targetDir, "src", "config", "site.ts");
|
|
244
|
+
if (!fs.existsSync(file)) return;
|
|
245
|
+
const content = fs.readFileSync(file, "utf8").replaceAll("{{PROJECT_TITLE}}", projectTitle);
|
|
246
|
+
fs.writeFileSync(file, content);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function createEnvLocal(targetDir, projectSlug) {
|
|
250
|
+
const example = path.join(targetDir, ".env.example");
|
|
251
|
+
const local = path.join(targetDir, ".env.local");
|
|
252
|
+
if (!fs.existsSync(example) || fs.existsSync(local)) return;
|
|
253
|
+
const secret = crypto.randomBytes(48).toString("base64url");
|
|
254
|
+
const dbName = projectSlug.replace(/[^a-z0-9_-]/g, "_") || "app";
|
|
255
|
+
const content = fs
|
|
256
|
+
.readFileSync(example, "utf8")
|
|
257
|
+
.replace(/^JWT_SECRET=.*$/m, `JWT_SECRET=${secret}`)
|
|
258
|
+
.replace(/^MONGODB_URI=(mongodb:\/\/[^/]+)\/[^\s?]*/m, `MONGODB_URI=$1/${dbName}`);
|
|
259
|
+
fs.writeFileSync(local, content);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function installDependencies(targetDir, pm) {
|
|
263
|
+
const s = spinner();
|
|
264
|
+
s.start(`Installing dependencies with ${pm} (this can take a minute)`);
|
|
265
|
+
const { ok, output } = await run(pm, ["install"], targetDir, { shell: process.platform === "win32" });
|
|
266
|
+
if (ok) {
|
|
267
|
+
s.stop("Dependencies installed");
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
s.error(`Dependency installation failed. Run ${pc.cyan(`${pm} install`)} manually.`);
|
|
271
|
+
p.log.message(pc.dim(output.trim().split("\n").slice(-15).join("\n")));
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function initGit(targetDir) {
|
|
276
|
+
const git = spawnSync("git", ["--version"], { stdio: "ignore" });
|
|
277
|
+
if (git.error || git.status !== 0) {
|
|
278
|
+
p.log.warn("git not found - skipping repository initialization.");
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
const inside = spawnSync("git", ["rev-parse", "--is-inside-work-tree"], { cwd: targetDir, stdio: "pipe" });
|
|
282
|
+
if (inside.status === 0 && String(inside.stdout).trim() === "true") {
|
|
283
|
+
p.log.info("Already inside a git repository - skipping git init.");
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
const s = spinner();
|
|
287
|
+
s.start("Initializing git repository");
|
|
288
|
+
let init = await run("git", ["init", "-b", "main"], targetDir);
|
|
289
|
+
if (!init.ok) init = await run("git", ["init"], targetDir); // git < 2.28
|
|
290
|
+
if (!init.ok) {
|
|
291
|
+
s.error("git init failed - skipping.");
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
await run("git", ["add", "-A"], targetDir);
|
|
295
|
+
const commit = await run("git", ["commit", "-m", "Initial commit from create-mohit-next"], targetDir);
|
|
296
|
+
if (!commit.ok) {
|
|
297
|
+
s.stop("Git repository initialized (initial commit skipped - configure git user.name/user.email)");
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
s.stop("Git repository initialized with an initial commit");
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------------------------------------------------------------------------
|
|
305
|
+
// Main
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
|
|
308
|
+
async function main() {
|
|
309
|
+
const args = parseArgs(process.argv.slice(2));
|
|
310
|
+
if (args.help) {
|
|
311
|
+
console.log(HELP);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (!fs.existsSync(TEMPLATE_DIR)) bail(`Template directory not found at ${TEMPLATE_DIR}`);
|
|
316
|
+
|
|
317
|
+
const pm = detectPackageManager();
|
|
318
|
+
// Without a TTY (CI, piped input) prompts can't be answered, so fall back to defaults.
|
|
319
|
+
const interactive = !args.yes && process.stdin.isTTY && process.stdout.isTTY;
|
|
320
|
+
|
|
321
|
+
console.log();
|
|
322
|
+
p.intro(pc.bgCyan(pc.black(" create-mohit-next ")));
|
|
323
|
+
|
|
324
|
+
// --- project name ---------------------------------------------------------
|
|
325
|
+
let projectName = args.name;
|
|
326
|
+
if (projectName) {
|
|
327
|
+
const error = validateTarget(projectName);
|
|
328
|
+
if (error) bail(error);
|
|
329
|
+
} else if (interactive) {
|
|
330
|
+
projectName = guard(
|
|
331
|
+
await p.text({
|
|
332
|
+
message: "What is your project named?",
|
|
333
|
+
placeholder: DEFAULT_NAME,
|
|
334
|
+
defaultValue: DEFAULT_NAME,
|
|
335
|
+
validate: (value) => validateTarget(value || DEFAULT_NAME),
|
|
336
|
+
}),
|
|
337
|
+
);
|
|
338
|
+
} else {
|
|
339
|
+
projectName = DEFAULT_NAME;
|
|
340
|
+
const error = validateTarget(projectName);
|
|
341
|
+
if (error) bail(error);
|
|
342
|
+
}
|
|
343
|
+
projectName = projectName.trim();
|
|
344
|
+
|
|
345
|
+
const targetDir = path.resolve(process.cwd(), projectName);
|
|
346
|
+
const packageName = path.basename(targetDir).toLowerCase();
|
|
347
|
+
const projectTitle = toTitle(packageName) || "My App";
|
|
348
|
+
|
|
349
|
+
// --- options --------------------------------------------------------------
|
|
350
|
+
const ask = async (flag, message) => {
|
|
351
|
+
if (flag !== undefined) return flag;
|
|
352
|
+
if (!interactive) return true;
|
|
353
|
+
return guard(await p.confirm({ message, initialValue: true }));
|
|
354
|
+
};
|
|
355
|
+
const includeDocker = await ask(args.docker, "Include Docker (Dockerfile + docker-compose with MongoDB)?");
|
|
356
|
+
const install = await ask(args.install, `Install dependencies with ${pm}?`);
|
|
357
|
+
const gitInit = await ask(args.git, "Initialize a git repository?");
|
|
358
|
+
|
|
359
|
+
// --- scaffold -------------------------------------------------------------
|
|
360
|
+
const s = spinner();
|
|
361
|
+
s.start(`Scaffolding project in ${pc.cyan(targetDir)}`);
|
|
362
|
+
try {
|
|
363
|
+
copyTemplate(targetDir);
|
|
364
|
+
updatePackageJson(targetDir, packageName, includeDocker);
|
|
365
|
+
if (!includeDocker) removeDocker(targetDir);
|
|
366
|
+
processReadme(targetDir, { projectTitle, includeDocker, pm });
|
|
367
|
+
setSiteName(targetDir, projectTitle);
|
|
368
|
+
createEnvLocal(targetDir, packageName);
|
|
369
|
+
} catch (err) {
|
|
370
|
+
s.error("Scaffolding failed");
|
|
371
|
+
bail(err instanceof Error ? err.message : String(err));
|
|
372
|
+
}
|
|
373
|
+
s.stop("Project files created (.env.local generated with a random JWT_SECRET)");
|
|
374
|
+
|
|
375
|
+
let installed = false;
|
|
376
|
+
if (install) installed = await installDependencies(targetDir, pm);
|
|
377
|
+
if (gitInit) await initGit(targetDir);
|
|
378
|
+
|
|
379
|
+
// --- next steps -----------------------------------------------------------
|
|
380
|
+
const rel = path.relative(process.cwd(), targetDir) || ".";
|
|
381
|
+
const steps = [
|
|
382
|
+
rel !== "." && `cd ${rel}`,
|
|
383
|
+
!installed && `${pm} install`,
|
|
384
|
+
includeDocker
|
|
385
|
+
? `${runScriptCmd(pm, "docker:db")} ${pc.dim("# MongoDB in Docker (or set MONGODB_URI)")}`
|
|
386
|
+
: `${pc.dim("# set MONGODB_URI in .env.local")}`,
|
|
387
|
+
`${runScriptCmd(pm, "dev")}`,
|
|
388
|
+
].filter(Boolean);
|
|
389
|
+
|
|
390
|
+
p.note(steps.join("\n"), "Next steps");
|
|
391
|
+
p.outro(
|
|
392
|
+
`Open ${pc.cyan("http://localhost:3000")} and register - ${pc.bold("the first account becomes admin")}. Happy hacking!`,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
main().catch((err) => {
|
|
397
|
+
p.cancel(err instanceof Error ? err.message : String(err));
|
|
398
|
+
process.exit(1);
|
|
399
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-mohit-next",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Scaffold a production-ready full-stack Next.js 16 + TypeScript app with MongoDB, JWT auth, RBAC, shadcn/ui and Docker.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"create-mohit-next": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js",
|
|
11
|
+
"template"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"start": "node index.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"create",
|
|
18
|
+
"next",
|
|
19
|
+
"nextjs",
|
|
20
|
+
"starter",
|
|
21
|
+
"template",
|
|
22
|
+
"boilerplate",
|
|
23
|
+
"mongodb",
|
|
24
|
+
"mongoose",
|
|
25
|
+
"rbac",
|
|
26
|
+
"auth",
|
|
27
|
+
"shadcn",
|
|
28
|
+
"tailwind"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.19.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@clack/prompts": "^1.9.0",
|
|
36
|
+
"picocolors": "^1.1.1"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<!-- BEGIN:nextjs-agent-rules -->
|
|
2
|
+
|
|
3
|
+
## This is NOT the Next.js you know
|
|
4
|
+
|
|
5
|
+
This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.
|
|
6
|
+
|
|
7
|
+
This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.
|
|
8
|
+
|
|
9
|
+
<!-- END:nextjs-agent-rules -->
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# syntax=docker/dockerfile:1
|
|
2
|
+
# Multi-stage build for a Next.js app using `output: "standalone"`.
|
|
3
|
+
|
|
4
|
+
ARG NODE_VERSION=24-alpine
|
|
5
|
+
|
|
6
|
+
# ---- 1. Install dependencies (cached unless the lockfile changes) ----------
|
|
7
|
+
FROM node:${NODE_VERSION} AS deps
|
|
8
|
+
RUN apk add --no-cache libc6-compat
|
|
9
|
+
WORKDIR /app
|
|
10
|
+
COPY package.json package-lock.json* pnpm-lock.yaml* yarn.lock* bun.lock* ./
|
|
11
|
+
RUN \
|
|
12
|
+
if [ -f package-lock.json ]; then npm ci; \
|
|
13
|
+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm install --frozen-lockfile; \
|
|
14
|
+
elif [ -f yarn.lock ]; then corepack enable yarn && yarn install --frozen-lockfile; \
|
|
15
|
+
elif [ -f bun.lock ]; then npm install -g bun && bun install --frozen-lockfile; \
|
|
16
|
+
else npm install; \
|
|
17
|
+
fi
|
|
18
|
+
|
|
19
|
+
# ---- 2. Build ---------------------------------------------------------------
|
|
20
|
+
FROM node:${NODE_VERSION} AS builder
|
|
21
|
+
WORKDIR /app
|
|
22
|
+
COPY --from=deps /app/node_modules ./node_modules
|
|
23
|
+
COPY . .
|
|
24
|
+
ENV NEXT_TELEMETRY_DISABLED=1
|
|
25
|
+
# Env vars are validated lazily at request time, so no secrets are needed here.
|
|
26
|
+
RUN npm run build
|
|
27
|
+
|
|
28
|
+
# ---- 3. Runtime (only the standalone server + static assets) ---------------
|
|
29
|
+
FROM node:${NODE_VERSION} AS runner
|
|
30
|
+
WORKDIR /app
|
|
31
|
+
ENV NODE_ENV=production \
|
|
32
|
+
NEXT_TELEMETRY_DISABLED=1 \
|
|
33
|
+
PORT=3000 \
|
|
34
|
+
HOSTNAME=0.0.0.0
|
|
35
|
+
|
|
36
|
+
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
|
|
37
|
+
|
|
38
|
+
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
39
|
+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
40
|
+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
41
|
+
|
|
42
|
+
USER nextjs
|
|
43
|
+
EXPOSE 3000
|
|
44
|
+
|
|
45
|
+
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
|
46
|
+
CMD wget -qO- http://127.0.0.1:3000/api/health || exit 1
|
|
47
|
+
|
|
48
|
+
CMD ["node", "server.js"]
|