Skip to content

Commit 5677e41

Browse files
committed
Emit warnings about invalid FS path references found in HTML.
Also removed the use of emojis in output for now. Next PR will tackle improving CLI output overall. closes #18 related to #1
1 parent 644f4c5 commit 5677e41

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

src/build.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl BuildSystem {
154154
if self.release {
155155
args.push("--release");
156156
}
157-
println!("📦 starting cargo build on {}", &self.manifest.package.name); // TODO: pin down logging.
157+
println!("starting cargo build on {}", &self.manifest.package.name); // TODO: pin down logging.
158158
let app_target_wasm = self.app_target_wasm.clone();
159159
spawn(async move {
160160
// Spawn the cargo build process.
@@ -187,7 +187,7 @@ impl BuildSystem {
187187
fn spawn_wasm_bindgen_build(&self, file_name: String) -> JoinHandle<Result<WasmBindgenOutput>> {
188188
let (dist, bindgen_out, app_target_wasm) = (self.dist.clone(), self.bindgen_out.clone(), self.app_target_wasm.clone());
189189

190-
println!("📦 starting wasm-bindgen build"); // TODO: pin down logging.
190+
println!("starting wasm-bindgen build"); // TODO: pin down logging.
191191
spawn(async move {
192192
let arg_out_path = format!("--out-dir={}", bindgen_out.display());
193193
let arg_out_name = format!("--out-name={}", &file_name);
@@ -232,10 +232,10 @@ impl BuildSystem {
232232
/// for the asset is finished, it will be able to update the DOM correctly based on its own
233233
/// ID. All of these trunk specific IDs will be removed from the DOM before it is written.
234234
async fn spawn_asset_pipelines(&mut self, target_html: &mut Document) -> Result<()> {
235-
println!("📦 spawning asset pipelines");
235+
println!("spawning asset pipelines");
236236

237-
// Accumulate stylesheet assets to be processed.
238-
let style_assets = target_html.select(r#"html head link"#)
237+
// Accumulate assets declared in HTML head section links for processing.
238+
let asset_links = target_html.select(r#"html head link"#)
239239
.iter()
240240
.filter_map(|node| {
241241
// Be sure our link has an href to process, else skip.
@@ -245,24 +245,26 @@ impl BuildSystem {
245245
};
246246
Some((node, href))
247247
})
248-
.enumerate()
249-
.fold(vec![], |mut acc, (idx, (mut node, href))| {
250-
// Take the path to referenced resource, if it is a valid asset, then we continue.
251-
let path = self.target_html_dir.join(href.as_ref());
252-
let rel = node.attr_or("rel", "").to_string().to_lowercase();
253-
let id = format!("link-{}", idx);
254-
let asset = match AssetFile::new(path, AssetType::Link{rel}, id) {
255-
Ok(asset) => asset,
256-
Err(_) => return acc,
257-
};
258-
// Update the DOM with an ID for async processing.
259-
node.set_attr(TRUNK_ID, &asset.id);
260-
acc.push(asset);
261-
acc
262-
});
248+
.enumerate();
249+
250+
// Update the DOM for each extracted asset as long as it is a valid FS path.
251+
let mut assets = vec![];
252+
for (idx, (mut node, href)) in asset_links {
253+
// Take the path to referenced resource, if it is a valid asset, then we continue.
254+
let path = self.target_html_dir.join(href.as_ref());
255+
let rel = node.attr_or("rel", "").to_string().to_lowercase();
256+
let id = format!("link-{}", idx);
257+
let asset = match AssetFile::new(path, AssetType::Link{rel}, id).await {
258+
Ok(asset) => asset,
259+
Err(_) => continue,
260+
};
261+
// Update the DOM with an ID for async processing.
262+
node.set_attr(TRUNK_ID, &asset.id);
263+
assets.push(asset);
264+
}
263265

264266
// Route assets over to the appropriate pipeline handler.
265-
for asset in style_assets {
267+
for asset in assets {
266268
self.spawn_asset_bundle(asset).await?;
267269
}
268270
Ok(())
@@ -375,10 +377,18 @@ impl AssetFile {
375377
///
376378
/// Any errors returned from this constructor indicate that one of these invariants was not
377379
/// upheld.
378-
pub fn new(path: PathBuf, atype: AssetType, id: String) -> Result<Self> {
380+
pub async fn new(path: PathBuf, atype: AssetType, id: String) -> Result<Self> {
379381
// Take the path to referenced resource, if it is actually an FS path, then we continue.
380-
let path = path.canonicalize()?;
381-
ensure!(path.is_file(), "target file does not exist on the FS");
382+
let path = match fs::canonicalize(&path).await {
383+
Ok(path) => path,
384+
Err(_) => {
385+
if !path.to_string_lossy().contains("://") {
386+
eprintln!("skipping invalid path: {}", path.to_string_lossy());
387+
}
388+
return Err(anyhow!("skipping asset which is not a valid path"));
389+
}
390+
};
391+
ensure!(path.is_file().await, "target file does not exist on the FS");
382392
let file_name = match path.file_name() {
383393
Some(file_name) => file_name.to_owned(),
384394
None => bail!("asset has no file name"),
@@ -391,7 +401,7 @@ impl AssetFile {
391401
Some(ext) => ext.to_string_lossy().to_lowercase(),
392402
None => bail!("asset has no file extension"),
393403
};
394-
Ok(Self{path, file_name, file_stem, ext, atype, id})
404+
Ok(Self{path: path.into(), file_name, file_stem, ext, atype, id})
395405
}
396406
}
397407

src/cmd/serve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Serve {
7575
app.at(&self.public_url).serve_dir(self.dist.to_string_lossy().as_ref())?;
7676

7777
// Listen and serve.
78-
println!("server running at {}", &http_addr);
78+
println!("Server running at {}", &http_addr);
7979
Ok(spawn(async move {
8080
if let Err(err) = app.listen(listen_addr).await {
8181
eprintln!("{}", err);

0 commit comments

Comments
 (0)