Skip to content

Commit 3949533

Browse files
authored
fix(templates/angular): add missing -- for npm, closes #411 (#412)
* fix(templates/angular): add missing `--` for npm, closes #411 * add missing tests * clippy * clippy * fix build on 1.59
1 parent a3f3244 commit 3949533

7 files changed

Lines changed: 296 additions & 36 deletions

File tree

.changes/angular-analytics.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"create-tauri-app": "patch"
3+
"create-tauri-app-js": "patch"
4+
---
5+
6+
Disable analytics for Angular template

.changes/angular-before-dev.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"create-tauri-app": "patch"
3+
"create-tauri-app-js": "patch"
4+
---
5+
6+
Fix `beforeDevCommand` missing `--` for npm + Angular template

packages/cli/fragments/fragment-angular/_cta_manifest_

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# SPDX-License-Identifier: Apache-2.0
33
# SPDX-License-Identifier: MIT
44

5-
beforeDevCommand = {{pkg_manager_run_command}} start --port 1420
5+
beforeDevCommand = {{pkg_manager_run_command}} start{{double-dash}} --port 1420
66
beforeBuildCommand = {{pkg_manager_run_command}} build
77
devPath = http://localhost:1420
88
distDir = ../dist/{{package_name}}
99

1010
[mobile]
11-
beforeDevCommand = {{pkg_manager_run_command}} start --port 1420 --host $HOST --public-host $HOST
11+
beforeDevCommand = {{pkg_manager_run_command}} start{{double-dash}} --port 1420 --host $HOST --public-host $HOST
1212

1313
[files]
1414
tauri.svg = src/assets/tauri.svg

