forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbun-package-manager.ts
155 lines (140 loc) · 4.57 KB
/
bun-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
import * as path from "path";
import { BasePackageManager } from "./base-package-manager";
import { exported, cache } from "./common/decorators";
import { CACACHE_DIRECTORY_NAME } from "./constants";
import * as _ from "lodash";
import {
INodePackageManagerInstallOptions,
INpmInstallResultInfo,
INpmsResult,
} from "./declarations";
import {
IChildProcess,
IErrors,
IFileSystem,
IHostInfo,
Server,
} from "./common/declarations";
import { injector } from "./common/yok";
export class BunPackageManager extends BasePackageManager {
constructor(
$childProcess: IChildProcess,
private $errors: IErrors,
$fs: IFileSystem,
$hostInfo: IHostInfo,
private $logger: ILogger,
private $httpClient: Server.IHttpClient,
$pacoteService: IPacoteService
) {
super($childProcess, $fs, $hostInfo, $pacoteService, "bun");
}
@exported("bun")
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 = ["install"];
const isInstallingAllDependencies = packageName === pathToSave;
if (!isInstallingAllDependencies) {
params.push(packageName);
}
params = params.concat(flags);
const cwd = pathToSave;
try {
const result = await this.processPackageManagerInstall(
packageName,
params,
{ cwd, isInstallingAllDependencies }
);
return result;
} catch (err) {
// Revert package.json contents to preserve valid state
this.$fs.writeJson(packageJsonPath, jsonContentBefore);
throw err;
}
}
@exported("bun")
public async uninstall(
packageName: string,
config?: any,
cwd?: string
): Promise<string> {
const flags = this.getFlagsString(config, false);
return this.$childProcess.exec(`bun remove ${packageName} ${flags}`, {
cwd,
});
}
// Bun does not have a `view` command; use npm.
@exported("bun")
public async view(packageName: string, config: Object): Promise<any> {
const wrappedConfig = _.extend({}, config, { json: true }); // always require view response as JSON
const flags = this.getFlagsString(wrappedConfig, false);
let viewResult: any;
try {
viewResult = await this.$childProcess.exec(
`npm view ${packageName} ${flags}`
);
} catch (e) {
this.$errors.fail(e.message);
}
try {
return JSON.parse(viewResult);
} catch (err) {
return null;
}
}
// Bun does not have a `search` command; use npm.
@exported("bun")
public async search(filter: string[], config: any): Promise<string> {
const flags = this.getFlagsString(config, false);
return this.$childProcess.exec(`npm search ${filter.join(" ")} ${flags}`);
}
public async searchNpms(keyword: string): Promise<INpmsResult> {
// Bugs with npms.io:
// 1. API returns no results when a valid package name contains @ or /
// even if using encodeURIComponent().
// 2. npms.io's API no longer returns updated results; see
// https://github.com/npms-io/npms-api/issues/112. Better to switch to
// https://registry.npmjs.org/<query>
const httpRequestResult = await this.$httpClient.httpRequest(
`https://api.npms.io/v2/search?q=keywords:${keyword}`
);
const result: INpmsResult = JSON.parse(httpRequestResult.body);
return result;
}
// Bun does not have a command analogous to `npm config get registry`; Bun
// uses `bunfig.toml` to define custom registries.
// - TODO: read `bunfig.toml`, if it exists, and return the registry URL.
public async getRegistryPackageData(packageName: string): Promise<any> {
const registry = await this.$childProcess.exec(`npm config get registry`);
const url = registry.trim() + packageName;
this.$logger.trace(
`Trying to get data from npm registry for package ${packageName}, url is: ${url}`
);
const responseData = (await this.$httpClient.httpRequest(url)).body;
this.$logger.trace(
`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`
);
const jsonData = JSON.parse(responseData);
this.$logger.trace(
`Successfully parsed data from npm registry for package ${packageName}.`
);
return jsonData;
}
@cache()
public async getCachePath(): Promise<string> {
const cachePath = await this.$childProcess.exec(`bun pm cache`);
return path.join(cachePath.trim(), CACACHE_DIRECTORY_NAME);
}
}
injector.register("bun", BunPackageManager);