monochange_deno 0.5.1

Deno workspace and package discovery for monochange
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#![forbid(clippy::indexing_slicing)]

//! # `monochange_deno`
//!
//! <!-- {=monochangeDenoCrateDocs|trim|linePrefix:"//! ":true} -->
//! `monochange_deno` discovers Deno packages and workspace members for the shared planner.
//!
//! Reach for this crate when you need to scan `deno.json` or `deno.jsonc` files, expand Deno workspaces, and normalize Deno dependencies into `monochange_core` records.
//!
//! ## Why use it?
//!
//! - discover Deno workspaces and standalone packages with one adapter
//! - normalize manifest and dependency data for cross-ecosystem release planning
//! - include Deno-specific import and dependency extraction in the shared graph
//!
//! ## Best for
//!
//! - scanning Deno repos without adopting the full workspace CLI
//! - turning `deno.json` metadata into shared package and dependency records
//! - mixing Deno packages into a broader cross-ecosystem monorepo plan
//!
//! ## Public entry points
//!
//! - `discover_deno_packages(root)` discovers Deno workspaces and standalone packages
//! - `DenoAdapter` exposes the shared adapter interface
//!
//! ## Scope
//!
//! - `deno.json` and `deno.jsonc`
//! - workspace glob expansion
//! - normalized dependency and import extraction
//! <!-- {/monochangeDenoCrateDocs} -->

pub mod analysis;

use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

pub use analysis::DenoSemanticAnalyzer;
pub use analysis::semantic_analyzer;
use glob::glob;
use monochange_core::AdapterDiscovery;
use monochange_core::DependencyKind;
use monochange_core::DiscoveryPathFilter;
use monochange_core::Ecosystem;
use monochange_core::EcosystemAdapter;
use monochange_core::LockfileCommandExecution;
use monochange_core::MonochangeError;
use monochange_core::MonochangeResult;
use monochange_core::PackageDependency;
use monochange_core::PackageRecord;
use monochange_core::PublishState;
use monochange_core::SourceConfiguration;
use monochange_core::normalize_path;
use monochange_publish::PublishRequest;
use semver::Version;
use serde_json::Value;
use walkdir::DirEntry;
use walkdir::WalkDir;

pub const DENO_MANIFEST_FILES: [&str; 2] = ["deno.json", "deno.jsonc"];

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum DenoVersionedFileKind {
	Manifest,
	Lock,
}

/// Classify a Deno versioned file path.
pub fn supported_versioned_file_kind(path: &Path) -> Option<DenoVersionedFileKind> {
	let file_name = path
		.file_name()
		.and_then(|name| name.to_str())
		.unwrap_or_default();
	match file_name {
		"deno.lock" => Some(DenoVersionedFileKind::Lock),
		_ if path.extension().and_then(|ext| ext.to_str()) == Some("json")
			|| path.extension().and_then(|ext| ext.to_str()) == Some("jsonc") =>
		{
			Some(DenoVersionedFileKind::Manifest)
		}
		_ => None,
	}
}

pub fn write_jsr_placeholder_manifest(
	dir: &Path,
	request: &PublishRequest,
	source: Option<&SourceConfiguration>,
) -> MonochangeResult<()> {
	let mut manifest = serde_json::Map::new();
	manifest.insert(
		"name".to_string(),
		Value::String(request.package_name.clone()),
	);
	manifest.insert(
		"version".to_string(),
		Value::String(request.version.clone()),
	);
	manifest.insert(
		"exports".to_string(),
		Value::Object(
			[(".".to_string(), Value::String("./mod.ts".to_string()))]
				.into_iter()
				.collect(),
		),
	);
	if let Some(source) = source {
		manifest.insert(
			"repository".to_string(),
			Value::String(format!(
				"https://github.com/{}/{}",
				source.owner, source.repo
			)),
		);
	}
	fs::write(dir.join("deno.json"), Value::Object(manifest).to_string()).map_err(|error| {
		MonochangeError::Io(format!("failed to write placeholder deno.json: {error}"))
	})?;
	fs::write(dir.join("mod.ts"), "export {};\n").map_err(|error| {
		MonochangeError::Io(format!("failed to write placeholder mod.ts: {error}"))
	})
}

