|
| 1 | +/* @flow */ |
| 2 | +import path from 'path'; |
| 3 | +import semver from 'semver'; |
| 4 | +import minimatch from 'minimatch'; |
| 5 | +import map from './util/map.js'; |
| 6 | +import type Config from './config.js'; |
| 7 | +import type {Reporter} from './reporters/index.js'; |
| 8 | +import PackageRequest from './package-request'; |
| 9 | + |
| 10 | +const DIRECTORY_SEPARATOR = '/'; |
| 11 | + |
| 12 | +export type Resolution = { |
| 13 | + name: string, |
| 14 | + version: string, |
| 15 | + pattern: string, |
| 16 | +}; |
| 17 | + |
| 18 | +export type ResolutionMap = { |
| 19 | + [string]: Array<Resolution>, |
| 20 | +}; |
| 21 | + |
| 22 | +export type ResolvedPatternsMap = { |
| 23 | + [string]: string, |
| 24 | +}; |
| 25 | + |
| 26 | +export default class Resolutions { |
| 27 | + constructor(config: Config) { |
| 28 | + this._resolutions = map(); |
| 29 | + this.resolutionsByPackage = map(); |
| 30 | + this.resolvedPatterns = map(); |
| 31 | + this.config = config; |
| 32 | + this.reporter = config.reporter; |
| 33 | + } |
| 34 | + |
| 35 | + _resolutions: {[packageName: string]: string}; |
| 36 | + resolutionsByPackage: ResolutionMap; |
| 37 | + resolvedPatterns: ResolvedPatternsMap; |
| 38 | + config: Config; |
| 39 | + reporter: Reporter; |
| 40 | + |
| 41 | + init(resolutions) { |
| 42 | + this._resolutions = {...this._resolutions, ...resolutions}; |
| 43 | + this.resolvedPatterns = map(); |
| 44 | + |
| 45 | + Object.keys(this._resolutions).map(pattern => { |
| 46 | + const info = this.parsePatternInfo(pattern, this._resolutions[pattern]); |
| 47 | + |
| 48 | + if (info) { |
| 49 | + const resolution = this.resolutionsByPackage[info.name] || []; |
| 50 | + this.resolutionsByPackage[info.name] = [...resolution, info]; |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + getResolutionByPattern(pattern: string): ?string { |
| 56 | + return this.resolvedPatterns[pattern]; |
| 57 | + } |
| 58 | + |
| 59 | + parsePatternInfo(pattern: string, version: string): ?Object { |
| 60 | + const directories = pattern.split(DIRECTORY_SEPARATOR); |
| 61 | + const name = directories.pop(); |
| 62 | + |
| 63 | + if (!name) { |
| 64 | + this.reporter.warn(this.reporter.lang('invalidResolutionName', pattern)); |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + name, |
| 70 | + version, |
| 71 | + pattern, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + find(reqPattern: string, parentNames: Array<string>): ?string { |
| 76 | + const {name, version} = PackageRequest.normalizePattern(reqPattern); |
| 77 | + const resolutions = this.resolutionsByPackage[name]; |
| 78 | + |
| 79 | + if (!resolutions) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + const resolvedPath = path.join(...parentNames, name); |
| 84 | + const resolution = resolutions.find(({pattern}) => minimatch(resolvedPath, pattern)); |
| 85 | + const resolvedPattern = resolution && `${name}@${resolution.version}`; |
| 86 | + |
| 87 | + if (resolvedPattern) { |
| 88 | + if (!semver.valid(resolvedPattern)) { |
| 89 | + this.reporter.warn(this.reporter.lang('invalidResolutionVersion', resolvedPattern)); |
| 90 | + } |
| 91 | + |
| 92 | + if (!semver.satisfies(resolution.version, version)) { |
| 93 | + this.reporter.warn(this.reporter.lang('incompatibleResolutionVersion', resolvedPattern, reqPattern)); |
| 94 | + } |
| 95 | + |
| 96 | + this.resolvedPatterns[reqPattern] = resolvedPattern; |
| 97 | + } |
| 98 | + |
| 99 | + return resolvedPattern; |
| 100 | + } |
| 101 | +} |
0 commit comments