Skip to content

Commit

Permalink
feat: add workspace flag (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpreston321 authored Apr 12, 2023
1 parent dc44a17 commit 2f76fea
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ export type OperationOptions = {

/**
* The package manager info to use (auto detected)
*
*/
packageManager?: PackageManager;

/**
* Whether to add the dependency as a dev dependency
*
* @default false
*/
dev?: boolean;

/**
* Whether to use the workspace package manager
* Only works only with yarn@2+, pnpm and npm
*
* @default false
*/
workspace?: boolean;
};

/**
Expand All @@ -44,6 +52,9 @@ export async function addDependency(

const args = [
options.packageManager.name === "npm" ? "install" : "add",
options.workspace
? (options.packageManager.name === "yarn" ? "-W" : "-w")
: "",
options.dev ? "-D" : "",
name,
].filter(Boolean);
Expand Down Expand Up @@ -76,7 +87,7 @@ export async function addDevDependency(
*/
export async function removeDependency(
name: string,
_options: OperationOptions = {}
_options: Exclude<OperationOptions, "workspace"> = {}
) {
const options = await _resolveOptions(_options);

Expand Down
35 changes: 34 additions & 1 deletion test/fixtures/npm/package-lock.json

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

3 changes: 3 additions & 0 deletions test/fixtures/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"name": "fixture-npm",
"private": true,
"version": "0.0.0",
"workspaces": [
"packages/workspace-a"
],
"packageManager": "npm@9.1.3"
}
9 changes: 9 additions & 0 deletions test/fixtures/npm/packages/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "workspace-a",
"private": true,
"version": "0.0.0",
"devDependencies": {
"ufo": "^1.1.1"
},
"packageManager": "npm@9.1.3"
}
9 changes: 9 additions & 0 deletions test/fixtures/pnpm/packages/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "fixture-pnpm/workspace-a",
"private": true,
"version": "0.0.0",
"devDependencies": {
"ufo": "^1.1.1"
},
"packageManager": "pnpm@8.1.1"
}
16 changes: 16 additions & 0 deletions test/fixtures/pnpm/pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions test/fixtures/pnpm/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- "packages/*"
3 changes: 3 additions & 0 deletions test/fixtures/yarn-berry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"name": "fixture-yarn",
"private": true,
"version": "0.0.0",
"workspaces": [
"packages/*"
],
"packageManager": "yarn@3.5.0"
}
8 changes: 8 additions & 0 deletions test/fixtures/yarn-berry/packages/workspace-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"version": "0.0.0",
"devDependencies": {
"ufo": "^1.1.1"
},
"packageManager": "yarn@3.5.0"
}
6 changes: 6 additions & 0 deletions test/fixtures/yarn-berry/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ __metadata:
resolution: "fixture-yarn@workspace:."
languageName: unknown
linkType: soft

"workspace-a-2ddc4d@workspace:packages/workspace-a":
version: 0.0.0-use.local
resolution: "workspace-a-2ddc4d@workspace:packages/workspace-a"
languageName: unknown
linkType: soft
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe("api", () => {
await addDependency("pathe", {
cwd: fixtureDirectory,
silent: false,
workspace: true,
})
).toBeTruthy();
}, 30_000);
Expand Down

0 comments on commit 2f76fea

Please sign in to comment.