-
Notifications
You must be signed in to change notification settings - Fork 232
feat(pm): add ci subcommand #2082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
forehalo
wants to merge
6
commits into
voidzero-dev:main
Choose a base branch
from
forehalo:feat/pm-ci
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
641db23
feat(pm): add ci subcommand
forehalo f400017
Potential fix for pull request finding
forehalo 4fee930
fix(pm): match pnpm ci semantics
forehalo 408cd04
fix(pm): share ci command envs
forehalo 3d4aadd
test(pm): snapshot pm ci delegation
forehalo 016c289
fix(pm): avoid new ci subcommands
forehalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_ci/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "pm-ci-snapshot", | ||
| "private": true | ||
| } |
38 changes: 38 additions & 0 deletions
38
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_ci/scripts/setup-fake-pms.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const { chmodSync, mkdirSync, writeFileSync } = require('node:fs'); | ||
| const { join } = require('node:path'); | ||
|
|
||
| const vpHome = process.env.VP_HOME; | ||
| if (!vpHome) { | ||
| throw new Error('VP_HOME is required'); | ||
| } | ||
|
|
||
| const packageManagers = [ | ||
| ['pnpm', '11.0.0'], | ||
| ['npm', '10.5.0'], | ||
| ['yarn', '1.22.22'], | ||
| ['yarn', '4.0.0'], | ||
| ['bun', '1.2.0'], | ||
| ]; | ||
|
|
||
| for (const [name, version] of packageManagers) { | ||
| const binDir = join(vpHome, 'package_manager', name, version, name, 'bin'); | ||
| mkdirSync(binDir, { recursive: true }); | ||
|
|
||
| const fakePm = join(binDir, 'fake-pm.cjs'); | ||
| writeFileSync( | ||
| fakePm, | ||
| `const args = process.argv.slice(2); | ||
| console.log(${JSON.stringify(name)} + (args.length ? ' ' + args.join(' ') : '')); | ||
| `, | ||
| ); | ||
|
|
||
| const unixShim = join(binDir, name); | ||
| writeFileSync(unixShim, "#!/usr/bin/env node\nrequire('./fake-pm.cjs');\n"); | ||
| chmodSync(unixShim, 0o755); | ||
|
|
||
| writeFileSync(join(binDir, `${name}.cmd`), `@echo off\r\nnode "%~dp0fake-pm.cjs" %*\r\n`); | ||
| writeFileSync( | ||
| join(binDir, `${name}.ps1`), | ||
| 'node "$PSScriptRoot/fake-pm.cjs" @args\nexit $LASTEXITCODE\n', | ||
| ); | ||
| } |
26 changes: 26 additions & 0 deletions
26
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_ci/snapshots.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [[case]] | ||
| name = "pm_ci_package_managers" | ||
| vp = "global" | ||
| comment = """ | ||
| Covers `vp pm ci` command delegation for each supported package manager. | ||
| Fake managed package-manager installs live under VP_HOME, so the case records | ||
| the exact argv Vite+ delegates without hitting the network or real installers. | ||
| """ | ||
| steps = [ | ||
| { argv = ["node", "scripts/setup-fake-pms.cjs"], snapshot = false }, | ||
|
|
||
| { argv = ["vpt", "json-edit", "package.json", "packageManager", "pnpm@11.0.0"], snapshot = false }, | ||
| { argv = ["vp", "pm", "ci"], comment = "pnpm uses frozen-lockfile install" }, | ||
|
|
||
| { argv = ["vpt", "json-edit", "package.json", "packageManager", "npm@10.5.0"], snapshot = false }, | ||
| { argv = ["vp", "pm", "ci"], comment = "npm keeps native ci delegation because npm has no frozen-lockfile install flag" }, | ||
|
|
||
| { argv = ["vpt", "json-edit", "package.json", "packageManager", "yarn@1.22.22"], snapshot = false }, | ||
| { argv = ["vp", "pm", "ci"], comment = "Yarn Classic uses frozen-lockfile install" }, | ||
|
|
||
| { argv = ["vpt", "json-edit", "package.json", "packageManager", "yarn@4.0.0"], snapshot = false }, | ||
| { argv = ["vp", "pm", "ci"], comment = "Yarn Berry uses immutable install" }, | ||
|
|
||
| { argv = ["vpt", "json-edit", "package.json", "packageManager", "bun@1.2.0"], snapshot = false }, | ||
| { argv = ["vp", "pm", "ci"], comment = "Bun uses frozen-lockfile install" }, | ||
| ] |
63 changes: 63 additions & 0 deletions
63
...napshots/tests/cli_snapshots/fixtures/pm_ci/snapshots/pm_ci_package_managers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # pm_ci_package_managers | ||
|
|
||
| Covers `vp pm ci` command delegation for each supported package manager. | ||
| Fake managed package-manager installs live under VP_HOME, so the case records | ||
| the exact argv Vite+ delegates without hitting the network or real installers. | ||
|
|
||
| ## `node scripts/setup-fake-pms.cjs` | ||
|
|
||
|
|
||
| ## `vpt json-edit package.json packageManager pnpm@11.0.0` | ||
|
|
||
|
|
||
| ## `vp pm ci` | ||
|
|
||
| pnpm uses frozen-lockfile install | ||
|
|
||
| ``` | ||
| pnpm install --frozen-lockfile | ||
| ``` | ||
|
|
||
| ## `vpt json-edit package.json packageManager npm@10.5.0` | ||
|
|
||
|
|
||
| ## `vp pm ci` | ||
|
|
||
| npm keeps native ci delegation because npm has no frozen-lockfile install flag | ||
|
|
||
| ``` | ||
| npm ci | ||
| ``` | ||
|
|
||
| ## `vpt json-edit package.json packageManager yarn@1.22.22` | ||
|
|
||
|
|
||
| ## `vp pm ci` | ||
|
|
||
| Yarn Classic uses frozen-lockfile install | ||
|
|
||
| ``` | ||
| yarn install --frozen-lockfile | ||
| ``` | ||
|
|
||
| ## `vpt json-edit package.json packageManager yarn@4.0.0` | ||
|
|
||
|
|
||
| ## `vp pm ci` | ||
|
|
||
| Yarn Berry uses immutable install | ||
|
|
||
| ``` | ||
| yarn install --immutable | ||
| ``` | ||
|
|
||
| ## `vpt json-edit package.json packageManager bun@1.2.0` | ||
|
|
||
|
|
||
| ## `vp pm ci` | ||
|
|
||
| Bun uses frozen-lockfile install | ||
|
|
||
| ``` | ||
| bun install --frozen-lockfile | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| use std::{collections::HashMap, process::ExitStatus}; | ||
|
|
||
| use vite_command::run_command; | ||
| use vite_error::Error; | ||
| use vite_path::AbsolutePath; | ||
|
|
||
| use crate::package_manager::{ | ||
| PackageManager, PackageManagerType, ResolveCommandResult, format_path_env, | ||
| }; | ||
|
|
||
| /// Options for the ci command. | ||
| #[derive(Debug, Default)] | ||
| pub struct CiCommandOptions<'a> { | ||
| pub pass_through_args: Option<&'a [String]>, | ||
| } | ||
|
|
||
| impl PackageManager { | ||
| /// Run the ci command with the package manager. | ||
| #[must_use] | ||
| pub async fn run_ci_command( | ||
| &self, | ||
| options: &CiCommandOptions<'_>, | ||
| cwd: impl AsRef<AbsolutePath>, | ||
| ) -> Result<ExitStatus, Error> { | ||
| let resolve_command = self.resolve_ci_command(options); | ||
| run_command(&resolve_command.bin_path, &resolve_command.args, &resolve_command.envs, cwd) | ||
| .await | ||
| } | ||
|
|
||
| /// Resolve the ci command. | ||
| #[must_use] | ||
| pub fn resolve_ci_command(&self, options: &CiCommandOptions<'_>) -> ResolveCommandResult { | ||
| let envs = HashMap::from([("PATH".to_string(), format_path_env(self.get_bin_prefix()))]); | ||
| let command = |bin_path: &str, mut args: Vec<String>| { | ||
| if let Some(pass_through_args) = options.pass_through_args { | ||
| args.extend_from_slice(pass_through_args); | ||
| } | ||
|
|
||
| ResolveCommandResult { bin_path: bin_path.into(), args, envs: envs.clone() } | ||
| }; | ||
|
|
||
| match self.client { | ||
| PackageManagerType::Npm => command("npm", vec!["ci".into()]), | ||
| PackageManagerType::Pnpm => { | ||
| command("pnpm", vec!["install".into(), "--frozen-lockfile".into()]) | ||
| } | ||
| PackageManagerType::Bun => { | ||
| command("bun", vec!["install".into(), "--frozen-lockfile".into()]) | ||
| } | ||
| PackageManagerType::Yarn => { | ||
| let mut args = vec!["install".into()]; | ||
| if self.is_yarn_berry() { | ||
| args.push("--immutable".into()); | ||
| } else { | ||
| args.push("--frozen-lockfile".into()); | ||
| } | ||
| command("yarn", args) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use tempfile::{TempDir, tempdir}; | ||
| use vite_path::AbsolutePathBuf; | ||
| use vite_str::Str; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn create_temp_dir() -> TempDir { | ||
| tempdir().expect("Failed to create temp directory") | ||
| } | ||
|
|
||
| fn create_mock_package_manager(pm_type: PackageManagerType, version: &str) -> PackageManager { | ||
| let temp_dir = create_temp_dir(); | ||
| let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap(); | ||
| let install_dir = temp_dir_path.join("install"); | ||
|
|
||
| PackageManager { | ||
| client: pm_type, | ||
| package_name: pm_type.to_string().into(), | ||
| version: Str::from(version), | ||
| hash: None, | ||
| bin_name: pm_type.to_string().into(), | ||
| workspace_root: temp_dir_path.clone(), | ||
| is_monorepo: false, | ||
| install_dir, | ||
| } | ||
| } | ||
|
|
||
| fn assert_path_env(command: &ResolveCommandResult) { | ||
| assert!(command.envs.contains_key("PATH")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_npm_ci() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Npm, "11.0.0"); | ||
| let result = pm.resolve_ci_command(&CiCommandOptions::default()); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "npm"); | ||
| assert_eq!(result.args, vec!["ci"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pnpm_ci() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Pnpm, "11.0.0"); | ||
| let result = pm.resolve_ci_command(&CiCommandOptions::default()); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "pnpm"); | ||
| assert_eq!(result.args, vec!["install", "--frozen-lockfile"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bun_ci() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Bun, "1.3.11"); | ||
| let result = pm.resolve_ci_command(&CiCommandOptions::default()); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "bun"); | ||
| assert_eq!(result.args, vec!["install", "--frozen-lockfile"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_yarn_ci_uses_immutable_install() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Yarn, "4.0.0"); | ||
| let result = pm.resolve_ci_command(&CiCommandOptions::default()); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "yarn"); | ||
| assert_eq!(result.args, vec!["install", "--immutable"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_yarn_classic_ci_uses_frozen_lockfile_install() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Yarn, "1.22.22"); | ||
| let result = pm.resolve_ci_command(&CiCommandOptions::default()); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "yarn"); | ||
| assert_eq!(result.args, vec!["install", "--frozen-lockfile"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_ci_pass_through_args() { | ||
| let pm = create_mock_package_manager(PackageManagerType::Pnpm, "11.0.0"); | ||
| let pass_through = vec!["--ignore-scripts".to_string()]; | ||
| let result = | ||
| pm.resolve_ci_command(&CiCommandOptions { pass_through_args: Some(&pass_through) }); | ||
| assert_path_env(&result); | ||
| assert_eq!(result.bin_path, "pnpm"); | ||
| assert_eq!(result.args, vec!["install", "--frozen-lockfile", "--ignore-scripts"]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.