Skip to content

Commit

Permalink
feat: add global mode support (#113)
Browse files Browse the repository at this point in the history
* feat: add global mode support

* chore: lint

* chore: apply automated lint fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
DamianGlowala and autofix-ci[bot] committed Mar 3, 2024
1 parent b36af77 commit bea8cd9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
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 @@ export async function installDependencies(
* @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 @@ export async function addDependency(
resolvedOptions.packageManager.name === "npm" ? "install" : "add",
...getWorkspaceArgs(resolvedOptions),
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
...names,
]
).filter(Boolean);
Expand All @@ -75,6 +77,7 @@ export async function addDependency(
* @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 @@ export async function addDevDependency(
* @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 @@ export async function removeDependency(
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"
: "global"
: "",
...getWorkspaceArgs(resolvedOptions),
"remove",
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
name,
]
: [
Expand All @@ -115,6 +125,7 @@ export async function removeDependency(
: "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;
};

0 comments on commit bea8cd9

Please sign in to comment.