diff options
| author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-11-17 02:28:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-17 01:28:38 +0000 |
| commit | 9534e6e1131542653c4e266f712c4067af2c8ec0 (patch) | |
| tree | 1a74cccdcdb6bb12c10ab3e2a34d93fd8c4ccc55 /cli/args | |
| parent | 544923afdc67e9946453901642746f37f22c8e24 (diff) | |
feat(unstable): Workspaces support (#20410)
This commit adds unstable workspace support. This is extremely
bare-bones and
minimal first-pass at this.
With this change `deno.json` supports specifying `workspaces` key, that
accepts a list of subdirectories. Each workspace can have its own import
map. It's required to specify a `"name"` and `"version"` properties in the
configuration file for the workspace:
```jsonc
// deno.json
{
"workspaces": [
"a",
"b"
},
"imports": {
"express": "npm:express@5"
}
}
```
``` jsonc
// a/deno.json
{
"name": "a",
"version": "1.0.2",
"imports": {
"kleur": "npm:kleur"
}
}
```
```jsonc
// b/deno.json
{
"name": "b",
"version": "0.51.0",
"imports": {
"chalk": "npm:chalk"
}
}
```
`--unstable-workspaces` flag is required to use this feature:
```
$ deno run --unstable-workspaces mod.ts
```
---------
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/args')
| -rw-r--r-- | cli/args/flags.rs | 11 | ||||
| -rw-r--r-- | cli/args/import_map.rs | 2 | ||||
| -rw-r--r-- | cli/args/mod.rs | 57 |
3 files changed, 69 insertions, 1 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 235743bda..6d1e41a19 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -431,6 +431,7 @@ pub struct Flags { pub unstable: bool, pub unstable_bare_node_builtins: bool, pub unstable_byonm: bool, + pub unstable_workspaces: bool, pub unstable_features: Vec<String>, pub unsafely_ignore_certificate_errors: Option<Vec<String>>, pub v8_flags: Vec<String>, @@ -871,6 +872,7 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::error::Result<Flags> { flags.unstable_bare_node_builtins = matches.get_flag("unstable-bare-node-builtins"); flags.unstable_byonm = matches.get_flag("unstable-byonm"); + flags.unstable_workspaces = matches.get_flag("unstable-workspaces"); if matches.get_flag("quiet") { flags.log_level = Some(Level::Error); @@ -984,6 +986,15 @@ fn clap_root() -> Command { .value_parser(FalseyValueParser::new()) .action(ArgAction::SetTrue) .global(true), + ) + .arg( + Arg::new("unstable-workspaces") + .long("unstable-workspaces") + .help("Enable unstable 'workspaces' feature") + .env("DENO_UNSTABLE_WORKSPACES") + .value_parser(FalseyValueParser::new()) + .action(ArgAction::SetTrue) + .global(true), ); for (flag_name, help, _) in crate::UNSTABLE_GRANULAR_FLAGS { diff --git a/cli/args/import_map.rs b/cli/args/import_map.rs index 9d1b2bbda..5ebb425eb 100644 --- a/cli/args/import_map.rs +++ b/cli/args/import_map.rs @@ -36,7 +36,7 @@ pub async fn resolve_import_map_from_specifier( import_map_from_value(specifier, value) } -fn import_map_from_value( +pub fn import_map_from_value( specifier: &Url, json_value: serde_json::Value, ) -> Result<ImportMap, AnyError> { diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 24d1237aa..dd8de2a6f 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -29,6 +29,7 @@ pub use deno_config::TsConfig; pub use deno_config::TsConfigForEmit; pub use deno_config::TsConfigType; pub use deno_config::TsTypeLib; +pub use deno_config::WorkspaceConfig; pub use flags::*; pub use lockfile::Lockfile; pub use lockfile::LockfileError; @@ -619,6 +620,7 @@ pub struct CliOptions { maybe_package_json: Option<PackageJson>, maybe_lockfile: Option<Arc<Mutex<Lockfile>>>, overrides: CliOptionOverrides, + maybe_workspace_config: Option<WorkspaceConfig>, } impl CliOptions { @@ -652,6 +654,20 @@ impl CliOptions { .with_context(|| "Resolving node_modules folder.")?; let maybe_vendor_folder = resolve_vendor_folder(&initial_cwd, &flags, maybe_config_file.as_ref()); + let maybe_workspace_config = + if let Some(config_file) = maybe_config_file.as_ref() { + config_file.to_workspace_config()? + } else { + None + }; + + // TODO(bartlomieju): remove in v1.39 or v1.40. + if let Some(wsconfig) = &maybe_workspace_config { + if !wsconfig.members.is_empty() && !flags.unstable_workspaces { + eprintln!("Use of unstable 'workspaces' feature. The --unstable-workspaces flags must be provided."); + std::process::exit(70); + } + } if let Some(env_file_name) = &flags.env_file { if (from_filename(env_file_name)).is_err() { @@ -668,6 +684,7 @@ impl CliOptions { maybe_node_modules_folder, maybe_vendor_folder, overrides: Default::default(), + maybe_workspace_config, }) } @@ -813,6 +830,41 @@ impl CliOptions { &self, file_fetcher: &FileFetcher, ) -> Result<Option<ImportMap>, AnyError> { + if let Some(workspace_config) = self.maybe_workspace_config.as_ref() { + let base_import_map_config = ::import_map::ext::ImportMapConfig { + base_url: self.maybe_config_file.as_ref().unwrap().specifier.clone(), + import_map_value: workspace_config.base_import_map_value.clone(), + }; + let children_configs = workspace_config + .members + .iter() + .map(|member| { + let import_map_value = member.config_file.to_import_map_value(); + ::import_map::ext::ImportMapConfig { + base_url: member.config_file.specifier.clone(), + import_map_value, + } + }) + .collect(); + + let maybe_import_map = ::import_map::ext::create_synthetic_import_map( + base_import_map_config, + children_configs, + ); + if let Some((_import_map_url, import_map)) = maybe_import_map { + log::debug!( + "Workspace config generated this import map {}", + serde_json::to_string_pretty(&import_map).unwrap() + ); + return import_map::import_map_from_value( + // TODO(bartlomieju): maybe should be stored on the workspace config? + &self.maybe_config_file.as_ref().unwrap().specifier, + import_map, + ) + .map(Some); + } + } + let import_map_specifier = match self.resolve_import_map_specifier()? { Some(specifier) => specifier, None => return Ok(None), @@ -940,6 +992,7 @@ impl CliOptions { maybe_config_file: self.maybe_config_file.clone(), maybe_package_json: self.maybe_package_json.clone(), maybe_lockfile: self.maybe_lockfile.clone(), + maybe_workspace_config: self.maybe_workspace_config.clone(), overrides: self.overrides.clone(), } } @@ -1037,6 +1090,10 @@ impl CliOptions { &self.maybe_config_file } + pub fn maybe_workspace_config(&self) -> &Option<WorkspaceConfig> { + &self.maybe_workspace_config + } + pub fn maybe_package_json(&self) -> &Option<PackageJson> { &self.maybe_package_json } |
