Skip to content
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

feat: add getSpecifiedPackageInfo #136

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
36 changes: 36 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,39 @@ export async function ensureDependencyInstalled(

await addDependency(name, resolvedOptions);
}

interface SpecifiedPackageInfo {
version: string;
dev?: boolean;
peer?: string | false;
optional?: boolean;
}

/**
* Gets information about a dependency requirement from the `package.json` in a user directory.
*
* @param name - Name of the dependency.
* @param options - Options to pass to the API call.
* @param options.cwd - The directory to run the command in.
*/
export async function getSpecifiedPackageInfo(
name: string,
options: Pick<OperationOptions, "cwd"> = {},
) {
const resolvedOptions = await resolveOperationOptions(options);
const userPkgJson = await readPackageJSON(resolvedOptions.cwd);
const dep = userPkgJson.dependencies?.[name];
const devDep = userPkgJson.devDependencies?.[name];
const peerDep = userPkgJson.peerDependencies?.[name];
const version = dep || devDep;
if (!version) {
return;
}
const meta: SpecifiedPackageInfo = {
version,
dev: !!devDep,
peer: peerDep || false,
optional: !!userPkgJson.peerDependenciesMeta?.[name]?.optional,
};
return meta;
}
Loading