fn rewrite_dependency_reference(text: &str, package_name: &str, version: &str) -> String {
	let mut updated = text.to_string();

	for prefix in [format!("npm:{package_name}@"), format!("{package_name}@")] {
		let mut cursor = 0usize;

		while let Some(found) = updated[cursor..].find(&prefix) {
			let start = cursor + found + prefix.len();
			let end = updated[start..]
				.char_indices()
				.find_map(|(index, ch)| {
					(!ch.is_ascii_alphanumeric() && ch != '.' && ch != '-' && ch != '+')
						.then_some(start + index)
				})
				.unwrap_or(updated.len());

			updated.replace_range(start..end, version);
			cursor = start + version.len();
		}
	}

	updated
}

/// Update versions embedded in a parsed `deno.lock` document.
pub fn update_lockfile(value: &mut Value, raw_versions: &BTreeMap<String, String>) {
	let Ok(mut rendered) = serde_json::to_string(value) else {
		return;
	};

	for (package_name, version) in raw_versions {
		rendered = rewrite_dependency_reference(&rendered, package_name, version);
	}

	if let Ok(updated) = serde_json::from_str::<Value>(&rendered) {
		*value = updated;
	}
}

/// Discover lockfiles that should be refreshed for `package`.
pub fn discover_lockfiles(package: &PackageRecord) -> Vec<PathBuf> {
	let manifest_dir = package
		.manifest_path
		.parent()
		.map_or_else(|| package.workspace_root.clone(), Path::to_path_buf);
	let scope = if manifest_dir == package.workspace_root {
		manifest_dir.clone()
	} else {
		package.workspace_root.clone()
	};

	let mut discovered = [scope.join("deno.lock")]
		.into_iter()
		.filter(|path| path.exists())
		.collect::<Vec<_>>();

	if discovered.is_empty() && scope != manifest_dir {
		discovered.extend(
			[manifest_dir.join("deno.lock")]
				.into_iter()
				.filter(|path| path.exists()),
		);
	}

	discovered
}

/// Return the default lockfile refresh commands for `package`.
pub fn default_lockfile_commands(_package: &PackageRecord) -> Vec<LockfileCommandExecution> {
	// Deno does not require lockfile commands for version updates.
	Vec::new()
}

pub struct DenoAdapter;

/// Return the shared Deno ecosystem adapter.
#[must_use]
pub const fn adapter() -> DenoAdapter {
	DenoAdapter
}

impl EcosystemAdapter for DenoAdapter {
	fn ecosystem(&self) -> Ecosystem {
		Ecosystem::Deno
	}

	fn discover(&self, root: &Path) -> MonochangeResult<AdapterDiscovery> {
		discover_deno_packages(root)
	}

	fn load_configured(
		&self,
		root: &Path,
		package_path: &Path,
	) -> MonochangeResult<Option<PackageRecord>> {
		load_configured_deno_package(root, package_path)
	}

	fn supported_versioned_file_kind(&self, path: &Path) -> bool {
		supported_versioned_file_kind(path).is_some()
	}

	fn validate_versioned_file(
		&self,
		full_path: &Path,
		display_path: &str,
		custom_fields: Option<&[String]>,
	) -> MonochangeResult<()> {
		validate_versioned_file(full_path, display_path, custom_fields)
	}
}

