Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Add bun support.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Feb 3, 2024
1 parent 50e1a39 commit 531c8a4
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 32 deletions.
19 changes: 13 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ moon_pdk_test_utils = "0.0.2"
moon_target = "0.0.1"
rustc-hash = "1.1.0"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.111"
serde_yaml = "0.9.30"
starbase_utils = { version = "0.4.0", default-features = false }
serde_json = "1.0.113"
serde_yaml = "0.9.31"
starbase_utils = { version = "0.4.2", default-features = false }
starbase_sandbox = "0.3.0"

# moon_common = { path = "../moon/nextgen/common" }
Expand Down
8 changes: 7 additions & 1 deletion crates/download/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
## 0.1.0
## 0.0.2

#### 🚀 Updates

- Updated dependencies.

## 0.0.1

#### 🚀 Updates

Expand Down
7 changes: 6 additions & 1 deletion crates/migrate-turborepo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 0.1.0
## 0.0.1

#### 🚀 Updates

- Initial release!
- New features from moon migration:
- Bun support behind a new `--bun` flag.
- Runs scripts through a package manager, instead of `moon node run-script`.
- Root-level tasks will now create a root config, instead of warning.
- Supports `globalDotEnv`, `dotEnv`, and `outputMode`.
3 changes: 1 addition & 2 deletions crates/migrate-turborepo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ moon_pdk = { workspace = true }
moon_target = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
starbase_utils = { workspace = true, features = ["yaml"] }
starbase_utils = { workspace = true, features = ["json", "yaml"] }

[dev-dependencies]
moon_pdk_test_utils = { workspace = true }
Expand Down
55 changes: 36 additions & 19 deletions crates/migrate-turborepo/src/migrate_turborepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use moon_extension_common::{map_miette_error, project_graph::*};
use moon_pdk::*;
use moon_target::Target;
use rustc_hash::FxHashMap;
use starbase_utils::{fs, yaml};
use starbase_utils::{fs, json, yaml};
use std::collections::BTreeMap;
use std::str::FromStr;

Expand All @@ -20,7 +20,14 @@ extern "ExtismHost" {
fn host_log(input: Json<HostLogInput>);
}

#[derive(Args)]
pub struct MigrateTurborepoExtensionArgs {
#[arg(long, short = 'd')]
pub bun: bool,
}