packages/cli/fragments/fragment-angular/angular.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
33
"version": 1,
44
"newProjectRoot": "projects",
5+
"cli": {
6+
"analytics": false
7+
},
58
"projects": {
69
"{{package_name}}": {
710
"root": "",

packages/cli/src/deps.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn is_webview2_installed() -> bool {
6767
.arg("Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\WOW6432Node\\Microsoft\\EdgeUpdate\\Clients\\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' | ForEach-Object {$_.pv}")
6868
.output().map(|o|o.status.success());
6969
if let Ok(o) = output {
70-
if o == true {
70+
if o {
7171
return true;
7272
}
7373
}
@@ -77,7 +77,7 @@ fn is_webview2_installed() -> bool {
7777
.arg("Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\EdgeUpdate\\Clients\\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' | ForEach-Object {$_.pv}")
7878
.output().map(|o|o.status.success());
7979
if let Ok(o) = output {
80-
if o == true {
80+
if o {
8181
return true;
8282
}
8383
}
@@ -87,7 +87,7 @@ fn is_webview2_installed() -> bool {
8787
.arg("Get-ItemProperty -Path 'HKCU:\\SOFTWARE\\Microsoft\\EdgeUpdate\\Clients\\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' | ForEach-Object {$_.pv}")
8888
.output().map(|o|o.status.success());
8989
if let Ok(o) = output {
90-
if o == true {
90+
if o {
9191
return true;
9292
}
9393
}

packages/cli/src/manifest.rs

Lines changed: 178 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ use std::collections::HashMap;
22

33
use anyhow::{bail, Context};
44

5-
#[derive(Default, Clone)]
5+
#[derive(Default, Clone, PartialEq, Eq, Debug)]
66
pub struct Manifest<'a> {
77
pub before_dev_command: Option<&'a str>,
88
pub before_build_command: Option<&'a str>,
99
pub dev_path: Option<&'a str>,
1010
pub dist_dir: Option<&'a str>,
11-
pub with_global_tauri: bool,
11+
pub with_global_tauri: Option<bool>,
1212
pub files: HashMap<&'a str, &'a str>,
1313
}
1414

1515
impl<'a> Manifest<'a> {
1616
pub fn parse(s: &'a str, mobile: bool) -> Result<Self, anyhow::Error> {
17-
let mut manifest = Manifest::default();
17+
let mut manifest = Self::default();
18+
1819
let mut in_files_section = false;
1920
let mut in_mobile_section = false;
21+
2022
for (i, line) in s.split('\n').enumerate() {
2123
let line_number = i + 1;
2224

@@ -71,7 +73,7 @@ impl<'a> Manifest<'a> {
7173
"beforeBuildCommand" if replace => manifest.before_build_command = Some(v),
7274
"devPath" if replace => manifest.dev_path = Some(v),
7375
"distDir" if replace => manifest.dist_dir = Some(v),
74-
"withGlobalTauri" if replace => manifest.with_global_tauri = v.parse()?,
76+
"withGlobalTauri" if replace => manifest.with_global_tauri = Some(v.parse()?),
7577
_ if in_files_section => {
7678
manifest.files.insert(k, v);
7779
}
@@ -81,4 +83,176 @@ impl<'a> Manifest<'a> {
8183
}
8284
Ok(manifest)
8385
}
86+
87+
pub fn replace_vars(&self, content: &str) -> String {
88+
content
89+
.replace(
90+
"{{fragment_before_dev_command}}",
91+
self.before_dev_command.unwrap_or_default(),
92+
)
93+
.replace(
94+
"{{fragment_before_build_command}}",
95+
self.before_build_command.unwrap_or_default(),
96+
)
97+
.replace("{{fragment_dev_path}}", self.dev_path.unwrap_or_default())
98+
.replace("{{fragment_dist_dir}}", self.dist_dir.unwrap_or_default())
99+
.replace(
100+
r#""withGlobalTauri": "{{fragment_with_global_tauri}}""#,
101+
&format!(
102+
r#""withGlobalTauri": {}"#,
103+
self.with_global_tauri.unwrap_or(false)
104+
),
105+
)
106+
}
107+
}
108+
109+
#[cfg(test)]
110+
mod test {
111+
use super::*;
112+
113+
#[test]
114+
fn it_parses() {
115+
let manifest_file = r#"
116+
# Copyright 2019-2022 Tauri Programme within The Commons Conservancy
117+
# SPDX-License-Identifier: Apache-2.0
118+
# SPDX-License-Identifier: MIT
119+
120+
beforeDevCommand = npm start -- --port 1420
121+
beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
122+
devPath = http://localhost:1420
123+
124+
[mobile]
125+
beforeBuildCommand = {{pkg_manager_run_command}} build mobile
126+
127+
[files]
128+
tauri.svg = src/assets/tauri.svg
129+
styles.css = src/styles.css
130+
"#;
131+
132+
assert_eq!(Manifest::parse(manifest_file, false).unwrap(), {
133+
let mut files = HashMap::new();
134+
files.insert("tauri.svg", "src/assets/tauri.svg");
135+
files.insert("styles.css", "src/styles.css");
136+
137+
Manifest {
138+
before_dev_command: Some("npm start -- --port 1420"),
139+
before_build_command: Some("{{pkg_manager_run_command}} build"),
140+
dev_path: Some("http://localhost:1420"),
141+
dist_dir: None,
142+
with_global_tauri: None,
143+
files,
144+
}
145+
});
146+
147+
assert_eq!(Manifest::parse(manifest_file, true).unwrap(), {
148+
let mut files = HashMap::new();
149+
files.insert("tauri.svg", "src/assets/tauri.svg");
150+
files.insert("styles.css", "src/styles.css");
151+
152+
Manifest {
153+
before_dev_command: Some("npm start -- --port 1420"),
154+
before_build_command: Some("{{pkg_manager_run_command}} build mobile"),
155+
dev_path: Some("http://localhost:1420"),
156+
dist_dir: None,
157+
with_global_tauri: None,
158+
files,
159+
}
160+
});
161+
}
162+
163+
#[test]
164+
#[should_panic]
165+
fn it_panics_while_parsing() {
166+
let manifest_file = r#"
167+
# Copyright 2019-2022 Tauri Programme within The Commons Conservancy
168+
# SPDX-License-Identifier: Apache-2.0
169+
# SPDX-License-Identifier: MIT
170+
171+
beforeDevCommand = npm start -- --port 1420
172+
beforeBuildCommand =
173+
devPath = http://localhost:1420
174+
175+
[mobile]
176+
beforeBuildCommand = {{pkg_manager_run_command}} build mobile
177+
178+
[files]
179+
tauri.svg = src/assets/tauri.svg
180+
styles.css = src/styles.css
181+
"#;
182+
183+
Manifest::parse(manifest_file, false).unwrap();
184+
}
185+
186+
#[test]
187+
fn later_should_override_former() {
188+
let manifest_file = r#"
189+
# Copyright 2019-2022 Tauri Programme within The Commons Conservancy
190+
# SPDX-License-Identifier: Apache-2.0
191+
# SPDX-License-Identifier: MIT
192+
193+
beforeDevCommand = npm start -- --port 1420
194+
beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
195+
devPath = http://localhost:1420
196+
beforeBuildCommand = {{pkg_manager_run_command}} build mobile
197+
198+
[files]
199+
tauri.svg = src/assets/tauri.svg
200+
styles.css = src/styles.css
201+
"#;
202+
203+
assert_eq!(Manifest::parse(manifest_file, false).unwrap(), {
204+
let mut files = HashMap::new();
205+
files.insert("tauri.svg", "src/assets/tauri.svg");
206+
files.insert("styles.css", "src/styles.css");
207+
208+
Manifest {
209+
before_dev_command: Some("npm start -- --port 1420"),
210+
before_build_command: Some("{{pkg_manager_run_command}} build mobile"),
211+
dev_path: Some("http://localhost:1420"),
212+
dist_dir: None,
213+
with_global_tauri: None,
214+
files,
215+
}
216+
});
217+
}
218+
219+
#[test]
220+
fn it_replaces_vars() {
221+
let manifest_file = r#"
222+
# Copyright 2019-2022 Tauri Programme within The Commons Conservancy
223+
# SPDX-License-Identifier: Apache-2.0
224+
# SPDX-License-Identifier: MIT
225+
226+
beforeDevCommand = npm start -- --port 1420
227+
beforeBuildCommand = {{pkg_manager_run_command}} build # this comment should be stripped
228+
devPath = http://localhost:1420
229+
230+
[files]
231+
tauri.svg = src/assets/tauri.svg
232+
styles.css = src/styles.css
233+
"#;
234+
235+
let manifest = Manifest::parse(manifest_file, false).unwrap();
236+
237+
let content = r#"{
238+
"build": {
239+
"beforeDevCommand": "{{fragment_before_dev_command}}",
240+
"beforeBuildCommand": "{{fragment_before_build_command}}",
241+
"devPath": "{{fragment_dev_path}}",
242+
"distDir": "{{fragment_dist_dir}}"
243+
},
244+
}"#;
245+
assert_eq!(
246+
manifest.replace_vars(content).as_str(),
247+
r#"{
248+
"build": {
249+
"beforeDevCommand": "npm start -- --port 1420",
250+
"beforeBuildCommand": "{{pkg_manager_run_command}} build",
251+
"devPath": "http://localhost:1420",
252+
"distDir": ""
253+
},
254+
}"#
255+
.to_string()
256+
)
257+
}
84258
}

0 commit comments

Comments
 (0)