#[tracing::instrument(skip_all)]
#[must_use = "the discovery result must be checked"]
/// Discover Deno packages rooted at `root`.
pub fn discover_deno_packages(root: &Path) -> MonochangeResult<AdapterDiscovery> {
	let workspace_manifests = find_workspace_manifests(root);
	let mut included_manifests = HashSet::new();
	let mut packages = Vec::new();
	let mut warnings = Vec::new();

	for workspace_manifest in workspace_manifests {
		let (workspace_packages, workspace_warnings) =
			discover_workspace_packages(&workspace_manifest)?;
		warnings.extend(workspace_warnings);
		for package in workspace_packages {
			included_manifests.insert(package.manifest_path.clone());
			packages.push(package);
		}
	}

	for manifest_path in find_all_manifests(root) {
		if included_manifests.contains(&manifest_path) {
			continue;
		}

		if let Some(package) =
			parse_manifest(&manifest_path, manifest_path.parent().unwrap_or(root))?
		{
			packages.push(package);
		}
	}

	packages.sort_by(|left, right| left.id.cmp(&right.id));
	packages.dedup_by(|left, right| left.id == right.id);
	tracing::debug!(packages = packages.len(), "discovered deno packages");

	Ok(AdapterDiscovery { packages, warnings })
}

/// Load one explicitly configured Deno package without scanning unrelated manifests.
#[must_use = "the package result must be checked"]
pub fn load_configured_deno_package(
	root: &Path,
	package_path: &Path,
) -> MonochangeResult<Option<PackageRecord>> {
	let manifest_path = if package_path.is_file() {
		package_path.to_path_buf()
	} else {
		DENO_MANIFEST_FILES
			.into_iter()
			.map(|name| package_path.join(name))
			.find(|candidate| candidate.exists())
			.unwrap_or_else(|| package_path.join(DENO_MANIFEST_FILES[0]))
	};
	parse_manifest(&manifest_path, manifest_path.parent().unwrap_or(root))
}

fn find_workspace_manifests(root: &Path) -> Vec<PathBuf> {
	let mut manifests = find_all_manifests(root)
		.into_iter()
		.filter(|manifest_path| has_workspace_section(manifest_path).unwrap_or(false))
		.collect::<Vec<_>>();
	manifests.sort();
	manifests
}

fn discover_workspace_packages(
	workspace_manifest: &Path,
) -> MonochangeResult<(Vec<PackageRecord>, Vec<String>)> {
	let parsed = parse_json_manifest(workspace_manifest)?;
	let workspace_root = workspace_manifest
		.parent()
		.unwrap_or_else(|| Path::new("."));
	let patterns = parsed
		.get("workspace")
		.and_then(Value::as_array)
		.map(|items| {
			items
				.iter()
				.filter_map(Value::as_str)
				.map(ToString::to_string)
				.collect::<Vec<_>>()
		})
		.unwrap_or_default();
	let mut warnings = Vec::new();
	let member_manifests = expand_workspace_patterns(workspace_root, &patterns, &mut warnings);
	let mut packages = Vec::new();

	for member_manifest in member_manifests {
		if let Some(package) = parse_manifest(&member_manifest, workspace_root)? {
			packages.push(package);
		}
	}

	Ok((packages, warnings))
}

fn expand_workspace_patterns(
	root: &Path,
	patterns: &[String],
	warnings: &mut Vec<String>,
) -> BTreeSet<PathBuf> {
	let filter = DiscoveryPathFilter::new(root);
	let mut manifests = BTreeSet::new();
	for pattern in patterns {
		let joined_pattern = root.join(pattern).to_string_lossy().to_string();
		let matches = glob(&joined_pattern)
			.into_iter()
			.flat_map(|paths| paths.filter_map(Result::ok))
			.map(|path| normalize_path(&path))
			.filter(|path| filter.allows(path))
			.collect::<Vec<_>>();
		if matches.is_empty() {
			warnings.push(format!(
				"deno workspace pattern `{pattern}` under {} matched no packages",
				root.display()
			));
		}

		for matched_path in matches {
			for manifest_name in DENO_MANIFEST_FILES {
				let manifest_path = if matched_path.is_dir() {
					matched_path.join(manifest_name)
				} else {
					matched_path.clone()
				};
				if manifest_path.file_name().and_then(|name| name.to_str()) == Some(manifest_name)
					&& manifest_path.exists()
					&& filter.allows(&manifest_path)
				{
					manifests.insert(manifest_path);
				}
			}
		}
	}
	manifests
}

