|
| 1 | +/* tslint:disable:no-constant-condition */ |
| 2 | + |
| 3 | +import * as fsx from 'fs-extra'; |
| 4 | +import * as path from 'path'; |
| 5 | +import JsonFile from './JsonFile'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents a package.json file. |
| 9 | + */ |
| 10 | +interface IPackageJson { |
| 11 | + name: string; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * This class provides methods for finding the nearest "package.json" for a folder |
| 16 | + * and retrieving the name of the package. The results are cached. |
| 17 | + */ |
| 18 | +export default class PackageJsonLookup { |
| 19 | + // Cached the return values for tryFindPackagePathUpwards(): |
| 20 | + // sourceFilePath --> packageJsonFolder |
| 21 | + private _packageFolderCache: Map<string, string | undefined>; |
| 22 | + |
| 23 | + // Cached the return values for readPackageName(): |
| 24 | + // packageJsonPath --> packageName |
| 25 | + private _packageNameCache: Map<string, string>; |
| 26 | + |
| 27 | + constructor() { |
| 28 | + this._packageFolderCache = new Map<string, string | undefined>(); |
| 29 | + this._packageNameCache = new Map<string, string>(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Finds the path to the package folder of a given currentPath, by probing |
| 34 | + * upwards from the currentPath until a package.json file is found. |
| 35 | + * If no package.json can be found, undefined is returned. |
| 36 | + * |
| 37 | + * @param currentPath - a path (relative or absolute) of the current location |
| 38 | + * @returns a relative path to the package folder |
| 39 | + */ |
| 40 | + public tryFindPackagePathUpwards(sourceFilePath: string): string | undefined { |
| 41 | + // Two lookups are required, because get() cannot distinguish the undefined value |
| 42 | + // versus a missing key. |
| 43 | + if (this._packageFolderCache.has(sourceFilePath)) { |
| 44 | + return this._packageFolderCache.get(sourceFilePath); |
| 45 | + } |
| 46 | + |
| 47 | + let result: string | undefined; |
| 48 | + |
| 49 | + const parentFolder: string = path.dirname(sourceFilePath); |
| 50 | + if (!parentFolder || parentFolder === sourceFilePath) { |
| 51 | + result = undefined; |
| 52 | + } else if (fsx.existsSync(path.join(parentFolder, 'package.json'))) { |
| 53 | + result = path.normalize(parentFolder); |
| 54 | + } else { |
| 55 | + result = this.tryFindPackagePathUpwards(parentFolder); |
| 56 | + } |
| 57 | + |
| 58 | + this._packageFolderCache.set(sourceFilePath, result); |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Loads the package.json file and returns the name of the package. |
| 64 | + * |
| 65 | + * @param packageJsonPath - an absolute path to the folder containing the |
| 66 | + * package.json file, it does not include the 'package.json' suffix. |
| 67 | + * @returns the name of the package (E.g. @microsoft/api-extractor) |
| 68 | + */ |
| 69 | + public readPackageName(packageJsonPath: string): string { |
| 70 | + let result: string = this._packageNameCache.get(packageJsonPath); |
| 71 | + if (result !== undefined) { |
| 72 | + return result; |
| 73 | + } |
| 74 | + |
| 75 | + const packageJson: IPackageJson = JsonFile.loadJsonFile(path.join(packageJsonPath, 'package.json')) as IPackageJson; |
| 76 | + result = packageJson.name; |
| 77 | + |
| 78 | + this._packageNameCache.set(packageJsonPath, result); |
| 79 | + return result; |
| 80 | + } |
| 81 | +} |
0 commit comments