Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: monorepo support #34

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ path = "src/main.rs"
[dependencies]
anyhow = { version = "1.0.89" }
clap = { version = "4.5.20", features = ["cargo"] }
compact_str = { version = "0.8.0" }
glob = { version = "0.3.1" }
ignore = { version = "0.4.23" }
json-strip-comments = { version = "1.0.4" }
log = { version = "0.4.22" }
Expand All @@ -28,6 +30,9 @@ pretty_env_logger = { version = "0.5.0" }
serde = { version = "1.0.210" }
serde_json = { version = "1.0.128" }
static_assertions = { version = "1.1.0" }
thiserror = { version = "1.0.64" }
# use our own fork
tsconfig = { git = "https://github.com/DonIsaac/tsconfig" }

[lints.clippy]
all = { level = "warn", priority = -1 }
Expand Down
3 changes: 1 addition & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
mod error;
mod root;

use std::{env, num::NonZeroUsize, path::PathBuf};

use clap::{self, command, Arg, ArgMatches, ValueHint};
use miette::{Context, IntoDiagnostic, Result};

pub(crate) use root::Root;
use crate::Root;

pub fn cli() -> ArgMatches {
command!()
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod compiler;
mod options;
mod reporter;
mod walk;
mod workspace;

use std::{process::ExitCode, thread, time::Instant};

Expand All @@ -15,13 +16,14 @@ use crate::{
cli::{cli, CliOptions},
options::OxbuildOptions,
reporter::{DiagnosticSender, Reporter},
workspace::{Manifest, Root},
};

#[allow(clippy::print_stdout)]
fn main() -> Result<ExitCode> {
pretty_env_logger::init();
let matches = cli();
let opts = CliOptions::new(matches).and_then(OxbuildOptions::new)?;
let cli_args = CliOptions::new(cli())?;
let opts = OxbuildOptions::new(cli_args)?;
let num_threads = opts.num_threads.get();

let (mut reporter, report_sender) = Reporter::new();
Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cli::{CliOptions, Root};
use crate::{cli::CliOptions, Root};
use std::{
fs::{self},
num::NonZeroUsize,
Expand Down
69 changes: 69 additions & 0 deletions src/workspace/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{fs, path::PathBuf};

use miette::{Context as _, IntoDiagnostic as _, Result};
use package_json::PackageJson;
use tsconfig::TsConfig;

use super::workspace_globs::Workspaces;

/// A package manifest.
///
/// May be a single package, a package in a monorepo, or the monorepo root itself.
pub struct Manifest {
/// absolute
dir: PathBuf,
package_json: PackageJson,
tsconfig: Option<TsConfig>,
workspaces: Option<Workspaces>,
}

impl Manifest {
pub fn new(package_json_path: PathBuf, tsconfig: Option<TsConfig>) -> Result<Self> {
assert!(
package_json_path.is_absolute(),
"package.json paths must be absolute"
);
assert!(
package_json_path
.file_name()
.is_some_and(|p| p == "package.json"),
"Manifest received path to non-package.json: {}",
package_json_path.display()
);
if !package_json_path.is_file() {
return Err(miette::Report::msg(format!(
"package.json at {} does not exist",
package_json_path.display()
)));
}
let package_folder = package_json_path.parent().unwrap().to_path_buf();
let package_json_raw = fs::read_to_string(&package_json_path)
.into_diagnostic()
.with_context(|| {
format!(
"Failed to read package.json at {}",
package_json_path.display()
)
})?;
let package_json: PackageJson = serde_json::from_str(&package_json_raw)
.into_diagnostic()
.with_context(|| {
format!(
"Failed to parse package.json at {}",
package_json_path.display()
)
})?;

let workspaces = package_json
.workspaces
.as_ref()
.map(|workspaces| Workspaces::from_iter(workspaces));

Ok(Self {
dir: package_folder,
package_json,
tsconfig,
workspaces,
})
}
}
6 changes: 6 additions & 0 deletions src/workspace/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod manifest;
mod root;
mod workspace_globs;

pub use manifest::Manifest;
pub use root::Root;
2 changes: 1 addition & 1 deletion src/cli/root.rs → src/workspace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use miette::{IntoDiagnostic, Report, Result, WrapErr};
use package_json::PackageJsonManager;

#[derive(Debug)]
pub(crate) struct Root {
pub struct Root {
/// Current working directory from where oxbuild was run.
cwd: PathBuf,
/// Path to directory containing nearest `package.json` file.
Expand Down
Loading
Loading