-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathyarn-package-manager.ts
103 lines (87 loc) · 3.5 KB
/
yarn-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
import * as path from "path";
import { BasePackageManager } from "./base-package-manager";
import { exported } from './common/decorators';
export class YarnPackageManager extends BasePackageManager {
constructor(
$childProcess: IChildProcess,
private $errors: IErrors,
$fs: IFileSystem,
$hostInfo: IHostInfo,
private $httpClient: Server.IHttpClient,
private $logger: ILogger,
$pacoteService: IPacoteService
) {
super($childProcess, $fs, $hostInfo, $pacoteService, 'yarn');
}
@exported("yarn")
public async install(packageName: string, pathToSave: string, config: INodePackageManagerInstallOptions): Promise<INpmInstallResultInfo> {
if (config.disableNpmInstall) {
return;
}
if (config.ignoreScripts) {
config['ignore-scripts'] = true;
}
const packageJsonPath = path.join(pathToSave, 'package.json');
const jsonContentBefore = this.$fs.readJson(packageJsonPath);
const flags = this.getFlagsString(config, true);
let params = [];
const isInstallingAllDependencies = packageName === pathToSave;
if (!isInstallingAllDependencies) {
params.push('add', packageName);
}
params = params.concat(flags);
const cwd = pathToSave;
try {
const result = await this.processPackageManagerInstall(packageName, params, { cwd, isInstallingAllDependencies });
return result;
} catch (e) {
this.$fs.writeJson(packageJsonPath, jsonContentBefore);
throw e;
}
}
@exported("yarn")
public uninstall(packageName: string, config?: IDictionary<string | boolean>, path?: string): Promise<string> {
const flags = this.getFlagsString(config, false);
return this.$childProcess.exec(`yarn remove ${packageName} ${flags}`, { cwd: path });
}
@exported("yarn")
public async view(packageName: string, config: Object): Promise<any> {
const wrappedConfig = _.extend({}, config, { json: true });
const flags = this.getFlagsString(wrappedConfig, false);
let viewResult: any;
try {
viewResult = await this.$childProcess.exec(`yarn info ${packageName} ${flags}`);
} catch (e) {
this.$errors.failWithoutHelp(e.message);
}
const result = JSON.parse(viewResult);
return result.data;
}
@exported("yarn")
public search(filter: string[], config: IDictionary<string | boolean>): Promise<string> {
this.$errors.fail("Method not implemented. Yarn does not support searching for packages in the registry.");
return null;
}
public async searchNpms(keyword: string): Promise<INpmsResult> {
const httpRequestResult = await this.$httpClient.httpRequest(`https://api.npms.io/v2/search?q=keywords:${keyword}`);
const result: INpmsResult = JSON.parse(httpRequestResult.body);
return result;
}
@exported("yarn")
public async getRegistryPackageData(packageName: string): Promise<any> {
const registry = await this.$childProcess.exec(`yarn config get registry`);
const url = `${registry.trim()}/${packageName}`;
this.$logger.trace(`Trying to get data from yarn registry for package ${packageName}, url is: ${url}`);
const responseData = (await this.$httpClient.httpRequest(url)).body;
this.$logger.trace(`Successfully received data from yarn registry for package ${packageName}. Response data is: ${responseData}`);
const jsonData = JSON.parse(responseData);
this.$logger.trace(`Successfully parsed data from yarn registry for package ${packageName}.`);
return jsonData;
}
@exported("yarn")
public async getCachePath(): Promise<string> {
const result = await this.$childProcess.exec(`yarn cache dir`);
return result.toString().trim();
}
}
$injector.register("yarn", YarnPackageManager);