Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Add "bundle" type
Browse files Browse the repository at this point in the history
Not sure *exactly* what this does but i basically just copied the
parts i thought were relevant from the site code. We have a src dir,
which is what needs to be watched, and an output dir, which is what
needs to be analyzed, and a build command, which is what needs to be
run when we wanna run the build.
  • Loading branch information
caass committed Sep 24, 2020
1 parent efd3a44 commit 5f41321
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/settings/toml/bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

use serde::{Deserialize, Serialize};

const OUTPUT_DIR: &str = "dist";
const SRC_DIR: &str = "src";

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Bundle {
#[serde(rename = "build-command")]
build_command: Option<String>,
#[serde(rename = "output-dir")]
output_dir: Option<PathBuf>,
#[serde(rename = "src-dir")]
src_dir: Option<PathBuf>,
}

impl Bundle {
fn dir_or_default(dir: &Option<PathBuf>, default: &str) -> PathBuf {
match dir {
Some(path) => path.to_owned(),
None => PathBuf::from(default),
}
}

pub fn output_dir(&self) -> Result<PathBuf, std::io::Error> {
let current_dir = env::current_dir()?;
let output_dir = current_dir.join(Bundle::dir_or_default(&self.output_dir, OUTPUT_DIR));
Ok(output_dir)
}

pub fn src_dir(&self) -> Result<PathBuf, std::io::Error> {
let current_dir = env::current_dir()?;
let src_dir = current_dir.join(Bundle::dir_or_default(&self.src_dir, SRC_DIR));
Ok(src_dir)
}

pub fn build_command(&self) -> Command {
let args_string = match &self.build_command {
Some(cmd) => cmd.to_owned(),
None => "npm run build".to_string(),
};
let args: Vec<&str> = args_string.split_whitespace().collect();

let command = if cfg!(target_os = "windows") {
let mut c = Command::new("cmd");
c.arg("/C").args(args.as_slice());
c
} else {
let mut c = Command::new(args[0]);
if args.len() > 1 {
c.args(&args[1..]);
}
c
};

command
}
}
2 changes: 2 additions & 0 deletions src/settings/toml/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bundle;
mod deploy_config;
mod dev;
mod environment;
Expand All @@ -8,6 +9,7 @@ mod site;
mod target;
mod target_type;

pub use bundle::Bundle;
pub use deploy_config::{DeployConfig, Zoned, Zoneless};
pub use environment::Environment;
pub use kv_namespace::{ConfigKvNamespace, KvNamespace};
Expand Down

0 comments on commit 5f41321

Please sign in to comment.