forked from unjs/nypm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
161 lines (147 loc) Β· 5.18 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
executeCommand,
resolveOperationOptions,
getWorkspaceArgs,
doesDependencyExist,
} from "./_utils";
import type { OperationOptions } from "./types";
/**
* Installs project dependencies.
*
* @param options - Options to pass to the API call.
* @param options.cwd - The directory to run the command in.
* @param options.silent - Whether to run the command in silent mode.
* @param options.packageManager - The package manager info to use (auto-detected).
*/
export async function installDependencies(
options: Pick<OperationOptions, "cwd" | "silent" | "packageManager"> = {},
) {
const resolvedOptions = await resolveOperationOptions(options);
await executeCommand(resolvedOptions.packageManager.command, ["install"], {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
}
/**
* Adds dependency to the project.
*
* @param name - Name of the dependency to add.
* @param options - Options to pass to the API call.
* @param options.cwd - The directory to run the command in.
* @param options.silent - Whether to run the command in silent mode.
* @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[],
options: OperationOptions = {},
) {
const resolvedOptions = await resolveOperationOptions(options);
const names = Array.isArray(name) ? name : [name];
const args = (
resolvedOptions.packageManager.name === "yarn"
? [
...getWorkspaceArgs(resolvedOptions),
"add",
resolvedOptions.dev ? "-D" : "",
...names,
]
: [
resolvedOptions.packageManager.name === "npm" ? "install" : "add",
...getWorkspaceArgs(resolvedOptions),
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
...names,
]
).filter(Boolean);
await executeCommand(resolvedOptions.packageManager.command, args, {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
}
/**
* Adds dev dependency to the project.
*
* @param name - Name of the dev dependency to add.
* @param options - Options to pass to the API call.
* @param options.cwd - The directory to run the command in.
* @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(
name: string | string[],
options: Omit<OperationOptions, "dev"> = {},
) {
await addDependency(name, { ...options, dev: true });
}
/**
* Removes dependency from the project.
*
* @param name - Name of the dependency to remove.
* @param options - Options to pass to the API call.
* @param options.cwd - The directory to run the command in.
* @param options.silent - Whether to run the command in silent mode.
* @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,
options: OperationOptions = {},
) {
const resolvedOptions = await resolveOperationOptions(options);
const args = (
resolvedOptions.packageManager.name === "yarn"
? [
resolvedOptions.global
? resolvedOptions.packageManager.majorVersion === "1"
? "dlx"
: "global"
: "",
...getWorkspaceArgs(resolvedOptions),
"remove",
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
name,
]
: [
resolvedOptions.packageManager.name === "npm"
? "uninstall"
: "remove",
...getWorkspaceArgs(resolvedOptions),
resolvedOptions.dev ? "-D" : "",
resolvedOptions.global ? "-g" : "",
name,
]
).filter(Boolean);
await executeCommand(resolvedOptions.packageManager.command, args, {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
}
/**
* Ensures dependency is installed.
*
* @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.
* @param options.dev - Whether to install as dev dependency (if not already installed).
* @param options.workspace - The name of the workspace to install dependency in (if not already installed).
*/
export async function ensureDependencyInstalled(
name: string,
options: Pick<OperationOptions, "cwd" | "dev" | "workspace"> = {},
) {
const resolvedOptions = await resolveOperationOptions(options);
const dependencyExists = doesDependencyExist(name, resolvedOptions);
if (dependencyExists) {
return true;
}
await addDependency(name, resolvedOptions);
}