struct TurboMigrator {
pub args: MigrateTurborepoExtensionArgs,
pub global_config: PartialInheritedTasksConfig,
pub global_config_path: VirtualPath,
pub global_config_modified: bool,
Expand All @@ -31,9 +38,13 @@ struct TurboMigrator {
}

impl TurboMigrator {
pub fn new(context: &MoonContext) -> AnyResult<Self> {
pub fn new(args: MigrateTurborepoExtensionArgs, context: &MoonContext) -> AnyResult<Self> {
// Load global config if it exists
let global_config_path = context.workspace_root.join(".moon/tasks/node.yml");
let global_config_path = context.workspace_root.join(if args.bun {
".moon/tasks/bun.yml"
} else {
".moon/tasks/node.yml"
});
let global_config = if global_config_path.exists() {
yaml::read_file(&global_config_path)?
} else {
Expand All @@ -46,15 +57,20 @@ impl TurboMigrator {
let project_graph: ProjectGraph = json::from_str(&project_graph_result.stdout)?;

// Determine the package manager to run tasks with
let mut package_manager = "npm";

if context.workspace_root.join("pnpm-lock.yaml").exists() {
package_manager = "pnpm";
} else if context.workspace_root.join("yarn.lock").exists() {
package_manager = "yarn";
let mut package_manager = if args.bun { "bun" } else { "npm" };

if !args.bun {
if context.workspace_root.join("pnpm-lock.yaml").exists() {
package_manager = "pnpm";
} else if context.workspace_root.join("yarn.lock").exists() {
package_manager = "yarn";
} else if context.workspace_root.join("bun.lockb").exists() {
package_manager = "bun";
}
}

Ok(Self {
args,
global_config,
global_config_path,
global_config_modified: false,
Expand Down Expand Up @@ -90,7 +106,7 @@ impl TurboMigrator {
}
}

Err(anyhow!("Unable to migrate task for package <id>{package_name}</id>. Has the project been configured in moon's workspace projects?"))
Err(anyhow!("Unable to migrate task for package <id>{package_name}</id>. Has the project been configured in <property>projects</property> in <file>.moon/workspace.yml</file>?"))
}

fn migrate_root_config(&mut self, mut turbo_json: TurboJson) -> AnyResult<()> {
Expand Down Expand Up @@ -352,7 +368,11 @@ impl TurboMigrator {
LanguageType::JavaScript
},
),
platform: Some(PlatformType::Node),
platform: Some(if self.args.bun {
PlatformType::Bun
} else {
PlatformType::Node
}),
..PartialProjectConfig::default()
},
);
Expand All @@ -365,7 +385,8 @@ impl TurboMigrator {

#[plugin_fn]
pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<()> {
let mut migrator = TurboMigrator::new(&input.context)?;
let args = parse_args::<MigrateTurborepoExtensionArgs>(&input.args)?;
let mut migrator = TurboMigrator::new(args, &input.context)?;

// Migrate the workspace root config first
let root_config_path = migrator.workspace_root.join("turbo.json");
Expand All @@ -380,9 +401,7 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
.display()
);

migrator.migrate_root_config(serde_json::from_slice(&fs::read_file_bytes(
&root_config_path,
)?)?)?;
migrator.migrate_root_config(json::read_file(&root_config_path)?)?;

fs::remove(root_config_path)?;
}
Expand Down Expand Up @@ -411,10 +430,8 @@ pub fn execute_extension(Json(input): Json<ExecuteExtensionInput>) -> FnResult<(
.display()
);

migrator.migrate_project_config(
&project_source,
serde_json::from_slice(&fs::read_file_bytes(&project_config_path)?)?,
)?;
migrator
.migrate_project_config(&project_source, json::read_file(&project_config_path)?)?;

fs::remove(project_config_path)?;
}
Expand Down
23 changes: 23 additions & 0 deletions crates/migrate-turborepo/tests/migrate_turborepo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ mod migrate_turborepo {
assert_snapshot!(fs::read_to_string(sandbox.path().join("server/moon.yml")).unwrap());
}

#[test]
fn can_force_bun_instead_of_node() {
let sandbox = create_sandbox("monorepo");
let plugin = create_extension("test", sandbox.path());

plugin.execute_extension(ExecuteExtensionInput {
args: vec!["--bun".into()],
context: plugin.create_context(sandbox.path()),
});

assert!(!sandbox.path().join("turbo.json").exists());
assert!(!sandbox.path().join("client/turbo.json").exists());
assert!(!sandbox.path().join("server/turbo.json").exists());
assert!(!sandbox.path().join(".moon/tasks/node.yml").exists());
assert!(sandbox.path().join(".moon/tasks/bun.yml").exists());
assert!(sandbox.path().join("client/moon.yml").exists());
assert!(sandbox.path().join("server/moon.yml").exists());

assert_snapshot!(fs::read_to_string(sandbox.path().join(".moon/tasks/bun.yml")).unwrap());
assert_snapshot!(fs::read_to_string(sandbox.path().join("client/moon.yml")).unwrap());
assert_snapshot!(fs::read_to_string(sandbox.path().join("server/moon.yml")).unwrap());
}

#[test]
fn converts_to_a_root_project() {
let sandbox = create_sandbox("root-project");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/migrate-turborepo/tests/migrate_turborepo_test.rs
expression: "fs::read_to_string(sandbox.path().join(\"client/moon.yml\")).unwrap()"
---
language: javascript
platform: bun
tasks:
build:
command: bun run build
deps:
- ^:build
outputs:
- client/**/*
typecheck:
command: bun run typecheck
deps:
- ~:build

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/migrate-turborepo/tests/migrate_turborepo_test.rs
expression: "fs::read_to_string(sandbox.path().join(\"server/moon.yml\")).unwrap()"
---
language: typescript
platform: bun
tasks:
build:
command: bun run build
deps:
- ^:build
outputs:
- server/**/*
lint:
command: bun run lint
options:
outputStyle: buffer-only-failure

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: crates/migrate-turborepo/tests/migrate_turborepo_test.rs
expression: "fs::read_to_string(sandbox.path().join(\".moon/tasks/bun.yml\")).unwrap()"
---
tasks:
build:
command: bun run build
deps:
- ^:build
outputs:
- dist/**/*

0 comments on commit 531c8a4

Please sign in to comment.