fn parse_manifest(
	manifest_path: &Path,
	workspace_root: &Path,
) -> MonochangeResult<Option<PackageRecord>> {
	let parsed = parse_json_manifest(manifest_path)?;
	let Some(name) = parsed.get("name").and_then(Value::as_str) else {
		return Ok(None);
	};
	let version = parsed
		.get("version")
		.and_then(Value::as_str)
		.and_then(|value| Version::parse(value).ok());

	let mut package = PackageRecord::new(
		Ecosystem::Deno,
		name,
		manifest_path.to_path_buf(),
		workspace_root.to_path_buf(),
		version,
		PublishState::Public,
	);
	package.declared_dependencies = ["dependencies", "imports"]
		.into_iter()
		.flat_map(|section| parse_dependency_map(&parsed, section))
		.collect();
	Ok(Some(package))
}

fn parse_dependency_map(parsed: &Value, section: &str) -> Vec<PackageDependency> {
	parsed
		.get(section)
		.and_then(Value::as_object)
		.map(|dependencies| {
			dependencies
				.iter()
				.filter_map(|(name, value)| {
					value.as_str().map(|constraint| {
						PackageDependency {
							name: name.clone(),
							kind: DependencyKind::Runtime,
							version_constraint: Some(constraint.to_string()),
							optional: false,
							source_field: Some(section.to_string()),
						}
					})
				})
				.collect::<Vec<_>>()
		})
		.unwrap_or_default()
}

fn has_workspace_section(manifest_path: &Path) -> MonochangeResult<bool> {
	let parsed = parse_json_manifest(manifest_path)?;
	Ok(parsed
		.get("workspace")
		.and_then(Value::as_array)
		.is_some_and(|items| !items.is_empty()))
}

fn parse_json_manifest(manifest_path: &Path) -> MonochangeResult<Value> {
	let contents = fs::read_to_string(manifest_path).map_err(|error| {
		MonochangeError::Io(format!(
			"failed to read {}: {error}",
			manifest_path.display()
		))
	})?;
	let normalized = monochange_core::strip_json_comments(&contents);
	serde_json::from_str::<Value>(&normalized).map_err(|error| {
		MonochangeError::Discovery(format!(
			"failed to parse {}: {error}",
			manifest_path.display()
		))
	})
}

fn find_all_manifests(root: &Path) -> Vec<PathBuf> {
	let filter = DiscoveryPathFilter::new(root);
	WalkDir::new(root)
		.into_iter()
		.filter_entry(|entry| filter.should_descend(entry.path()))
		.filter_map(Result::ok)
		.filter(|entry| DENO_MANIFEST_FILES.contains(&entry.file_name().to_string_lossy().as_ref()))
		.map(DirEntry::into_path)
		.map(|path| normalize_path(&path))
		.collect()
}

/// Return the default dependency-version prefix for this ecosystem.
/// Validate that a Deno versioned file contains a readable version field.
pub fn validate_versioned_file(
	full_path: &Path,
	display_path: &str,
	custom_fields: Option<&[String]>,
) -> MonochangeResult<()> {
	let contents = fs::read_to_string(full_path).map_err(|error| {
		MonochangeError::Config(format!(
			"versioned file `{display_path}` is not readable: {error}"
		))
	})?;
	let json: Value = serde_json::from_str(&contents).map_err(|error| {
		MonochangeError::Config(format!(
			"versioned file `{display_path}` is not valid JSON: {error}"
		))
	})?;

	let field_name = match custom_fields {
		Some(fields) if !fields.is_empty() => fields.first().map_or("version", String::as_str),
		_ => "version",
	};

	if json
		.get(field_name)
		.and_then(|value| value.as_str())
		.is_none()
	{
		return Err(MonochangeError::Config(format!(
			"versioned file `{display_path}` does not contain a `{field_name}` string field"
		)));
	}

	Ok(())
}

#[must_use]
pub fn default_dependency_version_prefix() -> &'static str {
	"^"
}

/// Return the manifest fields that usually contain dependency versions.
#[must_use]
pub fn default_dependency_fields() -> &'static [&'static str] {
	&["imports"]
}
#[cfg(test)]
#[path = "__tests__/lib_tests.rs"]
mod tests;