Skip to content

Commit

Permalink
feat: allow installing project dependencies (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianGlowala authored Jun 19, 2023
1 parent 7e045be commit 7cbd01f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ Install package:
# npm
npm install nypm

# yarn
yarn add nypm

# pnpm
pnpm install nypm

# yarn
yarn add nypm
```

Import:
Expand All @@ -63,6 +63,7 @@ Import:
// ESM
import {
detectPackageManager,
installDependencies,
addDependency,
addDevDependency,
removeDependendency,
Expand All @@ -71,6 +72,7 @@ import {
// CommonJS
const {
detectPackageManager,
installDependencies,
addDependency,
addDevDependency,
removeDependendency,
Expand Down
50 changes: 33 additions & 17 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,61 @@ import { PackageManager } from "./types";

export type OperationOptions = {
/**
* The directory to run the command in
* The directory to run the command in.
*
* @default process.cwd()
*/
cwd?: string;

/**
* Whether to run the command in silent mode
* Whether to run the command in silent mode.
*
* @default false
*/
silent?: boolean;

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

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

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

/**
* Add Dependency to the project
* Installs project dependencies.
*
* @param name- Name of the dependency to add
* @param _options - Options to pass to the API call
* @param _options - Options to pass to the API call.
*/
export async function installDependencies(
_options: Omit<OperationOptions, "dev" | "workspace"> = {}
) {
const options = await _resolveOptions(_options);

return await runCorepack(options.packageManager.command, ["install"], {
cwd: options.cwd,
silent: options.silent,
});
}

/**
* Adds dependency to the project.
*
* @param name - Name of the dependency to add.
* @param _options - Options to pass to the API call.
*/
export async function addDependency(
name: string,
Expand All @@ -69,27 +85,27 @@ export async function addDependency(
}

/**
* Add a dev dependency to the project
* Adds dev dependency to the project.
*
* @param name - Name of the dependency to add
* @param _options - Options to pass to the API call
* @param name - Name of the dependency to add.
* @param _options - Options to pass to the API call.
*/
export async function addDevDependency(
name: string,
_options: Exclude<OperationOptions, "dev"> = {}
_options: Omit<OperationOptions, "dev"> = {}
) {
return await addDependency(name, { ..._options, dev: true });
}

/**
* This function removes a dependency from the project
* Removes dependency from the project.
*
* @param name - Name of the dependency to remove
* @param _options - Options to pass to the API call
* @param name - Name of the dependency to remove.
* @param _options - Options to pass to the API call.
*/
export async function removeDependency(
name: string,
_options: Exclude<OperationOptions, "workspace"> = {}
_options: Omit<OperationOptions, "workspace"> = {}
) {
const options = await _resolveOptions(_options);

Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/yarn-berry/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@

__metadata:
version: 6
cacheKey: 8

"fixture-yarn@workspace:.":
version: 0.0.0-use.local
resolution: "fixture-yarn@workspace:."
languageName: unknown
linkType: soft

"ufo@npm:^1.1.1":
version: 1.1.2
resolution: "ufo@npm:1.1.2"
checksum: 83c940a6a23b6d4fc0cd116265bb5dcf88ab34a408ad9196e413270ca607a4781c09b547dc518f43caee128a096f20fe80b5a0e62b4bcc0a868619896106d048
languageName: node
linkType: hard

"workspace-a-2ddc4d@workspace:packages/workspace-a":
version: 0.0.0-use.local
resolution: "workspace-a-2ddc4d@workspace:packages/workspace-a"
dependencies:
ufo: ^1.1.1
languageName: unknown
linkType: soft
20 changes: 19 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { fileURLToPath } from "node:url";
import { expect, it, describe } from "vitest";
import { detectPackageManager, addDependency, removeDependency } from "../src";
import {
detectPackageManager,
addDependency,
removeDependency,
installDependencies,
} from "../src";

const resolveFixtureDirectory = (name: string) =>
fileURLToPath(new URL(`fixtures/${name}`, import.meta.url));
Expand Down Expand Up @@ -30,12 +35,14 @@ describe("detectPackageManager", () => {
for (const fixture of fixtures) {
describe(fixture.name, () => {
const fixtureDirectory = resolveFixtureDirectory(fixture.name);

it("should detect with lock file", async () => {
const detected = await detectPackageManager(fixtureDirectory, {
ignorePackageJSON: true,
});
expect(detected).toMatchObject({ name: fixture.pm });
});

it("should detect with package.json", async () => {
const detected = await detectPackageManager(fixtureDirectory, {
ignoreLockFile: true,
Expand All @@ -53,6 +60,16 @@ describe("api", () => {
for (const fixture of fixtures) {
describe(fixture.name, () => {
const fixtureDirectory = resolveFixtureDirectory(fixture.name);

it("installDependencies", async () => {
expect(
await installDependencies({
cwd: fixtureDirectory,
silent: false,
})
).toBeTruthy();
}, 30_000);

it("addDependency", async () => {
expect(
await addDependency("pathe", {
Expand All @@ -62,6 +79,7 @@ describe("api", () => {
})
).toBeTruthy();
}, 30_000);

it("removeDependency", async () => {
expect(
await removeDependency("pathe", {
Expand Down

0 comments on commit 7cbd01f

Please sign in to comment.