|
| 1 | +import * as path from "path"; |
| 2 | +import * as _ from "lodash"; |
| 3 | +import { BasePackageManager } from "./base-package-manager"; |
| 4 | +import { exported } from "./common/decorators"; |
| 5 | +import { |
| 6 | + INodePackageManagerInstallOptions, |
| 7 | + INpmInstallResultInfo, |
| 8 | + INpmsResult, |
| 9 | +} from "./declarations"; |
| 10 | +import { |
| 11 | + IChildProcess, |
| 12 | + IErrors, |
| 13 | + IFileSystem, |
| 14 | + IHostInfo, |
| 15 | + Server, |
| 16 | + IDictionary, |
| 17 | +} from "./common/declarations"; |
| 18 | +import { injector } from "./common/yok"; |
| 19 | + |
| 20 | +export class Yarn2PackageManager extends BasePackageManager { |
| 21 | + private $hostInfo_: IHostInfo; |
| 22 | + constructor( |
| 23 | + $childProcess: IChildProcess, |
| 24 | + private $errors: IErrors, |
| 25 | + $fs: IFileSystem, |
| 26 | + $hostInfo: IHostInfo, |
| 27 | + private $httpClient: Server.IHttpClient, |
| 28 | + private $logger: ILogger, |
| 29 | + $pacoteService: IPacoteService |
| 30 | + ) { |
| 31 | + super($childProcess, $fs, $hostInfo, $pacoteService, "yarn2"); |
| 32 | + this.$hostInfo_ = $hostInfo; |
| 33 | + } |
| 34 | + |
| 35 | + protected getPackageManagerExecutableName(): string { |
| 36 | + let executableName = "yarn"; |
| 37 | + |
| 38 | + if (this.$hostInfo_.isWindows) { |
| 39 | + executableName += ".cmd"; |
| 40 | + } |
| 41 | + |
| 42 | + return executableName; |
| 43 | + } |
| 44 | + |
| 45 | + @exported("yarn2") |
| 46 | + public async install( |
| 47 | + packageName: string, |
| 48 | + pathToSave: string, |
| 49 | + config: INodePackageManagerInstallOptions |
| 50 | + ): Promise<INpmInstallResultInfo> { |
| 51 | + if (config.disableNpmInstall) { |
| 52 | + return; |
| 53 | + } |
| 54 | + if (config.ignoreScripts) { |
| 55 | + config["ignore-scripts"] = true; |
| 56 | + } |
| 57 | + |
| 58 | + const packageJsonPath = path.join(pathToSave, "package.json"); |
| 59 | + const jsonContentBefore = this.$fs.readJson(packageJsonPath); |
| 60 | + |
| 61 | + // remove unsupported flags |
| 62 | + // todo: refactor all package managers to map typed flags to the actual flags |
| 63 | + const cleanedConfig = _.omit(config, ["save-dev", "save-exact"]); |
| 64 | + |
| 65 | + const flags = this.getFlagsString(cleanedConfig, true); |
| 66 | + let params = []; |
| 67 | + const isInstallingAllDependencies = packageName === pathToSave; |
| 68 | + if (!isInstallingAllDependencies) { |
| 69 | + params.push("add", packageName); |
| 70 | + } |
| 71 | + |
| 72 | + params = params.concat(flags); |
| 73 | + const cwd = pathToSave; |
| 74 | + |
| 75 | + try { |
| 76 | + const result = await this.processPackageManagerInstall( |
| 77 | + packageName, |
| 78 | + params, |
| 79 | + { cwd, isInstallingAllDependencies } |
| 80 | + ); |
| 81 | + return result; |
| 82 | + } catch (e) { |
| 83 | + this.$fs.writeJson(packageJsonPath, jsonContentBefore); |
| 84 | + throw e; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @exported("yarn2") |
| 89 | + public uninstall( |
| 90 | + packageName: string, |
| 91 | + config?: IDictionary<string | boolean>, |
| 92 | + cwd?: string |
| 93 | + ): Promise<string> { |
| 94 | + const flags = this.getFlagsString(config, false); |
| 95 | + return this.$childProcess.exec(`yarn remove ${packageName} ${flags}`, { |
| 96 | + cwd, |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + @exported("yarn2") |
| 101 | + public async view(packageName: string, config: Object): Promise<any> { |
| 102 | + const wrappedConfig = _.extend({}, config, { json: true }); |
| 103 | + |
| 104 | + const flags = this.getFlagsString(wrappedConfig, false); |
| 105 | + let viewResult: any; |
| 106 | + try { |
| 107 | + viewResult = await this.$childProcess.exec( |
| 108 | + `yarn npm info ${packageName} ${flags}` |
| 109 | + ); |
| 110 | + } catch (e) { |
| 111 | + this.$errors.fail(e.message); |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + return JSON.parse(viewResult); |
| 116 | + } catch (err) { |
| 117 | + this.$errors.fail(err.message); |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @exported("yarn2") |
| 123 | + public search( |
| 124 | + filter: string[], |
| 125 | + config: IDictionary<string | boolean> |
| 126 | + ): Promise<string> { |
| 127 | + this.$errors.fail( |
| 128 | + "Method not implemented. Yarn does not support searching for packages in the registry." |
| 129 | + ); |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + public async searchNpms(keyword: string): Promise<INpmsResult> { |
| 134 | + const httpRequestResult = await this.$httpClient.httpRequest( |
| 135 | + `https://api.npms.io/v2/search?q=keywords:${keyword}` |
| 136 | + ); |
| 137 | + const result: INpmsResult = JSON.parse(httpRequestResult.body); |
| 138 | + return result; |
| 139 | + } |
| 140 | + |
| 141 | + @exported("yarn2") |
| 142 | + public async getRegistryPackageData(packageName: string): Promise<any> { |
| 143 | + const registry = await this.$childProcess.exec( |
| 144 | + `yarn config get npmRegistryServer` |
| 145 | + ); |
| 146 | + const url = `${registry.trim()}/${packageName}`; |
| 147 | + this.$logger.trace( |
| 148 | + `Trying to get data from yarn registry for package ${packageName}, url is: ${url}` |
| 149 | + ); |
| 150 | + const responseData = (await this.$httpClient.httpRequest(url)).body; |
| 151 | + this.$logger.trace( |
| 152 | + `Successfully received data from yarn registry for package ${packageName}. Response data is: ${responseData}` |
| 153 | + ); |
| 154 | + const jsonData = JSON.parse(responseData); |
| 155 | + this.$logger.trace( |
| 156 | + `Successfully parsed data from yarn registry for package ${packageName}.` |
| 157 | + ); |
| 158 | + return jsonData; |
| 159 | + } |
| 160 | + |
| 161 | + @exported("yarn2") |
| 162 | + public async getCachePath(): Promise<string> { |
| 163 | + const result = await this.$childProcess.exec(`yarn config get cacheFolder`); |
| 164 | + return result.toString().trim(); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +injector.register("yarn2", Yarn2PackageManager); |
0 commit comments