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 global mode support #113

Merged
merged 3 commits into from Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/_utils.ts
Expand Up @@ -73,7 +73,7 @@ export const NO_PACKAGE_MANAGER_DETECTED_ERROR_MSG =
export async function resolveOperationOptions(
options: OperationOptions = {},
): Promise<
NonPartial<Pick<OperationOptions, "cwd" | "silent" | "dev">> &
NonPartial<Pick<OperationOptions, "cwd" | "silent" | "dev" | "global">> &
Pick<OperationOptions, "workspace"> & {
packageManager: PackageManager;
}
Expand All @@ -96,6 +96,7 @@ export async function resolveOperationOptions(
packageManager,
dev: options.dev ?? false,
workspace: options.workspace,
global: options.global ?? false,
};
}

Expand Down
11 changes: 11 additions & 0 deletions src/api.ts
Expand Up @@ -35,6 +35,7 @@
* @param options.packageManager - The package manager info to use (auto-detected).
* @param options.dev - Whether to add the dependency as dev dependency.
* @param options.workspace - The name of the workspace to use.
* @param options.global - Whether to run the command in global mode.
*/
export async function addDependency(
name: string | string[],
Expand All @@ -56,6 +57,7 @@
resolvedOptions.packageManager.name === "npm" ? "install" : "add",
...getWorkspaceArgs(resolvedOptions),
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
...names,
]
).filter(Boolean);
Expand All @@ -75,6 +77,7 @@
* @param options.silent - Whether to run the command in silent mode.
* @param options.packageManager - The package manager info to use (auto-detected).
* @param options.workspace - The name of the workspace to use.
* @param options.global - Whether to run the command in global mode.
*
*/
export async function addDevDependency(
Expand All @@ -94,6 +97,7 @@
* @param options.packageManager - The package manager info to use (auto-detected).
* @param options.dev - Whether to remove dev dependency.
* @param options.workspace - The name of the workspace to use.
* @param options.global - Whether to run the command in global mode.
*/
export async function removeDependency(
name: string,
Expand All @@ -104,9 +108,15 @@
const args = (
resolvedOptions.packageManager.name === "yarn"
? [
resolvedOptions.global
? resolvedOptions.packageManager.majorVersion === "1"

Check failure on line 112 in src/api.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Nest ternary expression should be parenthesized
? "dlx"
DamianGlowala marked this conversation as resolved.
Show resolved Hide resolved
: "global"
: "",
...getWorkspaceArgs(resolvedOptions),
"remove",
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
name,
]
: [
Expand All @@ -115,6 +125,7 @@
: "remove",
...getWorkspaceArgs(resolvedOptions),
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
name,
]
).filter(Boolean);
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Expand Up @@ -34,6 +34,11 @@ const install = defineCommand({
alias: "D",
description: "Add as dev dependency",
},
global: {
type: "boolean",
alias: "g",
description: "Add globally",
},
},
run: async ({ args }) => {
await (args._.length > 0
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Expand Up @@ -15,4 +15,5 @@ export type OperationOptions = {
packageManager?: PackageManager | PackageManagerName;
dev?: boolean;
workspace?: boolean | string;
global?: boolean;
};