From 2f76feaeb5e85382d2a398c98cfd5e99300f2bbb Mon Sep 17 00:00:00 2001 From: Christian Preston Date: Wed, 12 Apr 2023 12:04:58 -0400 Subject: [PATCH] feat: add workspace flag (#57) --- src/api.ts | 15 ++++++-- test/fixtures/npm/package-lock.json | 35 ++++++++++++++++++- test/fixtures/npm/package.json | 3 ++ .../npm/packages/workspace-a/package.json | 9 +++++ .../pnpm/packages/workspace-a/package.json | 9 +++++ test/fixtures/pnpm/pnpm-lock.yaml | 16 +++++++++ test/fixtures/pnpm/pnpm-workspace.yaml | 2 ++ test/fixtures/yarn-berry/package.json | 3 ++ .../packages/workspace-a/package.json | 8 +++++ test/fixtures/yarn-berry/yarn.lock | 6 ++++ test/index.test.ts | 1 + 11 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/npm/packages/workspace-a/package.json create mode 100644 test/fixtures/pnpm/packages/workspace-a/package.json create mode 100644 test/fixtures/pnpm/pnpm-workspace.yaml create mode 100644 test/fixtures/yarn-berry/packages/workspace-a/package.json diff --git a/src/api.ts b/src/api.ts index f6bb18a..9c069f2 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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; }; /** @@ -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); @@ -76,7 +87,7 @@ export async function addDevDependency( */ export async function removeDependency( name: string, - _options: OperationOptions = {} + _options: Exclude = {} ) { const options = await _resolveOptions(_options); diff --git a/test/fixtures/npm/package-lock.json b/test/fixtures/npm/package-lock.json index 6d2d054..39f6fd8 100644 --- a/test/fixtures/npm/package-lock.json +++ b/test/fixtures/npm/package-lock.json @@ -6,7 +6,40 @@ "packages": { "": { "name": "fixture-npm", - "version": "0.0.0" + "version": "0.0.0", + "workspaces": [ + "packages/workspace-a" + ] + }, + "node_modules/ufo": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.1.1.tgz", + "integrity": "sha512-MvlCc4GHrmZdAllBc0iUDowff36Q9Ndw/UzqmEKyrfSzokTd9ZCy1i+IIk5hrYKkjoYVQyNbrw7/F8XJ2rEwTg==", + "dev": true + }, + "node_modules/workspace-a": { + "resolved": "packages/workspace-a", + "link": true + }, + "packages/workspace-a": { + "version": "0.0.0", + "devDependencies": { + "ufo": "^1.1.1" + } + } + }, + "dependencies": { + "ufo": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.1.1.tgz", + "integrity": "sha512-MvlCc4GHrmZdAllBc0iUDowff36Q9Ndw/UzqmEKyrfSzokTd9ZCy1i+IIk5hrYKkjoYVQyNbrw7/F8XJ2rEwTg==", + "dev": true + }, + "workspace-a": { + "version": "file:packages/workspace-a", + "requires": { + "ufo": "^1.1.1" + } } } } diff --git a/test/fixtures/npm/package.json b/test/fixtures/npm/package.json index f6eb771..86cc39f 100644 --- a/test/fixtures/npm/package.json +++ b/test/fixtures/npm/package.json @@ -2,5 +2,8 @@ "name": "fixture-npm", "private": true, "version": "0.0.0", + "workspaces": [ + "packages/workspace-a" + ], "packageManager": "npm@9.1.3" } diff --git a/test/fixtures/npm/packages/workspace-a/package.json b/test/fixtures/npm/packages/workspace-a/package.json new file mode 100644 index 0000000..d155977 --- /dev/null +++ b/test/fixtures/npm/packages/workspace-a/package.json @@ -0,0 +1,9 @@ +{ + "name": "workspace-a", + "private": true, + "version": "0.0.0", + "devDependencies": { + "ufo": "^1.1.1" + }, + "packageManager": "npm@9.1.3" +} diff --git a/test/fixtures/pnpm/packages/workspace-a/package.json b/test/fixtures/pnpm/packages/workspace-a/package.json new file mode 100644 index 0000000..adb7b92 --- /dev/null +++ b/test/fixtures/pnpm/packages/workspace-a/package.json @@ -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" +} diff --git a/test/fixtures/pnpm/pnpm-lock.yaml b/test/fixtures/pnpm/pnpm-lock.yaml index 7a06cc7..ef44372 100644 --- a/test/fixtures/pnpm/pnpm-lock.yaml +++ b/test/fixtures/pnpm/pnpm-lock.yaml @@ -1 +1,17 @@ lockfileVersion: '6.0' + +importers: + + .: {} + + packages/workspace-a: + devDependencies: + ufo: + specifier: ^1.1.1 + version: 1.1.1 + +packages: + + /ufo@1.1.1: + resolution: {integrity: sha512-MvlCc4GHrmZdAllBc0iUDowff36Q9Ndw/UzqmEKyrfSzokTd9ZCy1i+IIk5hrYKkjoYVQyNbrw7/F8XJ2rEwTg==} + dev: true diff --git a/test/fixtures/pnpm/pnpm-workspace.yaml b/test/fixtures/pnpm/pnpm-workspace.yaml new file mode 100644 index 0000000..dee51e9 --- /dev/null +++ b/test/fixtures/pnpm/pnpm-workspace.yaml @@ -0,0 +1,2 @@ +packages: + - "packages/*" diff --git a/test/fixtures/yarn-berry/package.json b/test/fixtures/yarn-berry/package.json index e9cd0fe..3d91221 100644 --- a/test/fixtures/yarn-berry/package.json +++ b/test/fixtures/yarn-berry/package.json @@ -2,5 +2,8 @@ "name": "fixture-yarn", "private": true, "version": "0.0.0", + "workspaces": [ + "packages/*" + ], "packageManager": "yarn@3.5.0" } diff --git a/test/fixtures/yarn-berry/packages/workspace-a/package.json b/test/fixtures/yarn-berry/packages/workspace-a/package.json new file mode 100644 index 0000000..bc24ab0 --- /dev/null +++ b/test/fixtures/yarn-berry/packages/workspace-a/package.json @@ -0,0 +1,8 @@ +{ + "private": true, + "version": "0.0.0", + "devDependencies": { + "ufo": "^1.1.1" + }, + "packageManager": "yarn@3.5.0" +} diff --git a/test/fixtures/yarn-berry/yarn.lock b/test/fixtures/yarn-berry/yarn.lock index d4857f8..3f54c6e 100644 --- a/test/fixtures/yarn-berry/yarn.lock +++ b/test/fixtures/yarn-berry/yarn.lock @@ -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 diff --git a/test/index.test.ts b/test/index.test.ts index 874a165..91c2631 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -58,6 +58,7 @@ describe("api", () => { await addDependency("pathe", { cwd: fixtureDirectory, silent: false, + workspace: true, }) ).toBeTruthy(); }, 30_000);