use std::fs;
use std::io::Write as _;
use std::path::Path;
pub const AUDIT_MATERIALIZED_CONTEXT_DIRS: &[&str] =
&["node_modules", ".nuxt", ".astro", SVELTE_KIT_DIR];
pub const SVELTE_KIT_DIR: &str = ".svelte-kit";
const TSCONFIG_FILE: &str = "tsconfig.json";
const GENERATED_COPY_MARKER: &str = ".fallow-base-copy";
const STAGING_FILE: &str = ".fallow-base-copy.tmp";
const NODE_MODULES_MARKERS: &[&str] = &[".package-lock.json", ".modules.yaml", ".yarn-state.yml"];
const NUXT_MARKERS: &[&str] = &[
"tsconfig.json",
"tsconfig.app.json",
"imports.d.ts",
"components.d.ts",
"types/nitro-routes.d.ts",
"types/nitro-imports.d.ts",
];
const ASTRO_MARKERS: &[&str] = &["types.d.ts", "content.d.ts", "env.d.ts"];
const SVELTE_KIT_MARKERS: &[&str] = &["tsconfig.json", "ambient.d.ts", "non-ambient.d.ts"];
pub const AUDIT_CONTEXT_FILE_MAX_BYTES: u64 = 16 * 1024 * 1024;
pub fn context_markers(name: &str) -> &'static [&'static str] {
match name {
"node_modules" => NODE_MODULES_MARKERS,
".nuxt" => NUXT_MARKERS,
".astro" => ASTRO_MARKERS,
SVELTE_KIT_DIR => SVELTE_KIT_MARKERS,
_ => &[],
}
}
#[expect(
clippy::filetype_is_file,
reason = "the copy reads and trusts only regular files, never a link, a pipe or a device"
)]
pub fn copy_generated_tsconfig(source_dir: &Path, destination_dir: &Path) -> std::io::Result<()> {
let marker = destination_dir.join(GENERATED_COPY_MARKER);
match fs::symlink_metadata(destination_dir) {
Ok(metadata) if metadata.file_type().is_dir() => {
if !fs::symlink_metadata(&marker).is_ok_and(|metadata| metadata.file_type().is_file()) {
return Ok(());
}
}
Ok(metadata) if metadata.file_type().is_symlink() => {
fs::remove_file(destination_dir)?;
fs::create_dir(destination_dir)?;
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
fs::create_dir(destination_dir)?;
}
Ok(_) => return Ok(()),
Err(error) => return Err(error),
}
replace_without_following(destination_dir, &marker, b"")?;
let source = source_dir.join(TSCONFIG_FILE);
let destination = destination_dir.join(TSCONFIG_FILE);
let is_small_regular_file = fs::symlink_metadata(&source).is_ok_and(|metadata| {
metadata.file_type().is_file() && metadata.len() <= AUDIT_CONTEXT_FILE_MAX_BYTES
});
if !is_small_regular_file {
return match fs::remove_file(&destination) {
Err(error) if error.kind() != std::io::ErrorKind::NotFound => Err(error),
_ => Ok(()),
};
}
replace_without_following(destination_dir, &destination, &fs::read(&source)?)
}
fn replace_without_following(
directory: &Path,
destination: &Path,
contents: &[u8],
) -> std::io::Result<()> {
let staging = directory.join(STAGING_FILE);
match fs::remove_file(&staging) {
Err(error) if error.kind() != std::io::ErrorKind::NotFound => return Err(error),
_ => {}
}
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&staging)?;
file.write_all(contents)?;
drop(file);
fs::rename(&staging, destination)
}
pub fn base_has_slot_parent(worktree_path: &Path, kind: &str, relative: &Path) -> bool {
if kind != "node_modules" && kind != SVELTE_KIT_DIR {
return true;
}
match relative.parent() {
Some(parent) if !parent.as_os_str().is_empty() => {
fs::symlink_metadata(worktree_path.join(parent))
.is_ok_and(|metadata| metadata.file_type().is_dir())
}
_ => true,
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn copy_replaces_a_tracked_link_and_leaves_its_target_unchanged() {
let root =
std::env::temp_dir().join(format!("fallow-base-context-link-{}", std::process::id()));
let _ = fs::remove_dir_all(&root);
let source = root.join("checkout");
let destination = root.join("base");
let outside = root.join("outside.txt");
fs::create_dir_all(&source).unwrap();
fs::create_dir_all(&destination).unwrap();
fs::write(source.join(TSCONFIG_FILE), b"{\"compilerOptions\":{}}").unwrap();
fs::write(&outside, b"keep").unwrap();
fs::write(destination.join(GENERATED_COPY_MARKER), b"").unwrap();
std::os::unix::fs::symlink(&outside, destination.join(TSCONFIG_FILE)).unwrap();
copy_generated_tsconfig(&source, &destination).unwrap();
assert_eq!(fs::read(&outside).unwrap(), b"keep");
let copied = destination.join(TSCONFIG_FILE);
assert!(
!fs::symlink_metadata(&copied)
.unwrap()
.file_type()
.is_symlink()
);
assert_eq!(fs::read(&copied).unwrap(), b"{\"compilerOptions\":{}}");
assert!(!destination.join(STAGING_FILE).exists());
fs::remove_dir_all(&root).unwrap();
}
}