|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * @copyright Copyright 2024 Kevin Locke <[email protected]> |
| 4 | + * @license MIT |
| 5 | + */ |
| 6 | + |
| 7 | +/* eslint-disable no-console */ |
| 8 | + |
| 9 | +import { readdir, readFile } from 'node:fs/promises'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | +import { debuglog } from 'node:util'; |
| 13 | + |
| 14 | +const debug = debuglog('openapi-transformers-preversion'); |
| 15 | + |
| 16 | +const packagePath = path.dirname(path.dirname(fileURLToPath(import.meta.url))); |
| 17 | + |
| 18 | +const indexJsPath = path.join(packagePath, 'index.js'); |
| 19 | +const indexJs = await readFile(indexJsPath, { encoding: 'utf8' }); |
| 20 | + |
| 21 | +const packageJsonPath = path.join(packagePath, 'package.json'); |
| 22 | +const packageJsonStr = await readFile(packageJsonPath, { encoding: 'utf8' }); |
| 23 | +const packageJson = JSON.parse(packageJsonStr); |
| 24 | + |
| 25 | +const readmePath = path.join(packagePath, 'README.md'); |
| 26 | +const readme = await readFile(readmePath, { encoding: 'utf8' }); |
| 27 | + |
| 28 | +let errors = 0; |
| 29 | +const packageFiles = await readdir(packagePath); |
| 30 | +for (const packageFile of packageFiles) { |
| 31 | + if (packageFile.slice(-3) === '.js' |
| 32 | + && packageFile !== 'index.js') { |
| 33 | + debug('Checking %s...', packageFile); |
| 34 | + |
| 35 | + const dotPackageFile = `./${packageFile}`; |
| 36 | + |
| 37 | + if (!indexJs.includes(dotPackageFile)) { |
| 38 | + console.error('Error: %s not exported from index.js', dotPackageFile); |
| 39 | + errors += 1; |
| 40 | + } |
| 41 | + |
| 42 | + if (packageJson.exports[dotPackageFile] !== dotPackageFile) { |
| 43 | + console.error('Error: %s not in package.json#exports', dotPackageFile); |
| 44 | + errors += 1; |
| 45 | + } |
| 46 | + |
| 47 | + if (!readme.includes(dotPackageFile)) { |
| 48 | + console.error('Error: %s not in README.md', dotPackageFile); |
| 49 | + errors += 1; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +debug('preversion checks completed with %d errors', errors); |
| 55 | + |
| 56 | +process.exitCode = errors > 0 ? 1 : 0; |
0 commit comments