Skip to content
Merged
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
3 changes: 3 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export function actionHandler<
context.config,
context.ignore ?? [],
context.allowNodeModules ?? false,
context.debug,
);
const configContext: ConfigContext = {
...getAppFromConfig(config),
Expand Down Expand Up @@ -245,11 +246,13 @@ async function readConfig(
maybeConfigPath: string | undefined,
ignorePaths: string[],
allowNodeModules: boolean,
debug: boolean,
): Promise<Config> {
const config = resolve_config(
resolve(maybeConfigPath || rootPath),
ignorePaths,
allowNodeModules,
debug,
);

if (config.path) {
Expand Down
135 changes: 119 additions & 16 deletions rs_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ use sys_traits::FsMetadata;
use url::Url;
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console, js_name = log)]
fn console_log(s: &str);
}

#[cfg(target_arch = "wasm32")]
fn debug_log(debug: bool, msg: &str) {
if debug {
console_log(&format!("[rs_lib] {}", msg));
}
}

#[cfg(not(target_arch = "wasm32"))]
fn debug_log(_debug: bool, _msg: &str) {}

#[derive(Serialize)]
pub struct ConfigLookup {
pub path: Option<String>,
Expand All @@ -21,9 +38,10 @@ pub fn resolve_config(
root_path: String,
ignore_paths: Vec<String>,
allow_node_modules: bool,
debug: bool,
) -> Result<JsValue, JsValue> {
let result =
inner_resolve_config(root_path, ignore_paths, allow_node_modules);
inner_resolve_config(root_path, ignore_paths, allow_node_modules, debug);
result
.map_err(|err| create_js_error(&err))
.map(|val| serde_wasm_bindgen::to_value(&val).unwrap())
Expand All @@ -33,18 +51,30 @@ fn inner_resolve_config(
root_path: String,
ignore_paths: Vec<String>,
allow_node_modules: bool,
debug: bool,
) -> Result<ConfigLookup, anyhow::Error> {
debug_log(
debug,
&format!(
"resolve_config(root_path={:?}, ignore_paths={:?}, allow_node_modules={})",
root_path, ignore_paths, allow_node_modules
),
);

let real_sys = sys_traits::impls::RealSys;
let root_path = resolve_absolute_path(root_path)?;
debug_log(debug, &format!("resolved absolute root_path={:?}", root_path));

// When --config points to a file (not a directory), use ConfigFile
// discovery so non-standard filenames like deno-staging.json work.
let is_config_file = real_sys.fs_is_file(&root_path).unwrap_or(false);
debug_log(debug, &format!("is_config_file={}", is_config_file));
let dir_path = if is_config_file {
root_path.parent().unwrap().to_path_buf()
} else {
root_path.clone()
};
debug_log(debug, &format!("dir_path={:?}", dir_path));

let dir_paths = [dir_path.clone()];
let discover_start = if is_config_file {
Expand All @@ -65,10 +95,31 @@ fn inner_resolve_config(
maybe_vendor_override: None,
},
)?;
debug_log(
debug,
&format!(
"workspace discovered: member_deno_json={:?}, root_deno_json={:?}, members={:?}",
workspace_dir.member_deno_json().map(|c| c.specifier.to_string()),
workspace_dir
.workspace
.root_deno_json()
.map(|c| c.specifier.to_string()),
workspace_dir
.workspace
.config_folders()
.keys()
.map(|u| u.to_string())
.collect::<Vec<_>>(),
),
);

let mut pattern = FilePatterns::new_with_base(dir_path.clone());

if !ignore_paths.is_empty() {
debug_log(
debug,
&format!("applying ignore_paths={:?}", ignore_paths),
);
let exclude = PathOrPatternSet::from_exclude_relative_path_or_patterns(
&dir_path,
&ignore_paths,
Expand All @@ -79,6 +130,13 @@ fn inner_resolve_config(
}

if let Some(config) = workspace_dir.to_deploy_config(pattern)? {
debug_log(
debug,
&format!(
"deploy config: include={:?}, exclude={:?}",
config.files.include, config.files.exclude,
),
);
let specifier = workspace_dir
.member_deno_json()
.filter(|config| config.to_deploy_config().is_ok())
Expand All @@ -91,8 +149,9 @@ fn inner_resolve_config(
.expect(
"workspace_dir.to_deploy_config should have resolved a specifier",
);
debug_log(debug, &format!("deploy config specifier={}", specifier));
let files =
collect_files(&real_sys, dir_path, config.files, allow_node_modules);
collect_files(&real_sys, dir_path, config.files, allow_node_modules, debug);
Ok(ConfigLookup {
path: Some(specifier),
files,
Expand All @@ -105,15 +164,21 @@ fn inner_resolve_config(
workspace_dir.workspace.root_deno_json()
.map(|member| member.specifier.to_string())
});
Ok(ConfigLookup {
path,
files: collect_files(
&real_sys,
dir_path.clone(),
FilePatterns::new_with_base(dir_path),
allow_node_modules,
debug_log(
debug,
&format!(
"no deploy config found; fallback config path={:?}",
path,
),
})
);
let files = collect_files(
&real_sys,
dir_path.clone(),
FilePatterns::new_with_base(dir_path),
allow_node_modules,
debug,
);
Ok(ConfigLookup { path, files })
}
}

Expand All @@ -122,21 +187,58 @@ fn collect_files(
root_path: PathBuf,
files: FilePatterns,
allow_node_modules: bool,
debug: bool,
) -> Vec<String> {
let mut collector =
FileCollector::new(|entry| entry.path.starts_with(&root_path))
.ignore_git_folder()
.use_gitignore();
let filter_root = root_path.clone();
let mut collector = FileCollector::new(move |entry| {
let kept = entry.path.starts_with(&filter_root);
debug_log(
debug,
&format!(
"walk entry path={:?} is_dir={} kept={} (root={:?})",
entry.path,
entry.metadata.file_type().is_dir(),
kept,
filter_root,
),
);
kept
})
.ignore_git_folder()
.use_gitignore();

if !allow_node_modules {
collector = collector.ignore_node_modules();
}

collector
debug_log(
debug,
&format!(
"collector config: ignore_git_folder=true, use_gitignore=true, ignore_node_modules={}",
!allow_node_modules,
),
);

let collected: Vec<String> = collector
.collect_file_patterns(real_sys, &files)
.into_iter()
.map(|path| path.to_string_lossy().to_string())
.collect::<Vec<String>>()
.collect();

debug_log(
debug,
&format!(
"collect_files(root_path={:?}, allow_node_modules={}, include={:?}, exclude={:?}) -> {} file(s): {:?}",
root_path,
allow_node_modules,
files.include,
files.exclude,
collected.len(),
collected,
),
);

collected
}

fn resolve_absolute_path(path: String) -> Result<PathBuf, anyhow::Error> {
Expand Down Expand Up @@ -195,6 +297,7 @@ mod tests {
root.to_string_lossy().into_owned(),
Vec::new(),
false,
false,
)
.unwrap();

Expand Down
Loading