* Returns an array of `package.json` files under the given path and subdirectories. * @param {String} basePath The base path for recursive directory search.
(basePath = this.nodeModulesPath)
| 41 | * @param {String} basePath The base path for recursive directory search. |
| 42 | */ |
| 43 | async getPackageFiles(basePath = this.nodeModulesPath) { |
| 44 | try { |
| 45 | // Declare file list |
| 46 | const files = [] |
| 47 | |
| 48 | // Get files |
| 49 | const dirents = await fs.readdir(basePath, { withFileTypes: true }); |
| 50 | const validFiles = dirents.filter(d => d.name.toLowerCase() == 'package.json').map(d => path.join(basePath, d.name)); |
| 51 | files.push(...validFiles); |
| 52 | |
| 53 | // For each directory entry |
| 54 | for (const dirent of dirents) { |
| 55 | if (dirent.isDirectory()) { |
| 56 | const subFiles = await this.getPackageFiles(path.join(basePath, dirent.name)); |
| 57 | files.push(...subFiles); |
| 58 | } |
| 59 | } |
| 60 | return files; |
| 61 | } catch (e) { |
| 62 | throw `Failed to get package.json files in ${this.nodeModulesPath} with error: ${e}`; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Extracts and returns the node engine versions of the given package.json |