Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "command_add_npm11_with_workspace"
vp = "global"
skip-platforms = ["windows"]
steps = [
{ argv = ["vp", "add", "testnpm2", "-D", "-w", "--", "--no-audit"], comment = "should add package to workspace root" },
{ argv = ["vp", "add", "testnpm2", "-D", "-w", "--", "--no-audit"], cwd = "packages/app", comment = "add to the root from a workspace member" },
{ argv = ["vpt", "print-file", "package.json", "packages/app/package.json", "packages/utils/package.json"], continue-on-failure = true },
{ argv = ["vp", "add", "@vite-plus-test/utils", "--workspace", "--", "--no-audit"], comment = "reject unsupported --workspace without adding packages to the root", continue-on-failure = true },
{ argv = ["vpt", "print-file", "package.json", "packages/app/package.json", "packages/utils/package.json"], continue-on-failure = true },
Expand All @@ -17,4 +17,7 @@ steps = [
{ argv = ["vpt", "print-file", "package.json", "packages/app/package.json", "packages/utils/package.json"], continue-on-failure = true },
{ argv = ["vp", "install", "test-vite-plus-package@1.0.0", "--filter", "*", "--workspace-root", "--", "--no-audit"], comment = "should install packages alias for add command" },
{ argv = ["vpt", "print-file", "package.json", "packages/app/package.json", "packages/utils/package.json"], continue-on-failure = true },
{ argv = ["vp", "remove", "-w", "testnpm2", "--", "--no-audit"], cwd = "packages/app", comment = "remove the root devDependency and preserve the member dependencies" },
{ argv = ["vpt", "print-file", "package.json", "packages/app/package.json", "packages/utils/package.json"] },
{ argv = ["node", "-e", "const { packages } = require('./package-lock.json'); console.log(packages[''].devDependencies?.testnpm2, packages['packages/app'].dependencies.testnpm2)"], comment = "the lockfile records the same removal at the root and keeps the app dependency" },
]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# command_add_npm11_with_workspace

## `vp add testnpm2 -D -w -- --no-audit`
## `cd packages/app && vp add testnpm2 -D -w -- --no-audit`

should add package to workspace root
add to the root from a workspace member

```

Expand Down Expand Up @@ -274,3 +274,55 @@ added 1 package in <duration>
}
}
```

## `cd packages/app && vp remove -w testnpm2 -- --no-audit`

remove the root devDependency and preserve the member dependencies

```

up to date in <duration>
```

## `vpt print-file package.json packages/app/package.json packages/utils/package.json`

```
{
"name": "command-add-npm11-with-workspace",
"version": "1.0.0",
"workspaces": [
"packages/*"
],
"packageManager": "npm@11.6.2",
"dependencies": {
"test-vite-plus-install": "1.0.0",
"test-vite-plus-package": "^1.0.0"
}
}
{
"name": "app",
"dependencies": {
"test-vite-plus-install": "1.0.0",
"test-vite-plus-package": "^1.0.0",
"testnpm2": "1.0.1"
}
}
{
"name": "@vite-plus-test/utils",
"version": "1.0.0",
"private": true,
"dependencies": {
"test-vite-plus-install": "1.0.0",
"test-vite-plus-package": "^1.0.0",
"testnpm2": "1.0.1"
}
}
```

## `node -e 'const { packages } = require('\''./package-lock.json'\''); console.log(packages['\'''\''].devDependencies?.testnpm2, packages['\''packages/app'\''].dependencies.testnpm2)'`

the lockfile records the same removal at the root and keeps the app dependency

```
undefined 1.0.1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require('node:assert/strict');
const { realpathSync } = require('node:fs');
const path = require('node:path');

const member = realpathSync('packages/app');
assert.equal(realpathSync(require.resolve('fixture-member/package.json')), path.join(member, 'package.json'));
assert.equal(require('./package.json').dependencies['lifecycle-probe'], 'file:packages/app/lifecycle-probe-1.0.0.tgz');
assert.equal(require('./packages/app/package.json').dependencies?.['lifecycle-probe'], undefined);

const { caller, globalPrefix } = require('lifecycle-probe/install-context.json');
assert.equal(realpathSync(caller), member);
assert.equal(realpathSync(globalPrefix), path.join(member, 'test-global'));
console.log('Root dependencies resolve and the install script retains its caller and global prefix.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { execFileSync } = require('node:child_process');
const { writeFileSync } = require('node:fs');

const globalPrefix = execFileSync(process.execPath, [process.env.npm_execpath, 'prefix', '-g'], {
encoding: 'utf8',
}).trim();
writeFileSync('install-context.json', JSON.stringify({ caller: process.env.INIT_CWD, globalPrefix }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "lifecycle-probe",
"version": "1.0.0",
"scripts": {
"install": "node capture.cjs"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "workspace-root",
"private": true,
"version": "1.0.0",
"packageManager": "npm@10.9.2",
"workspaces": ["packages/*"],
"dependencies": {
"fixture-member": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "fixture-member",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[[case]]
name = "command_add_npm_workspace_lifecycle"
vp = "global"
steps = [
{ argv = ["npm", "pack", "./local-package", "--pack-destination", "packages/app", "--silent"], snapshot = false },
{ argv = ["vpt", "mkdir", "packages/app/test-global"], snapshot = false },
{ argv = ["vp", "add", "-w", "./lifecycle-probe-1.0.0.tgz", "--offline", "--", "--no-audit", "--no-fund"], cwd = "packages/app", envs = [["NPM_CONFIG_PREFIX", "${workspace}/test-global"]], snapshot = false },
["node", "check.cjs"],
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# command_add_npm_workspace_lifecycle

## `npm pack ./local-package --pack-destination packages/app --silent`


## `vpt mkdir packages/app/test-global`


## `cd packages/app && NPM_CONFIG_PREFIX=${workspace}/test-global vp add -w ./lifecycle-probe-1.0.0.tgz --offline -- --no-audit --no-fund`


## `node check.cjs`

```
Root dependencies resolve and the install script retains its caller and global prefix.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('node:assert/strict');
const { spawnSync } = require('node:child_process');
const { existsSync, readFileSync } = require('node:fs');

const files = ['package.json', 'packages/app/package.json', 'packages/docs/package.json'];
const manifests = () => files.map((file) => readFileSync(file, 'utf8'));
const before = manifests();
const result = spawnSync('vp', ['add', '-w', '--filter', 'docs', './docs-1.0.0.tgz', '--offline'], {
cwd: 'packages/app',
encoding: 'utf8',
});
assert.ifError(result.error);
assert.notEqual(result.status, 0);
assert.match(result.stdout + result.stderr, /requires npm 8\.19\.0 or later/);
assert.deepEqual(manifests(), before);
assert.equal(existsSync('package-lock.json'), false);
console.log('npm 8.1 rejected the combined tarball edit before writing manifests or a lockfile.');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "root",
"private": true,
"packageManager": "npm@8.1.0",
"workspaces": [
"packages/*"
],
"devDependencies": {
"testnpm2": "1.0.1",
"test-vite-plus-install": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "app",
"version": "1.0.0",
"packageManager": "npm@8.1.0",
"workspaces": [
"plugins/*"
],
"dependencies": {
"testnpm2": "1.0.1",
"test-vite-plus-install": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "docs",
"version": "1.0.0",
"dependencies": {
"test-vite-plus-install": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "docs",
"version": "1.0.0",
"dependencies": {
"testnpm2": "1.0.1",
"test-vite-plus-install": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[case]]
name = "command_remove_npm8_nested_workspace"
vp = "global"
steps = [
{ argv = ["npm", "pack", "./packages/docs", "--pack-destination", "packages/app", "--silent"], snapshot = false },
["node", "check-tarball.cjs"],
{ argv = ["vp", "remove", "-w", "testnpm2", "--", "--no-audit"], cwd = "packages/app", comment = "remove only the outer root declaration when the current member owns a workspace" },
{ argv = ["vp", "remove", "-w", "--filter", "docs", "test-vite-plus-install", "--", "--no-audit"], cwd = "packages/app", comment = "select the outer docs package when the inner workspace also has a member named docs" },
{ argv = ["vp", "remove", "-w", "testnpm2", "--", "--workspace=../docs", "--no-audit"], cwd = "packages/app", comment = "resolve an attached workspace path from the calling member" },
["vpt", "print-file", "package.json", "packages/app/package.json", "packages/docs/package.json", "packages/app/plugins/docs/package.json"],
["node", "-p", "require('./package-lock.json').packages['packages/app'].dependencies"],
["vpt", "stat-file", "packages/app/package-lock.json", "--assert", "missing"],
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# command_remove_npm8_nested_workspace

## `npm pack ./packages/docs --pack-destination packages/app --silent`


## `node check-tarball.cjs`

```
npm 8.1 rejected the combined tarball edit before writing manifests or a lockfile.
```

## `cd packages/app && vp remove -w testnpm2 -- --no-audit`

remove only the outer root declaration when the current member owns a workspace

```

added 4 packages in <duration>
```

## `cd packages/app && vp remove -w --filter docs test-vite-plus-install -- --no-audit`

select the outer docs package when the inner workspace also has a member named docs

```

up to date in <duration>
```

## `cd packages/app && vp remove -w testnpm2 -- --workspace=../docs --no-audit`

resolve an attached workspace path from the calling member

```

up to date in <duration>
```

## `vpt print-file package.json packages/app/package.json packages/docs/package.json packages/app/plugins/docs/package.json`

```
{
"name": "root",
"private": true,
"packageManager": "npm@8.1.0",
"workspaces": [
"packages/*"
]
}
{
"name": "app",
"version": "1.0.0",
"packageManager": "npm@8.1.0",
"workspaces": [
"plugins/*"
],
"dependencies": {
"testnpm2": "1.0.1",
"test-vite-plus-install": "1.0.0"
}
}
{
"name": "docs",
"version": "1.0.0"
}
{
"name": "docs",
"version": "1.0.0",
"dependencies": {
"test-vite-plus-install": "1.0.0"
}
}
```

## `node -p require('./package-lock.json').packages['packages/app'].dependencies`

```
{ 'test-vite-plus-install': '1.0.0', testnpm2: '1.0.1' }
```

## `vpt stat-file packages/app/package-lock.json --assert missing`

```
packages/app/package-lock.json: missing
```
8 changes: 7 additions & 1 deletion crates/vp_pm_cli/src/resolution/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct ResolvedCommand {
pub(crate) args: Vec<String>,
pub(crate) env: BTreeMap<String, String>,
pub(crate) pre_run: Vec<PreRunAction>,
pub(crate) npm_workspace_edit: bool,
}

impl ResolvedCommand {
Expand All @@ -23,7 +24,7 @@ impl ResolvedCommand {
// Include npm/npx fallbacks: Vite+ manages their versions too.
env.insert("npm_config_update_notifier".to_string(), "false".to_string());
}
Self { program, args: Vec::new(), env, pre_run: Vec::new() }
Self { program, args: Vec::new(), env, pre_run: Vec::new(), npm_workspace_edit: false }
}
}

Expand Down Expand Up @@ -94,6 +95,11 @@ impl CommandBuilder {
self
}

pub(crate) fn npm_workspace_edit(&mut self, edit: bool) -> &mut Self {
self.command.npm_workspace_edit = edit;
self
}

pub(crate) fn create_dir(&mut self, path: impl ToString) -> &mut Self {
self.command.pre_run.push(PreRunAction::CreateDir { path: path.to_string() });
self
Expand Down
3 changes: 2 additions & 1 deletion crates/vp_pm_cli/src/resolution/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ impl Npm {
return cmd.into();
}

cmd.arg("install")
cmd.npm_workspace_edit(args.workspace_root && !args.packages.is_empty())
.arg("install")
.repeated("--workspace", args.filter.iter())
.arg_if("--include-workspace-root", args.workspace_root);
match args.save_dependency.target() {
Expand Down
4 changes: 3 additions & 1 deletion crates/vp_pm_cli/src/resolution/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ impl Npm {
return cmd.into();
}

cmd.arg("uninstall").repeated("--workspace", args.filter.iter());
cmd.npm_workspace_edit(args.workspace_root && !args.recursive)
.arg("uninstall")
.repeated("--workspace", args.filter.iter());
if args.workspace_root || args.recursive {
cmd.arg("--include-workspace-root");
}
Expand Down
1 change: 1 addition & 0 deletions crates/vp_pm_cli/src/resolution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod command;
mod commands;
mod diagnostics;
mod dialect;
mod npm_workspace;
mod resolve;
mod runner;
mod support;
Expand Down
Loading
Loading