feat(new): multi-runtime starter (node/deno/bun) via --runtime, shared + overlay templates
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 parent
c63c121787
commit
e8cd20b7ec
20 files changed
+173
-16
No files matched your search
@@ -14,6 +14,8 @@ pub enum Command {
|
||||
name: String,
|
||||
#[arg(long)]
|
||||
template_url: Option<String>,
|
||||
#[arg(long, default_value = "deno", value_parser = ["node", "deno", "bun"])]
|
||||
runtime: String,
|
||||
},
|
||||
/// Detect the runtime and run the project in watch mode
|
||||
Run,
|
||||
|
||||
+9
-2
@@ -1,9 +1,10 @@
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::runtime::Runtime;
|
||||
use crate::template;
|
||||
|
||||
pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> {
|
||||
pub fn run(name: &str, template_url: Option<&str>, runtime: &str) -> std::io::Result<()> {
|
||||
let dest = Path::new(name);
|
||||
if dest.exists() {
|
||||
return Err(Error::new(
|
||||
@@ -14,8 +15,14 @@ pub fn run(name: &str, template_url: Option<&str>) -> std::io::Result<()> {
|
||||
|
||||
match template_url {
|
||||
None => {
|
||||
let rt = Runtime::from_str(runtime).ok_or_else(|| {
|
||||
Error::new(
|
||||
ErrorKind::InvalidInput,
|
||||
format!("unknown runtime '{runtime}'"),
|
||||
)
|
||||
})?;
|
||||
std::fs::create_dir_all(dest)?;
|
||||
template::write_starter(dest, name)?;
|
||||
template::write_starter(dest, name, rt)?;
|
||||
}
|
||||
Some(url) => clone_template(url, dest)?,
|
||||
}
|
||||
|
||||
+2
-5
@@ -6,7 +6,7 @@ use crate::runtime::{self, Runtime};
|
||||
fn command_for(rt: Runtime) -> (&'static str, Vec<&'static str>) {
|
||||
match rt {
|
||||
Runtime::Deno => ("deno", vec!["task", "dev"]),
|
||||
Runtime::Bun => ("bun", vec!["--watch", "run", "dev"]),
|
||||
Runtime::Bun => ("bun", vec!["run", "dev"]),
|
||||
Runtime::Node => ("npm", vec!["run", "dev"]),
|
||||
}
|
||||
}
|
||||
@@ -38,10 +38,7 @@ mod tests {
|
||||
}
|
||||
#[test]
|
||||
fn bun_watch() {
|
||||
assert_eq!(
|
||||
command_for(Runtime::Bun),
|
||||
("bun", vec!["--watch", "run", "dev"])
|
||||
);
|
||||
assert_eq!(command_for(Runtime::Bun), ("bun", vec!["run", "dev"]));
|
||||
}
|
||||
#[test]
|
||||
fn node_watch() {
|
||||
|
||||
+6
-2
@@ -14,8 +14,12 @@ use cli::{Cli, Command};
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
match cli.command {
|
||||
Command::New { name, template_url } => {
|
||||
if let Err(e) = cmd_new::run(&name, template_url.as_deref()) {
|
||||
Command::New {
|
||||
name,
|
||||
template_url,
|
||||
runtime,
|
||||
} => {
|
||||
if let Err(e) = cmd_new::run(&name, template_url.as_deref(), &runtime) {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
+59
-7
@@ -1,10 +1,24 @@
|
||||
use include_dir::{include_dir, Dir};
|
||||
use std::path::Path;
|
||||
|
||||
pub static STARTER: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/starter");
|
||||
use crate::runtime::Runtime;
|
||||
|
||||
pub fn write_starter(dest: &Path, project_name: &str) -> std::io::Result<()> {
|
||||
write_dir(&STARTER, dest, project_name)
|
||||
pub static SHARED: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/shared");
|
||||
pub static RUNTIME_DENO: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/deno");
|
||||
pub static RUNTIME_NODE: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/node");
|
||||
pub static RUNTIME_BUN: Dir = include_dir!("$CARGO_MANIFEST_DIR/template/runtimes/bun");
|
||||
|
||||
fn overlay_for(runtime: Runtime) -> &'static Dir<'static> {
|
||||
match runtime {
|
||||
Runtime::Deno => &RUNTIME_DENO,
|
||||
Runtime::Node => &RUNTIME_NODE,
|
||||
Runtime::Bun => &RUNTIME_BUN,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_starter(dest: &Path, project_name: &str, runtime: Runtime) -> std::io::Result<()> {
|
||||
write_dir(&SHARED, dest, project_name)?;
|
||||
write_dir(overlay_for(runtime), dest, project_name)
|
||||
}
|
||||
|
||||
fn write_dir(dir: &Dir, dest: &Path, name: &str) -> std::io::Result<()> {
|
||||
@@ -36,16 +50,54 @@ mod tests {
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn writes_starter_and_substitutes_name() {
|
||||
fn deno_writes_shared_and_deno_overlay() {
|
||||
let d = tempdir().unwrap();
|
||||
write_starter(d.path(), "my-api").unwrap();
|
||||
write_starter(d.path(), "my-api", Runtime::Deno).unwrap();
|
||||
|
||||
let html = std::fs::read_to_string(d.path().join("public/index.html")).unwrap();
|
||||
assert!(html.contains("<title>my-api</title>"));
|
||||
assert!(!html.contains("{{project_name}}"));
|
||||
assert!(d.path().join("src/controllers/home.controller.ts").exists());
|
||||
assert!(d.path().join("matcha.toml").exists());
|
||||
assert!(d.path().join("src/main.ts").exists());
|
||||
let module = std::fs::read_to_string(d.path().join("src/app.module.ts")).unwrap();
|
||||
assert!(module.contains("mountpoint"));
|
||||
|
||||
assert!(d.path().join("deno.json").exists());
|
||||
assert!(d.path().join("matcha.toml").exists());
|
||||
let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap();
|
||||
assert!(main.contains("Deno.serve"));
|
||||
assert!(!d.path().join("package.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_writes_shared_and_node_overlay() {
|
||||
let d = tempdir().unwrap();
|
||||
write_starter(d.path(), "my-api", Runtime::Node).unwrap();
|
||||
|
||||
assert!(d.path().join("public/index.html").exists());
|
||||
assert!(d.path().join("src/app.module.ts").exists());
|
||||
|
||||
let pkg = std::fs::read_to_string(d.path().join("package.json")).unwrap();
|
||||
assert!(pkg.contains("\"name\": \"my-api\""));
|
||||
assert!(!pkg.contains("{{project_name}}"));
|
||||
assert!(d.path().join("tsconfig.json").exists());
|
||||
let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap();
|
||||
assert!(main.contains("app.listen"));
|
||||
assert!(!d.path().join("deno.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bun_writes_shared_and_bun_overlay() {
|
||||
let d = tempdir().unwrap();
|
||||
write_starter(d.path(), "my-api", Runtime::Bun).unwrap();
|
||||
|
||||
assert!(d.path().join("public/index.html").exists());
|
||||
assert!(d.path().join("src/app.module.ts").exists());
|
||||
|
||||
let pkg = std::fs::read_to_string(d.path().join("package.json")).unwrap();
|
||||
assert!(pkg.contains("\"name\": \"my-api\""));
|
||||
assert!(!pkg.contains("{{project_name}}"));
|
||||
let main = std::fs::read_to_string(d.path().join("src/main.ts")).unwrap();
|
||||
assert!(main.contains("Bun.serve"));
|
||||
assert!(!d.path().join("deno.json").exists());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
runtime = "bun"
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "{{project_name}}",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": { "dev": "bun --watch run src/main.ts" },
|
||||
"dependencies": { "@green-tea/core": "^26.7.0-beta.0" }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { createApp } from '@green-tea/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
const app = createApp({ modules: [AppModule] });
|
||||
|
||||
Bun.serve({ port: 8000, fetch: app.fetch });
|
||||
console.log('🍵 green-tea running on http://localhost:8000');
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"experimentalDecorators": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1 @@
|
||||
runtime = "node"
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "{{project_name}}",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": { "dev": "tsx watch src/main.ts" },
|
||||
"dependencies": { "@green-tea/core": "^26.7.0-beta.0" },
|
||||
"devDependencies": { "tsx": "^4", "typescript": "^5" }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { createApp } from '@green-tea/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
const app = createApp({ modules: [AppModule] });
|
||||
|
||||
app.listen(3000);
|
||||
console.log('🍵 green-tea running on http://localhost:3000');
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2020",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"experimentalDecorators": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -16,6 +16,50 @@ fn new_scaffolds_into_named_dir() {
|
||||
assert!(root.join("public/index.html").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_default_runtime_is_deno() {
|
||||
let d = tempdir().unwrap();
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "my-api"])
|
||||
.assert()
|
||||
.success();
|
||||
let root = d.path().join("my-api");
|
||||
assert!(root.join("deno.json").exists());
|
||||
assert!(!root.join("package.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_with_node_runtime_produces_package_json() {
|
||||
let d = tempdir().unwrap();
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "demo", "--runtime", "node"])
|
||||
.assert()
|
||||
.success();
|
||||
let root = d.path().join("demo");
|
||||
let pkg = std::fs::read_to_string(root.join("package.json")).unwrap();
|
||||
assert!(pkg.contains("\"name\": \"demo\""));
|
||||
assert!(!root.join("deno.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_with_bun_runtime_produces_package_json() {
|
||||
let d = tempdir().unwrap();
|
||||
Command::cargo_bin("matcha")
|
||||
.unwrap()
|
||||
.current_dir(d.path())
|
||||
.args(["new", "demo-bun", "--runtime", "bun"])
|
||||
.assert()
|
||||
.success();
|
||||
let root = d.path().join("demo-bun");
|
||||
let pkg = std::fs::read_to_string(root.join("package.json")).unwrap();
|
||||
assert!(pkg.contains("\"name\": \"demo-bun\""));
|
||||
assert!(!root.join("deno.json").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_refuses_existing_dir() {
|
||||
let d = tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user