|
| 1 | +import * as path from "path"; |
| 2 | +import { BasePackageManager } from "./base-package-manager"; |
| 3 | +import { exported, cache } from "./common/decorators"; |
| 4 | +import { CACACHE_DIRECTORY_NAME } from "./constants"; |
| 5 | +import * as _ from "lodash"; |
| 6 | +import { |
| 7 | + INodePackageManagerInstallOptions, |
| 8 | + INpmInstallResultInfo, |
| 9 | + INpmsResult, |
| 10 | +} from "./declarations"; |
| 11 | +import { |
| 12 | + IChildProcess, |
| 13 | + IErrors, |
| 14 | + IFileSystem, |
| 15 | + IHostInfo, |
| 16 | + Server, |
| 17 | +} from "./common/declarations"; |
| 18 | +import { injector } from "./common/yok"; |
| 19 | + |
| 20 | +export class BunPackageManager extends BasePackageManager { |
| 21 | + constructor( |
| 22 | + $childProcess: IChildProcess, |
| 23 | + private $errors: IErrors, |
| 24 | + $fs: IFileSystem, |
| 25 | + $hostInfo: IHostInfo, |
| 26 | + private $logger: ILogger, |
| 27 | + private $httpClient: Server.IHttpClient, |
| 28 | + $pacoteService: IPacoteService |
| 29 | + ) { |
| 30 | + super($childProcess, $fs, $hostInfo, $pacoteService, "bun"); |
| 31 | + } |
| 32 | + |
| 33 | + @exported("bun") |
| 34 | + public async install( |
| 35 | + packageName: string, |
| 36 | + pathToSave: string, |
| 37 | + config: INodePackageManagerInstallOptions |
| 38 | + ): Promise<INpmInstallResultInfo> { |
| 39 | + if (config.disableNpmInstall) { |
| 40 | + return; |
| 41 | + } |
| 42 | + if (config.ignoreScripts) { |
| 43 | + config["ignore-scripts"] = true; |
| 44 | + } |
| 45 | + |
| 46 | + const packageJsonPath = path.join(pathToSave, "package.json"); |
| 47 | + const jsonContentBefore = this.$fs.readJson(packageJsonPath); |
| 48 | + |
| 49 | + const flags = this.getFlagsString(config, true); |
| 50 | + // TODO: Confirm desired behavior. The npm version uses --legacy-peer-deps |
| 51 | + // by default, we could use `--no-peer` for Bun if similar is needed; the |
| 52 | + // pnpm version uses `--shamefully-hoist`, but Bun has no similar flag. |
| 53 | + let params = ["install", "--legacy-peer-deps"]; |
| 54 | + const isInstallingAllDependencies = packageName === pathToSave; |
| 55 | + if (!isInstallingAllDependencies) { |
| 56 | + params.push(packageName); |
| 57 | + } |
| 58 | + |
| 59 | + params = params.concat(flags); |
| 60 | + const cwd = pathToSave; |
| 61 | + |
| 62 | + try { |
| 63 | + const result = await this.processPackageManagerInstall( |
| 64 | + packageName, |
| 65 | + params, |
| 66 | + { cwd, isInstallingAllDependencies } |
| 67 | + ); |
| 68 | + return result; |
| 69 | + } catch (err) { |
| 70 | + // Revert package.json contents to preserve valid state |
| 71 | + this.$fs.writeJson(packageJsonPath, jsonContentBefore); |
| 72 | + throw err; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @exported("bun") |
| 77 | + public async uninstall( |
| 78 | + packageName: string, |
| 79 | + config?: any, |
| 80 | + cwd?: string |
| 81 | + ): Promise<string> { |
| 82 | + const flags = this.getFlagsString(config, false); |
| 83 | + return this.$childProcess.exec(`bun remove ${packageName} ${flags}`, { |
| 84 | + cwd, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + // Bun does not have a `view` command; use npm. |
| 89 | + @exported("bun") |
| 90 | + public async view(packageName: string, config: Object): Promise<any> { |
| 91 | + const wrappedConfig = _.extend({}, config, { json: true }); // always require view response as JSON |
| 92 | + |
| 93 | + const flags = this.getFlagsString(wrappedConfig, false); |
| 94 | + let viewResult: any; |
| 95 | + try { |
| 96 | + viewResult = await this.$childProcess.exec( |
| 97 | + `npm view ${packageName} ${flags}` |
| 98 | + ); |
| 99 | + } catch (e) { |
| 100 | + this.$errors.fail(e.message); |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + return JSON.parse(viewResult); |
| 105 | + } catch (err) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Bun does not have a `search` command; use npm. |
| 111 | + @exported("bun") |
| 112 | + public async search(filter: string[], config: any): Promise<string> { |
| 113 | + const flags = this.getFlagsString(config, false); |
| 114 | + return this.$childProcess.exec(`npm search ${filter.join(" ")} ${flags}`); |
| 115 | + } |
| 116 | + |
| 117 | + public async searchNpms(keyword: string): Promise<INpmsResult> { |
| 118 | + // Bugs with npms.io: |
| 119 | + // 1. API returns no results when a valid package name contains @ or / |
| 120 | + // even if using encodeURIComponent(). |
| 121 | + // 2. npms.io's API no longer returns updated results; see |
| 122 | + // https://github.com/npms-io/npms-api/issues/112. Better to switch to |
| 123 | + // https://registry.npmjs.org/<query> |
| 124 | + const httpRequestResult = await this.$httpClient.httpRequest( |
| 125 | + `https://api.npms.io/v2/search?q=keywords:${keyword}` |
| 126 | + ); |
| 127 | + const result: INpmsResult = JSON.parse(httpRequestResult.body); |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + // Bun does not have a command analogous to `npm config get registry`; Bun |
| 132 | + // uses `bunfig.toml` to define custom registries. |
| 133 | + // - TODO: read `bunfig.toml`, if it exists, and return the registry URL. |
| 134 | + public async getRegistryPackageData(packageName: string): Promise<any> { |
| 135 | + const registry = await this.$childProcess.exec(`npm config get registry`); |
| 136 | + const url = registry.trim() + packageName; |
| 137 | + this.$logger.trace( |
| 138 | + `Trying to get data from npm registry for package ${packageName}, url is: ${url}` |
| 139 | + ); |
| 140 | + const responseData = (await this.$httpClient.httpRequest(url)).body; |
| 141 | + this.$logger.trace( |
| 142 | + `Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}` |
| 143 | + ); |
| 144 | + const jsonData = JSON.parse(responseData); |
| 145 | + this.$logger.trace( |
| 146 | + `Successfully parsed data from npm registry for package ${packageName}.` |
| 147 | + ); |
| 148 | + return jsonData; |
| 149 | + } |
| 150 | + |
| 151 | + @cache() |
| 152 | + public async getCachePath(): Promise<string> { |
| 153 | + const cachePath = await this.$childProcess.exec(`bun pm cache`); |
| 154 | + return path.join(cachePath.trim(), CACACHE_DIRECTORY_NAME); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +injector.register("bun", BunPackageManager); |
0 commit comments