1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use crate::commands;
use crate::error::CliResult;
use crate::naming::{Names, Transport};
const PROJECT_TAGLINE: &str = "Scalable Rust backend apps with native performance.";
const AFTER_HELP: &str = concat!(
"Documentation: ",
env!("CARGO_PKG_HOMEPAGE"),
"/cli/\n",
"Repository: ",
env!("CARGO_PKG_REPOSITORY"),
);
pub fn print_version() {
println!("NestRS {}", env!("CARGO_PKG_VERSION"));
}
pub fn print_about() {
println!("NestRS");
println!("Version: {}", env!("CARGO_PKG_VERSION"));
println!("Tagline: {PROJECT_TAGLINE}");
println!("Documentation: {}/cli/", env!("CARGO_PKG_HOMEPAGE"));
println!("Repository: {}", env!("CARGO_PKG_REPOSITORY"));
println!("License: {}", env!("CARGO_PKG_LICENSE"));
println!("Authors: Yoann Vanitou");
}
#[derive(Parser, Debug)]
#[command(
name = "nestrs",
about = PROJECT_TAGLINE,
long_about = "Scalable Rust backend apps with native performance.\n\n\
Scaffolds NestRS projects, features, transport adapters, and toolchain checks.",
disable_version_flag = true,
after_help = AFTER_HELP,
)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug)]
pub enum Command {
/// Create a new NestRS project or workspace app.
///
/// Every layout ships the same `hello` module — a `#[public] GET /` that
/// proves the project started. Layout is inferred from the directory tree:
/// new monorepo nestrs new hello → ./hello/ + apps/hello/
/// new workspace app nestrs new blog → apps/blog/ (next free port)
/// single crate nestrs new hello --standalone
New {
/// Project name (kebab-case recommended, e.g. `hello` or `blog`).
name: String,
/// Single-crate layout (logic in `src/`) instead of the default monorepo.
#[arg(long)]
standalone: bool,
/// Parent directory (default: current directory).
#[arg(long, short = 'o', default_value = ".")]
output: PathBuf,
/// Run `cargo check` after scaffolding.
#[arg(long)]
check: bool,
/// Print what would be written without touching the filesystem.
#[arg(long)]
dry_run: bool,
},
/// Verify toolchain and optional NestRS environment variables.
Doctor {
/// Project directory to inspect (default: current directory).
#[arg(long, short = 'p')]
path: Option<PathBuf>,
},
/// Print the CLI version.
Version,
/// Print project metadata (tagline, docs, license, author).
About,
/// Install the latest nestrs CLI from crates.io when a newer version exists.
Update {
/// Reinstall from `crates/nest-rs-cli` in the nestrs monorepo instead of crates.io.
#[arg(long)]
from_path: bool,
/// Monorepo root when using `--from-path` (default: auto-discover).
#[arg(long, requires = "from_path")]
workspace: Option<PathBuf>,
/// Reinstall even when already on the latest version (`cargo install --force`).
#[arg(long, short = 'f')]
force: bool,
},
/// Generate features, resources, and transport adapters (workspace only).
#[command(subcommand, visible_aliases = ["g"])]
Generate(GenerateCommand),
/// Run a project task through `just` (bootstraps the dev toolchain on first use).
///
/// Forwards the recipe and its arguments verbatim:
/// nestrs run dev → just dev
/// nestrs run test → just test
/// nestrs run db up → just db up
/// nestrs run → list available recipes
Run {
/// Skip the first-run toolchain bootstrap (CI / offline).
#[arg(long)]
no_bootstrap: bool,
/// Recipe and arguments forwarded to `just` (omit to list recipes).
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
},
}
/// Shared positional + flags for every generator.
#[derive(Args, Debug)]
pub struct GenTarget {
/// Name (e.g. `posts`).
pub name: String,
/// Workspace root or working directory (default: auto-discover from cwd).
#[arg(long, short = 'p')]
pub path: Option<PathBuf>,
/// Print what would be written without touching the filesystem.
#[arg(long)]
pub dry_run: bool,
}
/// `g auth` takes no name — a workspace has exactly one auth adapter.
#[derive(Args, Debug)]
pub struct AuthTarget {
/// Workspace root or working directory (default: auto-discover from cwd).
#[arg(long, short = 'p')]
pub path: Option<PathBuf>,
/// Print what would be written without touching the filesystem.
#[arg(long)]
pub dry_run: bool,
}
#[derive(Subcommand, Debug)]
pub enum GenerateCommand {
/// A transport-agnostic port (mod + module + service).
Feature(GenTarget),
/// A DB-backed CRUD slice (entity + CrudService + guarded HTTP adapter).
Resource(GenTarget),
/// The app's authn/authz adapter (Claims, AuthnGuard, AppAbility, AuthzGuard).
Auth(AuthTarget),
/// A SeaORM migration, registered in both lib.rs and migrator.rs.
Migration(GenTarget),
/// Add an HTTP controller adapter to an existing feature.
Http(GenTarget),
/// Add a GraphQL resolver adapter to an existing feature.
Graphql(GenTarget),
/// Add a WebSocket gateway adapter to an existing feature.
Ws(GenTarget),
/// Add a queue processor adapter to an existing feature.
Queue(GenTarget),
/// Add a scheduled-tasks adapter to an existing feature.
Schedule(GenTarget),
/// Add an MCP tool adapter to an existing feature.
Mcp(GenTarget),
}
pub fn run(cli: Cli) -> CliResult<()> {
match cli.command {
Command::New {
name,
standalone,
output,
check,
dry_run,
} => {
let names = Names::parse(&name);
let opts = commands::NewOptions {
name,
output: output.clone(),
standalone,
dry_run,
};
commands::run_new(opts.clone())?;
if check && !dry_run {
run_check(&opts, &names, standalone, &output)?;
}
Ok(())
}
Command::Doctor { path } => {
commands::run_doctor(commands::DoctorOptions { path })?;
Ok(())
}
Command::Version => {
print_version();
Ok(())
}
Command::About => {
print_about();
Ok(())
}
Command::Update {
from_path,
workspace,
force,
} => commands::run_update(commands::UpdateOptions {
from_path,
path: workspace,
force,
}),
Command::Generate(cmd) => run_generate(cmd),
Command::Run { no_bootstrap, args } => {
commands::run_task(commands::RunOptions { args, no_bootstrap })
}
}
}
fn run_generate(cmd: GenerateCommand) -> CliResult<()> {
use GenerateCommand::*;
match cmd {
Feature(t) => commands::run_feature(commands::FeatureOptions {
name: t.name,
path: t.path,
dry_run: t.dry_run,
}),
Resource(t) => commands::run_resource(commands::ResourceOptions {
name: t.name,
path: t.path,
dry_run: t.dry_run,
}),
Auth(t) => commands::run_auth(commands::AuthOptions {
path: t.path,
dry_run: t.dry_run,
}),
Migration(t) => commands::run_migration(commands::MigrationOptions {
name: t.name,
path: t.path,
dry_run: t.dry_run,
}),
Http(t) => adapter(Transport::Http, t),
Graphql(t) => adapter(Transport::Graphql, t),
Ws(t) => adapter(Transport::Ws, t),
Queue(t) => adapter(Transport::Queue, t),
Schedule(t) => adapter(Transport::Schedule, t),
Mcp(t) => adapter(Transport::Mcp, t),
}
}
fn adapter(transport: Transport, t: GenTarget) -> CliResult<()> {
commands::run_adapter(
transport,
commands::AdapterOptions {
name: t.name,
path: t.path,
dry_run: t.dry_run,
},
)
}
fn run_check(
opts: &commands::NewOptions,
names: &Names,
standalone: bool,
output: &std::path::Path,
) -> CliResult<()> {
let project = commands::project_dir_for_check(opts, names)?;
let ws_root = crate::context::NestrsWorkspace::discover(output)?;
if let Some(ws) = ws_root.filter(|_| !standalone) {
let status = std::process::Command::new("cargo")
.args(["check", "-p", &names.kebab])
.current_dir(&ws.root)
.status()
.map_err(crate::error::CliError::Io)?;
if !status.success() {
return Err(crate::error::CliError::Anyhow(anyhow::anyhow!(
"cargo check -p {} failed",
names.kebab
)));
}
} else {
commands::run_cargo_check(&project)?;
}
println!("cargo check passed.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn after_help_includes_docs_and_repo() {
assert!(AFTER_HELP.contains("/cli/"));
assert!(AFTER_HELP.contains("github.com"));
}
}