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

Feature/monorepo support #1748

Merged
merged 8 commits into from
Feb 25, 2021
Merged
Changes from 4 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
50 changes: 41 additions & 9 deletions src/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,21 @@ fn run_npm_install(dir: &PathBuf) -> Result<(), failure::Error> {
flock.lock_exclusive()?;

if !dir.join("node_modules").exists() {
let mut command = build_npm_command();
command.current_dir(dir.clone());
command.arg("install");
log::info!("Running {:?} in directory {:?}", command, dir);

let status = command.status()?;

if !status.success() {
failure::bail!("failed to execute `{:?}`: exited with {}", command, status)
// no dir in current path, search for closest
match find_closest("node_modules", dir.as_path()) {
Some(_) => log::info!("skipping npm install because node_modules exists in parent dir"),
None => {
let mut command = build_npm_command();
command.current_dir(dir.clone());
command.arg("install");
log::info!("Running {:?} in directory {:?}", command, dir);

let status = command.status()?;

if !status.success() {
failure::bail!("failed to execute `{:?}`: exited with {}", command, status)
}
}
}
} else {
log::info!("skipping npm install because node_modules exists");
Expand All @@ -256,6 +262,32 @@ fn run_npm_install(dir: &PathBuf) -> Result<(), failure::Error> {
Ok(())
}

// find closest (bottom-up traversal) location of a file or dir
fn find_closest<'a>(name: &str, path: &'a Path) -> Option<&'a Path> {
if has_dir(name, path) {
return Option::from(path);
}

// If has parent lets check it
let parent = path.parent();
match parent {
Some(parent_path) => find_closest(name, parent_path),
None => None,
}
}

// check if dir has file with provided name
fn has_dir(name: &str, path: &Path) -> bool {
for entry in fs::read_dir(path).unwrap() {
let dir = entry.unwrap();
if dir.file_name() == name {
return true;
}
}

false
}

// build a Command for npm
//
// Here's the deal: on Windows, `npm` isn't a binary, it's a shell script.
Expand Down