|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2022 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import glob from 'glob'; |
| 19 | +import { existsSync } from 'fs'; |
| 20 | +import { resolve } from 'path'; |
| 21 | +import { projectRoot as root } from '../utils'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Top level fields in package.json that may point to entry point and |
| 25 | + * typings files. |
| 26 | + */ |
| 27 | +const TOP_LEVEL_FIELDS = [ |
| 28 | + 'main', |
| 29 | + 'browser', |
| 30 | + 'module', |
| 31 | + 'typings', |
| 32 | + 'react-native', |
| 33 | + 'cordova', |
| 34 | + 'esm5', |
| 35 | + 'webworker', |
| 36 | + 'main-esm' |
| 37 | +]; |
| 38 | + |
| 39 | +interface Result { |
| 40 | + packageName: string; |
| 41 | + found: boolean; |
| 42 | + filePath: string; |
| 43 | + fieldPath: string; |
| 44 | +} |
| 45 | +const results: Result[] = []; |
| 46 | + |
| 47 | +/** |
| 48 | + * Get paths to packages. Only check the ones we actually |
| 49 | + * publish (packages/*). |
| 50 | + */ |
| 51 | +function getPaths(): Promise<string[]> { |
| 52 | + return new Promise((resolve, reject) => { |
| 53 | + glob('packages/*', (err, paths) => { |
| 54 | + if (err) reject(err); |
| 55 | + resolve(paths); |
| 56 | + }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Recursively check `exports` field in package.json. |
| 62 | + */ |
| 63 | +function checkExports( |
| 64 | + pkgName: string, |
| 65 | + pkgRoot: string, |
| 66 | + path: string = '', |
| 67 | + exports: Record<string, any> |
| 68 | +) { |
| 69 | + for (const key in exports) { |
| 70 | + if (typeof exports[key] === 'string') { |
| 71 | + const filePath = resolve(pkgRoot, exports[key]); |
| 72 | + const result = { |
| 73 | + packageName: pkgName, |
| 74 | + found: false, |
| 75 | + filePath, |
| 76 | + fieldPath: `exports${path}[${key}]` |
| 77 | + }; |
| 78 | + if (existsSync(filePath)) { |
| 79 | + result.found = true; |
| 80 | + } |
| 81 | + results.push(result); |
| 82 | + } else { |
| 83 | + checkExports( |
| 84 | + pkgName, |
| 85 | + pkgRoot, |
| 86 | + path ? `${path}[${key}]` : `[${key}]`, |
| 87 | + exports[key] |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +async function main() { |
| 94 | + const paths = await getPaths(); |
| 95 | + |
| 96 | + for (const path of paths) { |
| 97 | + const pkgRoot = `${root}/${path}`; |
| 98 | + if (existsSync(`${pkgRoot}/package.json`)) { |
| 99 | + const pkg = require(`${pkgRoot}/package.json`); |
| 100 | + |
| 101 | + /** |
| 102 | + * Check top level fields. |
| 103 | + */ |
| 104 | + for (const field of TOP_LEVEL_FIELDS) { |
| 105 | + if (pkg[field]) { |
| 106 | + const filePath = resolve(pkgRoot, pkg[field]); |
| 107 | + const result = { |
| 108 | + packageName: pkg.name, |
| 109 | + found: false, |
| 110 | + filePath, |
| 111 | + fieldPath: field |
| 112 | + }; |
| 113 | + if (existsSync(filePath)) { |
| 114 | + result.found = true; |
| 115 | + } |
| 116 | + results.push(result); |
| 117 | + } |
| 118 | + } |
| 119 | + /** |
| 120 | + * Check all levels of exports field. |
| 121 | + */ |
| 122 | + if (pkg.exports) { |
| 123 | + checkExports(pkg.name, pkgRoot, '', pkg.exports); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + let missingPaths: boolean = false; |
| 129 | + |
| 130 | + for (const result of results) { |
| 131 | + if (!result.found) { |
| 132 | + missingPaths = true; |
| 133 | + console.log( |
| 134 | + `${result.packageName}: Field "${result.fieldPath}" ` + |
| 135 | + `points to ${result.filePath} which is not found.` |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Fail CI if any missing paths found. |
| 142 | + */ |
| 143 | + if (missingPaths) { |
| 144 | + process.exit(1); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +main(); |
0 commit comments