use std::io::{self, BufRead, Error, ErrorKind, Write};
use std::path::{Component, Path};
use crate::ask::Ask;
use crate::{entry, naming, plugin_files, wire};
pub struct Opts {
pub name: Option<String>,
pub package: Option<Option<String>>,
pub folder: Option<String>,
pub scope: Option<String>,
pub registry: Option<String>,
pub npm_name: Option<String>,
pub check: bool,
}
pub fn run(opts: Opts) -> io::Result<()> {
run_with(opts, &mut Ask::stdio(), Path::new("."))
}
pub fn run_with<R: BufRead, W: Write>(
opts: Opts,
ask: &mut Ask<R, W>,
root: &Path,
) -> io::Result<()> {
if let Some(name) = &opts.name {
check_name(name)?;
}
let package = match opts.package {
Some(_) => true,
None => ask.yes_no("Separate it as a package?", false)?,
};
if package {
for (flag, given) in [("--folder", opts.folder.is_some()), ("--check", opts.check)] {
if given {
println!("note: {flag} only applies to an in-app plugin; ignored");
}
}
package_mode(opts, ask, root)
} else {
for (flag, given) in [
("--scope", opts.scope.is_some()),
("--registry", opts.registry.is_some()),
("--npm-name", opts.npm_name.is_some()),
] {
if given {
println!("note: {flag} only applies to --package; ignored");
}
}
in_app(opts, ask, root)
}
}
fn invalid(msg: String) -> Error {
Error::new(ErrorKind::InvalidInput, msg)
}
fn check_name(raw: &str) -> io::Result<String> {
let slug = naming::slug(raw).ok_or_else(|| {
invalid(format!(
"'{raw}' does not make a plugin name: start with a letter, and use letters, digits, spaces or hyphens"
))
})?;
let fun = naming::camel(&slug);
if naming::is_reserved(&fun) {
return Err(invalid(format!(
"'{raw}' would make the factory `{fun}`, which is a reserved word in TypeScript; pick another name"
)));
}
Ok(slug)
}
fn resolve_slug<R: BufRead, W: Write>(
name: Option<String>,
ask: &mut Ask<R, W>,
) -> io::Result<String> {
let raw = match name {
Some(n) => n,
None => ask.text("Plugin name?", None, "the NAME argument")?,
};
check_name(&raw)
}
fn inside_project(folder: &str) -> bool {
!folder.is_empty()
&& Path::new(folder)
.components()
.all(|c| matches!(c, Component::Normal(_) | Component::CurDir))
}
fn in_app<R: BufRead, W: Write>(opts: Opts, ask: &mut Ask<R, W>, root: &Path) -> io::Result<()> {
let Some(entry_path) = entry::find(root) else {
return Err(invalid(format!(
"an in-app plugin is wired into createApp, and there is no {} here; run this in a project, or pass --package for a package of its own",
entry::candidates()
)));
};
let folder = match opts.folder {
Some(f) => f,
None => ask.text("Folder?", Some("plugins"), "--folder")?,
};
if !inside_project(&folder) {
return Err(invalid(format!(
"the plugin folder has to be inside the project, got '{folder}'"
)));
}
let folder: Vec<String> = Path::new(&folder)
.components()
.filter_map(|c| match c {
Component::Normal(p) => Some(p.to_string_lossy().into_owned()),
_ => None,
})
.collect();
let folder = folder.join("/");
let slug = resolve_slug(opts.name, ask)?;
let fun = naming::camel(&slug);
let src = std::fs::read_to_string(&entry_path)?;
let entry_shown = entry_path
.strip_prefix(root)
.unwrap_or(&entry_path)
.display()
.to_string();
if wire::binds(&src, &fun) {
return Err(invalid(format!(
"{entry_shown} already uses `{fun}`, so the plugin's factory would collide with it; pick another name"
)));
}
let shown = Path::new(&folder).join(&slug);
let dir = root.join(&shown);
if dir.exists() {
return Err(Error::new(
ErrorKind::AlreadyExists,
format!("{} already exists", shown.display()),
));
}
let call = format!("{fun}()");
let from = if folder.is_empty() {
format!("../{slug}/index")
} else {
format!("../{folder}/{slug}/index")
};
let wired = wire::add_to_createapp(&wire::add_import(&src, &fun, &from), "plugins", &call);
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("index.ts"), plugin_files::factory(&slug))?;
println!("✓ {}", shown.join("index.ts").display());
match wired {
Some(wired) => {
if let Err(e) = std::fs::write(&entry_path, wired) {
let _ = std::fs::remove_dir_all(&dir);
return Err(Error::new(
e.kind(),
format!(
"could not write {entry_shown} ({e}); removed {} again",
shown.display()
),
));
}
println!("✓ registered {call} in createApp");
}
None => println!(
"→ could not auto-register; add {call} to createApp plugins[] in {entry_shown}, and import {{ {fun} }} from '{from}'"
),
}
crate::cmd_create::maybe_check(opts.check)
}
const EMPTY_DIR: &str = "a plugin package needs an empty directory, so the package stays self-contained — pass --package=<dir> or run it in an empty folder";
const NPM_CONVENTION: &str =
"green-tea-<x> is the naming convention; the plugin listing uses it to find green-tea plugins.";
fn valid_scope(s: &str) -> bool {
(2..=20).contains(&s.len())
&& !s.starts_with('-')
&& s.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}
fn write_all(dir: &Path, root: &Path, files: &[(String, String)]) -> io::Result<()> {
let mut written: Vec<&str> = Vec::new();
for (rel, content) in files {
let path = dir.join(rel);
let result = std::fs::create_dir_all(path.parent().unwrap())
.and_then(|()| std::fs::write(&path, content));
if let Err(e) = result {
let so_far = if written.is_empty() {
"none".to_string()
} else {
written.join(", ")
};
return Err(Error::new(
e.kind(),
format!("could not write {rel} ({e}); already written: {so_far}"),
));
}
println!("✓ {}", path.strip_prefix(root).unwrap_or(&path).display());
written.push(rel);
}
Ok(())
}
fn valid_npm_name(s: &str) -> bool {
let part = |p: &str| {
!p.is_empty()
&& !p.starts_with(['.', '_'])
&& p.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || "-._~".contains(c))
};
match s.strip_prefix('@').and_then(|rest| rest.split_once('/')) {
Some((scope, name)) => part(scope) && part(name),
None => !s.starts_with('@') && part(s),
}
}
fn package_mode<R: BufRead, W: Write>(
opts: Opts,
ask: &mut Ask<R, W>,
root: &Path,
) -> io::Result<()> {
let dir = match &opts.package {
Some(Some(d)) => root.join(d),
_ => root.to_path_buf(),
};
let dir_shown = dir.strip_prefix(root).unwrap_or(&dir).display().to_string();
let dir_shown = if dir_shown.is_empty() {
".".to_string()
} else {
dir_shown
};
if dir.is_file() {
return Err(invalid(format!("{dir_shown} is a file; {EMPTY_DIR}")));
}
if dir.exists() && std::fs::read_dir(&dir)?.next().is_some() {
return Err(invalid(format!("{dir_shown}: {EMPTY_DIR}")));
}
let slug = resolve_slug(opts.name, ask)?;
let scope = match opts.scope {
Some(s) => s,
None => ask.text("JSR scope?", None, "--scope")?,
};
let scope = scope.trim_start_matches('@').to_string();
if !valid_scope(&scope) {
return Err(invalid(format!(
"'{scope}' is not a JSR scope: 2 to 20 lowercase letters, digits or hyphens, not starting with a hyphen"
)));
}
let registry = match opts.registry {
Some(r) => r,
None => {
ask.say(&format!("\n{}\n", plugin_files::JSR_BANNER))?;
ask.choice(
"Publish to: (1) JSR only [default] (2) JSR + npm",
&[("1", "jsr"), ("2", "both")],
"jsr",
"--registry",
)?
.to_string()
}
};
let npm = if registry == "both" {
let suggested = format!("@{scope}/green-tea-{slug}");
let name = match opts.npm_name {
Some(n) => n,
None => ask.text("npm package name?", Some(&suggested), "--npm-name")?,
};
if !valid_npm_name(&name) {
return Err(invalid(format!("'{name}' is not a valid npm name")));
}
let unscoped = name.rsplit('/').next().unwrap_or(&name);
if !unscoped.starts_with("green-tea-") {
println!("note: {NPM_CONVENTION}");
}
Some(name)
} else {
None
};
let package = plugin_files::Package { slug, scope, npm };
write_all(&dir, root, &plugin_files::package(&package))?;
println!(
"→ next: fill in the Runtimes table in README.md, then `deno test` and `deno publish`"
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
fn opts(name: Option<&str>) -> Opts {
Opts {
name: name.map(str::to_string),
package: None,
folder: None,
scope: None,
registry: None,
npm_name: None,
check: false,
}
}
#[test]
fn a_bad_name_fails_before_any_question() {
let mut ask = Ask::new(true, Cursor::new(Vec::new()), Vec::new());
let err = run_with(opts(Some("🍵")), &mut ask, Path::new("/nonexistent")).unwrap_err();
assert!(
err.to_string().contains("does not make a plugin name"),
"{err}"
);
}
#[test]
fn write_all_reports_what_it_wrote_before_failing() {
let d = tempfile::tempdir().unwrap();
let files = vec![
("a".to_string(), "x".to_string()),
("a/b".to_string(), "y".to_string()),
];
let err = write_all(d.path(), d.path(), &files).unwrap_err();
assert!(err.to_string().contains("already written: a"), "{err}");
}
}