-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpackage-manager.ts
177 lines (159 loc) · 4.56 KB
/
package-manager.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { cache, exported, invokeInit } from "./common/decorators";
import { performanceLog } from "./common/decorators";
import { PackageManagers } from "./constants";
import {
IPackageManager,
INodePackageManager,
IOptions,
INodePackageManagerInstallOptions,
INpmInstallResultInfo,
INpmsResult,
INpmPackageNameParts,
} from "./declarations";
import {
IErrors,
IUserSettingsService,
IDictionary,
} from "./common/declarations";
import { injector } from "./common/yok";
import { IProjectConfigService } from "./definitions/project";
export class PackageManager implements IPackageManager {
private packageManager: INodePackageManager;
private _packageManagerName: string;
constructor(
private $errors: IErrors,
private $npm: INodePackageManager,
private $options: IOptions,
private $yarn: INodePackageManager,
private $pnpm: INodePackageManager,
private $logger: ILogger,
private $userSettingsService: IUserSettingsService,
private $projectConfigService: IProjectConfigService
) {}
@cache()
protected async init(): Promise<void> {
this.packageManager = await this._determinePackageManager();
}
@invokeInit()
public async getPackageManagerName(): Promise<string> {
return this._packageManagerName;
}
@exported("packageManager")
@performanceLog()
@invokeInit()
public install(
packageName: string,
pathToSave: string,
config: INodePackageManagerInstallOptions
): Promise<INpmInstallResultInfo> {
return this.packageManager.install(packageName, pathToSave, config);
}
@exported("packageManager")
@invokeInit()
public uninstall(
packageName: string,
config?: IDictionary<string | boolean>,
path?: string
): Promise<string> {
return this.packageManager.uninstall(packageName, config, path);
}
@exported("packageManager")
@invokeInit()
public view(packageName: string, config: Object): Promise<any> {
return this.packageManager.view(packageName, config);
}
@exported("packageManager")
@invokeInit()
public search(
filter: string[],
config: IDictionary<string | boolean>
): Promise<string> {
return this.packageManager.search(filter, config);
}
@invokeInit()
public searchNpms(keyword: string): Promise<INpmsResult> {
return this.packageManager.searchNpms(keyword);
}
@invokeInit()
public async isRegistered(packageName: string): Promise<boolean> {
return this.packageManager.isRegistered(packageName);
}
@invokeInit()
public async getPackageFullName(
packageNameParts: INpmPackageNameParts
): Promise<string> {
return this.packageManager.getPackageFullName(packageNameParts);
}
@invokeInit()
public async getPackageNameParts(
fullPackageName: string
): Promise<INpmPackageNameParts> {
return this.packageManager.getPackageNameParts(fullPackageName);
}
@invokeInit()
public getRegistryPackageData(packageName: string): Promise<any> {
return this.packageManager.getRegistryPackageData(packageName);
}
@invokeInit()
public getCachePath(): Promise<string> {
return this.packageManager.getCachePath();
}
public async getTagVersion(
packageName: string,
tag: string
): Promise<string> {
let version: string = null;
if (!tag) {
return null;
}
try {
const result = await this.view(packageName, { "dist-tags": true });
version = result[tag];
} catch (err) {
this.$logger.trace(
`Error while getting tag version from view command: ${err}`
);
const registryData = await this.getRegistryPackageData(packageName);
version = registryData["dist-tags"][tag];
}
return version;
}
private async _determinePackageManager(): Promise<INodePackageManager> {
let pm = null;
try {
pm = await this.$userSettingsService.getSettingValue("packageManager");
} catch (err) {
this.$errors.fail(
`Unable to read package manager config from user settings ${err}`
);
}
try {
const configPm = this.$projectConfigService.getValue(
"cli.packageManager"
);
if (configPm) {
this.$logger.trace(
`Determined packageManager to use from user config is: ${configPm}`
);
pm = configPm;
}
} catch (err) {
// ignore error, but log info
this.$logger.trace(
"Tried to read cli.packageManager from project config and failed. Error is: ",
err
);
}
if (pm === PackageManagers.yarn || this.$options.yarn) {
this._packageManagerName = PackageManagers.yarn;
return this.$yarn;
} else if (pm === PackageManagers.pnpm || this.$options.pnpm) {
this._packageManagerName = PackageManagers.pnpm;
return this.$pnpm;
} else {
this._packageManagerName = PackageManagers.npm;
return this.$npm;
}
}
}
injector.register("packageManager", PackageManager);