Skip to content

Commit 507e9b6

Browse files
authored
[pgml-components] Add template-only option & remove boilerplate (#1403)
1 parent 209fd5b commit 507e9b6

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

packages/cargo-pgml-components/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cargo-pgml-components/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-pgml-components"
3-
version = "0.1.24"
3+
version = "0.1.25"
44
edition = "2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license = "MIT"

packages/cargo-pgml-components/src/frontend/components.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl From<&Path> for Component {
8686
}
8787

8888
/// Add a new component.
89-
pub fn add(path: &Path, overwrite: bool) {
89+
pub fn add(path: &Path, overwrite: bool, template_only: bool) {
9090
if let Some(_extension) = path.extension() {
9191
error("component name should not contain an extension");
9292
exit(1);
@@ -154,17 +154,21 @@ pub fn add(path: &Path, overwrite: bool) {
154154
unwrap_or_exit!(write_to_file(&html_path, &html));
155155
info(&format!("written {}", html_path.display()));
156156

157-
let stimulus_path = path.join(&component.controller_path());
158-
unwrap_or_exit!(write_to_file(&stimulus_path, &stimulus));
159-
info(&format!("written {}", stimulus_path.display()));
157+
if !template_only {
158+
let stimulus_path = path.join(&component.controller_path());
159+
unwrap_or_exit!(write_to_file(&stimulus_path, &stimulus));
160+
info(&format!("written {}", stimulus_path.display()));
161+
}
160162

161163
let rust_path = path.join("mod.rs");
162164
unwrap_or_exit!(write_to_file(&rust_path, &rust));
163165
info(&format!("written {}", rust_path.display()));
164166

165-
let scss_path = path.join(&format!("{}.scss", component.name()));
166-
unwrap_or_exit!(write_to_file(&scss_path, &scss));
167-
info(&format!("written {}", scss_path.display()));
167+
if !template_only {
168+
let scss_path = path.join(&format!("{}.scss", component.name()));
169+
unwrap_or_exit!(write_to_file(&scss_path, &scss));
170+
info(&format!("written {}", scss_path.display()));
171+
}
168172

169173
update_modules();
170174
}

packages/cargo-pgml-components/src/frontend/templates/component.rs.tpl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ use pgml_components::component;
33

44
#[derive(TemplateOnce, Default)]
55
#[template(path = "<%= component.path() %>/template.html")]
6-
pub struct <%= component.rust_name() %> {
7-
value: String,
8-
}
6+
pub struct <%= component.rust_name() %> {}
97

108
impl <%= component.rust_name() %> {
119
pub fn new() -> <%= component.rust_name() %> {
12-
<%= component.rust_name() %> {
13-
value: String::from("<%= component.full_path() %>"),
14-
}
10+
<%= component.rust_name() %> {}
1511
}
1612
}
1713

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
11
div[data-controller="<%= component.controller_name() %>"] {
2-
// Used to identify the component in the DOM.
3-
// Delete these styles if you don't need them.
4-
min-width: 100px;
5-
width: 100%;
6-
height: 100px;
72
8-
background: red;
9-
10-
display: flex;
11-
justify-content: center;
12-
align-items: center;
13-
14-
h3 {
15-
color: white;
16-
}
173
}

packages/cargo-pgml-components/src/frontend/templates/stimulus.js.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Controller } from '@hotwired/stimulus'
22

33
export default class extends Controller {
4-
static targets = []
5-
static outlets = []
4+
static targets = [];
5+
static outlets = [];
66
77
initialize() {
8-
console.log('Initialized <%= controller_name %>')
8+
console.log("Initialized <%= controller_name %>");
99
}
1010

1111
connect() {}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<div data-controller="<%= component.controller_name() %>">
2-
<h3 class="text-center h3">
3-
<%%= value %>
4-
</h3>
2+
53
</div>

packages/cargo-pgml-components/src/main.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@ enum Commands {
8989
#[derive(Subcommand, Debug)]
9090
enum AddCommands {
9191
/// Add a new component.
92-
Component { name: String },
92+
Component {
93+
/// Name of the new component.
94+
name: String,
95+
96+
/// Generate only the HTML template. Don't generate SCSS and JavaScript.
97+
#[arg(short, long, default_value = "false")]
98+
template_only: bool,
99+
},
93100
}
94101

95102
#[derive(Subcommand, Debug)]
@@ -114,9 +121,14 @@ fn main() {
114121
lock,
115122
} => bundle(config, minify, debug, lock),
116123
Commands::Add(command) => match command {
117-
AddCommands::Component { name } => {
118-
crate::frontend::components::add(&Path::new(&name), pgml_commands.overwrite)
119-
}
124+
AddCommands::Component {
125+
name,
126+
template_only,
127+
} => crate::frontend::components::add(
128+
&Path::new(&name),
129+
pgml_commands.overwrite,
130+
template_only,
131+
),
120132
},
121133
Commands::LocalDev(command) => match command {
122134
LocalDevCommands::Check {} => local_dev::setup(),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy